this commit will most likely break things: main Z_*/BZ_* calls no longer have zone logic, tagged allocs redone and now partially thread safe (still Sys_Error), code using reallocs should no longer assume new memory is zeroed, minor mysql fixes

git-svn-id: https://svn.code.sf.net/p/fteqw/code/trunk@2951 fc73d0e0-1445-4013-8a0c-d673dee63da5
This commit is contained in:
TimeServ 2008-05-09 14:22:37 +00:00
parent 74c6f76f50
commit ce5cb75832
20 changed files with 462 additions and 187 deletions

View file

@ -519,7 +519,7 @@ void CL_ParsePacketEntities (qboolean delta)
if (newindex >= newp->max_entities) if (newindex >= newp->max_entities)
{ {
newp->max_entities = newindex+1; newp->max_entities = newindex+1;
newp->entities = BZ_Realloc(newp->entities, sizeof(entity_state_t)*newp->max_entities); newp->entities = BZ_Realloc(newp->entities, sizeof(entity_state_t)*newp->max_entities);
} }
if (oldindex >= oldp->max_entities) if (oldindex >= oldp->max_entities)
Host_EndGame("Old packet entity too big\n"); Host_EndGame("Old packet entity too big\n");
@ -1425,8 +1425,10 @@ void CL_TransitionPacketEntities(packet_entities_t *newpack, packet_entities_t *
if (snew->number >= cl.maxlerpents) if (snew->number >= cl.maxlerpents)
{ {
cl.maxlerpents = snew->number+16; int newmaxle = snew->number+16;
cl.lerpents = BZ_Realloc(cl.lerpents, cl.maxlerpents*sizeof(lerpents_t)); cl.lerpents = BZ_Realloc(cl.lerpents, newmaxle*sizeof(lerpents_t));
memset(cl.lerpents + cl.maxlerpents, 0, sizeof(lerpents_t)*(newmaxle - cl.maxlerpents));
cl.maxlerpents = newmaxle;
} }
le = &cl.lerpents[snew->number]; le = &cl.lerpents[snew->number];

View file

@ -3664,6 +3664,8 @@ void Host_Shutdown(void)
Cvar_Shutdown(); Cvar_Shutdown();
Validation_FlushFileList(); Validation_FlushFileList();
Memory_DeInit();
} }
#ifdef CLIENTONLY #ifdef CLIENTONLY

View file

