engine: make separate mempools for cmds, cvars and basecmd

This commit is contained in:
Alibek Omarov 2025-01-21 11:26:35 +03:00
parent 6cfa5877a4
commit 232d1f8583
7 changed files with 51 additions and 22 deletions

View file

@ -30,6 +30,7 @@ struct base_command_hashmap_s
}; };
static base_command_hashmap_t *hashed_cmds[HASH_SIZE]; static base_command_hashmap_t *hashed_cmds[HASH_SIZE];
static poolhandle_t basecmd_pool;
#define BaseCmd_HashKey( x ) COM_HashKey( name, HASH_SIZE ) #define BaseCmd_HashKey( x ) COM_HashKey( name, HASH_SIZE )
@ -128,7 +129,7 @@ void BaseCmd_Insert( base_command_type_e type, base_command_t *basecmd, const ch
uint hash = BaseCmd_HashKey( name ); uint hash = BaseCmd_HashKey( name );
size_t len = Q_strlen( name ); size_t len = Q_strlen( name );
elem = Z_Malloc( sizeof( base_command_hashmap_t ) + len ); elem = Mem_Malloc( basecmd_pool, sizeof( base_command_hashmap_t ) + len );
elem->basecmd = basecmd; elem->basecmd = basecmd;
elem->type = type; elem->type = type;
Q_strncpy( elem->name, name, len + 1 ); Q_strncpy( elem->name, name, len + 1 );
@ -183,9 +184,15 @@ initialize base command hashmap system
*/ */
void BaseCmd_Init( void ) void BaseCmd_Init( void )
{ {
basecmd_pool = Mem_AllocPool( "BaseCmd" );
memset( hashed_cmds, 0, sizeof( hashed_cmds ) ); memset( hashed_cmds, 0, sizeof( hashed_cmds ) );
} }
void BaseCmd_Shutdown( void )
{
Mem_FreePool( &basecmd_pool );
}
/* /*
============ ============
BaseCmd_Stats_f BaseCmd_Stats_f

View file

@ -32,6 +32,7 @@ typedef enum base_command_type
typedef void base_command_t; typedef void base_command_t;
void BaseCmd_Init( void ); void BaseCmd_Init( void );
void BaseCmd_Shutdown( void );
base_command_t *BaseCmd_Find( base_command_type_e type, const char *name ); base_command_t *BaseCmd_Find( base_command_type_e type, const char *name );
void BaseCmd_FindAll( const char *name, void BaseCmd_FindAll( const char *name,
base_command_t **cmd, base_command_t **alias, base_command_t **cvar ); base_command_t **cmd, base_command_t **alias, base_command_t **cvar );

View file

@ -46,6 +46,7 @@ static cmdalias_t *cmd_alias;
static uint cmd_condition; static uint cmd_condition;
static int cmd_condlevel; static int cmd_condlevel;
static qboolean cmd_currentCommandIsPrivileged; static qboolean cmd_currentCommandIsPrivileged;
static poolhandle_t cmd_pool;
static void Cmd_ExecuteStringWithPrivilegeCheck( const char *text, qboolean isPrivileged ); static void Cmd_ExecuteStringWithPrivilegeCheck( const char *text, qboolean isPrivileged );
@ -432,7 +433,7 @@ static void Cmd_Alias_f( void )
{ {
if( !Q_strcmp( s, a->name )) if( !Q_strcmp( s, a->name ))
{ {
Z_Free( a->value ); Mem_Free( a->value );
break; break;
} }
} }
@ -441,7 +442,7 @@ static void Cmd_Alias_f( void )
{ {
cmdalias_t *cur, *prev; cmdalias_t *cur, *prev;
a = Z_Malloc( sizeof( cmdalias_t )); a = Mem_Malloc( cmd_pool, sizeof( cmdalias_t ));
Q_strncpy( a->name, s, sizeof( a->name )); Q_strncpy( a->name, s, sizeof( a->name ));
@ -469,7 +470,7 @@ static void Cmd_Alias_f( void )
} }
Q_strncat( cmd, "\n", sizeof( cmd )); Q_strncat( cmd, "\n", sizeof( cmd ));
a->value = copystring( cmd ); a->value = copystringpool( cmd_pool, cmd );
} }
/* /*
@ -636,7 +637,7 @@ void Cmd_TokenizeString( const char *text )
// clear the args from the last string // clear the args from the last string
for( i = 0; i < cmd_argc; i++ ) for( i = 0; i < cmd_argc; i++ )
Z_Free( cmd_argv[i] ); Mem_Free( cmd_argv[i] );
cmd_argc = 0; // clear previous args cmd_argc = 0; // clear previous args
cmd_args = NULL; cmd_args = NULL;
@ -670,7 +671,7 @@ void Cmd_TokenizeString( const char *text )
if( cmd_argc < MAX_CMD_TOKENS ) if( cmd_argc < MAX_CMD_TOKENS )
{ {
cmd_argv[cmd_argc] = copystring( cmd_token ); cmd_argv[cmd_argc] = copystringpool( cmd_pool, cmd_token );
cmd_argc++; cmd_argc++;
} }
} }
@ -708,7 +709,7 @@ int Cmd_AddCommandEx( const char *cmd_name, xcommand_t function, const char *cmd
if( FBitSet( cmd->flags, CMD_OVERRIDABLE )) if( FBitSet( cmd->flags, CMD_OVERRIDABLE ))
{ {
Mem_Free( cmd->desc ); Mem_Free( cmd->desc );
cmd->desc = copystring( cmd_desc ); cmd->desc = copystringpool( cmd_pool, cmd_desc );
cmd->function = function; cmd->function = function;
cmd->flags = iFlags; cmd->flags = iFlags;
@ -723,9 +724,9 @@ int Cmd_AddCommandEx( const char *cmd_name, xcommand_t function, const char *cmd
} }
// use a small malloc to avoid zone fragmentation // use a small malloc to avoid zone fragmentation
cmd = Z_Malloc( sizeof( cmd_t ) ); cmd = Mem_Malloc( cmd_pool, sizeof( cmd_t ) );
cmd->name = copystring( cmd_name ); cmd->name = copystringpool( cmd_pool, cmd_name );
cmd->desc = copystring( cmd_desc ); cmd->desc = copystringpool( cmd_pool, cmd_desc );
cmd->function = function; cmd->function = function;
cmd->flags = iFlags; cmd->flags = iFlags;
@ -1340,6 +1341,7 @@ Cmd_Init
*/ */
void Cmd_Init( void ) void Cmd_Init( void )
{ {
cmd_pool = Mem_AllocPool( "Console Commands" );
cmd_functions = NULL; cmd_functions = NULL;
cmd_condition = 0; cmd_condition = 0;
cmd_alias = NULL; cmd_alias = NULL;
@ -1366,6 +1368,11 @@ void Cmd_Init( void )
#endif #endif
} }
void Cmd_Shutdown( void )
{
Mem_FreePool( &cmd_pool );
}
#if XASH_ENGINE_TESTS #if XASH_ENGINE_TESTS
#include "tests.h" #include "tests.h"

View file

@ -432,6 +432,7 @@ int Cmd_Argc( void );
const char *Cmd_Args( void ) RETURNS_NONNULL; const char *Cmd_Args( void ) RETURNS_NONNULL;
const char *Cmd_Argv( int arg ) RETURNS_NONNULL; const char *Cmd_Argv( int arg ) RETURNS_NONNULL;
void Cmd_Init( void ); void Cmd_Init( void );
void Cmd_Shutdown( void );
void Cmd_Unlink( int group ); void Cmd_Unlink( int group );
int Cmd_AddCommandEx( const char *cmd_name, xcommand_t function, const char *cmd_desc, int flags, const char *funcname ); int Cmd_AddCommandEx( const char *cmd_name, xcommand_t function, const char *cmd_desc, int flags, const char *funcname );
@ -628,6 +629,7 @@ int pfnNumberOfEntities( void );
int pfnIsInGame( void ); int pfnIsInGame( void );
float pfnTime( void ); float pfnTime( void );
#define copystring( s ) _copystring( host.mempool, s, __FILE__, __LINE__ ) #define copystring( s ) _copystring( host.mempool, s, __FILE__, __LINE__ )
#define copystringpool( pool, s ) _copystring( pool, s, __FILE__, __LINE__ )
#define SV_CopyString( s ) _copystring( svgame.stringspool, s, __FILE__, __LINE__ ) #define SV_CopyString( s ) _copystring( svgame.stringspool, s, __FILE__, __LINE__ )
#define freestring( s ) if( s != NULL ) { Mem_Free( s ); s = NULL; } #define freestring( s ) if( s != NULL ) { Mem_Free( s ); s = NULL; }
char *_copystring( poolhandle_t mempool, const char *s, const char *filename, int fileline ); char *_copystring( poolhandle_t mempool, const char *s, const char *filename, int fileline );

View file

@ -19,6 +19,7 @@ GNU General Public License for more details.
#include "eiface.h" // ARRAYSIZE #include "eiface.h" // ARRAYSIZE
static convar_t *cvar_vars = NULL; // head of list static convar_t *cvar_vars = NULL; // head of list
static poolhandle_t cvar_pool;
CVAR_DEFINE_AUTO( cmd_scripting, "0", FCVAR_ARCHIVE|FCVAR_PRIVILEGED, "enable simple condition checking and variable operations" ); CVAR_DEFINE_AUTO( cmd_scripting, "0", FCVAR_ARCHIVE|FCVAR_PRIVILEGED, "enable simple condition checking and variable operations" );
typedef struct cvar_filter_quirks_s typedef struct cvar_filter_quirks_s
@ -432,7 +433,7 @@ convar_t *Cvar_Get( const char *name, const char *value, int flags, const char *
{ {
// directly set value // directly set value
freestring( var->string ); freestring( var->string );
var->string = copystring( value ); var->string = copystringpool( cvar_pool, value );
var->value = Q_atof( var->string ); var->value = Q_atof( var->string );
SetBits( var->flags, flags ); SetBits( var->flags, flags );
@ -452,18 +453,18 @@ convar_t *Cvar_Get( const char *name, const char *value, int flags, const char *
Con_Reportf( "%s change description from %s to %s\n", var->name, var->desc, var_desc ); Con_Reportf( "%s change description from %s to %s\n", var->name, var->desc, var_desc );
// update description if needs // update description if needs
freestring( var->desc ); freestring( var->desc );
var->desc = copystring( var_desc ); var->desc = copystringpool( cvar_pool, var_desc );
} }
return var; return var;
} }
// allocate a new cvar // allocate a new cvar
var = Z_Malloc( sizeof( *var )); var = Mem_Malloc( cvar_pool, sizeof( *var ));
var->name = copystring( name ); var->name = copystringpool( cvar_pool, name );
var->string = copystring( value ); var->string = copystringpool( cvar_pool, value );
var->def_string = copystring( value ); var->def_string = copystringpool( cvar_pool, value );
var->desc = copystring( var_desc ); var->desc = copystringpool( cvar_pool, var_desc );
var->value = Q_atof( var->string ); var->value = Q_atof( var->string );
var->flags = flags|FCVAR_ALLOCATED; var->flags = flags|FCVAR_ALLOCATED;
@ -549,7 +550,7 @@ void Cvar_RegisterVariable( convar_t *var )
if( FBitSet( var->flags, FCVAR_EXTENDED )) if( FBitSet( var->flags, FCVAR_EXTENDED ))
var->def_string = var->string; // just swap pointers var->def_string = var->string; // just swap pointers
var->string = copystring( var->string ); var->string = copystringpool( cvar_pool, var->string );
var->value = Q_atof( var->string ); var->value = Q_atof( var->string );
// find the supposed position in chain (alphanumerical order) // find the supposed position in chain (alphanumerical order)
@ -679,7 +680,7 @@ static convar_t *Cvar_Set2( const char *var_name, const char *value )
// and finally changed the cvar itself // and finally changed the cvar itself
freestring( var->string ); freestring( var->string );
var->string = copystring( pszValue ); var->string = copystringpool( cvar_pool, pszValue );
var->value = Q_atof( var->string ); var->value = Q_atof( var->string );
// tell engine about changes // tell engine about changes
@ -738,7 +739,7 @@ void GAME_EXPORT Cvar_DirectSet( convar_t *var, const char *value )
// and finally changed the cvar itself // and finally changed the cvar itself
freestring( var->string ); freestring( var->string );
var->string = copystring( pszValue ); var->string = copystringpool( cvar_pool, pszValue );
var->value = Q_atof( var->string ); var->value = Q_atof( var->string );
// tell engine about changes // tell engine about changes
@ -781,7 +782,7 @@ void Cvar_FullSet( const char *var_name, const char *value, int flags )
} }
freestring( var->string ); freestring( var->string );
var->string = copystring( value ); var->string = copystringpool( cvar_pool, value );
var->value = Q_atof( var->string ); var->value = Q_atof( var->string );
SetBits( var->flags, flags ); SetBits( var->flags, flags );
@ -1260,7 +1261,7 @@ pending_cvar_t *Cvar_PrepareToUnlink( int group )
continue; continue;
namelen = Q_strlen( cv->name ) + 1; namelen = Q_strlen( cv->name ) + 1;
p = Mem_Malloc( host.mempool, sizeof( *list ) + namelen ); p = Mem_Malloc( cvar_pool, sizeof( *list ) + namelen );
p->next = NULL; p->next = NULL;
p->cv_cur = cv; p->cv_cur = cv;
p->cv_next = cv->next; p->cv_next = cv->next;
@ -1336,6 +1337,7 @@ Reads in all archived cvars
*/ */
void Cvar_Init( void ) void Cvar_Init( void )
{ {
cvar_pool = Mem_AllocPool( "Console Variables" );
cvar_vars = NULL; cvar_vars = NULL;
cvar_active_filter_quirks = NULL; cvar_active_filter_quirks = NULL;
Cvar_RegisterVariable( &cmd_scripting ); Cvar_RegisterVariable( &cmd_scripting );
@ -1348,6 +1350,11 @@ void Cvar_Init( void )
Cmd_AddCommand( "cvarlist", Cvar_List_f, "display all console variables beginning with the specified prefix" ); Cmd_AddCommand( "cvarlist", Cvar_List_f, "display all console variables beginning with the specified prefix" );
} }
void Cvar_Shutdown( void )
{
Mem_FreePool( &cvar_pool );
}
/* /*
============ ============
Cvar_PostFSInit Cvar_PostFSInit

View file

@ -52,6 +52,7 @@ void Cvar_Reset( const char *var_name );
void Cvar_SetCheatState( void ); void Cvar_SetCheatState( void );
qboolean Cvar_CommandWithPrivilegeCheck( convar_t *v, qboolean isPrivileged ); qboolean Cvar_CommandWithPrivilegeCheck( convar_t *v, qboolean isPrivileged );
void Cvar_Init( void ); void Cvar_Init( void );
void Cvar_Shutdown( void );
void Cvar_PostFSInit( void ); void Cvar_PostFSInit( void );
void Cvar_Unlink( int group ); void Cvar_Unlink( int group );

View file

@ -1400,6 +1400,10 @@ void Host_ShutdownWithReason( const char *reason )
Host_FreeCommon(); Host_FreeCommon();
Platform_Shutdown(); Platform_Shutdown();
BaseCmd_Shutdown();
Cmd_Shutdown();
Cvar_Shutdown();
// must be last, console uses this // must be last, console uses this
Mem_FreePool( &host.mempool ); Mem_FreePool( &host.mempool );