Clean up and simplify zone memory allocation

This commit is contained in:
Gustaf Alhäll 2023-07-19 23:17:21 +02:00 committed by Eidolon
parent 7dafacaf7d
commit 2fa73d45d7
4 changed files with 48 additions and 180 deletions

View file

@ -91,7 +91,7 @@ huddrawlist_h LUA_HUD_CreateDrawList(void)
{
huddrawlist_h drawlist;
drawlist = (huddrawlist_h) Z_CallocAlign(sizeof(struct huddrawlist_s), PU_STATIC, NULL, 64);
drawlist = (huddrawlist_h) Z_Calloc(sizeof(struct huddrawlist_s), PU_STATIC, NULL);
drawlist->items = NULL;
drawlist->items_capacity = 0;
drawlist->items_len = 0;
@ -152,7 +152,7 @@ static size_t AllocateDrawItem(huddrawlist_h list)
{
if (list->items_capacity == 0) list->items_capacity = 128;
else list->items_capacity *= 2;
list->items = (drawitem_t *) Z_ReallocAlign(list->items, sizeof(struct drawitem_s) * list->items_capacity, PU_STATIC, NULL, 64);
list->items = (drawitem_t *) Z_Realloc(list->items, sizeof(struct drawitem_s) * list->items_capacity, PU_STATIC, NULL);
}
return list->items_len++;
@ -171,7 +171,7 @@ static size_t CopyString(huddrawlist_h list, const char* str)
{
if (list->strbuf_capacity == 0) list->strbuf_capacity = 256;
else list->strbuf_capacity *= 2;
list->strbuf = (char*) Z_ReallocAlign(list->strbuf, sizeof(char) * list->strbuf_capacity, PU_STATIC, NULL, 8);
list->strbuf = (char*) Z_Realloc(list->strbuf, sizeof(char) * list->strbuf_capacity, PU_STATIC, NULL);
}
{

View file

@ -328,12 +328,11 @@ static void AddInterpolator(levelinterpolator_t* interpolator)
levelinterpolators_size *= 2;
}
levelinterpolators = Z_ReallocAlign(
levelinterpolators = Z_Realloc(
(void*) levelinterpolators,
sizeof(levelinterpolator_t*) * levelinterpolators_size,
PU_LEVEL,
NULL,
sizeof(levelinterpolator_t*) * 8
NULL
);
}
@ -343,11 +342,8 @@ static void AddInterpolator(levelinterpolator_t* interpolator)
static levelinterpolator_t *CreateInterpolator(levelinterpolator_type_e type, thinker_t *thinker)
{
levelinterpolator_t *ret = (levelinterpolator_t*) Z_CallocAlign(
sizeof(levelinterpolator_t),
PU_LEVEL,
NULL,
sizeof(levelinterpolator_t) * 8
levelinterpolator_t *ret = (levelinterpolator_t*) Z_Calloc(
sizeof(levelinterpolator_t), PU_LEVEL, NULL
);
ret->type = type;
@ -662,12 +658,11 @@ void R_AddMobjInterpolator(mobj_t *mobj)
interpolated_mobjs_capacity *= 2;
}
interpolated_mobjs = Z_ReallocAlign(
interpolated_mobjs = Z_Realloc(
interpolated_mobjs,
sizeof(mobj_t *) * interpolated_mobjs_capacity,
PU_LEVEL,
NULL,
64
NULL
);
}

View file

@ -49,27 +49,11 @@ static boolean Z_calloc = false;
#define ZONEID 0xa441d13d
struct memblock_s;
typedef struct
{
struct memblock_s *block; // Describing this memory
UINT32 id; // Should be ZONEID
} ATTRPACK memhdr_t;
// Some code might want aligned memory. Assume it wants memory n bytes
// aligned -- then we allocate n-1 extra bytes and return a pointer to
// the first byte aligned as requested.
// Thus, "real" is the pointer we get from malloc() and will free()
// later, but "hdr" is where the memhdr_t starts.
// For non-aligned allocations they will be the same.
typedef struct memblock_s
{
void *real;
memhdr_t *hdr;
void **user;
INT32 tag; // purgelevel
UINT32 id; // Should be ZONEID
size_t size; // including the header and blocks
size_t realsize; // size of real data only
@ -78,7 +62,10 @@ typedef struct memblock_s
INT32 ownerline;
struct memblock_s *next, *prev;
} ATTRPACK memblock_t;
} memblock_t;
#define MEMORY(x) (void *)((uintptr_t)(x) + sizeof(memblock_t))
#define MEMBLOCK(x) (memblock_t *)((uintptr_t)(x) - sizeof(memblock_t))
// both the head and tail of the zone memory block list
static memblock_t head;
@ -119,52 +106,6 @@ void Z_Init(void)
// Zone memory allocation
// ----------------------
/** Returns the corresponding memblock_t for a given memory block.
*
* \param ptr A pointer to allocated memory,
* assumed to have been allocated with Z_Malloc/Z_Calloc.
* \param func A string containing the name of the function that called this,
* to be printed if the function I_Errors
* \return A pointer to the memblock_t for the given memory.
* \sa Z_Free, Z_ReallocAlign
*/
#define Ptr2Memblock(s, f) Ptr2Memblock2(s, f, __FILE__, __LINE__)
static memblock_t *Ptr2Memblock2(void *ptr, const char* func, const char *file, INT32 line)
{
memhdr_t *hdr;
memblock_t *block;
if (ptr == NULL)
return NULL;
#ifdef ZDEBUG
CONS_Debug(DBG_MEMORY, "%s %s:%d\n", func, file, line);
#endif
hdr = (memhdr_t *)((UINT8 *)ptr - sizeof *hdr);
#ifdef VALGRIND_MAKE_MEM_DEFINED
VALGRIND_MAKE_MEM_DEFINED(hdr, sizeof *hdr);
#endif
#ifdef VALGRIND_MEMPOOL_EXISTS
if (!VALGRIND_MEMPOOL_EXISTS(hdr->block))
{
I_Error("%s: bad memblock from %s:%d", func, file, line);
}
#endif
if (hdr->id != ZONEID)
{
I_Error("%s: wrong id from %s:%d", func, file, line);
}
block = hdr->block;
#ifdef VALGRIND_MAKE_MEM_NOACCESS
VALGRIND_MAKE_MEM_NOACCESS(hdr, sizeof *hdr);
#endif
return block;
}
/** Frees allocated memory.
*
* \param ptr A pointer to allocated memory,
@ -185,7 +126,11 @@ void Z_Free2(void *ptr, const char *file, INT32 line)
#endif
*/
block = Ptr2Memblock2(ptr, "Z_Free", file, line);
block = MEMBLOCK(ptr);
#ifdef PARANOIA
if (block->id != ZONEID)
I_Error("Z_Free at %s:%d: wrong id", file, line);
#endif
// Write every Z_Free call to a debug file.
CONS_Debug(DBG_MEMORY, "Z_Free at %s:%d\n", file, line);
@ -201,9 +146,6 @@ void Z_Free2(void *ptr, const char *file, INT32 line)
if (block->user != NULL)
*block->user = NULL;
// Free the memory and get rid of the block.
TracyCFree(block->real);
free(block->real);
#ifdef VALGRIND_DESTROY_MEMPOOL
VALGRIND_DESTROY_MEMPOOL(block);
#endif
@ -256,35 +198,18 @@ static void *xm(size_t size)
void *Z_Malloc2(size_t size, INT32 tag, void *user, INT32 alignbits,
const char *file, INT32 line)
{
size_t extrabytes = (1<<alignbits) - 1;
size_t padsize = 0;
memblock_t *block;
void *ptr;
memhdr_t *hdr;
void *given;
size_t blocksize = extrabytes + sizeof *hdr + size;
(void)(alignbits); // no longer used, so silence warnings.
#ifdef ZDEBUG
CONS_Debug(DBG_MEMORY, "Z_Malloc %s:%d\n", file, line);
#endif
if (blocksize < size)/* overflow check */
I_Error("You are allocating memory too large!");
block = xm(sizeof *block);
#ifdef HAVE_VALGRIND
padsize += (1<<sizeof(size_t))*2;
#endif
ptr = xm(blocksize + padsize*2);
TracyCAlloc(ptr, blocksize);
// This horrible calculation makes sure that "given" is aligned
// properly.
given = (void *)((size_t)((UINT8 *)ptr + extrabytes + sizeof *hdr + padsize/2)
& ~extrabytes);
// The mem header lives 'sizeof (memhdr_t)' bytes before given.
hdr = (memhdr_t *)((UINT8 *)given - sizeof *hdr);
block = xm(sizeof (memblock_t) + size);
TracyCAlloc(block, sizeof (memblock_t) + size);
ptr = MEMORY(block);
I_Assert((intptr_t)ptr % sizeof (void *) == 0);
#ifdef HAVE_VALGRIND
Z_calloc = false;
@ -295,39 +220,29 @@ void *Z_Malloc2(size_t size, INT32 tag, void *user, INT32 alignbits,
head.next = block;
block->next->prev = block;
block->real = ptr;
block->hdr = hdr;
block->tag = tag;
block->user = NULL;
block->ownerline = line;
block->ownerfile = file;
block->size = blocksize;
block->size = sizeof (memblock_t) + size;
block->realsize = size;
#ifdef VALGRIND_CREATE_MEMPOOL
VALGRIND_CREATE_MEMPOOL(block, padsize, Z_calloc);
VALGRIND_CREATE_MEMPOOL(block, size, Z_calloc);
#endif
//#ifdef VALGRIND_MEMPOOL_ALLOC
// VALGRIND_MEMPOOL_ALLOC(block, hdr, size + sizeof *hdr);
//#endif
hdr->id = ZONEID;
hdr->block = block;
#ifdef VALGRIND_MAKE_MEM_NOACCESS
VALGRIND_MAKE_MEM_NOACCESS(hdr, sizeof *hdr);
#endif
block->id = ZONEID;
if (user != NULL)
{
block->user = user;
*(void **)user = given;
*(void **)user = ptr;
}
else if (tag >= PU_PURGELEVEL)
I_Error("Z_Malloc: attempted to allocate purgable block "
"(size %s) with no user", sizeu1(size));
return given;
return ptr;
}
/** The Z_CallocAlign function.
@ -388,7 +303,11 @@ void *Z_Realloc2(void *ptr, size_t size, INT32 tag, void *user, INT32 alignbits,
return Z_Calloc2(size, tag, user, alignbits, file , line);
}
block = Ptr2Memblock2(ptr, "Z_Realloc", file, line);
block = MEMBLOCK(ptr);
#ifdef PARANOIA
if (block->id != ZONEID)
I_Error("Z_ReallocAlign at %s:%d: wrong id", file, line);
#endif
if (block == NULL)
return NULL;
@ -430,9 +349,8 @@ void Z_FreeTags(INT32 lowtag, INT32 hightag)
for (block = head.next; block != &head; block = next)
{
next = block->next; // get link before freeing
if (block->tag >= lowtag && block->tag <= hightag)
Z_Free((UINT8 *)block->hdr + sizeof *block->hdr);
Z_Free(MEMORY(block));
}
}
@ -455,7 +373,7 @@ void Z_IterateTags(INT32 lowtag, INT32 hightag, boolean (*iterfunc)(void *))
if (block->tag >= lowtag && block->tag <= hightag)
{
void *mem = (UINT8 *)block->hdr + sizeof *block->hdr;
void *mem = MEMORY(block);
boolean free = iterfunc(mem);
if (free)
Z_Free(mem);
@ -500,15 +418,13 @@ void Z_CheckMemCleanup(void)
void Z_CheckHeap(INT32 i)
{
memblock_t *block;
memhdr_t *hdr;
UINT32 blocknumon = 0;
void *given;
for (block = head.next; block != &head; block = block->next)
{
blocknumon++;
hdr = block->hdr;
given = (UINT8 *)hdr + sizeof *hdr;
given = MEMORY(block);
#ifdef ZDEBUG
CONS_Debug(DBG_MEMORY, "block %u owned by %s:%d\n",
blocknumon, block->ownerfile, block->ownerline);
@ -519,6 +435,7 @@ void Z_CheckHeap(INT32 i)
I_Error("Z_CheckHeap %d: block %u"
"(owned by %s:%d)"
" should not exist", i, blocknumon,
" should not exist", i, blocknumon,
block->ownerfile, block->ownerline
);
}
@ -550,16 +467,7 @@ void Z_CheckHeap(INT32 i)
#ifdef VALGRIND_MAKE_MEM_DEFINED
VALGRIND_MAKE_MEM_DEFINED(hdr, sizeof *hdr);
#endif
if (hdr->block != block)
{
I_Error("Z_CheckHeap %d: block %u"
"(owned by %s:%d)"
" doesn't have linkback from allocated memory",
i, blocknumon,
block->ownerfile, block->ownerline
);
}
if (hdr->id != ZONEID)
if (block->id != ZONEID)
{
I_Error("Z_CheckHeap %d: block %u"
"(owned by %s:%d)"
@ -567,9 +475,6 @@ void Z_CheckHeap(INT32 i)
block->ownerfile, block->ownerline
);
}
#ifdef VALGRIND_MAKE_MEM_NOACCESS
VALGRIND_MAKE_MEM_NOACCESS(hdr, sizeof *hdr);
#endif
}
}
@ -591,35 +496,14 @@ void Z_ChangeTag(void *ptr, INT32 tag)
#endif
{
memblock_t *block;
memhdr_t *hdr;
if (ptr == NULL)
return;
hdr = (memhdr_t *)((UINT8 *)ptr - sizeof *hdr);
block = MEMBLOCK(ptr);
#ifdef VALGRIND_MAKE_MEM_DEFINED
VALGRIND_MAKE_MEM_DEFINED(hdr, sizeof *hdr);
#endif
#ifdef VALGRIND_MEMPOOL_EXISTS
if (!VALGRIND_MEMPOOL_EXISTS(hdr->block))
{
#ifdef PARANOIA
I_Error("Z_CT at %s:%d: bad memblock", file, line);
#else
I_Error("Z_CT: bad memblock");
#endif
}
#endif
#ifdef PARANOIA
if (hdr->id != ZONEID) I_Error("Z_CT at %s:%d: wrong id", file, line);
#endif
block = hdr->block;
#ifdef VALGRIND_MAKE_MEM_NOACCESS
VALGRIND_MAKE_MEM_NOACCESS(hdr, sizeof *hdr);
if (block->id != ZONEID) I_Error("Z_ChangeTag at %s:%d: wrong id", file, line);
#endif
if (tag >= PU_PURGELEVEL && block->user == NULL)
@ -643,25 +527,14 @@ void Z_SetUser(void *ptr, void **newuser)
#endif
{
memblock_t *block;
memhdr_t *hdr;
if (ptr == NULL)
return;
hdr = (memhdr_t *)((UINT8 *)ptr - sizeof *hdr);
#ifdef VALGRIND_MAKE_MEM_DEFINED
VALGRIND_MAKE_MEM_DEFINED(hdr, sizeof *hdr);
#endif
block = MEMBLOCK(ptr);
#ifdef PARANOIA
if (hdr->id != ZONEID) I_Error("Z_CT at %s:%d: wrong id", file, line);
#endif
block = hdr->block;
#ifdef VALGRIND_MAKE_MEM_NOACCESS
VALGRIND_MAKE_MEM_NOACCESS(hdr, sizeof *hdr);
if (block->id != ZONEID) I_Error("Z_SetUser at %s:%d: wrong id", file, line);
#endif
if (block->tag >= PU_PURGELEVEL && newuser == NULL)

View file

@ -95,10 +95,10 @@ void *Z_Malloc2(size_t size, INT32 tag, void *user, INT32 alignbits, const char
void *Z_Calloc2(size_t size, INT32 tag, void *user, INT32 alignbits, const char *file, INT32 line) FUNCALLOC(1);
void *Z_Realloc2(void *ptr, size_t size, INT32 tag, void *user, INT32 alignbits, const char *file, INT32 line) FUNCALLOC(2);
// Alloc with no alignment
#define Z_Malloc(s,t,u) Z_MallocAlign(s, t, u, 0)
#define Z_Calloc(s,t,u) Z_CallocAlign(s, t, u, 0)
#define Z_Realloc(p,s,t,u) Z_ReallocAlign(p, s, t, u, 0)
// Alloc with standard alignment
#define Z_Malloc(s,t,u) Z_MallocAlign(s, t, u, sizeof(void *))
#define Z_Calloc(s,t,u) Z_CallocAlign(s, t, u, sizeof(void *))
#define Z_Realloc(p,s,t,u) Z_ReallocAlign(p, s, t, u, sizeof(void *))
// Free all memory by tag
// these don't give line numbers currently though