@ -2567,7 +2567,7 @@ qboolean CL_CheckBaselines (int size)
if (size < cl_baselines_count) if (size < cl_baselines_count)
return true; return true;
cl_baselines = BZ_Realloc(cl_baselines, sizeof(*cl_baselines)*size); cl_baselines = BZ_Realloc(cl_baselines, sizeof(*cl_baselines)*size);
for (i = cl_baselines_count; i < size; i++) for (i = cl_baselines_count; i < size; i++)
{ {
memcpy(cl_baselines + i, &nullentitystate, sizeof(*cl_baselines)); memcpy(cl_baselines + i, &nullentitystate, sizeof(*cl_baselines));

View file

@ -117,6 +117,7 @@ int VARGS Plug_Draw_LoadImage(void *offset, unsigned int mask, const int *arg)
{ {
pluginimagearraylen++; pluginimagearraylen++;
pluginimagearray = BZ_Realloc(pluginimagearray, pluginimagearraylen*sizeof(pluginimagearray_t)); pluginimagearray = BZ_Realloc(pluginimagearray, pluginimagearraylen*sizeof(pluginimagearray_t));
pluginimagearray[i].pic = NULL;
} }
if (pluginimagearray[i].pic) if (pluginimagearray[i].pic)

View file

@ -212,7 +212,7 @@ int Script_LoadFile(char *filename)
if (i == maxscripts) if (i == maxscripts)
{ {
maxscripts++; maxscripts++;
scripts = BZ_Realloc(scripts, sizeof(script_t)*maxscripts); scripts = BZ_Realloc(scripts, sizeof(script_t)*maxscripts);
} }
sc = scripts+i; sc = scripts+i;

View file

@ -3859,6 +3859,18 @@ qboolean CSQC_CenterPrint(char *cmd)
//this protocol allows up to 32767 edicts. //this protocol allows up to 32767 edicts.
#ifdef PEXT_CSQC #ifdef PEXT_CSQC
void CSQC_EntityCheck(int entnum)
{
int newmax;
if (entnum >= maxcsqcentities)
{
newmax = entnum+64;
csqcent = BZ_Realloc(csqcent, sizeof(*csqcent)*newmax);
memset(csqcent + maxcsqcentities, 0, newmax - maxcsqcentities);
maxcsqcentities = newmax;
}
}
int CSQC_StartSound(int entnum, int channel, char *soundname, vec3_t pos, float vol, float attenuation) int CSQC_StartSound(int entnum, int channel, char *soundname, vec3_t pos, float vol, float attenuation)
{ {
@ -3868,11 +3880,7 @@ int CSQC_StartSound(int entnum, int channel, char *soundname, vec3_t pos, float
if (!csqcprogs || !csqcg.serversound) if (!csqcprogs || !csqcg.serversound)
return false; return false;
if (entnum >= maxcsqcentities) CSQC_EntityCheck(entnum);
{
maxcsqcentities = entnum+64;
csqcent = BZ_Realloc(csqcent, sizeof(*csqcent)*maxcsqcentities);
}
ent = csqcent[entnum]; ent = csqcent[entnum];
if (!ent) if (!ent)
return false; return false;
@ -3926,11 +3934,8 @@ void CSQC_ParseEntities(void)
if (!entnum) if (!entnum)
Host_EndGame("CSQC cannot remove world!\n"); Host_EndGame("CSQC cannot remove world!\n");
if (entnum >= maxcsqcentities) CSQC_EntityCheck(entnum);
{
maxcsqcentities = entnum+64;
csqcent = BZ_Realloc(csqcent, sizeof(*csqcent)*maxcsqcentities);
}
if (cl_csqcdebug.value) if (cl_csqcdebug.value)
Con_Printf("Remove %i\n", entnum); Con_Printf("Remove %i\n", entnum);
@ -3946,11 +3951,7 @@ void CSQC_ParseEntities(void)
} }
else else
{ {
if (entnum >= maxcsqcentities) CSQC_EntityCheck(entnum);
{
maxcsqcentities = entnum+64;
csqcent = BZ_Realloc(csqcent, sizeof(*csqcent)*maxcsqcentities);
}
if (cl.csqcdebug) if (cl.csqcdebug)
{ {

View file

@ -209,7 +209,6 @@ static part_type_t *P_GetParticleType(char *name)
ptype->cliptype = -1; ptype->cliptype = -1;
ptype->emit = -1; ptype->emit = -1;
if (oldlist) if (oldlist)
{ {
part_run_list=NULL; part_run_list=NULL;
@ -219,13 +218,10 @@ static part_type_t *P_GetParticleType(char *name)
part_type[i].nexttorun = (part_type_t*)((char*)part_type[i].nexttorun - (char*)oldlist + (char*)part_type); part_type[i].nexttorun = (part_type_t*)((char*)part_type[i].nexttorun - (char*)oldlist + (char*)part_type);
} }
/*
Due to BZ_Realloc we can assume all of this anyway
ptype->loaded = 0; ptype->loaded = 0;
ptype->ramp = NULL; ptype->ramp = NULL;
ptype->particles = NULL; ptype->particles = NULL;
ptype->beams = NULL; ptype->beams = NULL;
*/
return ptype; return ptype;
} }

View file

@ -620,12 +620,6 @@ void Sys_Quit (void)
CloseHandle (qwclsemaphore); CloseHandle (qwclsemaphore);
#endif #endif
/* Yeah, right, just wishful thinking.
#ifdef _DEBUG
if (Z_Allocated())
MessageBox(0, "Some memory was left allocated", "Mem was left", 0);
#endif
*/
SetHookState(false); SetHookState(false);

View file

@ -179,8 +179,10 @@ void Plug_RegisterBuiltin(char *name, Plug_Builtin_t bi, int flags)
if (newnum >= numplugbuiltins) if (newnum >= numplugbuiltins)
{ {
numplugbuiltins = newnum+128; int newbuiltins = newnum+128;
plugbuiltins = BZ_Realloc(plugbuiltins, sizeof(Plug_Plugins_t)*numplugbuiltins); plugbuiltins = BZ_Realloc(plugbuiltins, sizeof(Plug_Plugins_t)*newbuiltins);
memset(plugbuiltins + numplugbuiltins, 0, sizeof(Plug_Plugins_t)*(newbuiltins - numplugbuiltins));
numplugbuiltins = newbuiltins;
} }
//got an empty number. //got an empty number.

View file

@ -64,10 +64,254 @@ void Cache_FreeHigh (int new_high_hunk);
qbyte sentinalkey; qbyte sentinalkey;
#endif #endif
#define TAGLESS 1
typedef struct memheader_s {
int size;
int tag;
} memheader_t;
typedef struct zone_s {
struct zone_s *next;
struct zone_s *pvdn; // down if first, previous if not
memheader_t mh;
} zone_t;
zone_t *zone_head;
#ifdef MULTITHREAD
void *zonelock;
#endif
void *VARGS Z_TagMalloc(int size, int tag)
{
zone_t *zone;
zone = (zone_t *)malloc(size + sizeof(zone_t));
if (!zone)
Sys_Error("Z_Malloc: Failed on allocation of %i bytes", size);
Q_memset(zone, 0, size + sizeof(zone_t));
zone->mh.tag = tag;
zone->mh.size = size;
#ifdef MULTITHREAD
if (zonelock)
Sys_LockMutex(zonelock);
#endif
if (zone_head == NULL)
zone_head = zone;
else if (zone_head->mh.tag == tag)
{
zone->next = zone_head->next;
zone_head->next = zone;
}
else
{
zone_t *s = zone_head->pvdn;
while (s && s->mh.tag != tag)
s = s->pvdn;
if (s)
{ // tag match
zone->next = s->next;
s->next = zone;
}
else
{
zone->pvdn = zone_head;
zone_head = zone;
}
}
#ifdef MULTITHREAD
if (zonelock)
Sys_UnlockMutex(zonelock);
#endif
return (void *)(zone + 1);
}
void *ZF_Malloc(int size)
{
return calloc(size, 1);
}
void *Z_Malloc(int size)
{
void *mem = ZF_Malloc(size);
if (!mem)
Sys_Error("Z_Malloc: Failed on allocation of %i bytes", size);
return mem;
}
void VARGS Z_TagFree(void *mem)
{
zone_t *zone = ((zone_t *)mem) - 1;
#ifdef MULTITHREAD
if (zonelock)
Sys_LockMutex(zonelock);
#endif
if (zone->next)
zone->next->pvdn = zone->pvdn;
if (zone->pvdn && zone->pvdn->mh.tag == zone->mh.tag)
zone->pvdn->next = zone->next;
else
{ // zone is first entry in a tag list
zone_t *s = zone_head;
if (zone != s)
{ // traverse and update down list
while (s->pvdn != zone)
s = s->next;
s->pvdn = zone->pvdn;
}
}
if (zone == zone_head)
{ // freeing head node so update head pointer
if (zone->next) // move to next, pvdn should be maintained properly
zone_head = zone->next;
else // no more entries with this tag so move head down
zone_head = zone->pvdn;
}
#ifdef MULTITHREAD
if (zonelock)
Sys_UnlockMutex(zonelock);
#endif
free(zone);
}
void VARGS Z_Free(void *mem)
{
free(mem);
}
void VARGS Z_FreeTags(int tag)
{
zone_t *taglist;
zone_t *t;
#ifdef MULTITHREAD
if (zonelock)
Sys_LockMutex(zonelock);
#endif
if (zone_head)
{
if (zone_head->mh.tag == tag)
{ // just pull off the head
taglist = zone_head;
zone_head = zone_head->pvdn;
}
else
{ // search for tag list and isolate it
zone_t *z;
z = zone_head;
while (z->next != NULL && z->next->mh.tag != tag)
z = z->next;
if (z->next == NULL)
taglist = NULL;
else
{
taglist = z->next;
z->next = z->next->next;
}
}
}
else
taglist = NULL;
#ifdef MULTITHREAD
if (zonelock)
Sys_UnlockMutex(zonelock);
#endif
// actually free list
while (taglist != NULL)
{
t = taglist->next;
free(taglist);
taglist = t;
}
}
/*
void *Z_Realloc(void *data, int newsize)
{
memheader_t *memref;
if (!data)
return Z_Malloc(newsize);
memref = ((memheader_t *)data) - 1;
if (memref[0].tag != TAGLESS)
{ // allocate a new block and copy since we need to maintain the lists
zone_t *zone = ((zone_t *)data) - 1;
int size = zone->mh.size;
if (size != newsize)
{
void *newdata = Z_Malloc(newsize);
if (size > newsize)
size = newsize;
memcpy(newdata, data, size);
Z_Free(data);
data = newdata;
}
}
else
{
int oldsize = memref[0].size;
memref = realloc(memref, newsize + sizeof(memheader_t));
memref->size = newsize;
if (newsize > oldsize)
memset((qbyte *)memref + sizeof(memheader_t) + oldsize, 0, newsize - oldsize);
data = ((memheader_t *)memref) + 1;
}
return data;
}
*/
void *BZF_Malloc(int size) //BZ_Malloc but allowed to fail - like straight malloc.
{
return calloc(size, 1); // TODO: this should be malloc but some code still assumes this is an alias to Z_Malloc
}
void *BZ_Malloc(int size) //Doesn't clear. The expectation is a large file, rather than sensative data structures.
{
void *mem = BZF_Malloc(size);
if (!mem)
Sys_Error("BZ_Malloc: Failed on allocation of %i bytes", size);
return mem;
}
void *BZF_Realloc(void *data, int newsize)
{
return realloc(data, newsize);
}
void *BZ_Realloc(void *data, int newsize)
{
void *mem = BZF_Realloc(data, newsize);
if (!mem)
Sys_Error("BZ_Realloc: Failed on reallocation of %i bytes", newsize);
return mem;
}
void BZ_Free(void *data)
{
free(data);
}
#if 0 //NOZONE //zone memory is for small dynamic things.
#ifdef NOZONE //zone memory is for small dynamic things.
/* /*
void *Z_TagMalloc(int size, int tag) void *Z_TagMalloc(int size, int tag)
{ {
@ -139,24 +383,6 @@ void Z_CheckSentinals(void)
} }
}*/ }*/
int Z_Allocated(void)
{
zone_t *zone;
int used = 0;
for(zone = zone_head; zone; zone=zone->next)
{
used += zone->size;
}
return used;
}
int Z_MemSize(void *c)
{
zone_t *nz;
nz = ((zone_t *)((char*)c-ZONEDEBUG))-1;
return nz->size;
}
void VARGS Z_Free (void *c) void VARGS Z_Free (void *c)
{ {
@ -553,7 +779,7 @@ void Zone_Print_f(void)
Con_Printf(CON_NOTICE "Overhead %i bytes\n", overhead); Con_Printf(CON_NOTICE "Overhead %i bytes\n", overhead);
} }
#else #elif 0//#else
@ -806,29 +1032,6 @@ void Z_Print (memzone_t *zone)
} }
/*
========================
Z_CheckHeap
========================
*/
void Z_CheckHeap (void)
{
memblock_t *block;
for (block = mainzone->blocklist.next ; ; block = block->next)
{
if (block->next == &mainzone->blocklist)
break; // all blocks have been hit
if ( (qbyte *)block + block->size != (qbyte *)block->next)
Sys_Error ("Z_CheckHeap: block size does not touch the next block\n");
if ( block->next->prev != block)
Sys_Error ("Z_CheckHeap: next block doesn't have proper back link\n");
if (!block->tag && !block->next->tag)
Sys_Error ("Z_CheckHeap: two consecutive free blocks\n");
}
}
@ -1456,7 +1659,6 @@ void Cache_Report (void)
void Hunk_Print_f (void) void Hunk_Print_f (void)
{ {
cache_system_t *cs; cache_system_t *cs;
zone_t *zone;
int zoneblocks; int zoneblocks;
int cacheused; int cacheused;
int zoneused; int zoneused;
@ -1469,19 +1671,27 @@ void Hunk_Print_f (void)
{ {
cacheused += cs->size; cacheused += cs->size;
} }
for(zone = zone_head; zone; zone=zone->next)
{
zoneused += zone->size + sizeof(zone_t);
zoneblocks++;
}
Con_Printf("Cache: %iKB\n", cacheused/1024); Con_Printf("Cache: %iKB\n", cacheused/1024);
Con_Printf("Zone: %i containing %iKB\n", zoneblocks, zoneused/1024); #if 0
{
zone_t *zone;
for(zone = zone_head; zone; zone=zone->next)
{
zoneused += zone->size + sizeof(zone_t);
zoneblocks++;
}
Con_Printf("Zone: %i containing %iKB\n", zoneblocks, zoneused/1024);
}
#endif
} }
void Cache_Init(void) void Cache_Init(void)
{ {
Cmd_AddCommand ("flush", Cache_Flush); Cmd_AddCommand ("flush", Cache_Flush);
Cmd_AddCommand ("hunkprint", Hunk_Print_f); Cmd_AddCommand ("hunkprint", Hunk_Print_f);
#if 0
Cmd_AddCommand ("zoneprint", Zone_Print_f); Cmd_AddCommand ("zoneprint", Zone_Print_f);
#endif
#ifdef NAMEDMALLOCS #ifdef NAMEDMALLOCS
Cmd_AddCommand ("zonegroups", Zone_Groups_f); Cmd_AddCommand ("zonegroups", Zone_Groups_f);
#endif #endif
@ -1938,7 +2148,7 @@ Memory_Init
*/ */
void Memory_Init (void *buf, int size) void Memory_Init (void *buf, int size)
{ {
#ifndef NOZONE #if 0 //ndef NOZONE
int p; int p;
int zonesize = DYNAMIC_SIZE; int zonesize = DYNAMIC_SIZE;
#endif #endif
@ -1955,7 +2165,12 @@ void Memory_Init (void *buf, int size)
Cache_Init (); Cache_Init ();
#ifndef NOZONE #ifdef MULTITHREAD
if (!zonelock)
zonelock = Sys_CreateMutex(); // this can fail!
#endif
#if 0 //ndef NOZONE
p = COM_CheckParm ("-zone"); p = COM_CheckParm ("-zone");
if (p) if (p)
{ {
@ -1969,3 +2184,13 @@ void Memory_Init (void *buf, int size)
#endif #endif
} }
void Memory_DeInit(void)
{
#ifdef MULTITHREAD
if (zonelock)
{
Sys_DestroyMutex(zonelock);
zonelock = NULL;
}
#endif
}

View file

@ -84,31 +84,23 @@ Zone block
*/ */
void Memory_Init (void *buf, int size); void Memory_Init (void *buf, int size);
void Memory_DeInit(void);
void VARGS Z_Free (void *ptr); void VARGS Z_Free (void *ptr);
int Z_MemSize(void *c); void *Z_Malloc (int size); // returns 0 filled memory
void *Z_Malloc (int size); // returns 0 filled memory void *ZF_Malloc (int size); // allowed to fail
void *Z_MallocNamed (int size, char *, int); // returns 0 filled memory
//#define Z_Malloc(x) Z_MallocNamed2(x, __FILE__, __LINE__ ) //#define Z_Malloc(x) Z_MallocNamed2(x, __FILE__, __LINE__ )
void *VARGS Z_TagMalloc (int size, int tag); void *VARGS Z_TagMalloc (int size, int tag);
void VARGS Z_TagFree(void *ptr);
void VARGS Z_FreeTags(int tag); void VARGS Z_FreeTags(int tag);
//void *Z_Realloc (void *ptr, int size);
void Z_DumpHeap (void);
void Z_CheckHeap (void);
int Z_FreeMemory (void);
int Z_Allocated(void);
#ifdef _DEBUG
#define NAMEDMALLOCS
#endif
//Big Zone: allowed to fail, doesn't clear. The expectation is a large file, rather than sensative data structures. //Big Zone: allowed to fail, doesn't clear. The expectation is a large file, rather than sensative data structures.
//(this is a nicer name for malloc) //(this is a nicer name for malloc)
void *BZ_Malloc(int size); void *BZ_Malloc(int size);
void *BZF_Malloc(int size); void *BZF_Malloc(int size);
void *BZ_Realloc(void *ptr, int size); void *BZ_Realloc(void *ptr, int size);
void *BZ_NamedRealloc(void *ptr, int size, char *, int); void *BZF_Realloc(void *data, int newsize);
void BZ_Free(void *ptr); void BZ_Free(void *ptr);
#ifdef NAMEDMALLOCS #ifdef NAMEDMALLOCS

View file

@ -989,17 +989,22 @@ int D3DFillBlock (int texnum, int w, int h, int x, int y)
while (texnum >= numlightmaps) //allocate 4 more lightmap slots. not much memory usage, but we don't want any caps here. while (texnum >= numlightmaps) //allocate 4 more lightmap slots. not much memory usage, but we don't want any caps here.
{ {
lightmap = BZ_Realloc(lightmap, sizeof(*lightmap)*(numlightmaps+4)); lightmap = BZ_Realloc(lightmap, sizeof(*lightmap)*(numlightmaps+4));
lightmap[numlightmaps+0] = 0;
lightmap[numlightmaps+1] = 0;
lightmap[numlightmaps+2] = 0;
lightmap[numlightmaps+3] = 0;
lightmap_d3dtextures = BZ_Realloc(lightmap_d3dtextures, sizeof(*lightmap_d3dtextures)*(numlightmaps+4)); lightmap_d3dtextures = BZ_Realloc(lightmap_d3dtextures, sizeof(*lightmap_d3dtextures)*(numlightmaps+4));
// lightmap_textures[numlightmaps+0] = texture_extension_number++; lightmap_d3dtextures[numlightmaps+0] = 0;
// lightmap_textures[numlightmaps+1] = texture_extension_number++; lightmap_d3dtextures[numlightmaps+1] = 0;
// lightmap_textures[numlightmaps+2] = texture_extension_number++; lightmap_d3dtextures[numlightmaps+2] = 0;
// lightmap_textures[numlightmaps+3] = texture_extension_number++; lightmap_d3dtextures[numlightmaps+3] = 0;
deluxmap_d3dtextures = BZ_Realloc(deluxmap_d3dtextures, sizeof(*deluxmap_d3dtextures)*(numlightmaps+4)); deluxmap_d3dtextures = BZ_Realloc(deluxmap_d3dtextures, sizeof(*deluxmap_d3dtextures)*(numlightmaps+4));
// deluxmap_textures[numlightmaps+0] = texture_extension_number++; deluxmap_d3dtextures[numlightmaps+0] = 0;
// deluxmap_textures[numlightmaps+1] = texture_extension_number++; deluxmap_d3dtextures[numlightmaps+1] = 0;
// deluxmap_textures[numlightmaps+2] = texture_extension_number++; deluxmap_d3dtextures[numlightmaps+2] = 0;
// deluxmap_textures[numlightmaps+3] = texture_extension_number++; deluxmap_d3dtextures[numlightmaps+3] = 0;
numlightmaps+=4; numlightmaps+=4;
} }
for (i = texnum; i >= 0; i--) for (i = texnum; i >= 0; i--)
@ -1035,17 +1040,22 @@ int D3D7_AllocBlock (int w, int h, int *x, int *y)
if (texnum == numlightmaps) //allocate 4 more lightmap slots. not much memory usage, but we don't want any caps here. if (texnum == numlightmaps) //allocate 4 more lightmap slots. not much memory usage, but we don't want any caps here.
{ {
lightmap = BZ_Realloc(lightmap, sizeof(*lightmap)*(numlightmaps+4)); lightmap = BZ_Realloc(lightmap, sizeof(*lightmap)*(numlightmaps+4));
lightmap[numlightmaps+0] = 0;
lightmap[numlightmaps+1] = 0;
lightmap[numlightmaps+2] = 0;
lightmap[numlightmaps+3] = 0;
lightmap_d3dtextures = BZ_Realloc(lightmap_d3dtextures, sizeof(*lightmap_d3dtextures)*(numlightmaps+4)); lightmap_d3dtextures = BZ_Realloc(lightmap_d3dtextures, sizeof(*lightmap_d3dtextures)*(numlightmaps+4));
// lightmap_textures[numlightmaps+0] = texture_extension_number++; lightmap_d3dtextures[numlightmaps+0] = 0;
// lightmap_textures[numlightmaps+1] = texture_extension_number++; lightmap_d3dtextures[numlightmaps+1] = 0;
// lightmap_textures[numlightmaps+2] = texture_extension_number++; lightmap_d3dtextures[numlightmaps+2] = 0;
// lightmap_textures[numlightmaps+3] = texture_extension_number++; lightmap_d3dtextures[numlightmaps+3] = 0;
deluxmap_d3dtextures = BZ_Realloc(deluxmap_d3dtextures, sizeof(*deluxmap_d3dtextures)*(numlightmaps+4)); deluxmap_d3dtextures = BZ_Realloc(deluxmap_d3dtextures, sizeof(*deluxmap_d3dtextures)*(numlightmaps+4));
// deluxmap_textures[numlightmaps+0] = texture_extension_number++; deluxmap_d3dtextures[numlightmaps+0] = 0;
// deluxmap_textures[numlightmaps+1] = texture_extension_number++; deluxmap_d3dtextures[numlightmaps+1] = 0;
// deluxmap_textures[numlightmaps+2] = texture_extension_number++; deluxmap_d3dtextures[numlightmaps+2] = 0;
// deluxmap_textures[numlightmaps+3] = texture_extension_number++; deluxmap_d3dtextures[numlightmaps+3] = 0;
numlightmaps+=4; numlightmaps+=4;
} }
if (!lightmap[texnum]) if (!lightmap[texnum])

View file

@ -990,17 +990,22 @@ int D3D9_FillBlock (int texnum, int w, int h, int x, int y)
while (texnum >= numlightmaps) //allocate 4 more lightmap slots. not much memory usage, but we don't want any caps here. while (texnum >= numlightmaps) //allocate 4 more lightmap slots. not much memory usage, but we don't want any caps here.
{ {
lightmap = BZ_Realloc(lightmap, sizeof(*lightmap)*(numlightmaps+4)); lightmap = BZ_Realloc(lightmap, sizeof(*lightmap)*(numlightmaps+4));
lightmap[numlightmaps+0] = 0;
lightmap[numlightmaps+1] = 0;
lightmap[numlightmaps+2] = 0;
lightmap[numlightmaps+3] = 0;
lightmap_d3d9textures = BZ_Realloc(lightmap_d3d9textures, sizeof(*lightmap_d3d9textures)*(numlightmaps+4)); lightmap_d3d9textures = BZ_Realloc(lightmap_d3d9textures, sizeof(*lightmap_d3d9textures)*(numlightmaps+4));
// lightmap_textures[numlightmaps+0] = texture_extension_number++; lightmap_d3d9textures[numlightmaps+0] = 0;
// lightmap_textures[numlightmaps+1] = texture_extension_number++; lightmap_d3d9textures[numlightmaps+1] = 0;
// lightmap_textures[numlightmaps+2] = texture_extension_number++; lightmap_d3d9textures[numlightmaps+2] = 0;
// lightmap_textures[numlightmaps+3] = texture_extension_number++; lightmap_d3d9textures[numlightmaps+3] = 0;
deluxmap_d3d9textures = BZ_Realloc(deluxmap_d3d9textures, sizeof(*deluxmap_d3d9textures)*(numlightmaps+4)); deluxmap_d3d9textures = BZ_Realloc(deluxmap_d3d9textures, sizeof(*deluxmap_d3d9textures)*(numlightmaps+4));
// deluxmap_textures[numlightmaps+0] = texture_extension_number++; deluxmap_d3d9textures[numlightmaps+0] = 0;
// deluxmap_textures[numlightmaps+1] = texture_extension_number++; deluxmap_d3d9textures[numlightmaps+1] = 0;
// deluxmap_textures[numlightmaps+2] = texture_extension_number++; deluxmap_d3d9textures[numlightmaps+2] = 0;
// deluxmap_textures[numlightmaps+3] = texture_extension_number++; deluxmap_d3d9textures[numlightmaps+3] = 0;
numlightmaps+=4; numlightmaps+=4;
} }
for (i = texnum; i >= 0; i--) for (i = texnum; i >= 0; i--)
@ -1036,17 +1041,22 @@ int D3D9_AllocBlock (int w, int h, int *x, int *y)
if (texnum == numlightmaps) //allocate 4 more lightmap slots. not much memory usage, but we don't want any caps here. if (texnum == numlightmaps) //allocate 4 more lightmap slots. not much memory usage, but we don't want any caps here.
{ {
lightmap = BZ_Realloc(lightmap, sizeof(*lightmap)*(numlightmaps+4)); lightmap = BZ_Realloc(lightmap, sizeof(*lightmap)*(numlightmaps+4));
lightmap[numlightmaps+0] = 0;
lightmap[numlightmaps+1] = 0;
lightmap[numlightmaps+2] = 0;
lightmap[numlightmaps+3] = 0;
lightmap_d3d9textures = BZ_Realloc(lightmap_d3d9textures, sizeof(*lightmap_d3d9textures)*(numlightmaps+4)); lightmap_d3d9textures = BZ_Realloc(lightmap_d3d9textures, sizeof(*lightmap_d3d9textures)*(numlightmaps+4));
// lightmap_textures[numlightmaps+0] = texture_extension_number++; lightmap_d3d9textures[numlightmaps+0] = 0;
// lightmap_textures[numlightmaps+1] = texture_extension_number++; lightmap_d3d9textures[numlightmaps+1] = 0;
// lightmap_textures[numlightmaps+2] = texture_extension_number++; lightmap_d3d9textures[numlightmaps+2] = 0;
// lightmap_textures[numlightmaps+3] = texture_extension_number++; lightmap_d3d9textures[numlightmaps+3] = 0;
deluxmap_d3d9textures = BZ_Realloc(deluxmap_d3d9textures, sizeof(*deluxmap_d3d9textures)*(numlightmaps+4)); deluxmap_d3d9textures = BZ_Realloc(deluxmap_d3d9textures, sizeof(*deluxmap_d3d9textures)*(numlightmaps+4));
// deluxmap_textures[numlightmaps+0] = texture_extension_number++; deluxmap_d3d9textures[numlightmaps+0] = 0;
// deluxmap_textures[numlightmaps+1] = texture_extension_number++; deluxmap_d3d9textures[numlightmaps+1] = 0;
// deluxmap_textures[numlightmaps+2] = texture_extension_number++; deluxmap_d3d9textures[numlightmaps+2] = 0;
// deluxmap_textures[numlightmaps+3] = texture_extension_number++; deluxmap_d3d9textures[numlightmaps+3] = 0;
numlightmaps+=4; numlightmaps+=4;
} }
if (!lightmap[texnum]) if (!lightmap[texnum])

View file

@ -5217,7 +5217,7 @@ void APIENTRY SH_End (void)
if (sh_maxindicies != i) if (sh_maxindicies != i)
{ {
sh_maxindicies = i; sh_maxindicies = i;
sh_shmesh->indicies = BZ_Realloc(sh_shmesh->indicies, i * sizeof(*sh_shmesh->indicies)); sh_shmesh->indicies = BZ_Realloc(sh_shmesh->indicies, i * sizeof(*sh_shmesh->indicies));
} }
//add the extra triangles //add the extra triangles
for (i = 0; i < sh_vertnum; i+=4) for (i = 0; i < sh_vertnum; i+=4)

View file

@ -3164,7 +3164,12 @@ int GLAllocBlock (int w, int h, int *x, int *y)
{ {
if (texnum == numlightmaps) //allocate 4 more lightmap slots. not much memory usage, but we don't want any caps here. if (texnum == numlightmaps) //allocate 4 more lightmap slots. not much memory usage, but we don't want any caps here.
{ {
lightmap = BZ_Realloc(lightmap, sizeof(*lightmap)*(numlightmaps+4)); lightmap = BZ_Realloc(lightmap, sizeof(*lightmap)*(numlightmaps+4));
lightmap[numlightmaps+0] = NULL;
lightmap[numlightmaps+1] = NULL;
lightmap[numlightmaps+2] = NULL;
lightmap[numlightmaps+3] = NULL;
lightmap_textures = BZ_Realloc(lightmap_textures, sizeof(*lightmap_textures)*(numlightmaps+4)); lightmap_textures = BZ_Realloc(lightmap_textures, sizeof(*lightmap_textures)*(numlightmaps+4));
lightmap_textures[numlightmaps+0] = texture_extension_number++; lightmap_textures[numlightmaps+0] = texture_extension_number++;
lightmap_textures[numlightmaps+1] = texture_extension_number++; lightmap_textures[numlightmaps+1] = texture_extension_number++;
@ -3226,14 +3231,19 @@ int GLFillBlock (int texnum, int w, int h, int x, int y)
int i, l; int i, l;
while (texnum >= numlightmaps) //allocate 4 more lightmap slots. not much memory usage, but we don't want any caps here. while (texnum >= numlightmaps) //allocate 4 more lightmap slots. not much memory usage, but we don't want any caps here.
{ {
lightmap = BZ_Realloc(lightmap, sizeof(*lightmap)*(numlightmaps+4)); lightmap = BZ_Realloc(lightmap, sizeof(*lightmap)*(numlightmaps+4));
lightmap_textures = BZ_Realloc(lightmap_textures, sizeof(*lightmap_textures)*(numlightmaps+4)); lightmap[numlightmaps+0] = NULL;
lightmap[numlightmaps+1] = NULL;
lightmap[numlightmaps+2] = NULL;
lightmap[numlightmaps+3] = NULL;
lightmap_textures = BZ_Realloc(lightmap_textures, sizeof(*lightmap_textures)*(numlightmaps+4));
lightmap_textures[numlightmaps+0] = texture_extension_number++; lightmap_textures[numlightmaps+0] = texture_extension_number++;
lightmap_textures[numlightmaps+1] = texture_extension_number++; lightmap_textures[numlightmaps+1] = texture_extension_number++;
lightmap_textures[numlightmaps+2] = texture_extension_number++; lightmap_textures[numlightmaps+2] = texture_extension_number++;
lightmap_textures[numlightmaps+3] = texture_extension_number++; lightmap_textures[numlightmaps+3] = texture_extension_number++;
deluxmap_textures = BZ_Realloc(deluxmap_textures, sizeof(*deluxmap_textures)*(numlightmaps+4)); deluxmap_textures = BZ_Realloc(deluxmap_textures, sizeof(*deluxmap_textures)*(numlightmaps+4));
deluxmap_textures[numlightmaps+0] = texture_extension_number++; deluxmap_textures[numlightmaps+0] = texture_extension_number++;
deluxmap_textures[numlightmaps+1] = texture_extension_number++; deluxmap_textures[numlightmaps+1] = texture_extension_number++;
deluxmap_textures[numlightmaps+2] = texture_extension_number++; deluxmap_textures[numlightmaps+2] = texture_extension_number++;

View file

@ -409,22 +409,14 @@ void IWebShutdown(void)
//replacement for Z_Malloc. It simply allocates up to a reserve ammount. //replacement for Z_Malloc. It simply allocates up to a reserve ammount.
void *IWebMalloc(int size) void *IWebMalloc(int size)
{ {
char *mem = Z_TagMalloc(size+32768, 15); void *mem = BZF_Malloc(size);
if (!mem) memset(mem, 0, size);
return NULL; //bother return mem;
Z_Free(mem);
return Z_Malloc(size); //allocate the real ammount
} }
void *IWebRealloc(void *old, int size) void *IWebRealloc(void *old, int size)
{ {
char *mem = Z_TagMalloc(size+32768, 15); return BZF_Realloc(old, size);
if (!mem) //make sure there will be padding left
return NULL; //bother
Z_Free(mem);
return BZ_Realloc(old, size);
} }
#endif #endif

View file

@ -5477,7 +5477,7 @@ void PF_forgetstring(progfuncs_t *prinst, struct globalvars_s *pr_globals)
return; return;
} }
((int *)s)[0] = 0xabcd1234; ((int *)s)[0] = 0xabcd1234;
Z_Free(s); Z_TagFree(s);
} }
void PF_strlen(progfuncs_t *prinst, struct globalvars_s *pr_globals) void PF_strlen(progfuncs_t *prinst, struct globalvars_s *pr_globals)
{ {
@ -6348,7 +6348,7 @@ typedef struct sqlserver_s
{ {
void *thread; // worker thread for server void *thread; // worker thread for server
MYSQL *mysql; // mysql server MYSQL *mysql; // mysql server
qboolean active; // set to false to kill thread volatile qboolean active; // set to false to kill thread
void *requestlock; // mutex for queue read/write void *requestlock; // mutex for queue read/write
void *resultlock; // mutex for queue read/write void *resultlock; // mutex for queue read/write
int querynum; // next reference number for queries int querynum; // next reference number for queries
@ -6377,14 +6377,12 @@ queryresult_t *SQL_PullResult(sqlserver_t *server)
queryresult_t *qres; queryresult_t *qres;
Sys_LockMutex(server->resultlock); Sys_LockMutex(server->resultlock);
qres = server->results; qres = server->results;
if (!qres) if (qres)
{ {
Sys_UnlockMutex(server->resultlock); server->results = qres->next;
return NULL; if (!server->results)
server->resultslast = NULL;
} }
server->results = qres->next;
if (!server->results)
server->resultslast = NULL;
Sys_UnlockMutex(server->resultlock); Sys_UnlockMutex(server->resultlock);
return qres; return qres;
@ -6406,14 +6404,12 @@ queryrequest_t *SQL_PullRequest(sqlserver_t *server)
queryrequest_t *qreq; queryrequest_t *qreq;
Sys_LockMutex(server->requestlock); Sys_LockMutex(server->requestlock);
qreq = server->requests; qreq = server->requests;
if (!qreq) if (qreq)
{ {
Sys_UnlockMutex(server->requestlock); server->requests = qreq->next;
return NULL; if (!server->requests)
server->requestslast = NULL;
} }
server->requests = qreq->next;
if (!server->requests)
server->requestslast = NULL;
Sys_UnlockMutex(server->requestlock); Sys_UnlockMutex(server->requestlock);
return qreq; return qreq;
@ -6427,8 +6423,9 @@ int sql_serverworker(void *sref)
sqlserver_t *server = (sqlserver_t *)sref; sqlserver_t *server = (sqlserver_t *)sref;
char *error = NULL; char *error = NULL;
my_bool reconnect = 1; my_bool reconnect = 1;
int tinit;
if (mysql_thread_init()) if (tinit = mysql_thread_init())
error = "MYSQL thread init failed"; error = "MYSQL thread init failed";
else if (!(server->mysql = mysql_init(NULL))) else if (!(server->mysql = mysql_init(NULL)))
error = "MYSQL init failed"; error = "MYSQL init failed";
@ -6483,36 +6480,49 @@ int sql_serverworker(void *sref)
if (qerror) if (qerror)
qesize = Q_strlen(qerror); qesize = Q_strlen(qerror);
qres = (queryresult_t *)Z_Malloc(sizeof(queryresult_t) + qesize); qres = (queryresult_t *)ZF_Malloc(sizeof(queryresult_t) + qesize);
if (qerror) if (qres)
Q_strncpy(qres->error, qerror, qesize); {
qres->result = mysqlres; if (qerror)
qres->rows = rows; Q_strncpy(qres->error, qerror, qesize);
qres->columns = columns; qres->result = mysqlres;
qres->request = qreq; qres->rows = rows;
qres->eof = true; // store result has no more rows to read afterwards qres->columns = columns;
qreq->next = NULL; qres->request = qreq;
qres->eof = true; // store result has no more rows to read afterwards
qreq->next = NULL;
SQL_PushResult(server, qres); SQL_PushResult(server, qres);
}
else // we're screwed here so bomb out
{
server->active = false;
error = "MALLOC ERROR! Unable to allocate query result!";
break;
}
} }
} }
if (server->mysql)
mysql_close(server->mysql);
// if we have a server error we still need to put it on the queue // if we have a server error we still need to put it on the queue
if (error) if (error)
{ {
int esize = Q_strlen(error); int esize = Q_strlen(error);
queryresult_t *qres = (queryresult_t *)Z_Malloc(sizeof(queryresult_t) + esize); queryresult_t *qres = (queryresult_t *)Z_Malloc(sizeof(queryresult_t) + esize);
if (qres)
{ // hopefully the mysql_close gained us some memory otherwise we're pretty screwed
qres->rows = qres->columns = -1;
Q_strncpy(qres->error, error, esize);
qres->rows = qres->columns = -1; SQL_PushResult(server, qres);
Q_strncpy(qres->error, error, esize); }
SQL_PushResult(server, qres);
} }
mysql_close(server->mysql); if (!tinit)
mysql_thread_end();
mysql_thread_end();
return 0; return 0;
} }
@ -6553,8 +6563,28 @@ void PF_sqlconnect (progfuncs_t *prinst, struct globalvars_s *pr_globals)
server->requestlock = Sys_CreateMutex(); server->requestlock = Sys_CreateMutex();
server->resultlock = Sys_CreateMutex(); server->resultlock = Sys_CreateMutex();
server->thread = Sys_CreateThread(sql_serverworker, (void *)server, 1024); if (!server->requestlock || !server->resultlock)
{
if (server->requestlock)
Sys_DestroyMutex(server->requestlock);
if (server->resultlock)
Sys_DestroyMutex(server->resultlock);
Z_Free(server);
sqlservercount--;
G_FLOAT(OFS_RETURN) = -1;
return;
}
server->thread = Sys_CreateThread(sql_serverworker, (void *)server, 1024);
if (!server->thread)
{
Z_Free(server);
sqlservercount--;
G_FLOAT(OFS_RETURN) = -1;
return;
}
G_FLOAT(OFS_RETURN) = serverref; G_FLOAT(OFS_RETURN) = serverref;
} }
@ -6576,10 +6606,10 @@ void PF_sqlopenquery (progfuncs_t *prinst, struct globalvars_s *pr_globals)
int callfunc = G_INT(OFS_PARM1); int callfunc = G_INT(OFS_PARM1);
char *querystr = PF_VarString(prinst, 2, pr_globals); char *querystr = PF_VarString(prinst, 2, pr_globals);
int qsize = Q_strlen(querystr); int qsize = Q_strlen(querystr);
queryrequest_t *qreq = (queryrequest_t *)Z_Malloc(sizeof(queryrequest_t) + qsize); queryrequest_t *qreq = (queryrequest_t *)ZF_Malloc(sizeof(queryrequest_t) + qsize);
int querynum; int querynum;
if (serverref < 0 || serverref >= sqlservercount || sqlservers[serverref]->active == false) if (!qreq || serverref < 0 || serverref >= sqlservercount || sqlservers[serverref]->active == false)
{ {
G_FLOAT(OFS_RETURN) = -1; G_FLOAT(OFS_RETURN) = -1;
return; return;

View file

@ -240,6 +240,8 @@ void SV_Shutdown (void)
#ifdef IWEB_H__ #ifdef IWEB_H__
IWebShutdown(); IWebShutdown();
#endif #endif
Memory_DeInit();
} }
/* /*

View file

@ -675,7 +675,7 @@ qboolean SVQ2_InitGameProgs(void)
import.WriteAngle = PFQ2_WriteAngle; import.WriteAngle = PFQ2_WriteAngle;
import.TagMalloc = Z_TagMalloc; import.TagMalloc = Z_TagMalloc;
import.TagFree = Z_Free; import.TagFree = Z_TagFree;
import.FreeTags = Z_FreeTags; import.FreeTags = Z_FreeTags;
import.cvar = Q2Cvar_Get; import.cvar = Q2Cvar_Get;

View file

@ -1653,13 +1653,19 @@ int BL_AvailableMemory(void)
} }
void *BL_Malloc(int size) void *BL_Malloc(int size)
{ {
int *mem;
botlibmemoryavailable-=size; botlibmemoryavailable-=size;
return Z_TagMalloc(size, Z_TAG_BOTLIB);
mem = (int *)Z_TagMalloc(size+sizeof(int), Z_TAG_BOTLIB);
mem[0] = size;
return (void *)(mem + 1);
} }
void BL_Free(void *mem) void BL_Free(void *mem)
{ {
botlibmemoryavailable+=Z_MemSize(mem); int *memref = ((int *)mem) - 1;
Z_Free(mem); botlibmemoryavailable+=memref[0];
Z_Free(memref);
} }
void *BL_HunkMalloc(int size) void *BL_HunkMalloc(int size)
{ {