From 7a25d015730fb7703c42b06643981829b05da1e6 Mon Sep 17 00:00:00 2001 From: Andre Leiradella Date: Thu, 27 Sep 2018 21:30:43 +0100 Subject: [PATCH] Use the correct alignment on allocations --- include/rcheevos.h | 8 -------- src/rcheevos/alloc.c | 4 ++-- src/rcheevos/condition.c | 2 +- src/rcheevos/condset.c | 2 +- src/rcheevos/expression.c | 2 +- src/rcheevos/internal.h | 7 ++++++- src/rcheevos/lboard.c | 6 +++--- src/rcheevos/term.c | 2 +- src/rcheevos/trigger.c | 4 ++-- src/rcheevos/value.c | 4 ++-- 10 files changed, 19 insertions(+), 22 deletions(-) diff --git a/include/rcheevos.h b/include/rcheevos.h index 58e831b6..0f76832a 100644 --- a/include/rcheevos.h +++ b/include/rcheevos.h @@ -7,14 +7,6 @@ extern "C" { typedef struct lua_State lua_State; -/*****************************************************************************\ -| User configuration | -\*****************************************************************************/ - -#ifndef RC_ALIGNMENT -#define RC_ALIGNMENT sizeof(void*) -#endif - /*****************************************************************************\ | Return values | \*****************************************************************************/ diff --git a/src/rcheevos/alloc.c b/src/rcheevos/alloc.c index 317cbd0b..fb573666 100644 --- a/src/rcheevos/alloc.c +++ b/src/rcheevos/alloc.c @@ -1,9 +1,9 @@ #include "internal.h" -void* rc_alloc(void* pointer, int* offset, int size, rc_scratch_t* scratch) { +void* rc_alloc(void* pointer, int* offset, int size, int alignment, rc_scratch_t* scratch) { void* ptr; - *offset = (*offset + RC_ALIGNMENT - 1) & ~(RC_ALIGNMENT - 1); + *offset = (*offset + alignment - 1) & ~(alignment - 1); if (pointer != 0) { ptr = (void*)((char*)pointer + *offset); diff --git a/src/rcheevos/condition.c b/src/rcheevos/condition.c index 93130327..f6f17204 100644 --- a/src/rcheevos/condition.c +++ b/src/rcheevos/condition.c @@ -8,7 +8,7 @@ rc_condition_t* rc_parse_condition(int* ret, void* buffer, rc_scratch_t* scratch int ret2; aux = *memaddr; - self = (rc_condition_t*)rc_alloc(buffer, ret, sizeof(rc_condition_t), scratch); + self = RC_ALLOC(rc_condition_t, buffer, ret, scratch); self->current_hits = 0; if (*aux != 0 && aux[1] == ':') { diff --git a/src/rcheevos/condset.c b/src/rcheevos/condset.c index b7e93c26..5062bd14 100644 --- a/src/rcheevos/condset.c +++ b/src/rcheevos/condset.c @@ -27,7 +27,7 @@ rc_condset_t* rc_parse_condset(int* ret, void* buffer, rc_scratch_t* scratch, co rc_condition_t** next; int in_pause; - self = (rc_condset_t*)rc_alloc(buffer, ret, sizeof(rc_condset_t), scratch); + self = RC_ALLOC(rc_condset_t, buffer, ret, scratch); self->has_pause = 0; next = &self->conditions; diff --git a/src/rcheevos/expression.c b/src/rcheevos/expression.c index 22c86fb9..87e45874 100644 --- a/src/rcheevos/expression.c +++ b/src/rcheevos/expression.c @@ -4,7 +4,7 @@ rc_expression_t* rc_parse_expression(int* ret, void* buffer, rc_scratch_t* scrat rc_expression_t* self; rc_term_t** next; - self = (rc_expression_t*)rc_alloc(buffer, ret, sizeof(rc_expression_t), scratch); + self = RC_ALLOC(rc_expression_t, buffer, ret, scratch); next = &self->terms; for (;;) { diff --git a/src/rcheevos/internal.h b/src/rcheevos/internal.h index edfd4969..52a36231 100644 --- a/src/rcheevos/internal.h +++ b/src/rcheevos/internal.h @@ -3,6 +3,11 @@ #include "rcheevos.h" +#define RC_OFFSETOF(s, f) ((int)(long long)(&((s*)0)->f)) +#define RC_ALIGNOF(t) RC_OFFSETOF(struct{char c; t d;}, d) + +#define RC_ALLOC(t, p, o, s) ((t*)rc_alloc(p, o, sizeof(t), RC_ALIGNOF(t), s)) + typedef union { rc_operand_t operand; rc_condition_t condition; @@ -14,7 +19,7 @@ typedef union { } rc_scratch_t; -void* rc_alloc(void* pointer, int* offset, int size, rc_scratch_t* scratch); +void* rc_alloc(void* pointer, int* offset, int size, int alignment, rc_scratch_t* scratch); void rc_parse_trigger_internal(rc_trigger_t* self, int* ret, void* buffer, rc_scratch_t* scratch, const char** memaddr, lua_State* L, int funcs_ndx); diff --git a/src/rcheevos/lboard.c b/src/rcheevos/lboard.c index 4e81c76c..56b216cd 100644 --- a/src/rcheevos/lboard.c +++ b/src/rcheevos/lboard.c @@ -92,7 +92,7 @@ void rc_parse_lboard_internal(rc_lboard_t* self, int* ret, void* buffer, void* s found |= RC_LBOARD_PROGRESS; memaddr += 4; - self->progress = (rc_value_t*)rc_alloc(buffer, ret, sizeof(rc_value_t), scratch); + self->progress = RC_ALLOC(rc_value_t, buffer, ret, scratch); rc_parse_value_internal(self->progress, ret, buffer, scratch, &memaddr, L, funcs_ndx); if (*ret < 0) { @@ -137,7 +137,7 @@ int rc_lboard_size(const char* memaddr) { rc_scratch_t scratch; ret = 0; - self = (rc_lboard_t*)rc_alloc(0, &ret, sizeof(rc_lboard_t), &scratch); + self = RC_ALLOC(rc_lboard_t, 0, &ret, &scratch); rc_parse_lboard_internal(self, &ret, 0, &scratch, memaddr, 0, 0); return ret; } @@ -148,7 +148,7 @@ rc_lboard_t* rc_parse_lboard(void* buffer, const char* memaddr, lua_State* L, in rc_scratch_t scratch; ret = 0; - self = (rc_lboard_t*)rc_alloc(buffer, &ret, sizeof(rc_lboard_t), &scratch); + self = RC_ALLOC(rc_lboard_t, buffer, &ret, &scratch); rc_parse_lboard_internal(self, &ret, buffer, 0, memaddr, L, funcs_ndx); return ret >= 0 ? self : 0; } diff --git a/src/rcheevos/term.c b/src/rcheevos/term.c index 9cc41d3e..33019fa5 100644 --- a/src/rcheevos/term.c +++ b/src/rcheevos/term.c @@ -6,7 +6,7 @@ rc_term_t* rc_parse_term(int* ret, void* buffer, rc_scratch_t* scratch, const ch int ret2; aux = *memaddr; - self = (rc_term_t*)rc_alloc(buffer, ret, sizeof(rc_term_t), scratch); + self = RC_ALLOC(rc_term_t, buffer, ret, scratch); self->invert = 0; ret2 = rc_parse_operand(&self->operand1, &aux, 0, L, funcs_ndx); diff --git a/src/rcheevos/trigger.c b/src/rcheevos/trigger.c index 0e7b46d1..bbc6cae0 100644 --- a/src/rcheevos/trigger.c +++ b/src/rcheevos/trigger.c @@ -41,7 +41,7 @@ int rc_trigger_size(const char* memaddr) { rc_scratch_t scratch; ret = 0; - self = (rc_trigger_t*)rc_alloc(0, &ret, sizeof(rc_trigger_t), &scratch); + self = RC_ALLOC(rc_trigger_t, 0, &ret, &scratch); rc_parse_trigger_internal(self, &ret, 0, &scratch, &memaddr, 0, 0); return ret; } @@ -52,7 +52,7 @@ rc_trigger_t* rc_parse_trigger(void* buffer, const char* memaddr, lua_State* L, rc_scratch_t scratch; ret = 0; - self = (rc_trigger_t*)rc_alloc(buffer, &ret, sizeof(rc_trigger_t), &scratch); + self = RC_ALLOC(rc_trigger_t, buffer, &ret, &scratch); rc_parse_trigger_internal(self, &ret, buffer, 0, &memaddr, L, funcs_ndx); return ret >= 0 ? self : 0; } diff --git a/src/rcheevos/value.c b/src/rcheevos/value.c index 4ada51c7..538088c6 100644 --- a/src/rcheevos/value.c +++ b/src/rcheevos/value.c @@ -30,7 +30,7 @@ int rc_value_size(const char* memaddr) { rc_scratch_t scratch; ret = 0; - self = (rc_value_t*)rc_alloc(0, &ret, sizeof(rc_value_t), &scratch); + self = RC_ALLOC(rc_value_t, 0, &ret, &scratch); rc_parse_value_internal(self, &ret, 0, &scratch, &memaddr, 0, 0); return ret; } @@ -41,7 +41,7 @@ rc_value_t* rc_parse_value(void* buffer, const char* memaddr, lua_State* L, int rc_scratch_t scratch; ret = 0; - self = (rc_value_t*)rc_alloc(buffer, &ret, sizeof(rc_value_t), &scratch); + self = RC_ALLOC(rc_value_t, buffer, &ret, &scratch); rc_parse_value_internal(self, &ret, buffer, 0, &memaddr, L, funcs_ndx); return ret >= 0 ? self : 0; }