# HG changeset patch # User Adam Chlipala # Date 1400453930 14400 # Node ID 77cc9169d6e033d57e2d832ec79cf89f3e2bf9af # Parent 2b2d07946e6554bba8f7c875713ef1a21f69d676 Change context-local memory allocation to return word-aligned addresses (based on patch by Evan Danaher) diff -r 2b2d07946e65 -r 77cc9169d6e0 src/c/urweb.c --- a/src/c/urweb.c Sun May 04 12:33:44 2014 -0400 +++ b/src/c/urweb.c Sun May 18 18:58:50 2014 -0400 @@ -1256,17 +1256,34 @@ ctx->amInitializing = 0; } +static void align_heap(uw_context ctx) { + size_t posn = ctx->heap.front - ctx->heap.start; + + if (posn % 4 != 0) { + size_t bump = 4 - posn % 4; + uw_check_heap(ctx, bump); + ctx->heap.front += bump; + } +} + void *uw_malloc(uw_context ctx, size_t len) { + // On some architectures, it's important that all word-sized memory accesses + // be to word-aligned addresses, so we'll do a little bit of extra work here + // in anticipation of a possible word-aligned access to the address we'll + // return. + void *result; if (ctx->amInitializing) { - result = malloc(len); - - if (result) + int error = posix_memalign(&result, 4, len); + + if (!error) return result; else - uw_error(ctx, FATAL, "uw_malloc: malloc() returns 0"); + uw_error(ctx, FATAL, "uw_malloc: posix_memalign() returns %d", error); } else { + align_heap(ctx); + uw_check_heap(ctx, len); result = ctx->heap.front; @@ -1276,6 +1293,8 @@ } void uw_begin_region(uw_context ctx) { + align_heap(ctx); + regions *r = (regions *) ctx->heap.front; uw_check_heap(ctx, sizeof(regions));