[util] Make zone functions 64-bit clean

This allows nq and qw clients/servers to use over 2GB of memory if
necessary.
This commit is contained in:
Bill Currie 2021-07-26 15:43:57 +09:00
parent 5fc1a36d92
commit c02fcee58a
5 changed files with 81 additions and 83 deletions

View file

@ -92,31 +92,32 @@
typedef struct memzone_s memzone_t; typedef struct memzone_s memzone_t;
void Memory_Init (void *buf, int size); void Memory_Init (void *buf, size_t size);
void Z_ClearZone (memzone_t *zone, int size, int zone_offset, int ele_size); void Z_ClearZone (memzone_t *zone, size_t size, size_t zone_offset,
size_t ele_size);
void Z_Free (memzone_t *zone, void *ptr); void Z_Free (memzone_t *zone, void *ptr);
void *Z_Malloc (memzone_t *zone, int size); // returns 0 filled memory void *Z_Malloc (memzone_t *zone, size_t size); // returns 0 filled memory
void *Z_TagMalloc (memzone_t *zone, int size, int tag); void *Z_TagMalloc (memzone_t *zone, size_t size, int tag);
void *Z_Realloc (memzone_t *zone, void *ptr, int size); void *Z_Realloc (memzone_t *zone, void *ptr, size_t size);
void Z_Print (memzone_t *zone); void Z_Print (memzone_t *zone);
void Z_CheckHeap (memzone_t *zone); void Z_CheckHeap (memzone_t *zone);
void Z_SetError (memzone_t *zone, void (*err) (void *data, const char *msg), void Z_SetError (memzone_t *zone, void (*err) (void *data, const char *msg),
void *data); void *data);
void Z_CheckPointer (const memzone_t *zone, const void *ptr, int size); void Z_CheckPointer (const memzone_t *zone, const void *ptr, size_t size);
void Hunk_Print (qboolean all); void Hunk_Print (qboolean all);
void *Hunk_Alloc (int size); // returns 0 filled memory void *Hunk_Alloc (size_t size); // returns 0 filled memory
void *Hunk_AllocName (int size, const char *name); void *Hunk_AllocName (size_t size, const char *name);
int Hunk_LowMark (void) __attribute__((pure)); size_t Hunk_LowMark (void) __attribute__((pure));
void Hunk_FreeToLowMark (int mark); void Hunk_FreeToLowMark (size_t mark);
void *Hunk_TempAlloc (int size); void *Hunk_TempAlloc (size_t size);
void Hunk_Check (void); void Hunk_Check (void);
struct cache_user_s; struct cache_user_s;
typedef void *(*cache_allocator_t) (struct cache_user_s *c, int size, const char *name); typedef void *(*cache_allocator_t) (struct cache_user_s *c, size_t size, const char *name);
typedef void (*cache_loader_t) (void *object, cache_allocator_t allocator); typedef void (*cache_loader_t) (void *object, cache_allocator_t allocator);
typedef struct cache_user_s { typedef struct cache_user_s {
void *data; void *data;
@ -130,7 +131,7 @@ void *Cache_Check (cache_user_t *c);
// if present, otherwise returns NULL // if present, otherwise returns NULL
void Cache_Free (cache_user_t *c); void Cache_Free (cache_user_t *c);
void *Cache_Alloc (cache_user_t *c, int size, const char *name); void *Cache_Alloc (cache_user_t *c, size_t size, const char *name);
// Returns NULL if all purgable data was tossed and there still // Returns NULL if all purgable data was tossed and there still
// wasn't enough room. // wasn't enough room.
void Cache_Report (void); void Cache_Report (void);

View file

@ -70,22 +70,21 @@ static qboolean Cache_FreeLRU (void);
all big things are allocated on the hunk. all big things are allocated on the hunk.
*/ */
typedef struct memblock_s typedef struct memblock_s {
{ size_t block_size; // including the header and possibly tiny fragments
int block_size; // including the header and possibly tiny fragments struct memblock_s *next;
struct memblock_s *prev;
size_t size; // requested size
int tag; // a tag of 0 is a free block int tag; // a tag of 0 is a free block
struct memblock_s *next, *prev;
int size; // requested size
int id; // should be ZONEID int id; // should be ZONEID
//int id2; // pad to 64 bit boundary //int id2; // pad to 64 bit boundary
} memblock_t; } memblock_t;
struct memzone_s struct memzone_s {
{ size_t size; // total bytes malloced, including header
int size; // total bytes malloced, including header size_t used; // ammount used, including header
int used; // ammount used, including header size_t offset;
int offset; size_t ele_size;
int ele_size;
void (*error) (void *, const char *); void (*error) (void *, const char *);
void *data; void *data;
memblock_t blocklist; // start / end cap for linked list memblock_t blocklist; // start / end cap for linked list
@ -108,7 +107,7 @@ z_offset (memzone_t *zone, memblock_t *block)
} }
VISIBLE void VISIBLE void
Z_ClearZone (memzone_t *zone, int size, int zone_offset, int ele_size) Z_ClearZone (memzone_t *zone, size_t size, size_t zone_offset, size_t ele_size)
{ {
memblock_t *block; memblock_t *block;
@ -201,7 +200,7 @@ Z_Free (memzone_t *zone, void *ptr)
} }
VISIBLE void * VISIBLE void *
Z_Malloc (memzone_t *zone, int size) Z_Malloc (memzone_t *zone, size_t size)
{ {
void *buf; void *buf;
@ -210,7 +209,7 @@ Z_Malloc (memzone_t *zone, int size)
buf = Z_TagMalloc (zone, size, 1); buf = Z_TagMalloc (zone, size, 1);
if (!buf) { if (!buf) {
const char *msg; const char *msg;
msg = nva ("Z_Malloc: failed on allocation of %i bytes",size); msg = nva ("Z_Malloc: failed on allocation of %zd bytes", size);
if (zone->error) if (zone->error)
zone->error (zone->data, msg); zone->error (zone->data, msg);
Sys_Error ("%s", msg); Sys_Error ("%s", msg);
@ -221,7 +220,7 @@ Z_Malloc (memzone_t *zone, int size)
} }
void * void *
Z_TagMalloc (memzone_t *zone, int size, int tag) Z_TagMalloc (memzone_t *zone, size_t size, int tag)
{ {
int extra; int extra;
int requested_size = size; int requested_size = size;
@ -284,9 +283,9 @@ Z_TagMalloc (memzone_t *zone, int size, int tag)
} }
VISIBLE void * VISIBLE void *
Z_Realloc (memzone_t *zone, void *ptr, int size) Z_Realloc (memzone_t *zone, void *ptr, size_t size)
{ {
int old_size; size_t old_size;
memblock_t *block; memblock_t *block;
void *old_ptr; void *old_ptr;
@ -315,7 +314,7 @@ Z_Realloc (memzone_t *zone, void *ptr, int size)
ptr = Z_TagMalloc (zone, size, 1); ptr = Z_TagMalloc (zone, size, 1);
if (!ptr) { if (!ptr) {
const char *msg; const char *msg;
msg = nva ("Z_Realloc: failed on allocation of %i bytes", size); msg = nva ("Z_Realloc: failed on allocation of %zd bytes", size);
if (zone->error) if (zone->error)
zone->error (zone->data, msg); zone->error (zone->data, msg);
Sys_Error ("%s", msg); Sys_Error ("%s", msg);
@ -334,7 +333,7 @@ Z_Print (memzone_t *zone)
{ {
memblock_t *block; memblock_t *block;
Sys_Printf ("zone size: %i location: %p used: %i\n", Sys_Printf ("zone size: %zd location: %p used: %zd\n",
zone->size, zone, zone->used); zone->size, zone, zone->used);
for (block = zone->blocklist.next ; ; block = block->next) { for (block = zone->blocklist.next ; ; block = block->next) {
@ -386,7 +385,7 @@ Z_SetError (memzone_t *zone, void (*err) (void *, const char *), void *data)
} }
void void
Z_CheckPointer (const memzone_t *zone, const void *ptr, int size) Z_CheckPointer (const memzone_t *zone, const void *ptr, size_t size)
{ {
const memblock_t *block; const memblock_t *block;
const char *block_mem; const char *block_mem;
@ -411,17 +410,19 @@ Z_CheckPointer (const memzone_t *zone, const void *ptr, int size)
//============================================================================ //============================================================================
typedef struct { typedef struct {
int sentinal; int sentinal1;
int size; // including sizeof(hunk_t), -1 = not allocated int sentinal2;
size_t size; // including sizeof(hunk_t), -1 = not allocated
char name[8]; char name[8];
char fill[48]; // pad out to 64 bytes // pad out to 64 bytes
char fill[64 - 2 * sizeof (int) - sizeof (size_t) - 8];
} hunk_t; } hunk_t;
byte *hunk_base; byte *hunk_base;
int hunk_size; size_t hunk_size;
int hunk_low_used; size_t hunk_low_used;
int hunk_high_used; size_t hunk_high_used;
int hunk_tempmark; size_t hunk_tempmark;
qboolean hunk_tempactive; qboolean hunk_tempactive;
@ -435,11 +436,11 @@ Hunk_Check (void)
{ {
hunk_t *h; hunk_t *h;
for (h = (hunk_t *) hunk_base; (byte *) h != hunk_base + hunk_low_used;) { for (h = (hunk_t *) hunk_base; (byte *) h < hunk_base + hunk_low_used; ) {
if (h->sentinal != HUNK_SENTINAL) if (h->sentinal1 != HUNK_SENTINAL || h->sentinal2 != HUNK_SENTINAL)
Sys_Error ("Hunk_Check: trashed sentinal"); Sys_Error ("Hunk_Check: trashed sentinal");
if (h->size < (int) sizeof (hunk_t) if (h->size < sizeof (hunk_t)
|| h->size + (byte *) h - hunk_base > hunk_size) || h->size + (byte *) h > hunk_base + hunk_size)
Sys_Error ("Hunk_Check: bad size"); Sys_Error ("Hunk_Check: bad size");
h = (hunk_t *) ((byte *) h + h->size); h = (hunk_t *) ((byte *) h + h->size);
} }
@ -468,14 +469,14 @@ Hunk_Print (qboolean all)
starthigh = (hunk_t *) (hunk_base + hunk_size - hunk_high_used); starthigh = (hunk_t *) (hunk_base + hunk_size - hunk_high_used);
endhigh = (hunk_t *) (hunk_base + hunk_size); endhigh = (hunk_t *) (hunk_base + hunk_size);
Sys_Printf (" :%8i total hunk size\n", hunk_size); Sys_Printf (" :%8zd total hunk size\n", hunk_size);
Sys_Printf ("-------------------------\n"); Sys_Printf ("-------------------------\n");
while (1) { while (1) {
// skip to the high hunk if done with low hunk // skip to the high hunk if done with low hunk
if (h == endlow) { if (h == endlow) {
Sys_Printf ("-------------------------\n"); Sys_Printf ("-------------------------\n");
Sys_Printf (" :%8i REMAINING\n", Sys_Printf (" :%8zd REMAINING\n",
hunk_size - hunk_low_used - hunk_high_used); hunk_size - hunk_low_used - hunk_high_used);
Sys_Printf ("-------------------------\n"); Sys_Printf ("-------------------------\n");
h = starthigh; h = starthigh;
@ -485,10 +486,10 @@ Hunk_Print (qboolean all)
break; break;
// run consistancy checks // run consistancy checks
if (h->sentinal != HUNK_SENTINAL) if (h->sentinal1 != HUNK_SENTINAL || h->sentinal2 != HUNK_SENTINAL)
Sys_Error ("Hunk_Check: trahsed sentinal"); Sys_Error ("Hunk_Check: trahsed sentinal");
if (h->size < (int) sizeof (hunk_t) if (h->size < (int) sizeof (hunk_t)
|| h->size + (byte *) h - hunk_base > hunk_size) || h->size + (byte *) h > hunk_base + hunk_size)
Sys_Error ("Hunk_Check: bad size"); Sys_Error ("Hunk_Check: bad size");
next = (hunk_t *) ((byte *) h + h->size); next = (hunk_t *) ((byte *) h + h->size);
@ -498,7 +499,7 @@ Hunk_Print (qboolean all)
// print the single block // print the single block
if (all) if (all)
Sys_Printf ("%8p :%8i %8.8s\n", h, h->size, h->name); Sys_Printf ("%8p :%8zd %8.8s\n", h, h->size, h->name);
// print the total // print the total
if (next == endlow || next == endhigh || if (next == endlow || next == endhigh ||
@ -517,14 +518,14 @@ Hunk_Print (qboolean all)
} }
static void static void
Hunk_FreeToHighMark (int mark) Hunk_FreeToHighMark (size_t mark)
{ {
if (hunk_tempactive) { if (hunk_tempactive) {
hunk_tempactive = false; hunk_tempactive = false;
Hunk_FreeToHighMark (hunk_tempmark); Hunk_FreeToHighMark (hunk_tempmark);
} }
if (mark < 0 || mark > hunk_high_used) if (mark > hunk_high_used)
Sys_Error ("Hunk_FreeToHighMark: bad mark %i", mark); Sys_Error ("Hunk_FreeToHighMark: bad mark %zd", mark);
memset (hunk_base + hunk_size - hunk_high_used, 0, hunk_high_used - mark); memset (hunk_base + hunk_size - hunk_high_used, 0, hunk_high_used - mark);
hunk_high_used = mark; hunk_high_used = mark;
} }
@ -541,7 +542,7 @@ Hunk_HighMark (void)
} }
VISIBLE void * VISIBLE void *
Hunk_AllocName (int size, const char *name) Hunk_AllocName (size_t size, const char *name)
{ {
hunk_t *h; hunk_t *h;
@ -549,9 +550,6 @@ Hunk_AllocName (int size, const char *name)
Hunk_Check (); Hunk_Check ();
#endif #endif
if (size < 0)
Sys_Error ("Hunk_Alloc: bad size: %i", size);
size = sizeof (hunk_t) + ((size + HUNK_ALIGN - 1) & ~(HUNK_ALIGN - 1)); size = sizeof (hunk_t) + ((size + HUNK_ALIGN - 1) & ~(HUNK_ALIGN - 1));
if (hunk_size - hunk_low_used - hunk_high_used < size) { if (hunk_size - hunk_low_used - hunk_high_used < size) {
@ -565,7 +563,7 @@ Hunk_AllocName (int size, const char *name)
Cache_Profile (); Cache_Profile ();
Sys_Error Sys_Error
("Not enough RAM allocated. Try starting using \"-mem %d\" on " ("Not enough RAM allocated. Try starting using \"-mem %d\" on "
"the %s command line. (%d - %d - %d < %d)", mem, "the %s command line. (%zd - %zd - %zd < %zd)", mem,
PACKAGE_NAME, hunk_size, hunk_low_used, hunk_high_used, size); PACKAGE_NAME, hunk_size, hunk_low_used, hunk_high_used, size);
} }
@ -577,7 +575,8 @@ Hunk_AllocName (int size, const char *name)
memset (h, 0, size); memset (h, 0, size);
h->size = size; h->size = size;
h->sentinal = HUNK_SENTINAL; h->sentinal1 = HUNK_SENTINAL;
h->sentinal2 = HUNK_SENTINAL;
memcpy (h->name, name, 8); memcpy (h->name, name, 8);
h->name[7] = 0; h->name[7] = 0;
@ -585,34 +584,31 @@ Hunk_AllocName (int size, const char *name)
} }
VISIBLE void * VISIBLE void *
Hunk_Alloc (int size) Hunk_Alloc (size_t size)
{ {
return Hunk_AllocName (size, "unknown"); return Hunk_AllocName (size, "unknown");
} }
VISIBLE int VISIBLE size_t
Hunk_LowMark (void) Hunk_LowMark (void)
{ {
return hunk_low_used; return hunk_low_used;
} }
VISIBLE void VISIBLE void
Hunk_FreeToLowMark (int mark) Hunk_FreeToLowMark (size_t mark)
{ {
if (mark < 0 || mark > hunk_low_used) if (mark > hunk_low_used)
Sys_Error ("Hunk_FreeToLowMark: bad mark %i", mark); Sys_Error ("Hunk_FreeToLowMark: bad mark %zd", mark);
memset (hunk_base + mark, 0, hunk_low_used - mark); memset (hunk_base + mark, 0, hunk_low_used - mark);
hunk_low_used = mark; hunk_low_used = mark;
} }
static void * static void *
Hunk_HighAlloc (int size) Hunk_HighAlloc (size_t size)
{ {
hunk_t *h; hunk_t *h;
if (size < 0)
Sys_Error ("Hunk_HighAlloc: bad size: %i", size);
if (hunk_tempactive) { if (hunk_tempactive) {
Hunk_FreeToHighMark (hunk_tempmark); Hunk_FreeToHighMark (hunk_tempmark);
hunk_tempactive = false; hunk_tempactive = false;
@ -624,14 +620,15 @@ Hunk_HighAlloc (int size)
size = sizeof (hunk_t) + ((size + HUNK_ALIGN - 1) & ~(HUNK_ALIGN - 1)); size = sizeof (hunk_t) + ((size + HUNK_ALIGN - 1) & ~(HUNK_ALIGN - 1));
if (hunk_size - hunk_low_used - hunk_high_used < size) { if (hunk_size - hunk_low_used - hunk_high_used < size) {
Sys_Printf ("Hunk_HighAlloc: failed on %i bytes\n", size); Sys_Printf ("Hunk_HighAlloc: failed on %zd bytes\n", size);
return NULL; return NULL;
} }
hunk_high_used += size; hunk_high_used += size;
h = (void *) (hunk_base + hunk_size - hunk_high_used); h = (void *) (hunk_base + hunk_size - hunk_high_used);
h->sentinal = HUNK_SENTINAL; h->sentinal1 = HUNK_SENTINAL;
h->sentinal2 = HUNK_SENTINAL;
h->size = size; h->size = size;
h->name[0] = 0; h->name[0] = 0;
return h + 1; return h + 1;
@ -643,7 +640,7 @@ Hunk_HighAlloc (int size)
Return space from the top of the hunk Return space from the top of the hunk
*/ */
VISIBLE void * VISIBLE void *
Hunk_TempAlloc (int size) Hunk_TempAlloc (size_t size)
{ {
void *buf; void *buf;
@ -1006,7 +1003,7 @@ Cache_Free (cache_user_t *c)
} }
VISIBLE void * VISIBLE void *
Cache_Alloc (cache_user_t *c, int size, const char *name) Cache_Alloc (cache_user_t *c, size_t size, const char *name)
{ {
cache_system_t *cs; cache_system_t *cs;
@ -1014,7 +1011,7 @@ Cache_Alloc (cache_user_t *c, int size, const char *name)
Sys_Error ("Cache_Alloc: already allocated"); Sys_Error ("Cache_Alloc: already allocated");
if (size <= 0) if (size <= 0)
Sys_Error ("Cache_Alloc: size %i", size); Sys_Error ("Cache_Alloc: size %zd", size);
size = (size + sizeof (cache_system_t) + HUNK_ALIGN - 1) & ~(HUNK_ALIGN-1); size = (size + sizeof (cache_system_t) + HUNK_ALIGN - 1) & ~(HUNK_ALIGN-1);
@ -1120,7 +1117,7 @@ Cache_ReadLock (cache_user_t *c)
//============================================================================ //============================================================================
VISIBLE void VISIBLE void
Memory_Init (void *buf, int size) Memory_Init (void *buf, size_t size)
{ {
hunk_base = buf; hunk_base = buf;
hunk_size = size; hunk_size = size;

View file

@ -96,7 +96,7 @@ double oldcon_realtime;
int host_framecount; int host_framecount;
int host_hunklevel; int host_hunklevel;
int host_in_game; int host_in_game;
int minimum_memory; size_t minimum_memory;
client_t *host_client; // current client client_t *host_client; // current client
@ -812,7 +812,7 @@ static void
Host_Init_Memory (void) Host_Init_Memory (void)
{ {
int mem_parm = COM_CheckParm ("-mem"); int mem_parm = COM_CheckParm ("-mem");
int mem_size; size_t mem_size;
void *mem_base; void *mem_base;
if (standard_quake) if (standard_quake)
@ -831,7 +831,7 @@ Host_Init_Memory (void)
Cvar_SetFlags (host_mem_size, host_mem_size->flags | CVAR_ROM); Cvar_SetFlags (host_mem_size, host_mem_size->flags | CVAR_ROM);
mem_size = (int) (host_mem_size->value * 1024 * 1024); mem_size = ((size_t) host_mem_size->value * 1024 * 1024);
if (mem_size < minimum_memory) if (mem_size < minimum_memory)
Sys_Error ("Only %4.1f megs of memory reported, can't execute game", Sys_Error ("Only %4.1f megs of memory reported, can't execute game",
@ -840,7 +840,7 @@ Host_Init_Memory (void)
mem_base = Sys_Alloc (mem_size); mem_base = Sys_Alloc (mem_size);
if (!mem_base) if (!mem_base)
Sys_Error ("Can't allocate %d", mem_size); Sys_Error ("Can't allocate %zd", mem_size);
Sys_PageIn (mem_base, mem_size); Sys_PageIn (mem_base, mem_size);
Memory_Init (mem_base, mem_size); Memory_Init (mem_base, mem_size);

View file

@ -1706,7 +1706,7 @@ static void
CL_Init_Memory (void) CL_Init_Memory (void)
{ {
int mem_parm = COM_CheckParm ("-mem"); int mem_parm = COM_CheckParm ("-mem");
int mem_size; size_t mem_size;
void *mem_base; void *mem_base;
cl_mem_size = Cvar_Get ("cl_mem_size", "32", CVAR_NONE, NULL, cl_mem_size = Cvar_Get ("cl_mem_size", "32", CVAR_NONE, NULL,
@ -1720,7 +1720,7 @@ CL_Init_Memory (void)
Cvar_SetFlags (cl_mem_size, cl_mem_size->flags | CVAR_ROM); Cvar_SetFlags (cl_mem_size, cl_mem_size->flags | CVAR_ROM);
mem_size = (int) (cl_mem_size->value * 1024 * 1024); mem_size = ((size_t) cl_mem_size->value * 1024 * 1024);
if (mem_size < MINIMUM_MEMORY) if (mem_size < MINIMUM_MEMORY)
Sys_Error ("Only %4.1f megs of memory reported, can't execute game", Sys_Error ("Only %4.1f megs of memory reported, can't execute game",
@ -1729,7 +1729,7 @@ CL_Init_Memory (void)
mem_base = Sys_Alloc (mem_size); mem_base = Sys_Alloc (mem_size);
if (!mem_base) if (!mem_base)
Sys_Error ("Can't allocate %d", mem_size); Sys_Error ("Can't allocate %zd", mem_size);
Sys_PageIn (mem_base, mem_size); Sys_PageIn (mem_base, mem_size);
Memory_Init (mem_base, mem_size); Memory_Init (mem_base, mem_size);

View file

@ -2450,7 +2450,7 @@ static void
SV_Init_Memory (void) SV_Init_Memory (void)
{ {
int mem_parm = COM_CheckParm ("-mem"); int mem_parm = COM_CheckParm ("-mem");
int mem_size; size_t mem_size;
void *mem_base; void *mem_base;
sv_mem_size = Cvar_Get ("sv_mem_size", "8", CVAR_NONE, NULL, sv_mem_size = Cvar_Get ("sv_mem_size", "8", CVAR_NONE, NULL,
@ -2464,7 +2464,7 @@ SV_Init_Memory (void)
Cvar_SetFlags (sv_mem_size, sv_mem_size->flags | CVAR_ROM); Cvar_SetFlags (sv_mem_size, sv_mem_size->flags | CVAR_ROM);
mem_size = (int) (sv_mem_size->value * 1024 * 1024); mem_size = ((size_t) sv_mem_size->value * 1024 * 1024);
if (mem_size < MINIMUM_MEMORY) if (mem_size < MINIMUM_MEMORY)
Sys_Error ("Only %4.1f megs of memory reported, can't execute game", Sys_Error ("Only %4.1f megs of memory reported, can't execute game",
@ -2473,7 +2473,7 @@ SV_Init_Memory (void)
mem_base = Sys_Alloc (mem_size); mem_base = Sys_Alloc (mem_size);
if (!mem_base) if (!mem_base)
Sys_Error ("Can't allocate %d", mem_size); Sys_Error ("Can't allocate %zd", mem_size);
Memory_Init (mem_base, mem_size); Memory_Init (mem_base, mem_size);
} }