implement Z_Realloc, PR_Zone_Realloc and pr_obj_realloc

This commit is contained in:
Bill Currie 2002-05-29 20:58:53 +00:00
parent ce8fbadf41
commit a7b5b56a2c
5 changed files with 46 additions and 4 deletions

View file

@ -234,6 +234,7 @@ void *PR_Resources_Find (progs_t *pr, const char *name);
void PR_Zone_Init (progs_t *pr);
void PR_Zone_Free (progs_t *pr, void *ptr);
void *PR_Zone_Malloc (progs_t *pr, int size);
void *PR_Zone_Realloc (progs_t *pr, void *ptr, int size);
//
// PR Debug stuff

View file

@ -102,6 +102,7 @@ void Z_ClearZone (memzone_t *zone, int size);
void Z_Free (memzone_t *zone, void *ptr);
void *Z_Malloc (memzone_t *zone, int size); // returns 0 filled memory
void *Z_TagMalloc (memzone_t *zone, int size, int tag);
void *Z_Realloc (memzone_t *zone, void *ptr, int size);
void Z_CheckHeap (memzone_t *zone);
void *Hunk_Alloc (int size); // returns 0 filled memory

View file

@ -314,10 +314,11 @@ pr_obj_valloc (progs_t *pr)
static void
pr_obj_realloc (progs_t *pr)
{
//void *mem = (void*)P_POINTER (pr, 0);
//int size = P_INT (pr, 1) * sizeof (pr_type_t);
//XXX
PR_RunError (pr, "%s, not implemented", __FUNCTION__);
void *mem = (void*)P_POINTER (pr, 0);
int size = P_INT (pr, 1) * sizeof (pr_type_t);
mem = PR_Zone_Realloc (pr, mem, size);
R_INT (pr) = POINTER_TO_PROG (pr, mem);
}
static void

View file

@ -64,3 +64,9 @@ PR_Zone_Malloc (progs_t *pr, int size)
{
return Z_Malloc (pr->zone, size);
}
void *
PR_Zone_Realloc (progs_t *pr, void *ptr, int size)
{
return Z_Realloc (pr->zone, ptr, size);
}

View file

@ -60,6 +60,8 @@ static const char rcsid[] =
#include "QF/sys.h"
#include "QF/zone.h"
#include "compat.h"
#define ZONEID 0x1d4a11
#define HUNK_SENTINAL 0x1df001ed
@ -95,6 +97,7 @@ struct memzone_s
int size; // total bytes malloced, including header
memblock_t blocklist; // start / end cap for linked list
memblock_t *rover;
int pad; // pad to 64 bit boundary
};
@ -225,6 +228,36 @@ void
return (void *) ((byte *)base + sizeof(memblock_t));
}
void *
Z_Realloc (memzone_t *zone, void *ptr, int size)
{
memblock_t *block;
int old_size;
void *old_ptr;
if (!ptr)
return Z_Malloc (zone, size);
block = (memblock_t *) ( (byte *)ptr - sizeof(memblock_t));
if (block->id != ZONEID)
Sys_Error ("Z_Realloc: realloced a pointer without ZONEID");
if (block->tag == 0)
Sys_Error ("Z_Realloc: realloced a freed pointer");
old_size = block->size;
old_ptr = ptr;
Z_Free (zone, ptr);
ptr = Z_TagMalloc (zone, size, 1);
if (!ptr)
Sys_Error ("Z_Realloc: failed on allocation of %i bytes", size);
if (ptr != old_ptr)
memmove (ptr, old_ptr, min (old_size, size));
return ptr;
}
void
Z_Print (memzone_t *zone)
{