void (Z_ChangeTag)(void *ptr, int tag, const char *file, int line) { memblock_t* block; block = (memblock_t*)((byte *)ptr - sizeof(memblock_t)); if(block->id != ZONEID) I_Error("Z_ChangeTag: block without a ZONEID! (%s:%d)", file, line); if(tag >= PU_PURGELEVEL && block->user == NULL) { I_Error("Z_ChangeTag: an owner is required for purgable blocks (%s:%d)", file, line); } // // Remove the block from its current list, and rehook it into // its new list. // Z_RemoveBlock(block); block->tag = tag; Z_InsertBlock(block); #ifdef ZONEFILE Z_LogPrintf("* Z_ChangeTag(ptr=%p, tag=%d, file=%s:%d)\n", ptr, tag, file, line); #endif }
static boolean ClearCache(int size) { memblock_t *block; memblock_t *next_block; int remaining; block = allocated_blocks[PU_CACHE]; if (block == NULL) { // Cache is already empty. return false; } // Search to the end of the PU_CACHE list. The blocks at the end // of the list are the ones that have been free for longer and // are more likely to be unneeded now. while (block->next != NULL) { block = block->next; } //printf("out of memory; cleaning out the cache: %i\n", test_malloced); // Search backwards through the list freeing blocks until we have // freed the amount of memory required. remaining = size; while (remaining > 0) { if (block == NULL) { // No blocks left to free; we've done our best. break; } next_block = block->prev; Z_RemoveBlock(block); remaining -= block->size; if (block->user) { *block->user = NULL; } free(block); block = next_block; } return true; }
void (Z_Free)(void* ptr, const char *file, int line) { memblock_t* block; block = (memblock_t *) ((byte *)ptr - sizeof(memblock_t)); if(block->id != ZONEID) Com_Error("Z_Free: freed a pointer without ZONEID (%s:%d)", file, line); // clear the user's mark if(block->user != NULL) *block->user = NULL; Z_RemoveBlock(block); // Free back to system free(block); }
void Z_ChangeTag2(void *ptr, int tag, char *file, int line) { memblock_t* block; block = (memblock_t *) ((byte *)ptr - sizeof(memblock_t)); if (block->id != ZONEID) I_Error("%s:%i: Z_ChangeTag: block without a ZONEID!", file, line); if (tag >= PU_PURGELEVEL && block->user == NULL) I_Error("%s:%i: Z_ChangeTag: an owner is required " "for purgable blocks", file, line); // Remove the block from its current list, and rehook it into // its new list. Z_RemoveBlock(block); block->tag = tag; Z_InsertBlock(block); }
void (Z_Free) (void *ptr, const char *file, int line) { memblock_t *block; block = (memblock_t *) ((byte *) ptr - sizeof(memblock_t)); if (block->id != ZONEID) I_Error("Z_Free: freed a pointer without ZONEID (%s:%d)", file, line); // clear the user's mark if (block->user != NULL) *block->user = NULL; Z_RemoveBlock(block); // Free back to system free(block); #ifdef ZONEFILE Z_LogPrintf("* Z_Free(ptr=%p, file=%s:%d)\n", ptr, file, line); #endif }
// // Z_Free // void Z_Free (void* ptr) { memblock_t* block; block = (memblock_t *) ((byte *)ptr - sizeof(memblock_t)); if (block->id != ZONEID) { I_Error ("Z_Free: freed a pointer without ZONEID"); } if (block->tag != PU_FREE && block->user != NULL) { // clear the user's mark *block->user = NULL; } Z_RemoveBlock(block); // Free back to system free(block); }
void *(Z_Realloc)(void *ptr, int size, int tag, void *user, const char *file, int line) { memblock_t *block; memblock_t *newblock; unsigned char *data; void *result; if(!ptr) { return (Z_Malloc)(size, tag, user, file, line); } if(size == 0) { (Z_Free)(ptr, file, line); return NULL; } if(tag < 0 || tag >= PU_MAX) { I_Error("Z_Realloc: tag out of range: %i (%s:%d)", tag, file, line); } if(user == NULL && tag >= PU_PURGELEVEL) { I_Error("Z_Realloc: an owner is required for purgable blocks (%s:%d)", file, line); } block = (memblock_t*)((byte *)ptr - sizeof(memblock_t)); newblock = NULL; if(block->id != ZONEID) { I_Error("Z_Realloc: Reallocated a pointer without ZONEID (%s:%d)", file, line); } Z_RemoveBlock(block); block->next = NULL; block->prev = NULL; if(block->user) { *block->user = NULL; } if(!(newblock = (memblock_t*)realloc(block, sizeof(memblock_t) + size))) { if(Z_ClearCache(sizeof(memblock_t) + size)) { newblock = (memblock_t*)realloc(block, sizeof(memblock_t) + size); } } if(!newblock) { I_Error("Z_Realloc: failed on allocation of %u bytes (%s:%d)", size, file, line); } newblock->tag = tag; newblock->id = ZONEID; newblock->user = user; newblock->size = size; Z_InsertBlock(newblock); data = (unsigned char*)newblock; result = data + sizeof(memblock_t); if(user != NULL) { *newblock->user = result; } #ifdef ZONEFILE Z_LogPrintf("* %p = Z_Realloc(ptr=%p, n=%lu, tag=%d, user=%p, source=%s:%d)\n", result, ptr, size, tag, user, file, line); #endif return result; }