diff --git a/common/com_model.h b/common/com_model.h index 0a914555..9f8ceda8 100644 --- a/common/com_model.h +++ b/common/com_model.h @@ -291,7 +291,7 @@ typedef struct model_s // shared modelinfo modtype_t type; // model type int numframes; // sprite's framecount - byte *mempool; // private mempool (was synctype) + poolhandle_t mempool; // private mempool (was synctype) int flags; // hl compatibility // diff --git a/common/xash3d_types.h b/common/xash3d_types.h index 7f979601..e1d1a0ae 100644 --- a/common/xash3d_types.h +++ b/common/xash3d_types.h @@ -20,6 +20,7 @@ typedef byte rgba_t[4]; // unsigned byte colorpack typedef byte rgb_t[3]; // unsigned byte colorpack typedef vec_t matrix3x4[3][4]; typedef vec_t matrix4x4[4][4]; +typedef uint32_t poolhandle_t; #undef true #undef false diff --git a/engine/client/client.h b/engine/client/client.h index 0a239d7f..8e4dafaa 100644 --- a/engine/client/client.h +++ b/engine/client/client.h @@ -437,7 +437,7 @@ typedef struct void *hInstance; // pointer to client.dll cldll_func_t dllFuncs; // dll exported funcs render_interface_t drawFuncs; // custom renderer support - byte *mempool; // client edicts pool + poolhandle_t mempool; // client edicts pool string mapname; // map name string maptitle; // display map title string itemspath; // path to items description for auto-complete func @@ -496,7 +496,7 @@ typedef struct void *hInstance; // pointer to client.dll UI_FUNCTIONS dllFuncs; // dll exported funcs UI_EXTENDED_FUNCTIONS dllFuncs2; // fwgs extension - byte *mempool; // client edicts pool + poolhandle_t mempool; // client edicts pool cl_entity_t playermodel; // uiPlayerSetup drawing model player_info_t playerinfo; // local playerinfo @@ -534,7 +534,7 @@ typedef struct keydest_t key_dest; - byte *mempool; // client premamnent pool: edicts etc + poolhandle_t mempool; // client premamnent pool: edicts etc netadr_t hltv_listen_address; diff --git a/engine/client/in_touch.c b/engine/client/in_touch.c index dec3a20b..ce398f1c 100644 --- a/engine/client/in_touch.c +++ b/engine/client/in_touch.c @@ -95,7 +95,7 @@ struct touch_s qboolean initialized; qboolean config_loaded; touchbuttonlist_t list_user, list_edit; - byte *mempool; + poolhandle_t mempool; touchState state; int look_finger; diff --git a/engine/client/ref_common.c b/engine/client/ref_common.c index 79f8a8a1..4b602c0e 100644 --- a/engine/client/ref_common.c +++ b/engine/client/ref_common.c @@ -178,7 +178,7 @@ static int pfnGetStudioModelInterface( int version, struct r_studio_interface_s 0; } -static byte *pfnImage_GetPool( void ) +static poolhandle_t pfnImage_GetPool( void ) { return host.imagepool; } diff --git a/engine/client/s_main.c b/engine/client/s_main.c index a0a83048..89b8da66 100644 --- a/engine/client/s_main.c +++ b/engine/client/s_main.c @@ -23,7 +23,7 @@ GNU General Public License for more details. #define SND_CLIP_DISTANCE 1000.0f dma_t dma; -byte *sndpool; +poolhandle_t sndpool; static soundfade_t soundfade; channel_t channels[MAX_CHANNELS]; sound_t ambient_sfx[NUM_AMBIENTS]; diff --git a/engine/client/sound.h b/engine/client/sound.h index 7be57049..a955882c 100644 --- a/engine/client/sound.h +++ b/engine/client/sound.h @@ -16,7 +16,7 @@ GNU General Public License for more details. #ifndef SOUND_H #define SOUND_H -extern byte *sndpool; +extern poolhandle_t sndpool; #include "xash3d_mathlib.h" diff --git a/engine/common/common.c b/engine/common/common.c index e2c0dcd7..8b694e77 100644 --- a/engine/common/common.c +++ b/engine/common/common.c @@ -842,7 +842,7 @@ Cache_Check consistency check ==================== */ -void *Cache_Check( byte *mempool, cache_user_t *c ) +void *Cache_Check( poolhandle_t mempool, cache_user_t *c ) { if( !c->data ) return NULL; @@ -1177,7 +1177,7 @@ qboolean COM_IsSafeFileToDownload( const char *filename ) return true; } -char *_copystring( byte *mempool, const char *s, const char *filename, int fileline ) +char *_copystring( poolhandle_t mempool, const char *s, const char *filename, int fileline ) { char *b; diff --git a/engine/common/common.h b/engine/common/common.h index 7f126dd2..c5f962f9 100644 --- a/engine/common/common.h +++ b/engine/common/common.h @@ -392,7 +392,7 @@ typedef struct host_parm_s uint type; // running at jmp_buf abortframe; // abort current frame dword errorframe; // to prevent multiple host error - byte *mempool; // static mempool for misc allocations + poolhandle_t mempool; // static mempool for misc allocations string finalmsg; // server shutdown final message string downloadfile; // filename to be downloading int downloadcount; // how many files remain to downloading @@ -446,8 +446,8 @@ typedef struct host_parm_s char rootdir[MAX_OSPATH]; // member root directory char rodir[MAX_OSPATH]; // readonly root char gamefolder[MAX_QPATH]; // it's a default gamefolder - byte *imagepool; // imagelib mempool - byte *soundpool; // soundlib mempool + poolhandle_t imagepool; // imagelib mempool + poolhandle_t soundpool; // soundlib mempool uint features; // custom features that enables by mod-maker request @@ -505,14 +505,14 @@ void Cmd_ForwardToServer( void ); // zone.c // void Memory_Init( void ); -void *_Mem_Realloc( byte *poolptr, void *memptr, size_t size, qboolean clear, const char *filename, int fileline ); -void *_Mem_Alloc( byte *poolptr, size_t size, qboolean clear, const char *filename, int fileline ); -byte *_Mem_AllocPool( const char *name, const char *filename, int fileline ); -void _Mem_FreePool( byte **poolptr, const char *filename, int fileline ); -void _Mem_EmptyPool( byte *poolptr, const char *filename, int fileline ); +void *_Mem_Realloc( poolhandle_t poolptr, void *memptr, size_t size, qboolean clear, const char *filename, int fileline ); +void *_Mem_Alloc( poolhandle_t poolptr, size_t size, qboolean clear, const char *filename, int fileline ); +poolhandle_t _Mem_AllocPool( const char *name, const char *filename, int fileline ); +void _Mem_FreePool( poolhandle_t *poolptr, const char *filename, int fileline ); +void _Mem_EmptyPool( poolhandle_t poolptr, const char *filename, int fileline ); void _Mem_Free( void *data, const char *filename, int fileline ); void _Mem_Check( const char *filename, int fileline ); -qboolean Mem_IsAllocatedExt( byte *poolptr, void *data ); +qboolean Mem_IsAllocatedExt( poolhandle_t poolptr, void *data ); void Mem_PrintList( size_t minallocationsize ); void Mem_PrintStats( void ); @@ -746,7 +746,7 @@ cvar_t *pfnCVarGetPointer( const char *szVarName ); int pfnDrawConsoleString( int x, int y, char *string ); void pfnDrawSetTextColor( float r, float g, float b ); void pfnDrawConsoleStringLen( const char *pText, int *length, int *height ); -void *Cache_Check( byte *mempool, struct cache_user_s *c ); +void *Cache_Check( poolhandle_t mempool, struct cache_user_s *c ); void COM_TrimSpace( const char *source, char *dest ); edict_t* pfnPEntityOfEntIndex( int iEntIndex ); void pfnGetModelBounds( model_t *mod, float *mins, float *maxs ); @@ -765,7 +765,7 @@ float pfnTime( void ); #define copystring( s ) _copystring( host.mempool, s, __FILE__, __LINE__ ) #define SV_CopyString( s ) _copystring( svgame.stringspool, s, __FILE__, __LINE__ ) #define freestring( s ) if( s != NULL ) { Mem_Free( s ); s = NULL; } -char *_copystring( byte *mempool, const char *s, const char *filename, int fileline ); +char *_copystring( poolhandle_t mempool, const char *s, const char *filename, int fileline ); // CS:CS engfuncs (stubs) void *pfnSequenceGet( const char *fileName, const char *entryName ); diff --git a/engine/common/filesystem.c b/engine/common/filesystem.c index 8dceb20f..5b3495b7 100644 --- a/engine/common/filesystem.c +++ b/engine/common/filesystem.c @@ -101,7 +101,7 @@ struct wfile_s string filename; int infotableofs; int numlumps; - byte *mempool; // W_ReadLump temp buffers + poolhandle_t mempool; // W_ReadLump temp buffers file_t *handle; dlumpinfo_t *lumps; time_t filetime; @@ -144,7 +144,7 @@ typedef struct searchpath_s struct searchpath_s *next; } searchpath_t; -static byte *fs_mempool; +static poolhandle_t fs_mempool; static searchpath_t *fs_searchpaths = NULL; // chain static searchpath_t fs_directpath; // static direct path static char fs_basedir[MAX_SYSPATH]; // base game directory diff --git a/engine/common/mod_bmodel.c b/engine/common/mod_bmodel.c index c6a4978c..da8443ad 100644 --- a/engine/common/mod_bmodel.c +++ b/engine/common/mod_bmodel.c @@ -1305,7 +1305,7 @@ static void Mod_MakeHull0( void ) Mod_SetupHull ================= */ -static void Mod_SetupHull( dbspmodel_t *bmod, model_t *mod, byte *mempool, int headnode, int hullnum ) +static void Mod_SetupHull( dbspmodel_t *bmod, model_t *mod, poolhandle_t mempool, int headnode, int hullnum ) { hull_t *hull = &mod->hulls[hullnum]; int count; @@ -1469,7 +1469,7 @@ for embedded submodels static void Mod_SetupSubmodels( dbspmodel_t *bmod ) { qboolean colored = false; - byte *mempool; + poolhandle_t mempool; char *ents; model_t *mod; dmodel_t *bm; @@ -1550,7 +1550,7 @@ static void Mod_SetupSubmodels( dbspmodel_t *bmod ) loadmodel = Mod_FindName( name, true ); *loadmodel = *mod; Q_strncpy( loadmodel->name, name, sizeof( loadmodel->name )); - loadmodel->mempool = NULL; + loadmodel->mempool = 0; mod = loadmodel; } } diff --git a/engine/common/mod_local.h b/engine/common/mod_local.h index 7b1d8cdf..0a794e5f 100644 --- a/engine/common/mod_local.h +++ b/engine/common/mod_local.h @@ -117,7 +117,7 @@ typedef struct world_static_s #ifndef REF_DLL extern world_static_t world; -extern byte *com_studiocache; +extern poolhandle_t com_studiocache; extern model_t *loadmodel; extern convar_t *mod_studiocache; extern convar_t *r_wadtextures; diff --git a/engine/common/model.c b/engine/common/model.c index 9075676d..1b75a2df 100644 --- a/engine/common/model.c +++ b/engine/common/model.c @@ -27,7 +27,7 @@ GNU General Public License for more details. static model_info_t mod_crcinfo[MAX_MODELS]; static model_t mod_known[MAX_MODELS]; static int mod_numknown = 0; -byte *com_studiocache; // cache for submodels +poolhandle_t com_studiocache; // cache for submodels convar_t *mod_studiocache; convar_t *r_wadtextures; convar_t *r_showhull; diff --git a/engine/common/net_chan.c b/engine/common/net_chan.c index 39560f32..ea65e1ea 100644 --- a/engine/common/net_chan.c +++ b/engine/common/net_chan.c @@ -92,7 +92,7 @@ convar_t *net_qport; int net_drop; netadr_t net_from; sizebuf_t net_message; -byte *net_mempool; +static poolhandle_t net_mempool; byte net_message_buffer[NET_MAX_MESSAGE]; const char *ns_strings[NS_COUNT] = diff --git a/engine/common/zone.c b/engine/common/zone.c index 03d70fb5..f3200ed1 100644 --- a/engine/common/zone.c +++ b/engine/common/zone.c @@ -50,19 +50,44 @@ typedef struct mempool_s struct mempool_s *next; // linked into global mempool list const char *filename; // file name and line where Mem_AllocPool was called int fileline; + poolhandle_t idx; char name[64]; // name of the pool uint sentinel2; // should always be MEMHEADER_SENTINEL1 } mempool_t; -mempool_t *poolchain = NULL; // critical stuff +static mempool_t *poolchain = NULL; // critical stuff -void *_Mem_Alloc( byte *poolptr, size_t size, qboolean clear, const char *filename, int fileline ) +// a1ba: due to mempool being passed with the model through reused 32-bit field +// which makes engine incompatible with 64-bit pointers I changed mempool type +// from pointer to 32-bit handle, thankfully mempool structure is private +// But! Mempools are handled through linked list so we can't index them safely +static uint lastidx = 0; + +static mempool_t *Mem_FindPool( poolhandle_t poolptr ) { - memheader_t *mem; - mempool_t *pool = (mempool_t *)poolptr; + mempool_t *pool; + + for( pool = poolchain; pool; pool = pool->next ) + { + if( pool->idx == poolptr ) + return pool; + } + + Sys_Error( "%s: not allocated or double freed pool %d", __FUNCTION__, poolptr ); + + return NULL; +} + +void *_Mem_Alloc( poolhandle_t poolptr, size_t size, qboolean clear, const char *filename, int fileline ) +{ + memheader_t *mem; + mempool_t *pool; if( size <= 0 ) return NULL; - if( poolptr == NULL ) Sys_Error( "Mem_Alloc: pool == NULL (alloc at %s:%i)\n", filename, fileline ); + if( !poolptr ) Sys_Error( "Mem_Alloc: pool == NULL (alloc at %s:%i)\n", filename, fileline ); + + pool = Mem_FindPool( poolptr ); + pool->totalsize += size; // big allocations are not clumped @@ -147,7 +172,7 @@ void _Mem_Free( void *data, const char *filename, int fileline ) Mem_FreeBlock((memheader_t *)((byte *)data - sizeof( memheader_t )), filename, fileline ); } -void *_Mem_Realloc( byte *poolptr, void *memptr, size_t size, qboolean clear, const char *filename, int fileline ) +void *_Mem_Realloc( poolhandle_t poolptr, void *memptr, size_t size, qboolean clear, const char *filename, int fileline ) { memheader_t *memhdr = NULL; char *nb; @@ -172,12 +197,16 @@ void *_Mem_Realloc( byte *poolptr, void *memptr, size_t size, qboolean clear, co return (void *)nb; } -byte *_Mem_AllocPool( const char *name, const char *filename, int fileline ) +poolhandle_t _Mem_AllocPool( const char *name, const char *filename, int fileline ) { mempool_t *pool; pool = (mempool_t *)Q_malloc( sizeof( mempool_t )); - if( pool == NULL ) Sys_Error( "Mem_AllocPool: out of memory (allocpool at %s:%i)\n", filename, fileline ); + if( pool == NULL ) + { + Sys_Error( "Mem_AllocPool: out of memory (allocpool at %s:%i)\n", filename, fileline ); + return 0; + } memset( pool, 0, sizeof( mempool_t )); // fill header @@ -190,17 +219,18 @@ byte *_Mem_AllocPool( const char *name, const char *filename, int fileline ) pool->realsize = sizeof( mempool_t ); Q_strncpy( pool->name, name, sizeof( pool->name )); pool->next = poolchain; + pool->idx = ++lastidx; poolchain = pool; - return (byte *)pool; + return pool->idx; } -void _Mem_FreePool( byte **poolptr, const char *filename, int fileline ) +void _Mem_FreePool( poolhandle_t *poolptr, const char *filename, int fileline ) { - mempool_t *pool = (mempool_t *)*poolptr; + mempool_t *pool; mempool_t **chainaddress; - if( pool ) + if( *poolptr && ( pool = Mem_FindPool( *poolptr ))) { // unlink pool from chain for( chainaddress = &poolchain; *chainaddress && *chainaddress != pool; chainaddress = &((*chainaddress)->next)); @@ -214,14 +244,14 @@ void _Mem_FreePool( byte **poolptr, const char *filename, int fileline ) // free the pool itself memset( pool, 0xBF, sizeof( mempool_t )); Q_free( pool ); - *poolptr = NULL; + *poolptr = 0; } } -void _Mem_EmptyPool( byte *poolptr, const char *filename, int fileline ) +void _Mem_EmptyPool( poolhandle_t poolptr, const char *filename, int fileline ) { - mempool_t *pool = (mempool_t *)poolptr; - if( poolptr == NULL ) Sys_Error( "Mem_EmptyPool: pool == NULL (emptypool at %s:%i)\n", filename, fileline ); + mempool_t *pool = Mem_FindPool( poolptr ); + if( !poolptr ) Sys_Error( "Mem_EmptyPool: pool == NULL (emptypool at %s:%i)\n", filename, fileline ); if( pool->sentinel1 != MEMHEADER_SENTINEL1 ) Sys_Error( "Mem_EmptyPool: trashed pool sentinel 1 (allocpool at %s:%i, emptypool at %s:%i)\n", pool->filename, pool->fileline, filename, fileline ); if( pool->sentinel2 != MEMHEADER_SENTINEL1 ) Sys_Error( "Mem_EmptyPool: trashed pool sentinel 2 (allocpool at %s:%i, emptypool at %s:%i)\n", pool->filename, pool->fileline, filename, fileline ); @@ -230,7 +260,7 @@ void _Mem_EmptyPool( byte *poolptr, const char *filename, int fileline ) while( pool->chain ) Mem_FreeBlock( pool->chain, filename, fileline ); } -qboolean Mem_CheckAlloc( mempool_t *pool, void *data ) +static qboolean Mem_CheckAlloc( mempool_t *pool, void *data ) { memheader_t *header, *target; @@ -256,15 +286,15 @@ qboolean Mem_CheckAlloc( mempool_t *pool, void *data ) Check pointer for memory ======================== */ -qboolean Mem_IsAllocatedExt( byte *poolptr, void *data ) +qboolean Mem_IsAllocatedExt( poolhandle_t poolptr, void *data ) { mempool_t *pool = NULL; - if( poolptr ) pool = (mempool_t *)poolptr; + if( poolptr ) pool = Mem_FindPool( poolptr ); return Mem_CheckAlloc( pool, data ); } -void Mem_CheckHeaderSentinels( void *data, const char *filename, int fileline ) +static void Mem_CheckHeaderSentinels( void *data, const char *filename, int fileline ) { memheader_t *mem; @@ -288,8 +318,8 @@ void Mem_CheckHeaderSentinels( void *data, const char *filename, int fileline ) void _Mem_Check( const char *filename, int fileline ) { - memheader_t *mem; - mempool_t *pool; + memheader_t *mem; + mempool_t *pool; for( pool = poolchain; pool; pool = pool->next ) { @@ -306,8 +336,8 @@ void _Mem_Check( const char *filename, int fileline ) void Mem_PrintStats( void ) { - size_t count = 0, size = 0, realsize = 0; - mempool_t *pool; + size_t count = 0, size = 0, realsize = 0; + mempool_t *pool; Mem_Check(); for( pool = poolchain; pool; pool = pool->next ) diff --git a/engine/ref_api.h b/engine/ref_api.h index 16030932..c8c66f06 100644 --- a/engine/ref_api.h +++ b/engine/ref_api.h @@ -356,10 +356,10 @@ typedef struct ref_api_s int (*pfnGetStudioModelInterface)( int version, struct r_studio_interface_s **ppinterface, struct engine_studio_api_s *pstudio ); // memory - byte *(*_Mem_AllocPool)( const char *name, const char *filename, int fileline ); - void (*_Mem_FreePool)( byte **poolptr, const char *filename, int fileline ); - void *(*_Mem_Alloc)( byte *poolptr, size_t size, qboolean clear, const char *filename, int fileline ); - void *(*_Mem_Realloc)( byte *poolptr, void *memptr, size_t size, qboolean clear, const char *filename, int fileline ); + poolhandle_t (*_Mem_AllocPool)( const char *name, const char *filename, int fileline ); + void (*_Mem_FreePool)( poolhandle_t *poolptr, const char *filename, int fileline ); + void *(*_Mem_Alloc)( poolhandle_t poolptr, size_t size, qboolean clear, const char *filename, int fileline ); + void *(*_Mem_Realloc)( poolhandle_t poolptr, void *memptr, size_t size, qboolean clear, const char *filename, int fileline ); void (*_Mem_Free)( void *data, const char *filename, int fileline ); // library management @@ -424,7 +424,7 @@ typedef struct ref_api_s rgbdata_t *(*FS_CopyImage)( rgbdata_t *in ); void (*FS_FreeImage)( rgbdata_t *pack ); void (*Image_SetMDLPointer)( byte *p ); - byte *(*Image_GetPool)( void ); + poolhandle_t (*Image_GetPool)( void ); const struct bpc_desc_s *(*Image_GetPFDesc)( int idx ); // client exports diff --git a/engine/server/server.h b/engine/server/server.h index 00646732..cb068f51 100644 --- a/engine/server/server.h +++ b/engine/server/server.h @@ -345,8 +345,8 @@ typedef struct NEW_DLL_FUNCTIONS dllFuncs2; // new dll exported funcs (may be NULL) physics_interface_t physFuncs; // physics interface functions (Xash3D extension) - byte *mempool; // server premamnent pool: edicts etc - byte *stringspool; // for engine strings + poolhandle_t mempool; // server premamnent pool: edicts etc + poolhandle_t stringspool; // for engine strings } svgame_static_t; typedef struct diff --git a/ref_gl/gl_local.h b/ref_gl/gl_local.h index aba1724f..2acc1098 100644 --- a/ref_gl/gl_local.h +++ b/ref_gl/gl_local.h @@ -56,7 +56,7 @@ GNU General Public License for more details. #define LM_SAMPLE_SIZE 16 -extern byte *r_temppool; +extern poolhandle_t r_temppool; #define BLOCK_SIZE tr.block_size // lightmap blocksize #define BLOCK_SIZE_DEFAULT 128 // for keep backward compatibility diff --git a/ref_gl/gl_opengl.c b/ref_gl/gl_opengl.c index 0a160a1c..49802a19 100644 --- a/ref_gl/gl_opengl.c +++ b/ref_gl/gl_opengl.c @@ -49,7 +49,7 @@ cvar_t *tracergreen; cvar_t *tracerblue; cvar_t *traceralpha; -byte *r_temppool; +poolhandle_t r_temppool; gl_globals_t tr; glconfig_t glConfig; diff --git a/ref_gl/gl_rsurf.c b/ref_gl/gl_rsurf.c index 5302fb0b..c720feff 100644 --- a/ref_gl/gl_rsurf.c +++ b/ref_gl/gl_rsurf.c @@ -1734,7 +1734,7 @@ extern decal_t gDecalPool[MAX_RENDER_DECALS]; struct vbo_static_s { // quickly free all allocations on map change - byte *mempool; + poolhandle_t mempool; // arays vbodecaldata_t *decaldata; // array diff --git a/ref_soft/r_context.c b/ref_soft/r_context.c index 62859aa0..2e688db0 100644 --- a/ref_soft/r_context.c +++ b/ref_soft/r_context.c @@ -19,7 +19,7 @@ ref_api_t gEngfuncs; ref_globals_t *gpGlobals; gl_globals_t tr; ref_speeds_t r_stats; -byte *r_temppool; +poolhandle_t r_temppool; cvar_t *gl_emboss_scale; cvar_t *r_norefresh; cvar_t *vid_brightness; diff --git a/ref_soft/r_local.h b/ref_soft/r_local.h index a2d6a87f..b75b072a 100644 --- a/ref_soft/r_local.h +++ b/ref_soft/r_local.h @@ -57,7 +57,7 @@ typedef int fixed16_t; #define LM_SAMPLE_SIZE 16 -extern byte *r_temppool; +extern poolhandle_t r_temppool; #define BLOCK_SIZE tr.block_size // lightmap blocksize #define BLOCK_SIZE_DEFAULT 128 // for keep backward compatibility