Skip to content

Commit

Permalink
feat: add mod for mem
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Pollind <mpollind@gmail.com>
  • Loading branch information
pollend committed Feb 19, 2024
1 parent c4938f0 commit 745b990
Show file tree
Hide file tree
Showing 32 changed files with 386 additions and 244 deletions.
2 changes: 1 addition & 1 deletion source/client/cl_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3063,7 +3063,7 @@ static void CL_CheckForUpdate( void )
*/
static void *CL_AsyncStream_Alloc( size_t size, const char *filename, int fileline )
{
return _Mem_Alloc( zoneMemPool, size, 0, 0, filename, fileline );
return _Mem_Alloc( Mem_DefaultZonePool(), size, 0, 0, filename, fileline );
}

/*
Expand Down
25 changes: 17 additions & 8 deletions source/client/cl_vid.c
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,10 @@ static void *VID_RefModule_MemAllocExt( mempool_t *pool, size_t size, size_t ali
return _Mem_AllocExt( pool, size, align, z, MEMPOOL_REFMODULE, 0, filename, fileline );
}

static void *VID_RefModule_MemAlloc(mempool_t *pool, size_t size, const char *filename, int fileline ) {
return _Mem_Alloc( pool, size,MEMPOOL_REFMODULE, 0, filename, fileline );
}

static void VID_RefModule_MemFree( void *data, const char *filename, int fileline ) {
_Mem_Free( data, MEMPOOL_REFMODULE, 0, filename, fileline );
}
Expand All @@ -299,6 +303,18 @@ static void VID_RefModule_MemEmptyPool( mempool_t *pool, const char *filename, i
_Mem_EmptyPool( pool, MEMPOOL_REFMODULE, 0, filename, fileline );
}

static struct mem_import_s vid_mem_import = {
._Mod_Mem_AllocExt = VID_RefModule_MemAllocExt,
._Mod_Mem_Alloc = VID_RefModule_MemAlloc,
._Mod_Mem_FreePool = VID_RefModule_MemFreePool,
._Mod_Mem_EmptyPool = VID_RefModule_MemEmptyPool,
._Mod_AllocPool = VID_RefModule_MemAllocPool,
._Mod_Free = VID_RefModule_MemFree,
._Mem_CopyString = _Mem_CopyString,
._Mem_CheckSentinels = _Mem_CheckSentinels,
.Mem_PoolTotalSize = Mem_PoolTotalSize
};

static struct cinematics_s *VID_RefModule_CIN_Open( const char *name, unsigned int start_time, bool *yuv, float *framerate )
{
return CIN_Open( name, start_time, CIN_LOOP, yuv, framerate );
Expand All @@ -318,6 +334,7 @@ static bool VID_LoadRefresh( const char *name )
VID_UnloadRefresh();

import.fsImport = &default_fs_imports_s;
import.memImport = &vid_mem_import;
import.Com_Error = &Com_Error;
import.Com_Printf = &Com_Printf;
import.Com_DPrintf = &Com_DPrintf;
Expand Down Expand Up @@ -353,14 +370,6 @@ static bool VID_LoadRefresh( const char *name )
import.CIN_Reset = &CIN_Reset;
import.CIN_Close = &CIN_Close;

import.Mem_AllocPool = &VID_RefModule_MemAllocPool;
import.Mem_FreePool = &VID_RefModule_MemFreePool;
import.Mem_EmptyPool = &VID_RefModule_MemEmptyPool;
import.Mem_AllocExt = &VID_RefModule_MemAllocExt;
import.Mem_Free = &VID_RefModule_MemFree;
import.Mem_Realloc = &_Mem_Realloc;
import.Mem_PoolTotalSize = &Mem_PoolTotalSize;

import.Thread_Create = QThread_Create;
import.Thread_Join = QThread_Join;
import.Thread_Yield = QThread_Yield;
Expand Down
3 changes: 2 additions & 1 deletion source/qcommon/autoupdate.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#include "mod_mem.h"
#include "qcommon.h"
#include "asyncstream.h"

Expand Down Expand Up @@ -55,7 +56,7 @@ static void (*au_newfiles_callback)( void );
* AU_MemAlloc
*/
static void *_AU_MemAlloc( size_t size, const char *filename, int fileline ) {
return _Mem_Alloc( zoneMemPool, size, 0, 0, filename, fileline );
return _Mem_Alloc( Mem_DefaultZonePool(), size, 0, 0, filename, fileline );
}

