From b8dadb5d154da978f78740ec8cc6273bdc1c785e Mon Sep 17 00:00:00 2001 From: cancel Date: Mon, 26 Nov 2018 05:15:50 +0900 Subject: [PATCH] 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. --- base.h | 8 ++++---- field.c | 60 +++++++++++++++++++++++++++++---------------------------- field.h | 24 ++++++++++++----------- sim.c | 4 ++-- 4 files changed, 50 insertions(+), 46 deletions(-) diff --git a/base.h b/base.h index e84bdca..aef0992 100644 --- a/base.h +++ b/base.h @@ -8,8 +8,8 @@ #include #include -#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 int64_t I64; typedef struct { Term* buffer; - U32 height; - U32 width; + U16 height; + U16 width; } Field; diff --git a/field.c b/field.c index 0000d32..c4bccd0 100644 --- a/field.c +++ b/field.c @@ -7,25 +7,28 @@ void field_init(Field* f) { 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_copy_subrect(Field* src, Field* dest, U32 src_y, U32 src_x, } } -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 @@ void field_fill_subrect(Field* f, U32 y, U32 x, U32 height, U32 width, } } -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 @@ Term field_peek(Field* f, U32 y, U32 x) { 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 @@ void field_poke(Field* f, U32 y, U32 x, Term term) { 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 @@ Field_load_error field_load_file(char const* filepath, Field* field) { 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 @@ Field_load_error field_load_file(char const* filepath, Field* field) { } 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 @@ Field_load_error field_load_file(char const* filepath, Field* field) { 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]; diff --git a/field.h b/field.h index 89c8da4..6053770 100644 --- 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); diff --git a/sim.c b/sim.c index 7f92d35..24249ad 100644 --- a/sim.c +++ b/sim.c @@ -45,7 +45,7 @@ static inline Term terms_mod(Term a, Term b) { 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_a(Field* f, U32 y, U32 x) { } } -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 != '.') {