Skip to content

Commit

Permalink
Use the correct alignment on allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
leiradel committed Sep 27, 2018
1 parent 62c928e commit 7a25d01
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 22 deletions.
8 changes: 0 additions & 8 deletions include/rcheevos.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
\*****************************************************************************/
Expand Down
4 changes: 2 additions & 2 deletions src/rcheevos/alloc.c
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/rcheevos/condition.c
Original file line number Diff line number Diff line change
Expand Up @@ -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] == ':') {
Expand Down
2 changes: 1 addition & 1 deletion src/rcheevos/condset.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/rcheevos/expression.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 (;;) {
Expand Down
7 changes: 6 additions & 1 deletion src/rcheevos/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand Down
6 changes: 3 additions & 3 deletions src/rcheevos/lboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/rcheevos/term.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/rcheevos/trigger.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/rcheevos/value.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down

0 comments on commit 7a25d01

Please sign in to comment.