+
+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));
+ }
+}