From 9a3c281232a9071e45e82295ce188a4d239527dc Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Thu, 18 Apr 2024 21:08:30 +0300 Subject: [PATCH] engine: common: zone: disallow migrating allocations from one pool to another, as this isn't practically used anywhere --- engine/common/zone.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/engine/common/zone.c b/engine/common/zone.c index 5e688b24..0cc9d158 100644 --- a/engine/common/zone.c +++ b/engine/common/zone.c @@ -246,7 +246,7 @@ void *_Mem_Realloc( poolhandle_t poolptr, void *data, size_t size, qboolean clea if( size <= 0 ) return data; // no need to reallocate - if( !poolptr ) + if( unlikely( !poolptr )) { Sys_Error( "Mem_Realloc: pool == NULL (alloc at %s:%i)\n", filename, fileline ); return NULL; @@ -260,6 +260,13 @@ void *_Mem_Realloc( poolhandle_t poolptr, void *data, size_t size, qboolean clea if( !Mem_CheckAllocHeader( "Mem_Realloc", mem, filename, fileline )) return NULL; + if( unlikely( mem->poolptr != poolptr )) + { + Sys_Error( "Mem_Realloc: pool migration is not allowed (alloc at %s:%i, realloc at %s:%i)\n", + Mem_CheckFilename( mem->filename ), mem->fileline, filename, fileline ); + return NULL; + } + oldsize = mem->size; if( size == oldsize ) return data; @@ -289,16 +296,7 @@ void *_Mem_Realloc( poolhandle_t poolptr, void *data, size_t size, qboolean clea } else Mem_PoolSubtract( pool, oldsize - size ); - // if allocation was migrated from one pool to another - // (this is possible with original Mem_Realloc func) - if( unlikely( mem->poolptr != poolptr )) - { - mempool_t *oldpool = Mem_FindPool( mem->poolptr ); - - Mem_PoolUnlinkAlloc( oldpool, mem ); - Mem_PoolLinkAlloc( pool, mem ); - } - else if( oldmem != (uintptr_t)mem ) // just relink pointers + if( oldmem != (uintptr_t)mem ) // just relink pointers { if( mem->next ) mem->next->prev = mem; if( mem->prev ) mem->prev->next = mem;