From f4532ab273173d96605ff22a7e47387023651625 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Wed, 23 Jan 2019 13:42:33 -0800 Subject: Properly align the arena. --- upb/msg.c | 2 -- upb/upb.c | 8 ++++++++ 2 files changed, 8 insertions(+), 2 deletions(-) (limited to 'upb') diff --git a/upb/msg.c b/upb/msg.c index 20401f9..b46d41c 100644 --- a/upb/msg.c +++ b/upb/msg.c @@ -17,8 +17,6 @@ bool upb_fieldtype_mapkeyok(upb_fieldtype_t type) { /** upb_msgval ****************************************************************/ -#define upb_alignof(t) offsetof(struct { char c; t x; }, x) - /* These functions will generate real memcpy() calls on ARM sadly, because * the compiler assumes they might not be aligned. */ diff --git a/upb/upb.c b/upb/upb.c index 9f80639..0cf5b50 100644 --- a/upb/upb.c +++ b/upb/upb.c @@ -177,11 +177,17 @@ static void *upb_arena_doalloc(upb_alloc *alloc, void *ptr, size_t oldsize, /* Public Arena API ***********************************************************/ +#define upb_alignof(type) offsetof (struct { char c; type member; }, member) + upb_arena *upb_arena_init(void *mem, size_t n, upb_alloc *alloc) { const size_t first_block_overhead = sizeof(upb_arena) + sizeof(mem_block); upb_arena *a; bool owned = false; + /* Round block size down to alignof(*a) since we will allocate the arena + * itself at the end. */ + n &= ~(upb_alignof(upb_arena) - 1); + if (n < first_block_overhead) { /* We need to malloc the initial block. */ n = first_block_overhead + 256; @@ -208,6 +214,8 @@ upb_arena *upb_arena_init(void *mem, size_t n, upb_alloc *alloc) { return a; } +#undef upb_alignof + void upb_arena_free(upb_arena *a) { cleanup_ent *ent = a->cleanup_head; mem_block *block = a->block_head; -- cgit v1.2.3