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 }
void *Z_Malloc(int size, int tag, void *user) { memblock_t *newblock; unsigned char *data; void *result; if (tag < 0 || tag >= PU_NUM_TAGS || tag == PU_FREE) { I_Error("Z_Malloc: attempted to allocate a block with an invalid " "tag: %i", tag); } if (user == NULL && tag >= PU_PURGELEVEL) { I_Error ("Z_Malloc: an owner is required for purgable blocks"); } // Malloc a block of the required size newblock = NULL; while (newblock == NULL) { newblock = (memblock_t *) malloc(sizeof(memblock_t) + size); if (newblock == NULL) { if (!ClearCache(sizeof(memblock_t) + size)) { I_Error("Z_Malloc: failed on allocation of %i bytes", size); } } } newblock->tag = tag; // Hook into the linked list for this tag type 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; } return result; }
void *(Z_Malloc) (int size, int tag, void *user, const char *file, int line) { memblock_t *newblock; unsigned char *data; void *result; if (tag < 0 || tag >= PU_MAX) I_Error("Z_Malloc: tag out of range: %i (%s:%d)", tag, file, line); if (user == NULL && tag >= PU_PURGELEVEL) I_Error ("Z_Malloc: an owner is required for purgable blocks (%s:%d)", file, line); // Malloc a block of the required size newblock = NULL; if (!(newblock = (memblock_t *) malloc(sizeof(memblock_t) + size))) { if (Z_ClearCache(sizeof(memblock_t) + size)) newblock = (memblock_t *) malloc(sizeof(memblock_t) + size); } if (!newblock) I_Error("Z_Malloc: failed on allocation of %u bytes (%s:%d)", size, file, line); // Hook into the linked list for this tag type 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_Malloc(size=%lu, tag=%d, user=%p, source=%s:%d)\n", result, size, tag, user, file, line); #endif return result; }
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_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; }