- int size; // total bytes malloced, including header
- memblock_t blocklist; // start / end cap for linked list
- memblock_t *rover;
-} memzone_t;
-
-void Cache_FreeLow (int new_low_hunk);
-void Cache_FreeHigh (int new_high_hunk);
-
-
-/*
-==============================================================================
-
- ZONE MEMORY ALLOCATION
-
-There is never any space between memblocks, and there will never be two
-contiguous free memblocks.
-
-The rover can be left pointing at a non-empty block
-
-The zone calls are pretty much only used for small strings and structures,
-all big things are allocated on the hunk.
-==============================================================================
-*/
-
-memzone_t *mainzone;
-
-void Z_ClearZone (memzone_t *zone, int size);
-
-
-/*
-========================
-Z_ClearZone
-========================
-*/
-void Z_ClearZone (memzone_t *zone, int size)
-{
- memblock_t *block;
-
-// set the entire zone to one free block
-
- zone->blocklist.next = zone->blocklist.prev = block =
- (memblock_t *)( (byte *)zone + sizeof(memzone_t) );
- zone->blocklist.tag = 1; // in use block
- zone->blocklist.id = 0;
- zone->blocklist.size = 0;
- zone->rover = block;
-
- block->prev = block->next = &zone->blocklist;
- block->tag = 0; // free block
- block->id = ZONEID;
- block->size = size - sizeof(memzone_t);
-}
-
-
-/*
-========================
-Z_Free
-========================
-*/
-void Z_Free (void *ptr)
-{
- memblock_t *block, *other;
-
- if (!ptr)
- Sys_Error ("Z_Free: NULL pointer");
-
- block = (memblock_t *) ( (byte *)ptr - sizeof(memblock_t));
- if (block->id != ZONEID)
- Sys_Error ("Z_Free: freed a pointer without ZONEID");
- if (block->tag == 0)
- Sys_Error ("Z_Free: freed a freed pointer");
-
- block->tag = 0; // mark as free
-
- other = block->prev;
- if (!other->tag)
- { // merge with previous free block
- other->size += block->size;
- other->next = block->next;
- other->next->prev = other;
- if (block == mainzone->rover)
- mainzone->rover = other;
- block = other;
- }
-
- other = block->next;
- if (!other->tag)
- { // merge the next free block onto the end
- block->size += other->size;
- block->next = other->next;
- block->next->prev = block;
- if (other == mainzone->rover)
- mainzone->rover = block;
- }
-}
-
-
-/*
-========================
-Z_Malloc
-========================
-*/
-void *Z_Malloc (int size)
-{
- void *buf;
-
-Z_CheckHeap (); // DEBUG
- buf = Z_TagMalloc (size, 1);
- if (!buf)
- Sys_Error ("Z_Malloc: failed on allocation of %i bytes",size);
- memset (buf, 0, size);
-
- return buf;
-}
-
-void *Z_TagMalloc (int size, int tag)
-{
- int extra;
- memblock_t *start, *rover, *new, *base;
-
- if (!tag)
- Sys_Error ("Z_TagMalloc: tried to use a 0 tag");
-
-//
-// scan through the block list looking for the first free block
-// of sufficient size
-//
- size += sizeof(memblock_t); // account for size of block header
- size += 4; // space for memory trash tester
- size = (size + 7) & ~7; // align to 8-byte boundary
-
- base = rover = mainzone->rover;
- start = base->prev;
-
- do
+#if MEMCLUMPING
+ int i, j, k, needed, endbit, largest;
+ memclump_t *clump, **clumpchainpointer;
+#endif
+ memheader_t *mem;
+ if (size <= 0)
+ return NULL;
+ if (pool == NULL)
+ Sys_Error("Mem_Alloc: pool == NULL (alloc at %s:%i)", filename, fileline);
+ Con_DPrintf("Mem_Alloc: pool %s, file %s:%i, size %i bytes\n", pool->name, filename, fileline, size);
+ pool->totalsize += size;
+#if MEMCLUMPING
+ if (size < 4096)