/*
Expand Down
9 changes: 6 additions & 3 deletions source/qcommon/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
// common.c -- misc functions used in client and server
#include "mod_mem.h"
#include "qcommon.h"
#include "l10n.h"
#if defined(__GNUC__) && defined(i386)
Expand Down Expand Up @@ -229,6 +230,7 @@ void Com_Printf( const char *format, ... )
va_start( argptr, format );
Q_vsnprintfz( msg, sizeof( msg ), format, argptr );
va_end( argptr );
printf("%s", msg);

QMutex_Lock( com_print_mutex );

Expand Down Expand Up @@ -308,6 +310,7 @@ void Com_Error( com_error_code_t code, const char *format, ... )
Q_vsnprintfz( msg, sizeof_msg, format, argptr );
va_end( argptr );

printf("%s", msg);
if( code == ERR_DROP )
{
Com_Printf( "********************\nERROR: %s\n********************\n", msg );
Expand Down Expand Up @@ -513,12 +516,12 @@ int Com_GlobMatch( const char *pattern, const char *text, const bool casecmp )

char *_ZoneCopyString( const char *str, const char *filename, int fileline )
{
return _Mem_CopyString( zoneMemPool, str, filename, fileline );
return _Mem_CopyString( Mem_DefaultZonePool(), str, filename, fileline );
}

char *_TempCopyString( const char *str, const char *filename, int fileline )
{
return _Mem_CopyString( tempMemPool, str, filename, fileline );
return _Mem_CopyString( Mem_DefaultTempPool(), str, filename, fileline );
}

void Info_Print( char *s )
Expand Down Expand Up @@ -574,7 +577,7 @@ void Com_AddPakToPureList( purelist_t **purelist, const char *pakname, const uns
purelist_t *purefile;
const size_t len = strlen( pakname ) + 1;

purefile = ( purelist_t* )Mem_Alloc( mempool ? mempool : zoneMemPool, sizeof( purelist_t ) + len );
purefile = ( purelist_t* )Mem_Alloc( mempool ? mempool : Mem_DefaultZonePool(), sizeof( purelist_t ) + len );
purefile->filename = ( char * )(( uint8_t * )purefile + sizeof( *purefile ));
memcpy( purefile->filename, pakname, len );
purefile->checksum = checksum;
Expand Down
2 changes: 1 addition & 1 deletion source/qcommon/files.c
Original file line number Diff line number Diff line change
Expand Up @@ -1778,7 +1778,7 @@ static int _FS_LoadFile( int fhandle, unsigned int len, void **buffer, void *sta
if( stack && ( stackSize > len ) )
buf = ( uint8_t* )stack;
else
buf = ( uint8_t* )_Mem_AllocExt( tempMemPool, len + 1, 0, 0, 0, 0, filename, fileline );
buf = ( uint8_t* )_Mem_AllocExt( Mem_DefaultTempPool(), len + 1, 0, 0, 0, 0, filename, fileline );
buf[len] = 0;
*buffer = buf;

Expand Down
14 changes: 12 additions & 2 deletions source/qcommon/mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ static mempool_t *poolChain = NULL;
// used for temporary memory allocations around the engine, not for longterm
// storage, if anything in this pool stays allocated during gameplay, it is
// considered a leak
mempool_t *tempMemPool;
static mempool_t *tempMemPool;

// only for zone
mempool_t *zoneMemPool;
static mempool_t *zoneMemPool;

static qmutex_t *memMutex;

Expand Down Expand Up @@ -670,6 +670,16 @@ void Memory_Init( void )
memory_initialized = true;
}

mempool_t *Mem_DefaultTempPool()
{
return tempMemPool;
}

mempool_t *Mem_DefaultZonePool()
{
return zoneMemPool;
}

/*
* Memory_InitCommands
*/
Expand Down
20 changes: 10 additions & 10 deletions source/qcommon/mod_fs.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#ifndef R_FS_MODULE_H
#define R_FS_MODULE_H
#ifndef _FS_MODULE_H
#define _FS_MODULE_H

