}
+unsigned int qmalloctotal_alloc, qmalloctotal_alloccount, qmalloctotal_free, qmalloctotal_freecount;
+
+void *qmalloc(unsigned int size)
+{
+ unsigned int *mem;
+ qmalloctotal_alloc += size;
+ qmalloctotal_alloccount++;
+ mem = malloc(size+sizeof(unsigned int));
+ *mem = size;
+ return (void *)(mem + 1);
+}
+
+void qfree(void *mem)
+{
+ unsigned int *m;
+ m = mem;
+ m--; // back up to size
+ qmalloctotal_free += *m; // size
+ qmalloctotal_freecount++;
+ free(m);
+}
+
+void GL_TextureStats_PrintTotal(void);
+extern int hunk_low_used, hunk_high_used, hunk_size;
+void COM_Memstats_f(void)
+{
+ Con_Printf("%i malloc calls totalling %i bytes (%.4gMB)\n%i free calls totalling %i bytes (%.4gMB)\n%i bytes (%.4gMB) currently allocated\n", qmalloctotal_alloccount, qmalloctotal_alloc, qmalloctotal_alloc / 1048576.0, qmalloctotal_freecount, qmalloctotal_free, qmalloctotal_free / 1048576.0, qmalloctotal_alloc - qmalloctotal_free, (qmalloctotal_alloc - qmalloctotal_free) / 1048576.0);
+ GL_TextureStats_PrintTotal();
+ Con_Printf ("%i bytes (%.4gMB) of %.4gMB hunk in use\n", hunk_low_used + hunk_high_used, (hunk_low_used + hunk_high_used) / 1048576.0, hunk_size / 1048576.0);
+}
+
+
/*
================
COM_Init
Cvar_RegisterVariable (®istered);
Cvar_RegisterVariable (&cmdline);
Cmd_AddCommand ("path", COM_Path_f);
+ Cmd_AddCommand ("memstats", COM_Memstats_f);
COM_InitFilesystem ();
COM_CheckRegistered ();
buf = loadbuf;
}
else if (usehunk == 5)
- buf = malloc (len+1);
+ buf = qmalloc (len+1);
else
Sys_Error ("COM_LoadFile: bad usehunk");
newfiles = Hunk_AllocName (numpackfiles * sizeof(packfile_t), "packfile");
- info = malloc(sizeof(*info)*MAX_FILES_IN_PACK);
+ info = qmalloc(sizeof(*info)*MAX_FILES_IN_PACK);
Sys_FileSeek (packhandle, header.dirofs);
Sys_FileRead (packhandle, (void *)info, header.dirlen);
newfiles[i].filepos = LittleLong(info[i].filepos);
newfiles[i].filelen = LittleLong(info[i].filelen);
}
- free(info);
+ qfree(info);
pack = Hunk_Alloc (sizeof (pack_t));
strcpy (pack->filename, packfile);
return false;
}
-
//============================================================================
+extern void *qmalloc(unsigned int size);
+extern void qfree(void *mem);
+
+//============================================================================
+
typedef struct sizebuf_s
{
qboolean allowoverflow; // if false, do a Sys_Error
-#include <stdlib.h>
+#include "quakedef.h"
void fractalnoise(unsigned char *noise, int size, int startgrid)
{
#define n(x,y) noisebuf[((y)&size1)*size+((x)&size1)]
if (startgrid > size)
startgrid = size;
- noisebuf = calloc(size*size, sizeof(int));
+ noisebuf = qmalloc(size*size*sizeof(int));
+ memset(noisebuf, 0, size*size*sizeof(int));
amplitude = 32767;
// quick 1x1 case which the rest of the code can't handle
for (y = 0;y < size;y++)
for (x = 0;x < size;x++)
*noise++ = (n(x,y) - min) * 255 / max;
- free(noisebuf);
+ qfree(noisebuf);
#undef n
}
\ No newline at end of file
//
// load the pic from disk
//
- dat = (qpic_t *)COM_LoadTempFile (path, false);
+ dat = (qpic_t *)COM_LoadMallocFile (path, false);
if (!dat)
Sys_Error ("Draw_CachePic: failed to load %s", path);
SwapPic (dat);
gl->tl = 0;
gl->th = 1;
+ qfree(dat);
+
return &pic->pic;
}
c = pic->width * pic->height;
src = menuplyr_pixels;
- dest = trans = malloc(c);
+ dest = trans = qmalloc(c);
for (i = 0;i < c;i++)
*dest++ = translation[*src++];
c = GL_LoadTexture ("translatedplayerpic", pic->width, pic->height, trans, false, true, 1);
- free(trans);
+ qfree(trans);
if (!r_render.value)
return;
void gl_models_start()
{
// allocate vertex processing arrays
- aliasvert = malloc(sizeof(float[MD2MAX_VERTS][3]));
- aliasvertnorm = malloc(sizeof(float[MD2MAX_VERTS][3]));
- aliasvertcolor = malloc(sizeof(byte[MD2MAX_VERTS][4]));
- aliasvertcolor2 = malloc(sizeof(byte[MD2MAX_VERTS][4])); // used temporarily for tinted coloring
+ aliasvert = qmalloc(sizeof(float[MD2MAX_VERTS][3]));
+ aliasvertnorm = qmalloc(sizeof(float[MD2MAX_VERTS][3]));
+ aliasvertcolor = qmalloc(sizeof(byte[MD2MAX_VERTS][4]));
+ aliasvertcolor2 = qmalloc(sizeof(byte[MD2MAX_VERTS][4])); // used temporarily for tinted coloring
makechrometexture();
}
void gl_models_shutdown()
{
- free(aliasvert);
- free(aliasvertnorm);
- free(aliasvertcolor);
- free(aliasvertcolor2);
+ qfree(aliasvert);
+ qfree(aliasvertnorm);
+ qfree(aliasvertcolor);
+ qfree(aliasvertcolor2);
}
void GL_Models_Init()
void gl_poly_start()
{
int i;
- transvert = malloc(MAX_TRANSVERTS * sizeof(transvert_t));
- transpoly = malloc(MAX_TRANSPOLYS * sizeof(transpoly_t));
- transpolyindex = malloc(MAX_TRANSPOLYS * sizeof(unsigned short));
- wallvert = malloc(MAX_WALLVERTS * sizeof(wallvert_t));
- wallpoly = malloc(MAX_WALLPOLYS * sizeof(wallpoly_t));
- skyvert = malloc(MAX_SKYVERTS * sizeof(skyvert_t));
- skypoly = malloc(MAX_SKYPOLYS * sizeof(skypoly_t));
+ transvert = qmalloc(MAX_TRANSVERTS * sizeof(transvert_t));
+ transpoly = qmalloc(MAX_TRANSPOLYS * sizeof(transpoly_t));
+ transpolyindex = qmalloc(MAX_TRANSPOLYS * sizeof(unsigned short));
+ wallvert = qmalloc(MAX_WALLVERTS * sizeof(wallvert_t));
+ wallpoly = qmalloc(MAX_WALLPOLYS * sizeof(wallpoly_t));
+ skyvert = qmalloc(MAX_SKYVERTS * sizeof(skyvert_t));
+ skypoly = qmalloc(MAX_SKYPOLYS * sizeof(skypoly_t));
transreciptable[0] = 0.0f;
for (i = 1;i < 256;i++)
transreciptable[i] = 1.0f / i;
}
void gl_poly_shutdown()
{
- free(transvert);
- free(transpoly);
- free(transpolyindex);
- free(wallvert);
- free(wallpoly);
- free(skyvert);
- free(skypoly);
+ qfree(transvert);
+ qfree(transpoly);
+ qfree(transpolyindex);
+ qfree(wallvert);
+ qfree(wallpoly);
+ qfree(skyvert);
+ qfree(skypoly);
}
void GL_Poly_Init()
if (nosubimagefragments || nosubimage)
{
if (!lightmaps[texnum])
- lightmaps[texnum] = calloc(BLOCK_WIDTH*BLOCK_HEIGHT*4, 1);
+ {
+ lightmaps[texnum] = qmalloc(BLOCK_WIDTH*BLOCK_HEIGHT*4);
+ memset(lightmaps[texnum], 0, BLOCK_WIDTH*BLOCK_HEIGHT*4);
+ }
}
// LordHavoc: clear texture to blank image, fragments are uploaded using subimage
else if (!allocated[texnum][0])
return;
}
- buffer = malloc(glwidth*glheight*3);
+ buffer = qmalloc(glwidth*glheight*3);
glReadPixels (glx, gly, glwidth, glheight, GL_RGB, GL_UNSIGNED_BYTE, buffer);
Image_WriteTGARGB_preflipped(filename, glwidth, glheight, buffer);
- free (buffer);
+ qfree(buffer);
Con_Printf ("Wrote %s\n", filename);
}
Con_Printf("%i textures, totalling %.3f mbytes\n", numgltextures, t / 1024.0 / 1024.0);
}
+void GL_TextureStats_PrintTotal(void)
+{
+ int i, t = 0;
+ gltexture_t *glt;
+ for (i = 0, glt = gltextures;i < numgltextures;i++, glt++)
+ t += glt->totaltexels;
+ Con_Printf("%i textures, totalling %.3f mbytes\n", numgltextures, t / 1024.0 / 1024.0);
+}
+
extern int buildnumber;
char engineversion[40];
out = outdata;
fstep = (int) (inheight*65536.0f/outheight);
- row1 = malloc(outwidth*4);
- row2 = malloc(outwidth*4);
+ row1 = qmalloc(outwidth*4);
+ row2 = qmalloc(outwidth*4);
inrow = indata;
oldy = 0;
GL_ResampleTextureLerpLine (inrow, row1, inwidth, outwidth);
row1 -= outwidth*4;
}
}
- free(row1);
- free(row2);
+ qfree(row1);
+ qfree(row2);
}
else
{
void GL_FreeTexels(gltexture_t *glt)
{
if (glt->texels[0])
- free(glt->texels[0]);
+ qfree(glt->texels[0]);
glt->texels[0] = 0;
}
glt->totaltexels = size;
while (i < MAXMIPS)
glt->texels[i++] = NULL;
- glt->texels[0] = malloc(size);
+ glt->texels[0] = qmalloc(size);
for (i = 1;i < MAXMIPS && glt->texels[i];i++)
glt->texels[i] += (int) glt->texels[0];
}
{
size = width*height*4;
glt->totaltexels = size;
- glt->texels[0] = malloc(size);
+ glt->texels[0] = qmalloc(size);
for (i = 1;i < MAXMIPS;i++)
glt->texels[i] = NULL;
}
else if (width == width2 && height == height2) // perfect match for top level, but needs to be reduced
{
byte *temptexels2;
- temptexels2 = malloc(width2*height2*4); // scaleup buffer
+ temptexels2 = qmalloc(width2*height2*4); // scaleup buffer
if (bytesperpixel == 1) // 8bit
Image_Copy8bitRGBA(data, temptexels2, width*height, d_8to24table);
else
else
GL_MipReduce(temptexels2, temptexels2, w, h, width3, height3);
}
- free(temptexels2);
+ qfree(temptexels2);
}
else // scaling...
{
byte *temptexels;
// pre-scaleup buffer
- temptexels = malloc(width*height*4);
+ temptexels = qmalloc(width*height*4);
if (bytesperpixel == 1) // 8bit
Image_Copy8bitRGBA(data, temptexels, width*height, d_8to24table);
else
if (width2 != width3 || height2 != height3) // reduced by gl_pic_mip or gl_max_size
{
byte *temptexels2;
- temptexels2 = malloc(width2*height2*4); // scaleup buffer
+ temptexels2 = qmalloc(width2*height2*4); // scaleup buffer
GL_ResampleTexture(temptexels, width, height, temptexels2, width2, height2);
while (width2 > width3 || height2 > height3)
{
else
GL_MipReduce(temptexels2, temptexels2, w, h, width3, height3);
}
- free(temptexels2);
+ qfree(temptexels2);
}
else // copy directly
GL_ResampleTexture(temptexels, width, height, glt->texels[0], width2, height2);
- free(temptexels);
+ qfree(temptexels);
}
if (alpha)
{
}
}
skyboxside[i] = GL_LoadTexture(va("skyboxside%d", i), image_width, image_height, image_rgba, false, false, 4);
- free (image_rgba);
+ qfree(image_rgba);
}
}
int length; // length of the chain
} hashindex[256][256];
int *hashtable;
- token = malloc(size*sizeof(struct hctoken));
- hashtable = malloc(size*sizeof(int));
+ token = qmalloc(size*sizeof(struct hctoken));
+ hashtable = qmalloc(size*sizeof(int));
in = indata;
memset(&hashindex, 0, sizeof(hashindex));
// count the chain lengths
Sys_Error("Invalid signature in vcr file\n");
Sys_FileRead (vcrFile, &com_argc, sizeof(int));
- com_argv = malloc(com_argc * sizeof(char *));
+ com_argv = qmalloc(com_argc * sizeof(char *));
com_argv[0] = host_parms.argv[0];
for (i = 0; i < com_argc; i++)
{
Sys_FileRead (vcrFile, &len, sizeof(int));
- p = malloc(len);
+ p = qmalloc(len);
Sys_FileRead (vcrFile, p, len);
com_argv[i+1] = p;
}
fseek (f, sizeof(pcxbuf) - 4, SEEK_SET);
count = (pcx->xmax+1) * (pcx->ymax+1);
- image_rgba = malloc( count * 4);
+ image_rgba = qmalloc( count * 4);
for (y=0 ; y<=pcx->ymax ; y++)
{
rows = targa_header.height;
numPixels = columns * rows;
- image_rgba = malloc (numPixels*4);
+ image_rgba = qmalloc(numPixels*4);
if (targa_header.id_length != 0)
fseek(fin, targa_header.id_length, SEEK_CUR); // skip TARGA image comment
if (matchheight && height != matchheight)
return NULL;
- image_rgba = malloc(width*height*4);
+ image_rgba = qmalloc(width*height*4);
fread(image_rgba + width*height*3, 1, width*height, f);
fclose(f);
return data; // some transparency
else
{
- free(data);
+ qfree(data);
return NULL; // all opaque
}
}
if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
return 0;
texnum = GL_LoadTexture (filename, image_width, image_height, data, mipmap, true, 4);
- free(data);
+ qfree(data);
return texnum;
}
if (!(data = loadimagepixelsmask (filename, complain, matchwidth, matchheight)))
return 0;
texnum = GL_LoadTexture (filename, image_width, image_height, data, mipmap, true, 4);
- free(data);
+ qfree(data);
return texnum;
}
count = image_makemask(data, data, image_width * image_height);
if (count)
{
- filename2 = malloc(strlen(filename) + 6);
+ filename2 = qmalloc(strlen(filename) + 6);
sprintf(filename2, "%s_mask", filename);
image_masktexnum = GL_LoadTexture (filename2, image_width, image_height, data, mipmap, true, 4);
- free(filename2);
+ qfree(filename2);
}
- free(data);
+ qfree(data);
return texnum;
}
{
byte *buffer, *in, *out, *end;
- buffer = malloc(width*height*3 + 18);
+ buffer = qmalloc(width*height*3 + 18);
memset (buffer, 0, 18);
buffer[2] = 2; // uncompressed type
}
COM_WriteFile (filename, buffer, width*height*3 + 18 );
- free(buffer);
+ qfree(buffer);
}
void Image_WriteTGARGB (char *filename, int width, int height, byte *data)
int y;
byte *buffer, *in, *out, *end;
- buffer = malloc(width*height*3 + 18);
+ buffer = qmalloc(width*height*3 + 18);
memset (buffer, 0, 18);
buffer[2] = 2; // uncompressed type
}
COM_WriteFile (filename, buffer, width*height*3 + 18 );
- free(buffer);
+ qfree(buffer);
}
void Image_WriteTGARGBA (char *filename, int width, int height, byte *data)
int y;
byte *buffer, *in, *out, *end;
- buffer = malloc(width*height*4 + 18);
+ buffer = qmalloc(width*height*4 + 18);
memset (buffer, 0, 18);
buffer[2] = 2; // uncompressed type
}
COM_WriteFile (filename, buffer, width*height*4 + 18 );
- free(buffer);
+ qfree(buffer);
}
Host_Error ("Mod_LoadAliasModel: Invalid # of skins: %d\n", numskins);
s = width * height;
- skintemp = malloc(s);
+ skintemp = qmalloc(s);
// LordHavoc: skim the data, measure the number of skins and number of groups
skinranges = numskins;
}
}
loadmodel->numskins = numskins;
- free(skintemp);
+ qfree(skintemp);
return (void *)pskintype;
}
start = Hunk_LowMark ();
// if (!temptris)
-// temptris = malloc(sizeof(temptris_t) * MD2MAX_TRIANGLES);
+// temptris = qmalloc(sizeof(temptris_t) * MD2MAX_TRIANGLES);
pinmodel = (md2_t *)buffer;
{
char name[64];
byte *data2;
- data2 = malloc(tx->width*tx->height);
+ data2 = qmalloc(tx->width*tx->height);
for (j = 0;j < tx->width*tx->height;j++)
data2[j] = data[j] >= 224 ? 0 : data[j]; // no fullbrights
tx->gl_texturenum = GL_LoadTexture (tx->name, tx->width, tx->height, data2, true, transparent, 1);
for (j = 0;j < tx->width*tx->height;j++)
data2[j] = data[j] >= 224 ? data[j] : 0; // only fullbrights
tx->gl_glowtexturenum = GL_LoadTexture (name, tx->width, tx->height, data2, true, transparent, 1);
- free(data2);
+ qfree(data2);
}
else
{
}
}
if (freeimage)
- free(data);
+ qfree(data);
}
//
{
void *d;
unsigned *buf;
-// byte stackbuf[1024]; // avoid dirtying the cache heap
if (!mod->needload)
{
// load the file
buf = (unsigned *)COM_LoadMallocFile (mod->name, false);
-// buf = (unsigned *)COM_LoadStackFile (mod->name, stackbuf, sizeof(stackbuf), false);
if (!buf)
{
if (crash)
Mod_LoadBrushModel (mod, buf);
break;
}
- free(buf);
+ qfree(buf);
return mod;
}
{
pspriteframe->gl_texturenum = GL_LoadTexture (name, width, height, (byte *)(pinframe + 1), true, true, bytesperpixel);
// make fog version (just alpha)
- pixbuf = pixel = malloc(width*height*4);
+ pixbuf = pixel = qmalloc(width*height*4);
inpixel = (byte *)(pinframe + 1);
if (bytesperpixel == 1)
{
}
sprintf (name, "%s_%ifog", loadmodel->name, framenum);
pspriteframe->gl_fogtexturenum = GL_LoadTexture (name, width, height, pixbuf, true, true, 4);
- free(pixbuf);
+ qfree(pixbuf);
}
return (void *)((byte *)pinframe + sizeof (dspriteframe_t) + size);
if (!pal)
Sys_Error ("Couldn't load gfx/palette.lmp");
memcpy(host_basepal, pal, 765);
- free(pal);
+ qfree(pal);
host_basepal[765] = host_basepal[766] = host_basepal[767] = 0; // LordHavoc: force the transparent color to black
Palette_Setup8to24();
// Palette_Setup15to8();
void r_part_start()
{
- particles = (particle_t *) malloc (r_numparticles * sizeof(particle_t));
- freeparticles = (void *) malloc (r_numparticles * sizeof(particle_t *));
+ particles = (particle_t *) qmalloc(r_numparticles * sizeof(particle_t));
+ freeparticles = (void *) qmalloc(r_numparticles * sizeof(particle_t *));
R_InitParticleTexture ();
}
void r_part_shutdown()
{
- free(particles);
- free(freeparticles);
+ qfree(particles);
+ qfree(freeparticles);
}
/*
int len;
float stepscale;
sfxcache_t *sc;
- byte stackbuf[1*1024]; // avoid dirtying the cache heap
// see if still in memory
sc = Cache_Check (&s->cache);
// Con_Printf ("loading %s\n",namebuffer);
- data = COM_LoadStackFile(namebuffer, stackbuf, sizeof(stackbuf), false);
+ data = COM_LoadMallocFile(namebuffer, false);
if (!data)
{
if (info.channels < 1 || info.channels > 2)
{
Con_Printf ("%s has an unsupported number of channels (%i)\n",s->name, info.channels);
+ qfree(data);
return NULL;
}
/*
sc = Cache_Alloc ( &s->cache, len + sizeof(sfxcache_t), s->name);
if (!sc)
+ {
+ qfree(data);
return NULL;
+ }
sc->length = info.samples;
sc->loopstart = info.loopstart;
ResampleSfx (s, sc->speed, data + info.dataofs);
+ qfree(data);
return sc;
}
j = COM_CheckParm("-mem");
if (j)
host_parms.memsize = (int) (atof(com_argv[j+1]) * 1024 * 1024);
- host_parms.membase = malloc (host_parms.memsize);
+ host_parms.membase = qmalloc(host_parms.memsize);
if (!host_parms.membase)
{
printf("Unable to allocate heap memory\n");
host_parms.memsize = atoi (com_argv[t]) * 1048576;
}
- host_parms.membase = malloc (host_parms.memsize);
+ host_parms.membase = qmalloc(host_parms.memsize);
if (!host_parms.membase)
Sys_Error ("Not enough memory free; check disk space\n");
memset (&host_parms, 0, sizeof(host_parms));
host_parms.memsize = 16384*1024;
- host_parms.membase = malloc (parms.memsize);
+ host_parms.membase = qmalloc(parms.memsize);
_getcwd (cwd, sizeof(cwd));
if (cwd[strlen(cwd)-1] == '\\')
infotableofs = LittleLong(header.infotableofs);
if (fseek(file, infotableofs, SEEK_SET))
{Con_Printf ("W_LoadTextureWadFile: unable to seek to lump table");return;}
- if (!(lumps = malloc(sizeof(lumpinfo_t)*numlumps)))
+ if (!(lumps = qmalloc(sizeof(lumpinfo_t)*numlumps)))
{Con_Printf ("W_LoadTextureWadFile: unable to allocate temporary memory for lump table");return;}
if (fread(lumps, sizeof(lumpinfo_t), numlumps, file) != numlumps)
texwadlump[j].position = LittleLong(lump_p->filepos);
texwadlump[j].size = LittleLong(lump_p->disksize);
}
- free(lumps);
+ qfree(lumps);
// leaves the file open
}
{Con_Printf("W_GetTexture: corrupt WAD3 file");return FALSE;}
// allocate space for expanded image,
// and load incoming image into upper area (overwritten as it expands)
- if (!(data = outdata = malloc(image_width*image_height*4)))
+ if (!(data = outdata = qmalloc(image_width*image_height*4)))
{Con_Printf("W_GetTexture: out of memory");return FALSE;}
indata = outdata + image_width*image_height*3;
datasize = image_width*image_height*85/64;