2010-08-31 09:21:48 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 1997-2001 Id Software, Inc.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or (at
|
|
|
|
* your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
*
|
|
|
|
* See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
|
|
* 02111-1307, USA.
|
|
|
|
*
|
|
|
|
* =======================================================================
|
|
|
|
*
|
2012-06-07 13:40:58 +00:00
|
|
|
* Zone malloc. It's just a normal malloc with tags.
|
2010-08-31 09:21:48 +00:00
|
|
|
*
|
|
|
|
* =======================================================================
|
2012-04-29 13:57:33 +00:00
|
|
|
*/
|
2010-08-31 09:21:48 +00:00
|
|
|
|
2010-09-01 08:45:26 +00:00
|
|
|
#include "header/common.h"
|
2010-08-31 09:21:48 +00:00
|
|
|
#include "header/zone.h"
|
|
|
|
|
|
|
|
#define Z_MAGIC 0x1d1d
|
|
|
|
|
2012-06-07 13:40:58 +00:00
|
|
|
zhead_t z_chain;
|
|
|
|
int z_count, z_bytes;
|
2010-08-31 09:21:48 +00:00
|
|
|
|
2012-06-07 13:40:58 +00:00
|
|
|
void
|
|
|
|
Z_Free(void *ptr)
|
2010-08-31 09:21:48 +00:00
|
|
|
{
|
2012-06-07 13:40:58 +00:00
|
|
|
zhead_t *z;
|
2010-08-31 09:21:48 +00:00
|
|
|
|
|
|
|
z = ((zhead_t *)ptr) - 1;
|
|
|
|
|
|
|
|
if (z->magic != Z_MAGIC)
|
|
|
|
{
|
2012-06-07 13:40:58 +00:00
|
|
|
printf("free: %p failed\n", ptr);
|
2010-08-31 09:21:48 +00:00
|
|
|
abort();
|
2012-06-07 13:40:58 +00:00
|
|
|
Com_Error(ERR_FATAL, "Z_Free: bad magic");
|
2010-08-31 09:21:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
z->prev->next = z->next;
|
|
|
|
z->next->prev = z->prev;
|
|
|
|
|
|
|
|
z_count--;
|
|
|
|
z_bytes -= z->size;
|
2012-06-07 13:40:58 +00:00
|
|
|
free(z);
|
2010-08-31 09:21:48 +00:00
|
|
|
}
|
|
|
|
|
2012-06-07 13:40:58 +00:00
|
|
|
void
|
|
|
|
Z_Stats_f(void)
|
2010-08-31 09:21:48 +00:00
|
|
|
{
|
2012-06-07 13:40:58 +00:00
|
|
|
Com_Printf("%i bytes in %i blocks\n", z_bytes, z_count);
|
2010-08-31 09:21:48 +00:00
|
|
|
}
|
|
|
|
|
2012-06-07 13:40:58 +00:00
|
|
|
void
|
|
|
|
Z_FreeTags(int tag)
|
2010-08-31 09:21:48 +00:00
|
|
|
{
|
2012-06-07 13:40:58 +00:00
|
|
|
zhead_t *z, *next;
|
2010-08-31 09:21:48 +00:00
|
|
|
|
2012-06-07 13:40:58 +00:00
|
|
|
for (z = z_chain.next; z != &z_chain; z = next)
|
2010-08-31 09:21:48 +00:00
|
|
|
{
|
|
|
|
next = z->next;
|
|
|
|
|
|
|
|
if (z->tag == tag)
|
2012-06-07 13:40:58 +00:00
|
|
|
{
|
|
|
|
Z_Free((void *)(z + 1));
|
|
|
|
}
|
2010-08-31 09:21:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-07 13:40:58 +00:00
|
|
|
void *
|
|
|
|
Z_TagMalloc(int size, int tag)
|
2010-08-31 09:21:48 +00:00
|
|
|
{
|
2012-06-07 13:40:58 +00:00
|
|
|
zhead_t *z;
|
2010-08-31 09:21:48 +00:00
|
|
|
|
|
|
|
size = size + sizeof(zhead_t);
|
|
|
|
z = malloc(size);
|
|
|
|
|
|
|
|
if (!z)
|
2012-06-07 13:40:58 +00:00
|
|
|
{
|
|
|
|
Com_Error(ERR_FATAL, "Z_Malloc: failed on allocation of %i bytes", size);
|
|
|
|
}
|
2010-08-31 09:21:48 +00:00
|
|
|
|
2012-06-07 13:40:58 +00:00
|
|
|
memset(z, 0, size);
|
2010-08-31 09:21:48 +00:00
|
|
|
z_count++;
|
|
|
|
z_bytes += size;
|
|
|
|
z->magic = Z_MAGIC;
|
|
|
|
z->tag = tag;
|
|
|
|
z->size = size;
|
|
|
|
|
|
|
|
z->next = z_chain.next;
|
|
|
|
z->prev = &z_chain;
|
|
|
|
z_chain.next->prev = z;
|
|
|
|
z_chain.next = z;
|
|
|
|
|
2012-06-07 13:40:58 +00:00
|
|
|
return (void *)(z + 1);
|
2010-08-31 09:21:48 +00:00
|
|
|
}
|
|
|
|
|
2012-06-07 13:40:58 +00:00
|
|
|
void *
|
|
|
|
Z_Malloc(int size)
|
2010-08-31 09:21:48 +00:00
|
|
|
{
|
2012-06-07 13:40:58 +00:00
|
|
|
return Z_TagMalloc(size, 0);
|
2010-08-31 09:21:48 +00:00
|
|
|
}
|
2012-06-07 13:40:58 +00:00
|
|
|
|