ref: b8dadb5d154da978f78740ec8cc6273bdc1c785e
parent: 0ad3b513a087018178d47504104f2bc9a6f257a6
author: cancel <cancel@cancel.fm>
date: Mon Nov 26 00:15:50 EST 2018
Change to use size_t/ssize_t for field operations All coordinates passed for y/x/height/width are implicitly assumed to be <= ORCA_Y_MAX or ORCA_X_MAX.
--- a/base.h
+++ b/base.h
@@ -8,8 +8,8 @@
#include <string.h>
#include <unistd.h>
-#define ORCA_ROW_MAX UINT16_MAX
-#define ORCA_COL_MAX UINT16_MAX
+#define ORCA_Y_MAX UINT16_MAX
+#define ORCA_X_MAX UINT16_MAX
typedef char Term;
typedef uint16_t U16;
@@ -21,6 +21,6 @@
typedef struct {
Term* buffer;
- U32 height;
- U32 width;
+ U16 height;
+ U16 width;
} Field;
--- a/field.c
+++ b/field.c
@@ -7,25 +7,28 @@
f->width = 0;
}
-void field_init_fill(Field* f, U32 height, U32 width, Term fill_char) {
+void field_init_fill(Field* f, size_t height, size_t width, Term fill_char) {
+ assert(height <= ORCA_Y_MAX && width <= ORCA_X_MAX);
size_t num_cells = height * width;
f->buffer = malloc(num_cells * sizeof(Term));
memset(f->buffer, fill_char, num_cells);
- f->height = height;
- f->width = width;
+ f->height = (U16)height;
+ f->width = (U16)width;
}
-void field_resize_raw(Field* f, U32 height, U32 width) {
+void field_resize_raw(Field* f, size_t height, size_t width) {
+ assert(height <= ORCA_Y_MAX && width <= ORCA_X_MAX);
size_t cells = height * width;
f->buffer = realloc(f->buffer, cells * sizeof(Term));
- f->height = height;
- f->width = width;
+ f->height = (U16)height;
+ f->width = (U16)width;
}
void field_deinit(Field* f) { free(f->buffer); }
-void field_copy_subrect(Field* src, Field* dest, U32 src_y, U32 src_x,
- U32 dest_y, U32 dest_x, U32 height, U32 width) {
+void field_copy_subrect(Field* src, Field* dest, size_t src_y, size_t src_x,
+ size_t dest_y, size_t dest_x, size_t height,
+ size_t width) {
size_t src_height = src->height;
size_t src_width = src->width;
size_t dest_height = dest->height;
@@ -74,8 +77,8 @@
}
}
-void field_fill_subrect(Field* f, U32 y, U32 x, U32 height, U32 width,
- Term fill_char) {
+void field_fill_subrect(Field* f, size_t y, size_t x, size_t height,
+ size_t width, Term fill_char) {
size_t f_height = f->height;
size_t f_width = f->width;
if (y >= f_height || x >= f_width)
@@ -102,7 +105,7 @@
}
}
-Term field_peek(Field* f, U32 y, U32 x) {
+Term field_peek(Field* f, size_t y, size_t x) {
size_t f_height = f->height;
size_t f_width = f->width;
assert(y < f_height && x < f_width);
@@ -111,17 +114,18 @@
return f->buffer[y * f_width + x];
}
-Term field_peek_relative(Field* f, U32 y, U32 x, I32 offs_y, I32 offs_x) {
- I64 f_height = f->height;
- I64 f_width = f->width;
- I64 y0 = (I64)y + (I64)offs_y;
- I64 x0 = (I64)x + (I64)offs_x;
+Term field_peek_relative(Field* f, size_t y, size_t x, ssize_t offs_y,
+ ssize_t offs_x) {
+ ssize_t f_height = f->height;
+ ssize_t f_width = f->width;
+ ssize_t y0 = (ssize_t)y + (ssize_t)offs_y;
+ ssize_t x0 = (ssize_t)x + (ssize_t)offs_x;
if (y0 >= f_height || x0 >= f_width || y0 < 0 || x0 < 0)
return '.';
return f->buffer[y0 * f_width + x0];
}
-void field_poke(Field* f, U32 y, U32 x, Term term) {
+void field_poke(Field* f, size_t y, size_t x, Term term) {
size_t f_height = f->height;
size_t f_width = f->width;
assert(y < f_height && x < f_width);
@@ -130,20 +134,18 @@
f->buffer[y * f_width + x] = term;
}
-void field_poke_relative(Field* f, U32 y, U32 x, I32 offs_y, I32 offs_x,
- Term term) {
- I64 f_height = f->height;
- I64 f_width = f->width;
- I64 y0 = (I64)y + (I64)offs_y;
- I64 x0 = (I64)x + (I64)offs_x;
+void field_poke_relative(Field* f, size_t y, size_t x, ssize_t offs_y,
+ ssize_t offs_x, Term term) {
+ ssize_t f_height = f->height;
+ ssize_t f_width = f->width;
+ ssize_t y0 = (ssize_t)y + (ssize_t)offs_y;
+ ssize_t x0 = (ssize_t)x + (ssize_t)offs_x;
if (y0 >= f_height || x0 >= f_width || y0 < 0 || x0 < 0)
return;
f->buffer[y0 * f_width + x0] = term;
}
-static inline bool term_char_is_valid(char c) {
- return c >= '#' && c <= '~';
-}
+static inline bool term_char_is_valid(char c) { return c >= '#' && c <= '~'; }
void field_fput(Field* f, FILE* stream) {
enum { Column_buffer_count = 4096 };
@@ -178,7 +180,7 @@
char* s = fgets(buf, Bufsize, file);
if (s == NULL)
break;
- if (rows == ORCA_ROW_MAX) {
+ if (rows == ORCA_Y_MAX) {
fclose(file);
return Field_load_error_too_many_rows;
}
@@ -196,7 +198,7 @@
}
if (len == 0)
continue;
- if (len >= ORCA_ROW_MAX) {
+ if (len >= ORCA_X_MAX) {
fclose(file);
return Field_load_error_too_many_columns;
}
@@ -207,7 +209,7 @@
fclose(file);
return Field_load_error_not_a_rectangle;
}
- field_resize_raw(field, (U32)(rows + 1), (U32)first_row_columns);
+ field_resize_raw(field, rows + 1, first_row_columns);
Term* rowbuff = field->buffer + first_row_columns * rows;
for (size_t i = 0; i < len; ++i) {
char c = buf[i];
--- a/field.h
+++ b/field.h
@@ -2,18 +2,20 @@
#include "base.h"
void field_init(Field* f);
-void field_init_fill(Field* f, U32 height, U32 width, Term fill_char);
-void field_resize_raw(Field* f, U32 height, U32 width);
+void field_init_fill(Field* f, size_t height, size_t width, Term fill_char);
+void field_resize_raw(Field* f, size_t height, size_t width);
void field_deinit(Field* f);
-void field_copy_subrect(Field* src, Field* dest, U32 src_y, U32 src_x,
- U32 dest_y, U32 dest_x, U32 height, U32 width);
-void field_fill_subrect(Field* f, U32 y, U32 x, U32 height, U32 width,
- Term fill_char);
-Term field_peek(Field* f, U32 y, U32 x);
-Term field_peek_relative(Field* f, U32 y, U32 x, I32 offs_y, I32 offs_x);
-void field_poke(Field* f, U32 y, U32 x, Term term);
-void field_poke_relative(Field* f, U32 y, U32 x, I32 offs_y, I32 offs_x,
- Term term);
+void field_copy_subrect(Field* src, Field* dest, size_t src_y, size_t src_x,
+ size_t dest_y, size_t dest_x, size_t height,
+ size_t width);
+void field_fill_subrect(Field* f, size_t y, size_t x, size_t height,
+ size_t width, Term fill_char);
+Term field_peek(Field* f, size_t y, size_t x);
+Term field_peek_relative(Field* f, size_t y, size_t x, ssize_t offs_y,
+ ssize_t offs_x);
+void field_poke(Field* f, size_t y, size_t x, Term term);
+void field_poke_relative(Field* f, size_t y, size_t x, ssize_t offs_y,
+ ssize_t offs_x, Term term);
void field_fput(Field* f, FILE* stream);
--- a/sim.c
+++ b/sim.c
@@ -45,7 +45,7 @@
return indexed_terms[ib == 0 ? 0 : (ia % ib)];
}
-static inline void act_a(Field* f, U32 y, U32 x) {
+static inline void act_a(Field* f, size_t y, size_t x) {
Term inp0 = field_peek_relative(f, y, x, 0, 1);
Term inp1 = field_peek_relative(f, y, x, 0, 2);
if (inp0 != '.' && inp1 != '.') {
@@ -54,7 +54,7 @@
}
}
-static inline void act_m(Field* f, U32 y, U32 x) {
+static inline void act_m(Field* f, size_t y, size_t x) {
Term inp0 = field_peek_relative(f, y, x, 0, 1);
Term inp1 = field_peek_relative(f, y, x, 0, 2);
if (inp0 != '.' && inp1 != '.') {