engine: zone: allow moving allocation from one pool to another during realloc

This commit is contained in:
Alibek Omarov 2024-07-16 05:06:44 +03:00
parent d3586c2143
commit 3510afd30b

View file

@ -241,6 +241,21 @@ void _Mem_Free( void *data, const char *filename, int fileline )
Mem_FreeBlock((memheader_t *)((byte *)data - sizeof( memheader_t )), filename, fileline ); Mem_FreeBlock((memheader_t *)((byte *)data - sizeof( memheader_t )), filename, fileline );
} }
static void Mem_MigratePool( poolhandle_t newpoolptr, memheader_t *mem, const char *filename, int fileline )
{
mempool_t *oldpool = Mem_FindPool( mem->poolptr );
mempool_t *newpool = Mem_FindPool( newpoolptr );
// dettach allocation from one pool and reattach it to new pool
// might be made into public function at some point
Mem_PoolUnlinkAlloc( oldpool, mem );
Mem_PoolSubtract( oldpool, mem->size );
Mem_PoolLinkAlloc( newpool, mem );
Mem_PoolAdd( newpool, mem->size );
}
void *_Mem_Realloc( poolhandle_t poolptr, void *data, size_t size, qboolean clear, const char *filename, int fileline ) void *_Mem_Realloc( poolhandle_t poolptr, void *data, size_t size, qboolean clear, const char *filename, int fileline )
{ {
memheader_t *mem; memheader_t *mem;
@ -262,15 +277,12 @@ void *_Mem_Realloc( poolhandle_t poolptr, void *data, size_t size, qboolean clea
mem = (memheader_t *)((byte *)data - sizeof( memheader_t )); mem = (memheader_t *)((byte *)data - sizeof( memheader_t ));
if( !Mem_CheckAllocHeader( "Mem_Realloc", mem, filename, fileline )) if( !Mem_CheckAllocHeader( __func__, mem, filename, fileline ))
return NULL; return NULL;
if( unlikely( mem->poolptr != poolptr )) // migrate pool if requested, even if no reallocation needed
{ if( mem->poolptr != poolptr )
Sys_Error( "%s: pool migration is not allowed (alloc at %s:%i, realloc at %s:%i)\n", Mem_MigratePool( poolptr, mem, filename, fileline );
__func__, Mem_CheckFilename( mem->filename ), mem->fileline, filename, fileline );
return NULL;
}
oldsize = mem->size; oldsize = mem->size;
if( size == oldsize ) if( size == oldsize )
@ -439,7 +451,7 @@ void _Mem_Check( const char *filename, int fileline )
for( i = 0, pool = poolchain; i < poolcount; i++, pool++ ) for( i = 0, pool = poolchain; i < poolcount; i++, pool++ )
for( mem = pool->chain; mem; mem = mem->next ) for( mem = pool->chain; mem; mem = mem->next )
Mem_CheckAllocHeader( "Mem_CheckSentinels", mem, filename, fileline ); Mem_CheckAllocHeader( __func__, mem, filename, fileline );
} }
void Mem_PrintStats( void ) void Mem_PrintStats( void )