Skip to content

Commit

Permalink
add asserts to flisp to check for alignment
Browse files Browse the repository at this point in the history
closes #10942
  • Loading branch information
JeffBezanson committed Apr 22, 2015
1 parent da60006 commit b4c9198
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/flisp/flisp.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,13 +260,15 @@ int fl_is_keyword_name(const char *str, size_t len)
return len>1 && ((str[0] == ':' || str[len-1] == ':') && str[1] != '\0');
}

#define CHECK_ALIGN8(p) assert((((uptrint_t)(p))&0x7)==0 && "flisp requires malloc to return 8-aligned pointers")

static symbol_t *mk_symbol(const char *str)
{
symbol_t *sym;
size_t len = strlen(str);

sym = (symbol_t*)malloc((sizeof(symbol_t)-sizeof(void*)+len+1+7)&-8);
assert(((uptrint_t)sym & 0x7) == 0); // make sure malloc aligns 8
CHECK_ALIGN8(sym);
sym->left = sym->right = NULL;
sym->flags = 0;
if (fl_is_keyword_name(str, len)) {
Expand Down Expand Up @@ -375,6 +377,7 @@ static value_t mk_cons(void)
if (n_allocd > GC_INTERVAL)
gc(0);
c = (cons_t*)((void**)malloc(3*sizeof(void*)) + 1);
CHECK_ALIGN8(c);
((void**)c)[-1] = tochain;
tochain = c;
n_allocd += sizeof(cons_t);
Expand All @@ -397,6 +400,7 @@ static value_t *alloc_words(int n)
if (n_allocd > GC_INTERVAL)
gc(0);
first = (value_t*)malloc((n+1)*sizeof(value_t)) + 1;
CHECK_ALIGN8(first);
first[-1] = (value_t)tochain;
tochain = first;
n_allocd += (n*sizeof(value_t));
Expand Down Expand Up @@ -2352,6 +2356,7 @@ static void lisp_init(size_t initial_heapsize)
comparehash_init();
N_STACK = 262144;
Stack = (value_t*)malloc(N_STACK*sizeof(value_t));
CHECK_ALIGN8(Stack);

FL_NIL = NIL = builtin(OP_THE_EMPTY_LIST);
FL_T = builtin(OP_BOOL_CONST_T);
Expand Down

0 comments on commit b4c9198

Please sign in to comment.