prvm_eval_t prvm_badvalue; // used only for error returns
-cvar_t prvm_language = {CVAR_SAVE, "prvm_language", "", "when set, loads PROGSFILE.LANGUAGENAME.po and common.LANGUAGENAME.po for string translations; when set to dump, PROGSFILE.pot is written from the strings in the progs"};
+cvar_t prvm_language = {CVAR_CLIENT | CVAR_SERVER | CVAR_SAVE, "prvm_language", "", "when set, loads PROGSFILE.LANGUAGENAME.po and common.LANGUAGENAME.po for string translations; when set to dump, PROGSFILE.pot is written from the strings in the progs"};
// LadyHavoc: prints every opcode as it executes - warning: this is significant spew
-cvar_t prvm_traceqc = {0, "prvm_traceqc", "0", "prints every QuakeC statement as it is executed (only for really thorough debugging!)"};
+cvar_t prvm_traceqc = {CVAR_CLIENT | CVAR_SERVER, "prvm_traceqc", "0", "prints every QuakeC statement as it is executed (only for really thorough debugging!)"};
// LadyHavoc: counts usage of each QuakeC statement
-cvar_t prvm_statementprofiling = {0, "prvm_statementprofiling", "0", "counts how many times each QuakeC statement has been executed, these counts are displayed in prvm_printfunction output (if enabled)"};
-cvar_t prvm_timeprofiling = {0, "prvm_timeprofiling", "0", "counts how long each function has been executed, these counts are displayed in prvm_profile output (if enabled)"};
-cvar_t prvm_coverage = {0, "prvm_coverage", "0", "report and count coverage events (1: per-function, 2: coverage() builtin, 4: per-statement)"};
-cvar_t prvm_backtraceforwarnings = {0, "prvm_backtraceforwarnings", "0", "print a backtrace for warnings too"};
-cvar_t prvm_leaktest = {0, "prvm_leaktest", "0", "try to detect memory leaks in strings or entities"};
-cvar_t prvm_leaktest_follow_targetname = {0, "prvm_leaktest_follow_targetname", "0", "if set, target/targetname links are considered when leak testing; this should normally not be required, as entities created during startup - e.g. info_notnull - are never considered leaky"};
-cvar_t prvm_leaktest_ignore_classnames = {0, "prvm_leaktest_ignore_classnames", "", "classnames of entities to NOT leak check because they are found by find(world, classname, ...) but are actually spawned by QC code (NOT map entities)"};
-cvar_t prvm_errordump = {0, "prvm_errordump", "0", "write a savegame on crash to crash-server.dmp"};
-cvar_t prvm_breakpointdump = {0, "prvm_breakpointdump", "0", "write a savegame on breakpoint to breakpoint-server.dmp"};
-cvar_t prvm_reuseedicts_startuptime = {0, "prvm_reuseedicts_startuptime", "2", "allows immediate re-use of freed entity slots during start of new level (value in seconds)"};
-cvar_t prvm_reuseedicts_neverinsameframe = {0, "prvm_reuseedicts_neverinsameframe", "1", "never allows re-use of freed entity slots during same frame"};
+cvar_t prvm_statementprofiling = {CVAR_CLIENT | CVAR_SERVER, "prvm_statementprofiling", "0", "counts how many times each QuakeC statement has been executed, these counts are displayed in prvm_printfunction output (if enabled)"};
+cvar_t prvm_timeprofiling = {CVAR_CLIENT | CVAR_SERVER, "prvm_timeprofiling", "0", "counts how long each function has been executed, these counts are displayed in prvm_profile output (if enabled)"};
+cvar_t prvm_coverage = {CVAR_CLIENT | CVAR_SERVER, "prvm_coverage", "0", "report and count coverage events (1: per-function, 2: coverage() builtin, 4: per-statement)"};
+cvar_t prvm_backtraceforwarnings = {CVAR_CLIENT | CVAR_SERVER, "prvm_backtraceforwarnings", "0", "print a backtrace for warnings too"};
+cvar_t prvm_leaktest = {CVAR_CLIENT | CVAR_SERVER, "prvm_leaktest", "0", "try to detect memory leaks in strings or entities"};
+cvar_t prvm_leaktest_follow_targetname = {CVAR_CLIENT | CVAR_SERVER, "prvm_leaktest_follow_targetname", "0", "if set, target/targetname links are considered when leak testing; this should normally not be required, as entities created during startup - e.g. info_notnull - are never considered leaky"};
+cvar_t prvm_leaktest_ignore_classnames = {CVAR_CLIENT | CVAR_SERVER, "prvm_leaktest_ignore_classnames", "", "classnames of entities to NOT leak check because they are found by find(world, classname, ...) but are actually spawned by QC code (NOT map entities)"};
+cvar_t prvm_errordump = {CVAR_CLIENT | CVAR_SERVER, "prvm_errordump", "0", "write a savegame on crash to crash-server.dmp"};
+cvar_t prvm_breakpointdump = {CVAR_CLIENT | CVAR_SERVER, "prvm_breakpointdump", "0", "write a savegame on breakpoint to breakpoint-server.dmp"};
+cvar_t prvm_reuseedicts_startuptime = {CVAR_CLIENT | CVAR_SERVER, "prvm_reuseedicts_startuptime", "2", "allows immediate re-use of freed entity slots during start of new level (value in seconds)"};
+cvar_t prvm_reuseedicts_neverinsameframe = {CVAR_CLIENT | CVAR_SERVER, "prvm_reuseedicts_neverinsameframe", "1", "never allows re-use of freed entity slots during same frame"};
+cvar_t prvm_garbagecollection_enable = {CVAR_CLIENT | CVAR_SERVER, "prvm_garbagecollection_enable", "1", "automatically scan for and free resources that are not referenced by the code being executed in the VM"};
+cvar_t prvm_garbagecollection_notify = {CVAR_CLIENT | CVAR_SERVER, "prvm_garbagecollection_notify", "0", "print out a notification for each resource freed by garbage collection"};
+cvar_t prvm_garbagecollection_scan_limit = {CVAR_CLIENT | CVAR_SERVER, "prvm_garbagecollection_scan_limit", "10000", "scan this many fields or resources per frame to free up unreferenced resources"};
+cvar_t prvm_garbagecollection_strings = {CVAR_CLIENT | CVAR_SERVER, "prvm_garbagecollection_strings", "1", "automatically call strunzone() on strings that are not referenced"};
+cvar_t prvm_stringdebug = {CVAR_CLIENT | CVAR_SERVER, "prvm_stringdebug", "0", "Print debug and warning messages related to strings"};
static double prvm_reuseedicts_always_allow = 0;
qboolean prvm_runawaycheck = true;
// alloc edict fields
prog->entityfieldsarea = prog->entityfields * prog->max_edicts;
- prog->edictsfields = (prvm_vec_t *)Mem_Alloc(prog->progs_mempool, prog->entityfieldsarea * sizeof(prvm_vec_t));
+ prog->edictsfields.fp = (prvm_vec_t *)Mem_Alloc(prog->progs_mempool, prog->entityfieldsarea * sizeof(prvm_vec_t));
// set edict pointers
for(i = 0; i < prog->max_edicts; i++)
{
prog->edicts[i].priv.required = (prvm_edict_private_t *)((unsigned char *)prog->edictprivate + i * prog->edictprivate_size);
- prog->edicts[i].fields.fp = prog->edictsfields + i * prog->entityfields;
+ prog->edicts[i].fields.fp = prog->edictsfields.fp + i * prog->entityfields;
}
}
prog->max_edicts = min(prog->max_edicts + 256, prog->limit_edicts);
prog->entityfieldsarea = prog->entityfields * prog->max_edicts;
- prog->edictsfields = (prvm_vec_t*)Mem_Realloc(prog->progs_mempool, (void *)prog->edictsfields, prog->entityfieldsarea * sizeof(prvm_vec_t));
+ prog->edictsfields.fp = (prvm_vec_t*)Mem_Realloc(prog->progs_mempool, (void *)prog->edictsfields.fp, prog->entityfieldsarea * sizeof(prvm_vec_t));
prog->edictprivate = (void *)Mem_Realloc(prog->progs_mempool, (void *)prog->edictprivate, prog->max_edicts * prog->edictprivate_size);
//set e and v pointers
for(i = 0; i < prog->max_edicts; i++)
{
prog->edicts[i].priv.required = (prvm_edict_private_t *)((unsigned char *)prog->edictprivate + i * prog->edictprivate_size);
- prog->edicts[i].fields.fp = prog->edictsfields + i * prog->entityfields;
+ prog->edicts[i].fields.fp = prog->edictsfields.fp + i * prog->entityfields;
}
prog->end_increase_edicts(prog);
{
memset(e->fields.fp, 0, prog->entityfields * sizeof(prvm_vec_t));
e->priv.required->free = false;
- e->priv.required->freetime = realtime;
+ e->priv.required->freetime = host.realtime;
if(e->priv.required->allocation_origin)
Mem_Free((char *)e->priv.required->allocation_origin);
e->priv.required->allocation_origin = PRVM_AllocationOrigin(prog);
{
if(!e->priv.required->free)
return false;
- if(prvm_reuseedicts_always_allow == realtime)
+ if(prvm_reuseedicts_always_allow == host.realtime)
return true;
- if(realtime <= e->priv.required->freetime + 0.1 && prvm_reuseedicts_neverinsameframe.integer)
+ if(host.realtime <= e->priv.required->freetime + 0.1 && prvm_reuseedicts_neverinsameframe.integer)
return false; // never allow reuse in same frame (causes networking trouble)
if(e->priv.required->freetime < prog->starttime + prvm_reuseedicts_startuptime.value)
return true;
- if(realtime > e->priv.required->freetime + 1)
+ if(host.realtime > e->priv.required->freetime + 1)
return true;
return false; // entity slot still blocked because the entity was freed less than one second ago
}
prog->free_edict(prog, ed);
ed->priv.required->free = true;
- ed->priv.required->freetime = realtime;
+ ed->priv.required->freetime = host.realtime;
if(ed->priv.required->allocation_origin)
{
Mem_Free((char *)ed->priv.required->allocation_origin);
dpsnprintf (line, linelength, "%s()", PRVM_GetString(prog, f->s_name));
}
else
- dpsnprintf (line, linelength, "function%i() (invalid!)", val->function);
+ dpsnprintf (line, linelength, "function%lli() (invalid!)", val->function);
break;
case ev_field:
def = PRVM_ED_FieldAtOfs ( prog, val->_int );
if (def != NULL)
dpsnprintf (line, linelength, ".%s", PRVM_GetString(prog, def->s_name));
else
- dpsnprintf (line, linelength, "field%i (invalid!)", val->_int );
+ dpsnprintf (line, linelength, "field%lli (invalid!)", val->_int );
break;
case ev_void:
dpsnprintf (line, linelength, "void");
strlcpy (line, PRVM_GetString (prog, f->s_name), linelength);
}
else
- dpsnprintf (line, linelength, "bad function %i (invalid!)", val->function);
+ dpsnprintf (line, linelength, "bad function %lli (invalid!)", val->function);
break;
case ev_field:
def = PRVM_ED_FieldAtOfs ( prog, val->_int );
if (def != NULL)
dpsnprintf (line, linelength, ".%s", PRVM_GetString(prog, def->s_name));
else
- dpsnprintf (line, linelength, "field%i (invalid!)", val->_int );
+ dpsnprintf (line, linelength, "field%lli (invalid!)", val->_int );
break;
case ev_void:
dpsnprintf (line, linelength, "void");
s = PRVM_UglyValueString(prog, (etype_t)key->type, v, valuebuf, sizeof(valuebuf));
if(Cmd_Argc(cmd) == 5)
{
- cvar_t *cvar = Cvar_FindVar(Cmd_Argv(cmd, 4));
- if (cvar && cvar->flags & CVAR_READONLY)
- {
- Con_Printf("prvm_edictget: %s is read-only\n", cvar->name);
- goto fail;
- }
- Cvar_Get(Cmd_Argv(cmd, 4), s, 0, NULL);
+ cvar_t *cvar = Cvar_FindVar(cmd->cvars, Cmd_Argv(cmd, 4), cmd->cvars_flagsmask);
+ if (cvar)
+ if(Cvar_Readonly(cvar, "prvm_edictget"))
+ goto fail;
+
+ Cvar_Get(cmd->cvars, Cmd_Argv(cmd, 4), s, cmd->cvars_flagsmask, NULL);
}
else
Con_Printf("%s\n", s);
s = PRVM_UglyValueString(prog, (etype_t)key->type, v, valuebuf, sizeof(valuebuf));
if(Cmd_Argc(cmd) == 4)
{
- cvar_t *cvar = Cvar_FindVar(Cmd_Argv(cmd, 3));
- if (cvar && cvar->flags & CVAR_READONLY)
- {
- Con_Printf("prvm_globalget: %s is read-only\n", cvar->name);
- goto fail;
- }
- Cvar_Get(Cmd_Argv(cmd, 3), s, 0, NULL);
+ cvar_t *cvar = Cvar_FindVar(cmd->cvars, Cmd_Argv(cmd, 3), cmd->cvars_flagsmask);
+ if (cvar)
+ if(Cvar_Readonly(cvar, "prvm_globalget"))
+ goto fail;
+ Cvar_Get(cmd->cvars, Cmd_Argv(cmd, 3), s, cmd->cvars_flagsmask, NULL);
}
else
Con_Printf("%s\n", s);
if (!init) {
ent->priv.required->free = true;
- ent->priv.required->freetime = realtime;
+ ent->priv.required->freetime = host.realtime;
}
return data;
spawned = 0;
died = 0;
- prvm_reuseedicts_always_allow = realtime;
+ prvm_reuseedicts_always_allow = host.realtime;
// parse ents
while (1)
// TODO bounds check header fields (e.g. numstatements), they must never go behind end of file
prog->profiletime = Sys_DirtyTime();
- prog->starttime = realtime;
+ prog->starttime = host.realtime;
Con_DPrintf("%s programs occupy %iK.\n", prog->name, (int)(filesize/1024));
prog->numknownstrings = 0;
prog->maxknownstrings = 0;
prog->knownstrings = NULL;
- prog->knownstrings_freeable = NULL;
+ prog->knownstrings_flags = NULL;
Mem_ExpandableArray_NewArray(&prog->stringbuffersarray, prog->progs_mempool, sizeof(prvm_stringbuffer_t), 64);
}
}
- for (cvar = cvar_vars; cvar; cvar = cvar->next)
+ for (cvar = prog->console_cmd->cvars->vars; cvar; cvar = cvar->next)
cvar->globaldefindex[prog - prvm_prog_list] = -1;
for (i=0 ; i<prog->numglobaldefs ; i++)
)
{
prvm_eval_t *val = PRVM_GLOBALFIELDVALUE(prog->globaldefs[i].ofs);
- cvar = Cvar_FindVar(name + 9);
+ cvar = Cvar_FindVar(prog->console_cmd->cvars, name + 9, prog->console_cmd->cvars_flagsmask);
//Con_Printf("PRVM_LoadProgs: autocvar global %s in %s, processing...\n", name, prog->name);
if(!cvar)
{
Con_Printf("PRVM_LoadProgs: invalid type of autocvar global %s in %s\n", name, prog->name);
goto fail;
}
- cvar = Cvar_Get(name + 9, value, 0, NULL);
+ cvar = Cvar_Get(prog->console_cmd->cvars, name + 9, value, prog->console_cmd->cvars_flagsmask, NULL);
if((prog->globaldefs[i].type & ~DEF_SAVEGLOBAL) == ev_string)
{
val->string = PRVM_SetEngineString(prog, cvar->string);
;
}
- prog->loaded = TRUE;
+ prog->loaded = true;
PRVM_UpdateBreakpoints(prog);
// Inittime is at least the time when this function finished. However,
// later events may bump it.
- prog->inittime = realtime;
+ prog->inittime = host.realtime;
}
*/
void PRVM_Init (void)
{
- Cmd_AddCommand(&cmd_client, "prvm_edict", PRVM_ED_PrintEdict_f, "print all data about an entity number in the selected VM (server, client, menu)");
- Cmd_AddCommand(&cmd_client, "prvm_edicts", PRVM_ED_PrintEdicts_f, "prints all data about all entities in the selected VM (server, client, menu)");
- Cmd_AddCommand(&cmd_client, "prvm_edictcount", PRVM_ED_Count_f, "prints number of active entities in the selected VM (server, client, menu)");
- Cmd_AddCommand(&cmd_client, "prvm_profile", PRVM_Profile_f, "prints execution statistics about the most used QuakeC functions in the selected VM (server, client, menu)");
- Cmd_AddCommand(&cmd_client, "prvm_childprofile", PRVM_ChildProfile_f, "prints execution statistics about the most used QuakeC functions in the selected VM (server, client, menu), sorted by time taken in function with child calls");
- Cmd_AddCommand(&cmd_client, "prvm_callprofile", PRVM_CallProfile_f, "prints execution statistics about the most time consuming QuakeC calls from the engine in the selected VM (server, client, menu)");
- Cmd_AddCommand(&cmd_client, "prvm_fields", PRVM_Fields_f, "prints usage statistics on properties (how many entities have non-zero values) in the selected VM (server, client, menu)");
- Cmd_AddCommand(&cmd_client, "prvm_globals", PRVM_Globals_f, "prints all global variables in the selected VM (server, client, menu)");
- Cmd_AddCommand(&cmd_client, "prvm_global", PRVM_Global_f, "prints value of a specified global variable in the selected VM (server, client, menu)");
- Cmd_AddCommand(&cmd_client, "prvm_globalset", PRVM_GlobalSet_f, "sets value of a specified global variable in the selected VM (server, client, menu)");
- Cmd_AddCommand(&cmd_client, "prvm_edictset", PRVM_ED_EdictSet_f, "changes value of a specified property of a specified entity in the selected VM (server, client, menu)");
- Cmd_AddCommand(&cmd_client, "prvm_edictget", PRVM_ED_EdictGet_f, "retrieves the value of a specified property of a specified entity in the selected VM (server, client menu) into a cvar or to the console");
- Cmd_AddCommand(&cmd_client, "prvm_globalget", PRVM_ED_GlobalGet_f, "retrieves the value of a specified global variable in the selected VM (server, client menu) into a cvar or to the console");
- Cmd_AddCommand(&cmd_client, "prvm_printfunction", PRVM_PrintFunction_f, "prints a disassembly (QuakeC instructions) of the specified function in the selected VM (server, client, menu)");
- Cmd_AddCommand(&cmd_client, "cl_cmd", PRVM_GameCommand_Client_f, "calls the client QC function GameCommand with the supplied string as argument");
- Cmd_AddCommand(&cmd_client, "menu_cmd", PRVM_GameCommand_Menu_f, "calls the menu QC function GameCommand with the supplied string as argument");
- Cmd_AddCommand(&cmd_client, "sv_cmd", PRVM_GameCommand_Server_f, "calls the server QC function GameCommand with the supplied string as argument");
- Cmd_AddCommand(&cmd_client, "prvm_breakpoint", PRVM_Breakpoint_f, "marks a statement or function as breakpoint (when this is executed, a stack trace is printed); to actually halt and investigate state, combine this with a gdb breakpoint on PRVM_Breakpoint, or with prvm_breakpointdump; run with just progs name to clear breakpoint");
- Cmd_AddCommand(&cmd_client, "prvm_globalwatchpoint", PRVM_GlobalWatchpoint_f, "marks a global as watchpoint (when this is executed, a stack trace is printed); to actually halt and investigate state, combine this with a gdb breakpoint on PRVM_Breakpoint, or with prvm_breakpointdump; run with just progs name to clear watchpoint");
- Cmd_AddCommand(&cmd_client, "prvm_edictwatchpoint", PRVM_EdictWatchpoint_f, "marks an entity field as watchpoint (when this is executed, a stack trace is printed); to actually halt and investigate state, combine this with a gdb breakpoint on PRVM_Breakpoint, or with prvm_breakpointdump; run with just progs name to clear watchpoint");
-
- Cmd_AddCommand(&cmd_server, "prvm_edict", PRVM_ED_PrintEdict_f, "print all data about an entity number in the selected VM (server, client, menu)");
- Cmd_AddCommand(&cmd_server, "prvm_edicts", PRVM_ED_PrintEdicts_f, "prints all data about all entities in the selected VM (server, client, menu)");
- Cmd_AddCommand(&cmd_server, "prvm_edictcount", PRVM_ED_Count_f, "prints number of active entities in the selected VM (server, client, menu)");
- Cmd_AddCommand(&cmd_server, "prvm_profile", PRVM_Profile_f, "prints execution statistics about the most used QuakeC functions in the selected VM (server, client, menu)");
- Cmd_AddCommand(&cmd_server, "prvm_childprofile", PRVM_ChildProfile_f, "prints execution statistics about the most used QuakeC functions in the selected VM (server, client, menu), sorted by time taken in function with child calls");
- Cmd_AddCommand(&cmd_server, "prvm_callprofile", PRVM_CallProfile_f, "prints execution statistics about the most time consuming QuakeC calls from the engine in the selected VM (server, client, menu)");
- Cmd_AddCommand(&cmd_server, "prvm_fields", PRVM_Fields_f, "prints usage statistics on properties (how many entities have non-zero values) in the selected VM (server, client, menu)");
- Cmd_AddCommand(&cmd_server, "prvm_globals", PRVM_Globals_f, "prints all global variables in the selected VM (server, client, menu)");
- Cmd_AddCommand(&cmd_server, "prvm_global", PRVM_Global_f, "prints value of a specified global variable in the selected VM (server, client, menu)");
- Cmd_AddCommand(&cmd_server, "prvm_globalset", PRVM_GlobalSet_f, "sets value of a specified global variable in the selected VM (server, client, menu)");
- Cmd_AddCommand(&cmd_server, "prvm_edictset", PRVM_ED_EdictSet_f, "changes value of a specified property of a specified entity in the selected VM (server, client, menu)");
- Cmd_AddCommand(&cmd_server, "prvm_edictget", PRVM_ED_EdictGet_f, "retrieves the value of a specified property of a specified entity in the selected VM (server, client menu) into a cvar or to the console");
- Cmd_AddCommand(&cmd_server, "prvm_globalget", PRVM_ED_GlobalGet_f, "retrieves the value of a specified global variable in the selected VM (server, client menu) into a cvar or to the console");
- Cmd_AddCommand(&cmd_server, "prvm_printfunction", PRVM_PrintFunction_f, "prints a disassembly (QuakeC instructions) of the specified function in the selected VM (server, client, menu)");
- Cmd_AddCommand(&cmd_server, "cl_cmd", PRVM_GameCommand_Client_f, "calls the client QC function GameCommand with the supplied string as argument");
- Cmd_AddCommand(&cmd_server, "menu_cmd", PRVM_GameCommand_Menu_f, "calls the menu QC function GameCommand with the supplied string as argument");
- Cmd_AddCommand(&cmd_server, "sv_cmd", PRVM_GameCommand_Server_f, "calls the server QC function GameCommand with the supplied string as argument");
- Cmd_AddCommand(&cmd_server, "prvm_breakpoint", PRVM_Breakpoint_f, "marks a statement or function as breakpoint (when this is executed, a stack trace is printed); to actually halt and investigate state, combine this with a gdb breakpoint on PRVM_Breakpoint, or with prvm_breakpointdump; run with just progs name to clear breakpoint");
- Cmd_AddCommand(&cmd_server, "prvm_globalwatchpoint", PRVM_GlobalWatchpoint_f, "marks a global as watchpoint (when this is executed, a stack trace is printed); to actually halt and investigate state, combine this with a gdb breakpoint on PRVM_Breakpoint, or with prvm_breakpointdump; run with just progs name to clear watchpoint");
- Cmd_AddCommand(&cmd_server, "prvm_edictwatchpoint", PRVM_EdictWatchpoint_f, "marks an entity field as watchpoint (when this is executed, a stack trace is printed); to actually halt and investigate state, combine this with a gdb breakpoint on PRVM_Breakpoint, or with prvm_breakpointdump; run with just progs name to clear watchpoint");
+ Cmd_AddCommand(CMD_SHARED, "prvm_edict", PRVM_ED_PrintEdict_f, "print all data about an entity number in the selected VM (server, client, menu)");
+ Cmd_AddCommand(CMD_SHARED, "prvm_edicts", PRVM_ED_PrintEdicts_f, "prints all data about all entities in the selected VM (server, client, menu)");
+ Cmd_AddCommand(CMD_SHARED, "prvm_edictcount", PRVM_ED_Count_f, "prints number of active entities in the selected VM (server, client, menu)");
+ Cmd_AddCommand(CMD_SHARED, "prvm_profile", PRVM_Profile_f, "prints execution statistics about the most used QuakeC functions in the selected VM (server, client, menu)");
+ Cmd_AddCommand(CMD_SHARED, "prvm_childprofile", PRVM_ChildProfile_f, "prints execution statistics about the most used QuakeC functions in the selected VM (server, client, menu), sorted by time taken in function with child calls");
+ Cmd_AddCommand(CMD_SHARED, "prvm_callprofile", PRVM_CallProfile_f, "prints execution statistics about the most time consuming QuakeC calls from the engine in the selected VM (server, client, menu)");
+ Cmd_AddCommand(CMD_SHARED, "prvm_fields", PRVM_Fields_f, "prints usage statistics on properties (how many entities have non-zero values) in the selected VM (server, client, menu)");
+ Cmd_AddCommand(CMD_SHARED, "prvm_globals", PRVM_Globals_f, "prints all global variables in the selected VM (server, client, menu)");
+ Cmd_AddCommand(CMD_SHARED, "prvm_global", PRVM_Global_f, "prints value of a specified global variable in the selected VM (server, client, menu)");
+ Cmd_AddCommand(CMD_SHARED, "prvm_globalset", PRVM_GlobalSet_f, "sets value of a specified global variable in the selected VM (server, client, menu)");
+ Cmd_AddCommand(CMD_SHARED, "prvm_edictset", PRVM_ED_EdictSet_f, "changes value of a specified property of a specified entity in the selected VM (server, client, menu)");
+ Cmd_AddCommand(CMD_SHARED, "prvm_edictget", PRVM_ED_EdictGet_f, "retrieves the value of a specified property of a specified entity in the selected VM (server, client menu) into a cvar or to the console");
+ Cmd_AddCommand(CMD_SHARED, "prvm_globalget", PRVM_ED_GlobalGet_f, "retrieves the value of a specified global variable in the selected VM (server, client menu) into a cvar or to the console");
+ Cmd_AddCommand(CMD_SHARED, "prvm_printfunction", PRVM_PrintFunction_f, "prints a disassembly (QuakeC instructions) of the specified function in the selected VM (server, client, menu)");
+ Cmd_AddCommand(CMD_SHARED, "cl_cmd", PRVM_GameCommand_Client_f, "calls the client QC function GameCommand with the supplied string as argument");
+ Cmd_AddCommand(CMD_SHARED, "menu_cmd", PRVM_GameCommand_Menu_f, "calls the menu QC function GameCommand with the supplied string as argument");
+ Cmd_AddCommand(CMD_SHARED, "sv_cmd", PRVM_GameCommand_Server_f, "calls the server QC function GameCommand with the supplied string as argument");
+ Cmd_AddCommand(CMD_SHARED, "prvm_breakpoint", PRVM_Breakpoint_f, "marks a statement or function as breakpoint (when this is executed, a stack trace is printed); to actually halt and investigate state, combine this with a gdb breakpoint on PRVM_Breakpoint, or with prvm_breakpointdump; run with just progs name to clear breakpoint");
+ Cmd_AddCommand(CMD_SHARED, "prvm_globalwatchpoint", PRVM_GlobalWatchpoint_f, "marks a global as watchpoint (when this is executed, a stack trace is printed); to actually halt and investigate state, combine this with a gdb breakpoint on PRVM_Breakpoint, or with prvm_breakpointdump; run with just progs name to clear watchpoint");
+ Cmd_AddCommand(CMD_SHARED, "prvm_edictwatchpoint", PRVM_EdictWatchpoint_f, "marks an entity field as watchpoint (when this is executed, a stack trace is printed); to actually halt and investigate state, combine this with a gdb breakpoint on PRVM_Breakpoint, or with prvm_breakpointdump; run with just progs name to clear watchpoint");
Cvar_RegisterVariable (&prvm_language);
Cvar_RegisterVariable (&prvm_traceqc);
Cvar_RegisterVariable (&prvm_breakpointdump);
Cvar_RegisterVariable (&prvm_reuseedicts_startuptime);
Cvar_RegisterVariable (&prvm_reuseedicts_neverinsameframe);
+ Cvar_RegisterVariable (&prvm_garbagecollection_enable);
+ Cvar_RegisterVariable (&prvm_garbagecollection_notify);
+ Cvar_RegisterVariable (&prvm_garbagecollection_scan_limit);
+ Cvar_RegisterVariable (&prvm_garbagecollection_strings);
+ Cvar_RegisterVariable (&prvm_stringdebug);
// COMMANDLINEOPTION: PRVM: -norunaway disables the runaway loop check (it might be impossible to exit DarkPlaces if used!)
prvm_runawaycheck = !COM_CheckParm("-norunaway");
PRVM_InitProg
===============
*/
-void PRVM_Prog_Init(prvm_prog_t *prog)
+void PRVM_Prog_Init(prvm_prog_t *prog, cmd_state_t *cmd)
{
PRVM_Prog_Reset(prog);
prog->leaktest_active = prvm_leaktest.integer != 0;
+ prog->console_cmd = cmd;
}
// LadyHavoc: turned PRVM_EDICT_NUM into a #define for speed reasons
if (num < 0)
{
// invalid
- VM_Warning(prog, "PRVM_GetString: Invalid string offset (%i < 0)\n", num);
+ if (prvm_stringdebug.integer)
+ VM_Warning(prog, "PRVM_GetString: Invalid string offset (%i < 0)\n", num);
return "";
}
else if (num < prog->stringssize)
return (char *)prog->tempstringsbuf.data + num;
else
{
- VM_Warning(prog, "PRVM_GetString: Invalid temp-string offset (%i >= %i prog->tempstringsbuf.cursize)\n", num, prog->tempstringsbuf.cursize);
+ if (prvm_stringdebug.integer)
+ VM_Warning(prog, "PRVM_GetString: Invalid temp-string offset (%i >= %i prog->tempstringsbuf.cursize)\n", num, prog->tempstringsbuf.cursize);
return "";
}
}
{
if (!prog->knownstrings[num])
{
- VM_Warning(prog, "PRVM_GetString: Invalid zone-string offset (%i has been freed)\n", num);
+ if (prvm_stringdebug.integer)
+ VM_Warning(prog, "PRVM_GetString: Invalid zone-string offset (%i has been freed)\n", num);
return "";
}
+ // refresh the garbage collection on the string - this guards
+ // against a certain sort of repeated migration to earlier
+ // points in the scan that could otherwise result in the string
+ // being freed for being unused
+ prog->knownstrings_flags[num] = (prog->knownstrings_flags[num] & ~KNOWNSTRINGFLAG_GCPRUNE) | KNOWNSTRINGFLAG_GCMARK;
return prog->knownstrings[num];
}
else
{
- VM_Warning(prog, "PRVM_GetString: Invalid zone-string offset (%i >= %i)\n", num, prog->numknownstrings);
+ if (prvm_stringdebug.integer)
+ VM_Warning(prog, "PRVM_GetString: Invalid zone-string offset (%i >= %i)\n", num, prog->numknownstrings);
return "";
}
}
else
{
// invalid string offset
- VM_Warning(prog, "PRVM_GetString: Invalid constant-string offset (%i >= %i prog->stringssize)\n", num, prog->stringssize);
+ if (prvm_stringdebug.integer)
+ VM_Warning(prog, "PRVM_GetString: Invalid constant-string offset (%i >= %i prog->stringssize)\n", num, prog->stringssize);
return "";
}
}
{
const char *old;
i = i - PRVM_KNOWNSTRINGBASE;
- if(i < 0 || i >= prog->numknownstrings)
- prog->error_cmd("PRVM_ChangeEngineString: s is not an engine string");
+ if (i < 0 || i >= prog->numknownstrings)
+ prog->error_cmd("PRVM_ChangeEngineString: string index %i is out of bounds", i);
+ else if ((prog->knownstrings_flags[i] & KNOWNSTRINGFLAG_ENGINE) == 0)
+ prog->error_cmd("PRVM_ChangeEngineString: string index %i is not an engine string", i);
old = prog->knownstrings[i];
prog->knownstrings[i] = s;
return old;
}
-int PRVM_SetEngineString(prvm_prog_t *prog, const char *s)
+static void PRVM_NewKnownString(prvm_prog_t *prog, int i, int flags, const char *s)
{
- int i;
- if (!s)
- return 0;
- if (s >= prog->strings && s <= prog->strings + prog->stringssize)
- prog->error_cmd("PRVM_SetEngineString: s in prog->strings area");
- // if it's in the tempstrings area, use a reserved range
- // (otherwise we'd get millions of useless string offsets cluttering the database)
- if (s >= (char *)prog->tempstringsbuf.data && s < (char *)prog->tempstringsbuf.data + prog->tempstringsbuf.maxsize)
- return prog->stringssize + (s - (char *)prog->tempstringsbuf.data);
- // see if it's a known string address
- for (i = 0;i < prog->numknownstrings;i++)
- if (prog->knownstrings[i] == s)
- return PRVM_KNOWNSTRINGBASE + i;
- // new unknown engine string
- if (developer_insane.integer)
- Con_DPrintf("new engine string %p = \"%s\"\n", s, s);
- for (i = prog->firstfreeknownstring;i < prog->numknownstrings;i++)
- if (!prog->knownstrings[i])
- break;
if (i >= prog->numknownstrings)
{
if (i >= prog->maxknownstrings)
{
const char **oldstrings = prog->knownstrings;
- const unsigned char *oldstrings_freeable = prog->knownstrings_freeable;
+ const unsigned char *oldstrings_flags = prog->knownstrings_flags;
const char **oldstrings_origin = prog->knownstrings_origin;
prog->maxknownstrings += 128;
prog->knownstrings = (const char **)PRVM_Alloc(prog->maxknownstrings * sizeof(char *));
- prog->knownstrings_freeable = (unsigned char *)PRVM_Alloc(prog->maxknownstrings * sizeof(unsigned char));
- if(prog->leaktest_active)
+ prog->knownstrings_flags = (unsigned char *)PRVM_Alloc(prog->maxknownstrings * sizeof(unsigned char));
+ if (prog->leaktest_active)
prog->knownstrings_origin = (const char **)PRVM_Alloc(prog->maxknownstrings * sizeof(char *));
if (prog->numknownstrings)
{
memcpy((char **)prog->knownstrings, oldstrings, prog->numknownstrings * sizeof(char *));
- memcpy((char **)prog->knownstrings_freeable, oldstrings_freeable, prog->numknownstrings * sizeof(unsigned char));
- if(prog->leaktest_active)
+ memcpy((char **)prog->knownstrings_flags, oldstrings_flags, prog->numknownstrings * sizeof(unsigned char));
+ if (prog->leaktest_active)
memcpy((char **)prog->knownstrings_origin, oldstrings_origin, prog->numknownstrings * sizeof(char *));
}
}
}
prog->firstfreeknownstring = i + 1;
prog->knownstrings[i] = s;
- prog->knownstrings_freeable[i] = false;
- if(prog->leaktest_active)
+ // it's in use right now, spare it until the next gc pass - that said, it is not freeable so this is probably moot
+ prog->knownstrings_flags[i] = flags;
+ if (prog->leaktest_active)
prog->knownstrings_origin[i] = NULL;
+}
+
+int PRVM_SetEngineString(prvm_prog_t *prog, const char *s)
+{
+ int i;
+ if (!s)
+ return 0;
+ if (s >= prog->strings && s <= prog->strings + prog->stringssize)
+ prog->error_cmd("PRVM_SetEngineString: s in prog->strings area");
+ // if it's in the tempstrings area, use a reserved range
+ // (otherwise we'd get millions of useless string offsets cluttering the database)
+ if (s >= (char *)prog->tempstringsbuf.data && s < (char *)prog->tempstringsbuf.data + prog->tempstringsbuf.maxsize)
+ return prog->stringssize + (s - (char *)prog->tempstringsbuf.data);
+ // see if it's a known string address
+ for (i = 0;i < prog->numknownstrings;i++)
+ if (prog->knownstrings[i] == s)
+ return PRVM_KNOWNSTRINGBASE + i;
+ // new unknown engine string
+ if (developer_insane.integer)
+ Con_DPrintf("new engine string %p = \"%s\"\n", s, s);
+ for (i = prog->firstfreeknownstring;i < prog->numknownstrings;i++)
+ if (!prog->knownstrings[i])
+ break;
+ PRVM_NewKnownString(prog, i, KNOWNSTRINGFLAG_GCMARK | KNOWNSTRINGFLAG_ENGINE, s);
return PRVM_KNOWNSTRINGBASE + i;
}
int PRVM_AllocString(prvm_prog_t *prog, size_t bufferlength, char **pointer)
{
int i;
+ char *s;
if (!bufferlength)
{
if (pointer)
for (i = prog->firstfreeknownstring;i < prog->numknownstrings;i++)
if (!prog->knownstrings[i])
break;
- if (i >= prog->numknownstrings)
- {
- if (i >= prog->maxknownstrings)
- {
- const char **oldstrings = prog->knownstrings;
- const unsigned char *oldstrings_freeable = prog->knownstrings_freeable;
- const char **oldstrings_origin = prog->knownstrings_origin;
- prog->maxknownstrings += 128;
- prog->knownstrings = (const char **)PRVM_Alloc(prog->maxknownstrings * sizeof(char *));
- prog->knownstrings_freeable = (unsigned char *)PRVM_Alloc(prog->maxknownstrings * sizeof(unsigned char));
- if(prog->leaktest_active)
- prog->knownstrings_origin = (const char **)PRVM_Alloc(prog->maxknownstrings * sizeof(char *));
- if (prog->numknownstrings)
- {
- memcpy((char **)prog->knownstrings, oldstrings, prog->numknownstrings * sizeof(char *));
- memcpy((char **)prog->knownstrings_freeable, oldstrings_freeable, prog->numknownstrings * sizeof(unsigned char));
- if(prog->leaktest_active)
- memcpy((char **)prog->knownstrings_origin, oldstrings_origin, prog->numknownstrings * sizeof(char *));
- }
- if (oldstrings)
- Mem_Free((char **)oldstrings);
- if (oldstrings_freeable)
- Mem_Free((unsigned char *)oldstrings_freeable);
- if (oldstrings_origin)
- Mem_Free((char **)oldstrings_origin);
- }
- prog->numknownstrings++;
- }
- prog->firstfreeknownstring = i + 1;
- prog->knownstrings[i] = (char *)PRVM_Alloc(bufferlength);
- prog->knownstrings_freeable[i] = true;
+ s = (char *)PRVM_Alloc(bufferlength);
+ PRVM_NewKnownString(prog, i, KNOWNSTRINGFLAG_GCMARK, s);
if(prog->leaktest_active)
prog->knownstrings_origin[i] = PRVM_AllocationOrigin(prog);
if (pointer)
num = num - PRVM_KNOWNSTRINGBASE;
if (!prog->knownstrings[num])
prog->error_cmd("PRVM_FreeString: attempt to free a non-existent or already freed string");
- if (!prog->knownstrings_freeable[num])
+ if (!prog->knownstrings_flags[num])
prog->error_cmd("PRVM_FreeString: attempt to free a string owned by the engine");
PRVM_Free((char *)prog->knownstrings[num]);
if(prog->leaktest_active)
if(prog->knownstrings_origin[num])
PRVM_Free((char *)prog->knownstrings_origin[num]);
prog->knownstrings[num] = NULL;
- prog->knownstrings_freeable[num] = false;
+ prog->knownstrings_flags[num] = 0;
prog->firstfreeknownstring = min(prog->firstfreeknownstring, num);
}
else
for (i = 0; i < prog->numknownstrings; ++i)
{
if(prog->knownstrings[i])
- if(prog->knownstrings_freeable[i])
+ if(prog->knownstrings_flags[i])
if(prog->knownstrings_origin[i])
if(!PRVM_IsStringReferenced(prog, PRVM_KNOWNSTRINGBASE + i))
{
if(!leaked)
Con_Printf("Congratulations. No leaks found.\n");
}
+
+void PRVM_GarbageCollection(prvm_prog_t *prog)
+{
+ int limit = prvm_garbagecollection_scan_limit.integer;
+ prvm_prog_garbagecollection_state_t *gc = &prog->gc;
+ if (!prvm_garbagecollection_enable.integer)
+ return;
+ // philosophy:
+ // we like to limit how much scanning we do so it doesn't put a significant
+ // burden on the cpu, so each of these are not complete scans, we also like
+ // to have consistent cpu usage so we do a bit of work on each category of
+ // leaked object every frame
+ switch (gc->stage)
+ {
+ case PRVM_GC_START:
+ gc->stage++;
+ break;
+ case PRVM_GC_GLOBALS_MARK:
+ for (; gc->globals_mark_progress < prog->numglobaldefs && (limit--) > 0; gc->globals_mark_progress++)
+ {
+ ddef_t *d = &prog->globaldefs[gc->globals_mark_progress];
+ switch (d->type)
+ {
+ case ev_string:
+ {
+ prvm_int_t s = prog->globals.ip[d->ofs];
+ if (s & PRVM_KNOWNSTRINGBASE)
+ {
+ prvm_int_t num = s - PRVM_KNOWNSTRINGBASE;
+ if (!prog->knownstrings[num])
+ {
+ // invalid
+ Con_DPrintf("PRVM_GarbageCollection: Found bogus strzone reference in global %i (global name: \"%s\"), erasing reference", d->ofs, PRVM_GetString(prog, d->s_name));
+ prog->globals.ip[d->ofs] = 0;
+ continue;
+ }
+ prog->knownstrings_flags[num] = (prog->knownstrings_flags[num] | KNOWNSTRINGFLAG_GCMARK) & ~KNOWNSTRINGFLAG_GCPRUNE;
+ }
+ }
+ break;
+ default:
+ break;
+ }
+ }
+ if (gc->globals_mark_progress >= prog->numglobaldefs)
+ gc->stage++;
+ break;
+ case PRVM_GC_FIELDS_MARK:
+ for (; gc->fields_mark_progress < prog->numfielddefs && limit > 0;)
+ {
+ ddef_t *d = &prog->fielddefs[gc->fields_mark_progress];
+ switch (d->type)
+ {
+ case ev_string:
+ //for (gc-> entityindex = 0; entityindex < prog->num_edicts; entityindex++)
+ for (;gc->fields_mark_progress_entity < prog->num_edicts && (limit--) > 0;gc->fields_mark_progress_entity++)
+ {
+ int entityindex = gc->fields_mark_progress_entity;
+ prvm_int_t s = prog->edictsfields.ip[entityindex * prog->entityfields + d->ofs];
+ if (s & PRVM_KNOWNSTRINGBASE)
+ {
+ prvm_int_t num = s - PRVM_KNOWNSTRINGBASE;
+ if (!prog->knownstrings[num])
+ {
+ // invalid
+ Con_DPrintf("PRVM_GarbageCollection: Found bogus strzone reference in edict %i field %i (field name: \"%s\"), erasing reference", entityindex, d->ofs, PRVM_GetString(prog, d->s_name));
+ prog->edictsfields.ip[entityindex * prog->entityfields + d->ofs] = 0;
+ continue;
+ }
+ prog->knownstrings_flags[num] = (prog->knownstrings_flags[num] | KNOWNSTRINGFLAG_GCMARK) & ~KNOWNSTRINGFLAG_GCPRUNE;
+ }
+ }
+ if (gc->fields_mark_progress_entity >= prog->num_edicts)
+ {
+ gc->fields_mark_progress_entity = 0;
+ gc->fields_mark_progress++;
+ }
+ break;
+ default:
+ gc->fields_mark_progress_entity = 0;
+ gc->fields_mark_progress++;
+ break;
+ }
+ }
+ if (gc->fields_mark_progress >= prog->numfielddefs)
+ gc->stage++;
+ break;
+ case PRVM_GC_KNOWNSTRINGS_SWEEP:
+ // free any strzone'd strings that are not marked
+ if (!prvm_garbagecollection_strings.integer)
+ {
+ gc->stage++;
+ break;
+ }
+ for (;gc->knownstrings_sweep_progress < prog->numknownstrings && (limit--) > 0;gc->knownstrings_sweep_progress++)
+ {
+ int num = gc->knownstrings_sweep_progress;
+ if (prog->knownstrings[num] && (prog->knownstrings_flags[num] & (KNOWNSTRINGFLAG_GCMARK | KNOWNSTRINGFLAG_ENGINE)) == 0)
+ {
+ if (prog->knownstrings_flags[num] & KNOWNSTRINGFLAG_GCPRUNE)
+ {
+ // string has been marked for pruning two passes in a row
+ if (prvm_garbagecollection_notify.integer)
+ Con_DPrintf("prvm_garbagecollection_notify: %s: freeing unreferenced string %i: \"%s\"\n", prog->name, num, prog->knownstrings[num]);
+ Mem_Free((char *)prog->knownstrings[num]);
+ prog->knownstrings[num] = NULL;
+ prog->knownstrings_flags[num] = 0;
+ prog->firstfreeknownstring = min(prog->firstfreeknownstring, num);
+ }
+ else
+ {
+ // mark it for pruning next pass
+ prog->knownstrings_flags[num] |= KNOWNSTRINGFLAG_GCPRUNE;
+ }
+ }
+ }
+ if (gc->knownstrings_sweep_progress >= prog->numknownstrings)
+ gc->stage++;
+ break;
+ case PRVM_GC_RESET:
+ default:
+ memset(gc, 0, sizeof(*gc));
+ }
+}