#include "../gameshared/q_arch.h"
#include "../gameshared/q_shared.h"

#define DECLARE_TYPEDEF_METHOD( ret, name, ... ) \
typedef ret(*name##Fn )( __VA_ARGS__ ); \
ret name (__VA_ARGS__);
typedef ret ( *name##Fn )( __VA_ARGS__ ); \
ret name( __VA_ARGS__ );

DECLARE_TYPEDEF_METHOD( const char *, FS_GameDirectory, void );
DECLARE_TYPEDEF_METHOD( const char *, FS_BaseGameDirectory, void );
DECLARE_TYPEDEF_METHOD( const char *, FS_GameDirectory, void );
DECLARE_TYPEDEF_METHOD( const char *, FS_WriteDirectory, void );
DECLARE_TYPEDEF_METHOD( const char *, FS_CacheDirectory, void );
DECLARE_TYPEDEF_METHOD( const char *, FS_SecureDirectory, void );
Expand Down Expand Up @@ -91,6 +91,8 @@ DECLARE_TYPEDEF_METHOD( void, FS_RemovePurePaks, void );

DECLARE_TYPEDEF_METHOD( void, FS_AddFileToMedia, const char *filename );

#undef DECLARE_TYPEDEF_METHOD

struct fs_import_s {
FS_GameDirectoryFn FS_GameDirectory;
FS_BaseGameDirectoryFn FS_BaseGameDirectory;
Expand Down Expand Up @@ -221,8 +223,8 @@ static const struct fs_import_s default_fs_imports_s = {
};

#if FS_DEFINE_INTERFACE_IMPL
static struct fs_import_s fs_import;
const char * FS_GameDirectory(void ){ return fs_import.FS_GameDirectory();}
extern struct fs_import_s fs_import;
const char * FS_GameDirectory(void){ return fs_import.FS_GameDirectory();}
const char * FS_BaseGameDirectory(void){ return fs_import.FS_BaseGameDirectory();}
const char * FS_WriteDirectory(void ){ return fs_import.FS_WriteDirectory();}
const char * FS_CacheDirectory(void){ return fs_import.FS_CacheDirectory();}
Expand All @@ -239,7 +241,7 @@ int FS_Printf( int file, const char *format, ...){
int ret;
va_list args;
va_start(args, format);
ret = fs_import.FS_Printf(file, args);
ret = fs_import.FS_Printf(file, format, args);
va_end(args);
return ret;
}
Expand Down Expand Up @@ -293,7 +295,5 @@ void FS_RemovePurePaks(void ){ fs_import.FS_RemovePurePaks();}
void FS_AddFileToMedia(const char *filename ){ fs_import.FS_AddFileToMedia(filename);}
#endif

#undef DECLARE_TYPEDEF_METHOD

#endif

72 changes: 72 additions & 0 deletions source/qcommon/mod_mem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#ifndef _MEM_MODULE_H
#define _MEM_MODULE_H

#include "../gameshared/q_arch.h"
#include "../gameshared/q_shared.h"

struct mempool_s;
typedef struct mempool_s mempool_t;

#define DECLARE_TYPEDEF_METHOD( ret, name, ... ) \
typedef ret ( *name##Fn )( __VA_ARGS__ ); \
ret name( __VA_ARGS__ );


// these functions are module specific
DECLARE_TYPEDEF_METHOD( void *, _Mod_Mem_AllocExt, mempool_t *pool, size_t size, size_t aligment, int value, const char *filename, int fileline );
DECLARE_TYPEDEF_METHOD( void *, _Mod_Mem_Alloc, mempool_t *pool, size_t size, const char *filename, int fileline );
DECLARE_TYPEDEF_METHOD( mempool_t *, _Mod_AllocPool, mempool_t *parent, const char *name, const char *filename, int fileline );
DECLARE_TYPEDEF_METHOD( void, _Mod_Free, void *data, const char *filename, int fileline );
DECLARE_TYPEDEF_METHOD( void, _Mod_Mem_FreePool, mempool_t **pool, const char *filename, int fileline );
DECLARE_TYPEDEF_METHOD( void, _Mod_Mem_EmptyPool, mempool_t *pool, const char *filename, int fileline );

DECLARE_TYPEDEF_METHOD( void *, _Mem_Realloc, void *data, size_t size, const char *filename, int fileline );
DECLARE_TYPEDEF_METHOD( char *, _Mem_CopyString, mempool_t *pool, const char *in, const char *filename, int fileline );
DECLARE_TYPEDEF_METHOD( void, _Mem_CheckSentinels, void *data, const char *filename, int fileline );
DECLARE_TYPEDEF_METHOD( size_t, Mem_PoolTotalSize, mempool_t *pool );

#undef DECLARE_TYPEDEF_METHOD

struct mem_import_s {
_Mod_Mem_AllocExtFn _Mod_Mem_AllocExt;
_Mod_Mem_AllocFn _Mod_Mem_Alloc;
_Mod_Mem_FreePoolFn _Mod_Mem_FreePool;
_Mod_Mem_EmptyPoolFn _Mod_Mem_EmptyPool;
_Mod_AllocPoolFn _Mod_AllocPool;
_Mod_FreeFn _Mod_Free;

_Mem_ReallocFn _Mem_Realloc;
_Mem_CopyStringFn _Mem_CopyString;
_Mem_CheckSentinelsFn _Mem_CheckSentinels;
Mem_PoolTotalSizeFn Mem_PoolTotalSize;
};

#define Mod_Mem_AllocExt( pool, size, align, value ) _Mod_Mem_AllocExt( pool, size, align, value, __FILE__, __LINE__ )
#define Mod_Mem_Alloc( pool, size ) _Mod_Mem_Alloc( pool, size, __FILE__, __LINE__ )
#define Mod_Mem_Realloc( data, size ) _Mem_Realloc( data, size, __FILE__, __LINE__ )
#define Mod_Mem_Free( mem ) _Mod_Free( mem, __FILE__, __LINE__ )
#define Mod_Mem_AllocPool( parent, name ) _Mod_AllocPool( parent, name, __FILE__, __LINE__ )
#define Mod_Mem_FreePool( pool ) _Mod_Mem_FreePool( pool, __FILE__, __LINE__ )
#define Mod_Mem_EmptyPool( pool ) _Mem_EmptyPool( pool, __FILE__, __LINE__ )
#define Mod_Mem_CopyString( pool, str ) _Mem_CopyString( pool, str, __FILE__, __LINE__ )
#define Mod_Mem_CheckSentinels( data ) _Mem_CheckSentinels( data, __FILE__, __LINE__ )

#if MEM_DEFINE_INTERFACE_IMPL
extern struct mem_import_s mem_import;

void * _Mod_Mem_AllocExt(mempool_t *pool, size_t size, size_t aligment, int z, const char *filename, int fileline ) {return mem_import._Mod_Mem_AllocExt( pool, size, aligment, z, filename, fileline );}
void * _Mod_Mem_Alloc(mempool_t *pool, size_t size, const char *filename, int fileline ) {return mem_import._Mod_Mem_Alloc( pool, size, filename, fileline );}
mempool_t * _Mod_AllocPool(mempool_t *parent, const char *name, const char *filename, int fileline ) {return mem_import._Mod_AllocPool( parent, name, filename, fileline );}
void _Mod_Free( void *data, const char *filename, int fileline ) {mem_import._Mod_Free( data, filename, fileline );}
void _Mod_Mem_FreePool( mempool_t **pool, const char *filename, int fileline ) {mem_import._Mod_Mem_FreePool( pool, filename, fileline );}
void _Mod_Mem_EmptyPool( mempool_t *pool, const char *filename, int fileline ) {mem_import._Mod_Mem_EmptyPool( pool, filename, fileline );}

void * _Mem_Realloc( void *data, size_t size, const char *filename, int fileline ) {return mem_import._Mem_Realloc( data, size, filename, fileline );}
char * _Mem_CopyString( mempool_t *pool, const char *in, const char *filename, int fileline ) {return mem_import._Mem_CopyString( pool, in, filename, fileline );}
void _Mem_CheckSentinels( void *data, const char *filename, int fileline ) {mem_import._Mem_CheckSentinels( data, filename, fileline );}
size_t Mem_PoolTotalSize( mempool_t *pool ) {return mem_import.Mem_PoolTotalSize( pool );}

#endif


#endif
Loading

0 comments on commit 745b990

Please sign in to comment.