Use offsetof() when sizing dynamic structures.

Base dynamic structure size on offsetof(), rather than subtracting the
size of the dynamic structure member.  Results could differ on systems
with strict data structure alignment requirements.
This commit is contained in:
Jason Evans 2010-10-01 18:02:43 -07:00
parent 3377ffa1f4
commit c2fc8c8b3a
3 changed files with 7 additions and 6 deletions

View File

@ -101,8 +101,8 @@ arenas_extend(unsigned ind)
arena_t *ret;
/* Allocate enough space for trailing bins. */
ret = (arena_t *)base_alloc(sizeof(arena_t)
+ (sizeof(arena_bin_t) * (nbins - 1)));
ret = (arena_t *)base_alloc(offsetof(arena_t, bins)
+ (sizeof(arena_bin_t) * nbins));
if (ret != NULL && arena_new(ret, ind) == false) {
arenas[ind] = ret;
return (ret);

View File

@ -13,11 +13,12 @@ rtree_new(unsigned bits)
height++;
assert(height * bits_per_level >= bits);
ret = (rtree_t*)base_alloc(sizeof(rtree_t) + (sizeof(unsigned) *
(height - 1)));
ret = (rtree_t*)base_alloc(offsetof(rtree_t, level2bits) +
(sizeof(unsigned) * height));
if (ret == NULL)
return (NULL);
memset(ret, 0, sizeof(rtree_t) + (sizeof(unsigned) * (height - 1)));
memset(ret, 0, offsetof(rtree_t, level2bits) + (sizeof(unsigned) *
height));
malloc_mutex_init(&ret->mutex);
ret->height = height;

View File

@ -204,7 +204,7 @@ tcache_create(arena_t *arena)
size_t size;
unsigned i;
size = sizeof(tcache_t) + (sizeof(tcache_bin_t) * (nhbins - 1));
size = offsetof(tcache_t, tbins) + (sizeof(tcache_bin_t) * nhbins);
/*
* Round up to the nearest multiple of the cacheline size, in order to
* avoid the possibility of false cacheline sharing.