static void JKG_Mem_RemoveBlock(void* ptr) { unsigned int i, idx = -1; if (*(int *)((int)ptr-4) != ALLOCMARKER) { // Alloc aint ours for sure return; } for(i = 0; i<BlockCount; i++){ if(ptr == MemBlocks[i].ptr){ idx = i; break; } } if(idx == -1) return; // Not one of our blocks, dont try to free // Free the block and remove the entry from the array #ifndef __linux__ (int)MemBlocks[idx].ptr -= 4; #else { // linux doesnt like -= 4 on pointers >.> int tmp = (int)MemBlocks[idx].ptr; tmp -= 4; MemBlocks[idx].ptr = (void *)tmp; } #endif trap_TrueFree(&MemBlocks[idx].ptr); JKG_Arrays_RemoveArrayElement((void **)&MemBlocks, idx, sizeof(JKGMemBlock_t), &BlockCount); }
void G_TerminateMemory( void ) { // Called in G_ShutdownGame, frees all allocations // Must be done to avoid memory leaking through map changes unsigned int i; for (i=0; i<BlockCount; i++) { #ifndef __linux__ (int)MemBlocks[i].ptr -= 4; #else { // linux doesnt like -= 4 on pointers >.> int tmp = (int)MemBlocks[i].ptr; tmp -= 4; MemBlocks[i].ptr = (void *)tmp; } #endif trap_TrueFree(&MemBlocks[i].ptr); } BlockCount = 0; JKG_Arrays_RemoveAllElements(&MemBlocks); }
void strap_TrueFree(void **ptr) { trap_TrueFree(ptr); }