From 9e861e310270633cf99773a020c54a8719ea850a Mon Sep 17 00:00:00 2001 From: Ozkan Sezer Date: Tue, 16 Feb 2010 21:22:20 +0000 Subject: [PATCH] 64 bit compatibility effort, 1/nn: type correctness work in common.c, common.h, gl_vidsdl.c, mathlib.h, zone.c, zone.h. added Z_Realloc as a new function (will be used later.) next step will be type correctness work in opengl stuff. git-svn-id: svn://svn.code.sf.net/p/quakespasm/code/trunk/quakespasm@33 af15c1b1-3010-417e-b628-4374ebc0bcbd --- Quake/common.c | 14 ++++---- Quake/common.h | 6 ++-- Quake/gl_vidsdl.c | 6 ++-- Quake/mathlib.h | 2 +- Quake/zone.c | 87 +++++++++++++++++++++++++++++++---------------- Quake/zone.h | 3 +- 6 files changed, 72 insertions(+), 46 deletions(-) diff --git a/Quake/common.c b/Quake/common.c index 88ff707d..c2737d38 100644 --- a/Quake/common.c +++ b/Quake/common.c @@ -150,11 +150,11 @@ void InsertLinkAfter (link_t *l, link_t *after) ============================================================================ */ -void Q_memset (void *dest, int fill, int count) +void Q_memset (void *dest, int fill, size_t count) { - int i; + size_t i; - if ( (((long)dest | count) & 3) == 0) + if ( (((size_t)dest | count) & 3) == 0) { count >>= 2; fill = fill | (fill<<8) | (fill<<16) | (fill<<24); @@ -166,11 +166,11 @@ void Q_memset (void *dest, int fill, int count) ((byte *)dest)[i] = fill; } -void Q_memcpy (void *dest, const void *src, int count) +void Q_memcpy (void *dest, const void *src, size_t count) { - int i; + size_t i; - if (( ( (long)dest | (long)src | count) & 3) == 0 ) + if (( ( (size_t)dest | (size_t)src | count) & 3) == 0 ) { count>>=2; for (i=0 ; i8 bpp modes diff --git a/Quake/mathlib.h b/Quake/mathlib.h index 8d57a46f..a50bc4b6 100644 --- a/Quake/mathlib.h +++ b/Quake/mathlib.h @@ -58,7 +58,7 @@ extern int nanmask; _number = DotProduct(_v, _v);\ if (_number != 0.0)\ {\ - *((long *)&_y) = 0x5f3759df - ((* (long *) &_number) >> 1);\ + *((int *)&_y) = 0x5f3759df - ((* (int *) &_number) >> 1);\ _y = _y * (1.5f - (_number * 0.5f * _y * _y));\ VectorScale(_v, _y, _v);\ }\ diff --git a/Quake/zone.c b/Quake/zone.c index 5d04b5e3..a6610a56 100644 --- a/Quake/zone.c +++ b/Quake/zone.c @@ -64,33 +64,6 @@ all big things are allocated on the hunk. memzone_t *mainzone; -void Z_ClearZone (memzone_t *zone, int size); - - -/* -======================== -Z_ClearZone -======================== -*/ -void Z_ClearZone (memzone_t *zone, int size) -{ - memblock_t *block; - -// set the entire zone to one free block - - zone->blocklist.next = zone->blocklist.prev = block = - (memblock_t *)( (byte *)zone + sizeof(memzone_t) ); - zone->blocklist.tag = 1; // in use block - zone->blocklist.id = 0; - zone->blocklist.size = 0; - zone->rover = block; - - block->prev = block->next = &zone->blocklist; - block->tag = 0; // free block - block->id = ZONEID; - block->size = size - sizeof(memzone_t); -} - /* ======================== @@ -153,6 +126,41 @@ Z_CheckHeap (); // DEBUG return buf; } +/* +======================== +Z_Realloc +======================== +*/ +void *Z_Realloc(void *ptr, int size) +{ + int old_size; + void *old_ptr; + memblock_t *block; + + if (!ptr) + return Z_Malloc (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 (ptr); + ptr = Z_TagMalloc (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_TagMalloc (int size, int tag) { int extra; @@ -299,7 +307,7 @@ void Hunk_Check (void) { if (h->sentinal != HUNK_SENTINAL) Sys_Error ("Hunk_Check: trahsed sentinal"); - if (h->size < 16 || h->size + (byte *)h - hunk_base > hunk_size) + if (h->size < sizeof(hunk_t) || h->size + (byte *)h - hunk_base > hunk_size) Sys_Error ("Hunk_Check: bad size"); h = (hunk_t *)((byte *)h+h->size); } @@ -357,7 +365,7 @@ void Hunk_Print (qboolean all) // if (h->sentinal != HUNK_SENTINAL) Sys_Error ("Hunk_Check: trahsed sentinal"); - if (h->size < 16 || h->size + (byte *)h - hunk_base > hunk_size) + if (h->size < sizeof(hunk_t) || h->size + (byte *)h - hunk_base > hunk_size) Sys_Error ("Hunk_Check: bad size"); next = (hunk_t *)((byte *)h+h->size); @@ -922,6 +930,25 @@ void *Cache_Alloc (cache_user_t *c, int size, char *name) //============================================================================ +static void Memory_InitZone (memzone_t *zone, int size) +{ + memblock_t *block; + +// set the entire zone to one free block + + zone->blocklist.next = zone->blocklist.prev = block = + (memblock_t *)( (byte *)zone + sizeof(memzone_t) ); + zone->blocklist.tag = 1; // in use block + zone->blocklist.id = 0; + zone->blocklist.size = 0; + zone->rover = block; + + block->prev = block->next = &zone->blocklist; + block->tag = 0; // free block + block->id = ZONEID; + block->size = size - sizeof(memzone_t); +} + /* ======================== Memory_Init @@ -947,7 +974,7 @@ void Memory_Init (void *buf, int size) Sys_Error ("Memory_Init: you must specify a size in KB after -zone"); } mainzone = Hunk_AllocName (zonesize, "zone" ); - Z_ClearZone (mainzone, zonesize); + Memory_InitZone (mainzone, zonesize); Cmd_AddCommand ("hunk_print", Hunk_Print_f); //johnfitz } diff --git a/Quake/zone.h b/Quake/zone.h index f7de6ff0..3a9a3d13 100644 --- a/Quake/zone.h +++ b/Quake/zone.h @@ -88,11 +88,10 @@ void Memory_Init (void *buf, int size); void Z_Free (void *ptr); void *Z_Malloc (int size); // returns 0 filled memory +void *Z_Realloc (void *ptr, int size); void *Z_TagMalloc (int size, int tag); -void Z_DumpHeap (void); void Z_CheckHeap (void); -int Z_FreeMemory (void); void *Hunk_Alloc (int size); // returns 0 filled memory void *Hunk_AllocName (int size, char *name);