2007-01-28 03:53:06 +01:00
|
|
|
// Emacs style mode select -*- C++ -*-
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// $Id: z_zone.c,v 1.2 1997/12/29 19:51:30 pekangas Exp $
|
|
|
|
//
|
|
|
|
// Copyright (C) 1993-1996 by id Software, Inc.
|
|
|
|
//
|
|
|
|
// This source is available for distribution and/or modification
|
|
|
|
// only under the terms of the DOOM Source Code License as
|
|
|
|
// published by id Software. All rights reserved.
|
|
|
|
//
|
|
|
|
// The source is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the DOOM Source Code License
|
|
|
|
// for more details.
|
|
|
|
//
|
|
|
|
// $Log: z_zone.c,v $
|
|
|
|
// Revision 1.2 1997/12/29 19:51:30 pekangas
|
|
|
|
// Ported to WinNT/95 environment using Watcom C 10.6.
|
|
|
|
// Everything expect joystick support is implemented, but networking is
|
|
|
|
// untested. Crashes way too often with problems in FixedDiv().
|
|
|
|
// Compiles with no warnings at warning level 3, uses MIDAS 1.1.1.
|
|
|
|
//
|
|
|
|
// Revision 1.1.1.1 1997/12/28 12:59:07 pekangas
|
|
|
|
// Initial DOOM source release from id Software
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// DESCRIPTION:
|
2007-02-08 23:53:29 +01:00
|
|
|
// Zone Memory Allocation. Neat.
|
2007-01-28 03:53:06 +01:00
|
|
|
//
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
const char
|
|
|
|
z_zone_rcsid[] = "$Id: z_zone.c,v 1.2 1997/12/29 19:51:30 pekangas Exp $";
|
|
|
|
|
|
|
|
#include "z_zone.h"
|
|
|
|
#include "i_system.h"
|
|
|
|
#include "doomdef.h"
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// It is of no value to free a cachable block,
|
|
|
|
// because it will get overwritten automatically if needed.
|
|
|
|
//
|
|
|
|
|
2007-02-08 23:53:29 +01:00
|
|
|
#define ZONEID 0x1d4a11
|
2007-01-28 03:53:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
// total bytes malloced, including header
|
2007-02-08 23:53:29 +01:00
|
|
|
int size;
|
2007-01-28 03:53:06 +01:00
|
|
|
|
|
|
|
// start / end cap for linked list
|
2007-02-08 23:53:29 +01:00
|
|
|
memblock_t blocklist;
|
2007-01-28 03:53:06 +01:00
|
|
|
|
2007-02-08 23:53:29 +01:00
|
|
|
memblock_t* rover;
|
2007-01-28 03:53:06 +01:00
|
|
|
|
|
|
|
} memzone_t;
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-02-08 23:53:29 +01:00
|
|
|
memzone_t* mainzone;
|
2007-01-28 03:53:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Z_ClearZone
|
|
|
|
//
|
|
|
|
void Z_ClearZone (memzone_t* zone)
|
|
|
|
{
|
2007-02-08 23:53:29 +01:00
|
|
|
memblock_t* block;
|
|
|
|
|
2007-01-28 03:53:06 +01:00
|
|
|
// set the entire zone to one free block
|
|
|
|
zone->blocklist.next =
|
2007-02-08 23:53:29 +01:00
|
|
|
zone->blocklist.prev =
|
|
|
|
block = (memblock_t *)( (byte *)zone + sizeof(memzone_t) );
|
2007-01-28 03:53:06 +01:00
|
|
|
|
|
|
|
zone->blocklist.user = (void *)zone;
|
|
|
|
zone->blocklist.tag = PU_STATIC;
|
|
|
|
zone->rover = block;
|
2007-02-08 23:53:29 +01:00
|
|
|
|
2007-01-28 03:53:06 +01:00
|
|
|
block->prev = block->next = &zone->blocklist;
|
|
|
|
|
|
|
|
// NULL indicates a free block.
|
2007-02-08 23:53:29 +01:00
|
|
|
block->user = NULL;
|
2007-01-28 03:53:06 +01:00
|
|
|
|
|
|
|
block->size = zone->size - sizeof(memzone_t);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Z_Init
|
|
|
|
//
|
|
|
|
void Z_Init (void)
|
|
|
|
{
|
2007-02-08 23:53:29 +01:00
|
|
|
memblock_t* block;
|
|
|
|
int size;
|
2007-01-28 03:53:06 +01:00
|
|
|
|
|
|
|
mainzone = (memzone_t *)I_ZoneBase (&size);
|
|
|
|
mainzone->size = size;
|
|
|
|
|
|
|
|
// set the entire zone to one free block
|
|
|
|
mainzone->blocklist.next =
|
2007-02-08 23:53:29 +01:00
|
|
|
mainzone->blocklist.prev =
|
|
|
|
block = (memblock_t *)( (byte *)mainzone + sizeof(memzone_t) );
|
2007-01-28 03:53:06 +01:00
|
|
|
|
|
|
|
mainzone->blocklist.user = (void *)mainzone;
|
|
|
|
mainzone->blocklist.tag = PU_STATIC;
|
|
|
|
mainzone->rover = block;
|
2007-02-08 23:53:29 +01:00
|
|
|
|
2007-01-28 03:53:06 +01:00
|
|
|
block->prev = block->next = &mainzone->blocklist;
|
|
|
|
|
|
|
|
// NULL indicates a free block.
|
|
|
|
block->user = NULL;
|
|
|
|
|
|
|
|
block->size = mainzone->size - sizeof(memzone_t);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Z_Free
|
|
|
|
//
|
|
|
|
void Z_Free (void* ptr)
|
|
|
|
{
|
2007-02-08 23:53:29 +01:00
|
|
|
memblock_t* block;
|
|
|
|
memblock_t* other;
|
|
|
|
|
2007-01-28 03:53:06 +01:00
|
|
|
block = (memblock_t *) ( (byte *)ptr - sizeof(memblock_t));
|
|
|
|
|
|
|
|
if (block->id != ZONEID)
|
2007-02-08 23:53:29 +01:00
|
|
|
I_Error ("Z_Free: freed a pointer without ZONEID");
|
|
|
|
|
2007-01-28 03:53:06 +01:00
|
|
|
if (block->user > (void **)0x100)
|
|
|
|
{
|
2007-02-08 23:53:29 +01:00
|
|
|
// smaller values are not pointers
|
|
|
|
// Note: OS-dependend?
|
|
|
|
|
|
|
|
// clear the user's mark
|
|
|
|
*block->user = 0;
|
2007-01-28 03:53:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// mark as free
|
2007-02-08 23:53:29 +01:00
|
|
|
block->user = NULL;
|
2007-01-28 03:53:06 +01:00
|
|
|
block->tag = 0;
|
|
|
|
block->id = 0;
|
2007-02-08 23:53:29 +01:00
|
|
|
|
2007-01-28 03:53:06 +01:00
|
|
|
other = block->prev;
|
|
|
|
|
|
|
|
if (!other->user)
|
|
|
|
{
|
2007-02-08 23:53:29 +01:00
|
|
|
// merge with previous free block
|
|
|
|
other->size += block->size;
|
|
|
|
other->next = block->next;
|
|
|
|
other->next->prev = other;
|
2007-01-28 03:53:06 +01:00
|
|
|
|
2007-02-08 23:53:29 +01:00
|
|
|
if (block == mainzone->rover)
|
|
|
|
mainzone->rover = other;
|
2007-01-28 03:53:06 +01:00
|
|
|
|
2007-02-08 23:53:29 +01:00
|
|
|
block = other;
|
2007-01-28 03:53:06 +01:00
|
|
|
}
|
2007-02-08 23:53:29 +01:00
|
|
|
|
2007-01-28 03:53:06 +01:00
|
|
|
other = block->next;
|
|
|
|
if (!other->user)
|
|
|
|
{
|
2007-02-08 23:53:29 +01:00
|
|
|
// merge the next free block onto the end
|
|
|
|
block->size += other->size;
|
|
|
|
block->next = other->next;
|
|
|
|
block->next->prev = block;
|
2007-01-28 03:53:06 +01:00
|
|
|
|
2007-02-08 23:53:29 +01:00
|
|
|
if (other == mainzone->rover)
|
|
|
|
mainzone->rover = block;
|
2007-01-28 03:53:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Z_Malloc
|
|
|
|
// You can pass a NULL user if the tag is < PU_PURGELEVEL.
|
|
|
|
//
|
2007-02-08 23:53:29 +01:00
|
|
|
#define MINFRAGMENT 64
|
2007-01-28 03:53:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
void*
|
|
|
|
Z_Malloc
|
2007-02-08 23:53:29 +01:00
|
|
|
( int size,
|
|
|
|
int tag,
|
|
|
|
void* user )
|
2007-01-28 03:53:06 +01:00
|
|
|
{
|
2007-02-08 23:53:29 +01:00
|
|
|
int extra;
|
|
|
|
memblock_t* start;
|
2007-01-28 03:53:06 +01:00
|
|
|
memblock_t* rover;
|
|
|
|
memblock_t* newblock;
|
2007-02-08 23:53:29 +01:00
|
|
|
memblock_t* base;
|
2007-01-28 03:53:06 +01:00
|
|
|
|
|
|
|
size = (size + 3) & ~3;
|
|
|
|
|
|
|
|
// scan through the block list,
|
|
|
|
// looking for the first free block
|
|
|
|
// of sufficient size,
|
|
|
|
// throwing out any purgable blocks along the way.
|
|
|
|
|
|
|
|
// account for size of block header
|
|
|
|
size += sizeof(memblock_t);
|
|
|
|
|
|
|
|
// if there is a free block behind the rover,
|
|
|
|
// back up over them
|
|
|
|
base = mainzone->rover;
|
|
|
|
|
|
|
|
if (!base->prev->user)
|
2007-02-08 23:53:29 +01:00
|
|
|
base = base->prev;
|
|
|
|
|
2007-01-28 03:53:06 +01:00
|
|
|
rover = base;
|
|
|
|
start = base->prev;
|
2007-02-08 23:53:29 +01:00
|
|
|
|
2007-01-28 03:53:06 +01:00
|
|
|
do
|
|
|
|
{
|
2007-02-08 23:53:29 +01:00
|
|
|
if (rover == start)
|
|
|
|
{
|
|
|
|
// scanned all the way around the list
|
|
|
|
I_Error ("Z_Malloc: failed on allocation of %i bytes", size);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rover->user)
|
|
|
|
{
|
|
|
|
if (rover->tag < PU_PURGELEVEL)
|
|
|
|
{
|
|
|
|
// hit a block that can't be purged,
|
|
|
|
// so move base past it
|
|
|
|
base = rover = rover->next;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// free the rover block (adding the size to base)
|
|
|
|
|
|
|
|
// the rover can be the base block
|
|
|
|
base = base->prev;
|
|
|
|
Z_Free ((byte *)rover+sizeof(memblock_t));
|
|
|
|
base = base->next;
|
|
|
|
rover = base->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
rover = rover->next;
|
2007-01-28 03:53:06 +01:00
|
|
|
} while (base->user || base->size < size);
|
|
|
|
|
|
|
|
|
|
|
|
// found a block big enough
|
|
|
|
extra = base->size - size;
|
|
|
|
|
|
|
|
if (extra > MINFRAGMENT)
|
|
|
|
{
|
2007-02-08 23:53:29 +01:00
|
|
|
// there will be a free fragment after the allocated block
|
|
|
|
newblock = (memblock_t *) ((byte *)base + size );
|
|
|
|
newblock->size = extra;
|
|
|
|
|
|
|
|
// NULL indicates free block.
|
|
|
|
newblock->user = NULL;
|
|
|
|
newblock->tag = 0;
|
|
|
|
newblock->prev = base;
|
|
|
|
newblock->next = base->next;
|
|
|
|
newblock->next->prev = newblock;
|
|
|
|
|
|
|
|
base->next = newblock;
|
|
|
|
base->size = size;
|
2007-01-28 03:53:06 +01:00
|
|
|
}
|
2007-02-08 23:53:29 +01:00
|
|
|
|
2007-01-28 03:53:06 +01:00
|
|
|
if (user)
|
|
|
|
{
|
2007-02-08 23:53:29 +01:00
|
|
|
// mark as an in use block
|
|
|
|
base->user = user;
|
|
|
|
*(void **)user = (void *) ((byte *)base + sizeof(memblock_t));
|
2007-01-28 03:53:06 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-02-08 23:53:29 +01:00
|
|
|
if (tag >= PU_PURGELEVEL)
|
|
|
|
I_Error ("Z_Malloc: an owner is required for purgable blocks");
|
2007-01-28 03:53:06 +01:00
|
|
|
|
2007-02-08 23:53:29 +01:00
|
|
|
// mark as in use, but unowned
|
|
|
|
base->user = (void *)2;
|
2007-01-28 03:53:06 +01:00
|
|
|
}
|
|
|
|
base->tag = tag;
|
|
|
|
|
|
|
|
// next allocation will start looking here
|
2007-02-08 23:53:29 +01:00
|
|
|
mainzone->rover = base->next;
|
|
|
|
|
2007-01-28 03:53:06 +01:00
|
|
|
base->id = ZONEID;
|
|
|
|
|
|
|
|
return (void *) ((byte *)base + sizeof(memblock_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Z_FreeTags
|
|
|
|
//
|
|
|
|
void
|
|
|
|
Z_FreeTags
|
2007-02-08 23:53:29 +01:00
|
|
|
( int lowtag,
|
|
|
|
int hightag )
|
2007-01-28 03:53:06 +01:00
|
|
|
{
|
2007-02-08 23:53:29 +01:00
|
|
|
memblock_t* block;
|
|
|
|
memblock_t* next;
|
|
|
|
|
2007-01-28 03:53:06 +01:00
|
|
|
for (block = mainzone->blocklist.next ;
|
2007-02-08 23:53:29 +01:00
|
|
|
block != &mainzone->blocklist ;
|
|
|
|
block = next)
|
2007-01-28 03:53:06 +01:00
|
|
|
{
|
2007-02-08 23:53:29 +01:00
|
|
|
// get link before freeing
|
|
|
|
next = block->next;
|
|
|
|
|
|
|
|
// free block?
|
|
|
|
if (!block->user)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (block->tag >= lowtag && block->tag <= hightag)
|
|
|
|
Z_Free ( (byte *)block+sizeof(memblock_t));
|
2007-01-28 03:53:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Z_DumpHeap
|
|
|
|
// Note: TFileDumpHeap( stdout ) ?
|
|
|
|
//
|
|
|
|
void
|
|
|
|
Z_DumpHeap
|
2007-02-08 23:53:29 +01:00
|
|
|
( int lowtag,
|
|
|
|
int hightag )
|
2007-01-28 03:53:06 +01:00
|
|
|
{
|
2007-02-08 23:53:29 +01:00
|
|
|
memblock_t* block;
|
|
|
|
|
|
|
|
//printf ("zone size: %i location: %p\n",
|
|
|
|
// mainzone->size,mainzone);
|
2007-01-28 03:53:06 +01:00
|
|
|
|
2007-02-08 23:53:29 +01:00
|
|
|
//printf ("tag range: %i to %i\n",
|
|
|
|
// lowtag, hightag);
|
|
|
|
|
2007-01-28 03:53:06 +01:00
|
|
|
for (block = mainzone->blocklist.next ; ; block = block->next)
|
|
|
|
{
|
2007-02-08 23:53:29 +01:00
|
|
|
if (block->tag >= lowtag && block->tag <= hightag)
|
|
|
|
printf ("block:%p size:%7i user:%p tag:%3i\n",
|
|
|
|
block, block->size, block->user, block->tag);
|
|
|
|
|
|
|
|
if (block->next == &mainzone->blocklist)
|
|
|
|
{
|
|
|
|
// all blocks have been hit
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( (byte *)block + block->size != (byte *)block->next)
|
|
|
|
printf ("ERROR: block size does not touch the next block\n");
|
|
|
|
|
|
|
|
if ( block->next->prev != block)
|
|
|
|
printf ("ERROR: next block doesn't have proper back link\n");
|
|
|
|
|
|
|
|
if (!block->user && !block->next->user)
|
|
|
|
printf ("ERROR: two consecutive free blocks\n");
|
2007-01-28 03:53:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Z_FileDumpHeap
|
|
|
|
//
|
|
|
|
void Z_FileDumpHeap (FILE* f)
|
|
|
|
{
|
2007-02-08 23:53:29 +01:00
|
|
|
memblock_t* block;
|
|
|
|
|
|
|
|
//printf ("zone size: %i location: %p\n",mainzone->size,mainzone);
|
|
|
|
|
2007-01-28 03:53:06 +01:00
|
|
|
for (block = mainzone->blocklist.next ; ; block = block->next)
|
|
|
|
{
|
2007-02-08 23:53:29 +01:00
|
|
|
printf ("block:%p size:%7i user:%p tag:%3i\n",
|
|
|
|
block, block->size, block->user, block->tag);
|
|
|
|
|
|
|
|
if (block->next == &mainzone->blocklist)
|
|
|
|
{
|
|
|
|
// all blocks have been hit
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( (byte *)block + block->size != (byte *)block->next)
|
|
|
|
printf ("ERROR: block size does not touch the next block\n");
|
|
|
|
|
|
|
|
if ( block->next->prev != block)
|
|
|
|
printf ("ERROR: next block doesn't have proper back link\n");
|
|
|
|
|
|
|
|
if (!block->user && !block->next->user)
|
|
|
|
printf ("ERROR: two consecutive free blocks\n");
|
2007-01-28 03:53:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Z_CheckHeap
|
|
|
|
//
|
|
|
|
void Z_CheckHeap (void)
|
|
|
|
{
|
2007-02-08 23:53:29 +01:00
|
|
|
memblock_t* block;
|
|
|
|
|
2007-01-28 03:53:06 +01:00
|
|
|
for (block = mainzone->blocklist.next ; ; block = block->next)
|
|
|
|
{
|
2007-02-08 23:53:29 +01:00
|
|
|
if (block->next == &mainzone->blocklist)
|
|
|
|
{
|
|
|
|
// all blocks have been hit
|
|
|
|
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");
|
2007-01-28 03:53:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Z_ChangeTag
|
|
|
|
//
|
|
|
|
void
|
|
|
|
Z_ChangeTag2
|
2007-02-08 23:53:29 +01:00
|
|
|
( void* ptr,
|
|
|
|
int tag )
|
2007-01-28 03:53:06 +01:00
|
|
|
{
|
2007-02-08 23:53:29 +01:00
|
|
|
memblock_t* block;
|
|
|
|
|
2007-01-28 03:53:06 +01:00
|
|
|
block = (memblock_t *) ( (byte *)ptr - sizeof(memblock_t));
|
|
|
|
|
|
|
|
if (block->id != ZONEID)
|
2007-02-08 23:53:29 +01:00
|
|
|
I_Error ("Z_ChangeTag: freed a pointer without ZONEID");
|
2007-01-28 03:53:06 +01:00
|
|
|
|
|
|
|
if (tag >= PU_PURGELEVEL && (unsigned)block->user < 0x100)
|
2007-02-08 23:53:29 +01:00
|
|
|
I_Error ("Z_ChangeTag: an owner is required for purgable blocks");
|
2007-01-28 03:53:06 +01:00
|
|
|
|
|
|
|
block->tag = tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Z_FreeMemory
|
|
|
|
//
|
|
|
|
int Z_FreeMemory (void)
|
|
|
|
{
|
2007-02-08 23:53:29 +01:00
|
|
|
memblock_t* block;
|
|
|
|
int free;
|
|
|
|
|
2007-01-28 03:53:06 +01:00
|
|
|
free = 0;
|
|
|
|
|
|
|
|
for (block = mainzone->blocklist.next ;
|
2007-02-08 23:53:29 +01:00
|
|
|
block != &mainzone->blocklist;
|
|
|
|
block = block->next)
|
2007-01-28 03:53:06 +01:00
|
|
|
{
|
2007-02-08 23:53:29 +01:00
|
|
|
if (!block->user || block->tag >= PU_PURGELEVEL)
|
|
|
|
free += block->size;
|
2007-01-28 03:53:06 +01:00
|
|
|
}
|
|
|
|
return free;
|
|
|
|
}
|
|
|
|
|