From b7715d9186b7c31f0c69c441c61a183d24243a7a Mon Sep 17 00:00:00 2001 From: Eidolon Date: Sun, 24 Sep 2023 18:08:28 -0500 Subject: [PATCH] Ensure Z_Malloc provides same alignment as libc --- src/z_zone.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/z_zone.c b/src/z_zone.c index 4cbd2341f..8701848c6 100644 --- a/src/z_zone.c +++ b/src/z_zone.c @@ -25,6 +25,9 @@ /// allocator was fragmenting badly. Finally, this version is a bit /// simpler (about half the lines of code). +#include +#include + #include #include "doomdef.h" @@ -49,6 +52,7 @@ static boolean Z_calloc = false; #define ZONEID 0xa441d13d + typedef struct memblock_s { void **user; @@ -64,8 +68,9 @@ typedef struct memblock_s struct memblock_s *next, *prev; } memblock_t; -#define MEMORY(x) (void *)((uintptr_t)(x) + sizeof(memblock_t)) -#define MEMBLOCK(x) (memblock_t *)((uintptr_t)(x) - sizeof(memblock_t)) +#define ALIGNPAD (((sizeof (memblock_t) + (alignof (max_align_t) - 1)) & ~(alignof (max_align_t) - 1)) - sizeof (memblock_t)) +#define MEMORY(x) (void *)((uintptr_t)(x) + sizeof(memblock_t) + ALIGNPAD) +#define MEMBLOCK(x) (memblock_t *)((uintptr_t)(x) - ALIGNPAD - sizeof(memblock_t)) // both the head and tail of the zone memory block list static memblock_t head; @@ -200,16 +205,17 @@ void *Z_Malloc2(size_t size, INT32 tag, void *user, INT32 alignbits, { memblock_t *block; void *ptr; - (void)(alignbits); // no longer used, so silence warnings. + + (void)(alignbits); // no longer used, so silence warnings. TODO we should figure out a solution for this #ifdef ZDEBUG CONS_Debug(DBG_MEMORY, "Z_Malloc %s:%d\n", file, line); #endif - block = xm(sizeof (memblock_t) + size); - TracyCAlloc(block, sizeof (memblock_t) + size); + block = xm(sizeof (memblock_t) + ALIGNPAD + size); + TracyCAlloc(block, sizeof (memblock_t) + ALIGNPAD + size); ptr = MEMORY(block); - I_Assert((intptr_t)ptr % sizeof (void *) == 0); + I_Assert((intptr_t)ptr % alignof (max_align_t) == 0); #ifdef HAVE_VALGRIND Z_calloc = false;