123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478 |
- #include "Precompiled.h"
- #include "z_zone.h"
- #include "i_system.h"
- #include "doomdef.h"
- #include "globaldata.h"
-
- #define ZONEID 0x1d4a11
- void Z_ClearZone (memzone_t* zone)
- {
- memblock_t* block;
-
-
- zone->blocklist.next =
- zone->blocklist.prev =
- block = (memblock_t *)( (byte *)zone + sizeof(memzone_t) );
-
- zone->blocklist.user = (void **)zone;
- zone->blocklist.tag = PU_STATIC;
- zone->rover = block;
-
- block->prev = block->next = &zone->blocklist;
-
-
- block->user = NULL;
- block->size = zone->size - sizeof(memzone_t);
- }
- void *I_ZoneBase( int *size )
- {
- enum
- {
- HEAP_SIZE = 15 * 1024 * 1024
- };
- *size = HEAP_SIZE;
- return malloc( HEAP_SIZE );
- }
- void Z_Init (void)
- {
- memblock_t* block;
- int size;
- ::g->mainzone = (memzone_t *)I_ZoneBase (&size);
- memset( ::g->mainzone, 0, size );
- ::g->mainzone->size = size;
-
- ::g->mainzone->blocklist.next =
- ::g->mainzone->blocklist.prev =
- block = (memblock_t *)( (byte *)::g->mainzone + sizeof(memzone_t) );
- ::g->mainzone->blocklist.user = (void **)::g->mainzone;
- ::g->mainzone->blocklist.tag = PU_STATIC;
- ::g->mainzone->rover = block;
-
- block->prev = block->next = &::g->mainzone->blocklist;
-
- block->user = NULL;
-
- block->size = ::g->mainzone->size - sizeof(memzone_t);
- }
- int NumAlloc = 0;
- void Z_Free (void* ptr)
- {
- memblock_t* block;
- memblock_t* other;
- block = (memblock_t *) ( (byte *)ptr - sizeof(memblock_t));
- NumAlloc -= block->size;
- if (block->id != ZONEID)
- I_Error ("Z_Free: freed a pointer without ZONEID");
-
- if (block->user > (void **)0x100)
- {
-
-
-
-
- *block->user = 0;
- }
-
- block->user = NULL;
- block->tag = 0;
- block->id = 0;
-
- other = block->prev;
- if (!other->user)
- {
-
- other->size += block->size;
- other->next = block->next;
- other->next->prev = other;
- if (block == ::g->mainzone->rover)
- ::g->mainzone->rover = other;
- block = other;
- }
-
- other = block->next;
- if (!other->user)
- {
-
- block->size += other->size;
- block->next = other->next;
- block->next->prev = block;
- if (other == ::g->mainzone->rover)
- ::g->mainzone->rover = block;
- }
- }
- #define MINFRAGMENT 64
- void*
- Z_Malloc
- ( int size,
- int tag,
- void* user )
- {
-
- int extra;
- memblock_t* start;
- memblock_t* rover;
- memblock_t* newblock;
- memblock_t* base;
- NumAlloc += size;
-
- size = (size + 3) & ~3;
-
-
-
-
-
-
- size += sizeof(memblock_t);
-
-
-
- base = ::g->mainzone->rover;
-
- if (!base->prev->user)
- base = base->prev;
-
- rover = base;
- start = base->prev;
-
- do
- {
- if (rover == start)
- {
-
- I_Error ("Z_Malloc: failed on allocation of %i bytes", size);
- }
-
- if (rover->user)
- {
- if (rover->tag < PU_PURGELEVEL)
- {
-
-
- base = rover = rover->next;
- }
- else
- {
-
-
- base = base->prev;
- Z_Free ((byte *)rover+sizeof(memblock_t));
- base = base->next;
- rover = base->next;
- }
- }
- else
- rover = rover->next;
- } while (base->user || base->size < size);
-
-
- extra = base->size - size;
-
- if (extra > MINFRAGMENT)
- {
-
- newblock = (memblock_t *) ((byte *)base + size );
- newblock->size = extra;
-
-
- newblock->user = NULL;
- newblock->tag = 0;
- newblock->prev = base;
- newblock->next = base->next;
- newblock->next->prev = newblock;
- base->next = newblock;
- base->size = size;
- }
-
- if (user)
- {
-
- base->user = (void**)user;
- *(void **)user = (void *) ((byte *)base + sizeof(memblock_t));
- }
- else
- {
- if (tag >= PU_PURGELEVEL)
- I_Error ("Z_Malloc: an owner is required for purgable blocks");
-
- base->user = (void **)2;
- }
- base->tag = tag;
-
- ::g->mainzone->rover = base->next;
-
- base->id = ZONEID;
-
- return (void *) ((byte *)base + sizeof(memblock_t));
- }
- void
- Z_FreeTags
- ( int lowtag,
- int hightag )
- {
- memblock_t* block;
- memblock_t* next;
-
- for (block = ::g->mainzone->blocklist.next ;
- block != &::g->mainzone->blocklist ;
- block = next)
- {
-
- next = block->next;
-
- if (!block->user)
- continue;
-
- if (block->tag >= lowtag && block->tag <= hightag)
- Z_Free ( (byte *)block+sizeof(memblock_t));
- }
- }
- void
- Z_DumpHeap
- ( int lowtag,
- int hightag )
- {
- memblock_t* block;
-
- I_Printf ("zone size: %i location: %p\n",
- ::g->mainzone->size,::g->mainzone);
-
- I_Printf ("tag range: %i to %i\n",
- lowtag, hightag);
-
- for (block = ::g->mainzone->blocklist.next ; ; block = block->next)
- {
- if (block->tag >= lowtag && block->tag <= hightag)
- I_Printf ("block:%p size:%7i user:%p tag:%3i\n",
- block, block->size, block->user, block->tag);
-
- if (block->next == &::g->mainzone->blocklist)
- {
-
- break;
- }
-
- if ( (byte *)block + block->size != (byte *)block->next)
- I_Printf ("ERROR: block size does not touch the next block\n");
- if ( block->next->prev != block)
- I_Printf ("ERROR: next block doesn't have proper back link\n");
- if (!block->user && !block->next->user)
- I_Printf ("ERROR: two consecutive free blocks\n");
- }
- }
- void Z_FileDumpHeap (FILE* f)
- {
- memblock_t* block;
-
- fprintf (f,"zone size: %i location: %p\n",::g->mainzone->size,::g->mainzone);
-
- for (block = ::g->mainzone->blocklist.next ; ; block = block->next)
- {
- fprintf (f,"block:%p size:%7i user:%p tag:%3i\n",
- block, block->size, block->user, block->tag);
-
- if (block->next == &::g->mainzone->blocklist)
- {
-
- break;
- }
-
- if ( (byte *)block + block->size != (byte *)block->next)
- fprintf (f,"ERROR: block size does not touch the next block\n");
- if ( block->next->prev != block)
- fprintf (f,"ERROR: next block doesn't have proper back link\n");
- if (!block->user && !block->next->user)
- fprintf (f,"ERROR: two consecutive free blocks\n");
- }
- }
- void Z_CheckHeap (void)
- {
- memblock_t* block;
-
- for (block = ::g->mainzone->blocklist.next ; ; block = block->next)
- {
- if (block->next == &::g->mainzone->blocklist)
- {
-
- break;
- }
-
- if ( (byte *)block + block->size != (byte *)block->next)
- I_Error ("Z_CheckHeap: block size does not touch the next block\n");
- if ( block->next->prev != block)
- I_Error ("Z_CheckHeap: next block doesn't have proper back link\n");
- if (!block->user && !block->next->user)
- I_Error ("Z_CheckHeap: two consecutive free blocks\n");
- }
- }
- void
- Z_ChangeTag2
- ( void* ptr,
- int tag )
- {
- memblock_t* block;
-
- block = (memblock_t *) ( (byte *)ptr - sizeof(memblock_t));
- if (block->id != ZONEID)
- I_Error ("Z_ChangeTag: freed a pointer without ZONEID");
- if (tag >= PU_PURGELEVEL && (unsigned)block->user < 0x100)
- I_Error ("Z_ChangeTag: an owner is required for purgable blocks");
- block->tag = tag;
- }
- void Z_ChangeTag2( void** pp, int tag ) { Z_ChangeTag2( *pp, tag ); }
- int Z_FreeMemory (void)
- {
- memblock_t* block;
- int free;
-
- free = 0;
-
- for (block = ::g->mainzone->blocklist.next ;
- block != &::g->mainzone->blocklist;
- block = block->next)
- {
- if (!block->user || block->tag >= PU_PURGELEVEL)
- free += block->size;
- }
- return free;
- }
|