From 4ee1d4780706dc1ed70ec4f7aa8b7712518a5c4f Mon Sep 17 00:00:00 2001 From: lordhavoc Date: Mon, 29 Jan 2001 08:14:37 +0000 Subject: [PATCH] every malloc/calloc/free converted to qmalloc/qfree with tracking (memstats command), and all uses of Hunk_TempAlloc have gone away git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@141 d7cf8633-e32d-0410-b094-e92efae38249 --- common.c | 40 ++++++++++++++++++++++++++++++++++++---- common.h | 5 +++++ fractalnoise.c | 7 ++++--- gl_draw.c | 8 +++++--- gl_models.c | 16 ++++++++-------- gl_poly.c | 28 ++++++++++++++-------------- gl_rsurf.c | 5 ++++- gl_screen.c | 4 ++-- gl_textures.c | 35 ++++++++++++++++++++++------------- gl_warp.c | 2 +- hcompress.c | 4 ++-- host.c | 4 ++-- image.c | 30 +++++++++++++++--------------- model_alias.c | 6 +++--- model_brush.c | 6 +++--- model_shared.c | 4 +--- model_sprite.c | 4 ++-- palette.c | 2 +- r_part.c | 8 ++++---- snd_mem.c | 8 ++++++-- sys_linux.c | 2 +- sys_win.c | 2 +- sys_wind.c | 2 +- wad.c | 6 +++--- 24 files changed, 146 insertions(+), 92 deletions(-) diff --git a/common.c b/common.c index 71624aa2..bd8d2f8b 100644 --- a/common.c +++ b/common.c @@ -1167,6 +1167,38 @@ void COM_InitArgv (int argc, char **argv) } +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 @@ -1201,6 +1233,7 @@ void COM_Init (char *basedir) Cvar_RegisterVariable (®istered); Cvar_RegisterVariable (&cmdline); Cmd_AddCommand ("path", COM_Path_f); + Cmd_AddCommand ("memstats", COM_Memstats_f); COM_InitFilesystem (); COM_CheckRegistered (); @@ -1617,7 +1650,7 @@ byte *COM_LoadFile (char *path, int usehunk, qboolean quiet) buf = loadbuf; } else if (usehunk == 5) - buf = malloc (len+1); + buf = qmalloc (len+1); else Sys_Error ("COM_LoadFile: bad usehunk"); @@ -1710,7 +1743,7 @@ pack_t *COM_LoadPackFile (char *packfile) 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); @@ -1730,7 +1763,7 @@ pack_t *COM_LoadPackFile (char *packfile) 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); @@ -1923,4 +1956,3 @@ int COM_FileExists(char *filename) return false; } - diff --git a/common.h b/common.h index 152112cb..051b2acd 100644 --- a/common.h +++ b/common.h @@ -31,6 +31,11 @@ typedef enum {false, true} qboolean; //============================================================================ +extern void *qmalloc(unsigned int size); +extern void qfree(void *mem); + +//============================================================================ + typedef struct sizebuf_s { qboolean allowoverflow; // if false, do a Sys_Error diff --git a/fractalnoise.c b/fractalnoise.c index c03039a7..e9899a47 100644 --- a/fractalnoise.c +++ b/fractalnoise.c @@ -1,5 +1,5 @@ -#include +#include "quakedef.h" void fractalnoise(unsigned char *noise, int size, int startgrid) { @@ -8,7 +8,8 @@ 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 @@ -56,6 +57,6 @@ void fractalnoise(unsigned char *noise, int size, int startgrid) 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 diff --git a/gl_draw.c b/gl_draw.c index bad2c8c7..8b0a4378 100644 --- a/gl_draw.c +++ b/gl_draw.c @@ -209,7 +209,7 @@ qpic_t *Draw_CachePic (char *path) // // 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); @@ -232,6 +232,8 @@ qpic_t *Draw_CachePic (char *path) gl->tl = 0; gl->th = 1; + qfree(dat); + return &pic->pic; } @@ -484,12 +486,12 @@ void Draw_PicTranslate (int x, int y, qpic_t *pic, byte *translation) 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; diff --git a/gl_models.c b/gl_models.c index 594ca0fa..87adbe99 100644 --- a/gl_models.c +++ b/gl_models.c @@ -30,19 +30,19 @@ void makechrometexture() 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() diff --git a/gl_poly.c b/gl_poly.c index 1f18658e..4252170e 100644 --- a/gl_poly.c +++ b/gl_poly.c @@ -37,26 +37,26 @@ float transreciptable[256]; 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() diff --git a/gl_rsurf.c b/gl_rsurf.c index 21d9d646..4377edc7 100644 --- a/gl_rsurf.c +++ b/gl_rsurf.c @@ -1082,7 +1082,10 @@ int AllocBlock (int w, int h, short *x, short *y) 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]) diff --git a/gl_screen.c b/gl_screen.c index 045afa6e..ab123c0c 100644 --- a/gl_screen.c +++ b/gl_screen.c @@ -615,11 +615,11 @@ void SCR_ScreenShot_f (void) 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); } diff --git a/gl_textures.c b/gl_textures.c index f90f8781..952ea1f8 100644 --- a/gl_textures.c +++ b/gl_textures.c @@ -126,6 +126,15 @@ void GL_TextureStats_f(void) 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]; @@ -228,8 +237,8 @@ void GL_ResampleTexture (void *indata, int inwidth, int inheight, void *outdata, 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); @@ -276,8 +285,8 @@ void GL_ResampleTexture (void *indata, int inwidth, int inheight, void *outdata, row1 -= outwidth*4; } } - free(row1); - free(row2); + qfree(row1); + qfree(row2); } else { @@ -305,7 +314,7 @@ void GL_ResampleTexture (void *indata, int inwidth, int inheight, void *outdata, void GL_FreeTexels(gltexture_t *glt) { if (glt->texels[0]) - free(glt->texels[0]); + qfree(glt->texels[0]); glt->texels[0] = 0; } @@ -345,7 +354,7 @@ void GL_AllocTexels(gltexture_t *glt, int width, int height, int mipmapped) 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]; } @@ -353,7 +362,7 @@ void GL_AllocTexels(gltexture_t *glt, int width, int height, int mipmapped) { 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; } @@ -532,7 +541,7 @@ GL_LoadTexture_setup: 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 @@ -547,13 +556,13 @@ GL_LoadTexture_setup: 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 @@ -561,7 +570,7 @@ GL_LoadTexture_setup: 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) { @@ -573,11 +582,11 @@ GL_LoadTexture_setup: 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) { diff --git a/gl_warp.c b/gl_warp.c index d15a34be..495f5939 100644 --- a/gl_warp.c +++ b/gl_warp.c @@ -212,7 +212,7 @@ void R_LoadSkyBox (void) } } skyboxside[i] = GL_LoadTexture(va("skyboxside%d", i), image_width, image_height, image_rgba, false, false, 4); - free (image_rgba); + qfree(image_rgba); } } diff --git a/hcompress.c b/hcompress.c index 1db33de3..9e36868b 100644 --- a/hcompress.c +++ b/hcompress.c @@ -170,8 +170,8 @@ int hcompress_compress(void *indata, void *outdata, int size) 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 diff --git a/host.c b/host.c index 59b4db59..f9dc58d5 100644 --- a/host.c +++ b/host.c @@ -750,12 +750,12 @@ void Host_InitVCR () 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; } diff --git a/image.c b/image.c index 4ba31707..d12ed712 100644 --- a/image.c +++ b/image.c @@ -130,7 +130,7 @@ byte* LoadPCX (FILE *f, int matchwidth, int matchheight) 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++) { @@ -258,7 +258,7 @@ byte* LoadTGA (FILE *fin, int matchwidth, int matchheight) 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 @@ -397,7 +397,7 @@ byte* LoadLMP (FILE *f, int matchwidth, int matchheight) 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); @@ -490,7 +490,7 @@ byte* loadimagepixelsmask (char* filename, qboolean complain, int matchwidth, in return data; // some transparency else { - free(data); + qfree(data); return NULL; // all opaque } } @@ -502,7 +502,7 @@ int loadtextureimage (char* filename, int matchwidth, int matchheight, qboolean 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; } @@ -513,7 +513,7 @@ int loadtextureimagemask (char* filename, int matchwidth, int matchheight, qbool 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; } @@ -530,12 +530,12 @@ int loadtextureimagewithmask (char* filename, int matchwidth, int matchheight, q 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; } @@ -543,7 +543,7 @@ void Image_WriteTGARGB_preflipped (char *filename, int width, int height, byte * { 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 @@ -565,7 +565,7 @@ void Image_WriteTGARGB_preflipped (char *filename, int width, int height, byte * } COM_WriteFile (filename, buffer, width*height*3 + 18 ); - free(buffer); + qfree(buffer); } void Image_WriteTGARGB (char *filename, int width, int height, byte *data) @@ -573,7 +573,7 @@ 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 @@ -598,7 +598,7 @@ void Image_WriteTGARGB (char *filename, int width, int height, byte *data) } COM_WriteFile (filename, buffer, width*height*3 + 18 ); - free(buffer); + qfree(buffer); } void Image_WriteTGARGBA (char *filename, int width, int height, byte *data) @@ -606,7 +606,7 @@ 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 @@ -632,5 +632,5 @@ void Image_WriteTGARGBA (char *filename, int width, int height, byte *data) } COM_WriteFile (filename, buffer, width*height*4 + 18 ); - free(buffer); + qfree(buffer); } diff --git a/model_alias.c b/model_alias.c index 4d4e5885..c6b72f66 100644 --- a/model_alias.c +++ b/model_alias.c @@ -299,7 +299,7 @@ void *Mod_LoadAllSkins (int numskins, daliasskintype_t *pskintype, int width, in 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; @@ -373,7 +373,7 @@ void *Mod_LoadAllSkins (int numskins, daliasskintype_t *pskintype, int width, in } } loadmodel->numskins = numskins; - free(skintemp); + qfree(skintemp); return (void *)pskintype; } @@ -691,7 +691,7 @@ void Mod_LoadQ2AliasModel (model_t *mod, void *buffer) start = Hunk_LowMark (); // if (!temptris) -// temptris = malloc(sizeof(temptris_t) * MD2MAX_TRIANGLES); +// temptris = qmalloc(sizeof(temptris_t) * MD2MAX_TRIANGLES); pinmodel = (md2_t *)buffer; diff --git a/model_brush.c b/model_brush.c index db1e398c..c070d421 100644 --- a/model_brush.c +++ b/model_brush.c @@ -206,7 +206,7 @@ void Mod_LoadTextures (lump_t *l) { 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); @@ -215,7 +215,7 @@ void Mod_LoadTextures (lump_t *l) 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 { @@ -224,7 +224,7 @@ void Mod_LoadTextures (lump_t *l) } } if (freeimage) - free(data); + qfree(data); } // diff --git a/model_shared.c b/model_shared.c index 80ca54b5..236a5d60 100644 --- a/model_shared.c +++ b/model_shared.c @@ -153,7 +153,6 @@ model_t *Mod_LoadModel (model_t *mod, qboolean crash) { void *d; unsigned *buf; -// byte stackbuf[1024]; // avoid dirtying the cache heap if (!mod->needload) { @@ -171,7 +170,6 @@ model_t *Mod_LoadModel (model_t *mod, qboolean crash) // load the file buf = (unsigned *)COM_LoadMallocFile (mod->name, false); -// buf = (unsigned *)COM_LoadStackFile (mod->name, stackbuf, sizeof(stackbuf), false); if (!buf) { if (crash) @@ -205,7 +203,7 @@ model_t *Mod_LoadModel (model_t *mod, qboolean crash) Mod_LoadBrushModel (mod, buf); break; } - free(buf); + qfree(buf); return mod; } diff --git a/model_sprite.c b/model_sprite.c index 1dde9211..c1d63e52 100644 --- a/model_sprite.c +++ b/model_sprite.c @@ -91,7 +91,7 @@ void * Mod_LoadSpriteFrame (void * pin, mspriteframe_t **ppframe, int framenum, { 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) { @@ -120,7 +120,7 @@ void * Mod_LoadSpriteFrame (void * pin, mspriteframe_t **ppframe, int framenum, } 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); diff --git a/palette.c b/palette.c index 60969b14..f43efcfc 100644 --- a/palette.c +++ b/palette.c @@ -90,7 +90,7 @@ void Palette_Init() 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(); diff --git a/r_part.c b/r_part.c index 678e95bd..03980806 100644 --- a/r_part.c +++ b/r_part.c @@ -189,15 +189,15 @@ void R_InitParticleTexture (void) 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); } /* diff --git a/snd_mem.c b/snd_mem.c index 278d2eb4..f573c370 100644 --- a/snd_mem.c +++ b/snd_mem.c @@ -226,7 +226,6 @@ sfxcache_t *S_LoadSound (sfx_t *s) 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); @@ -240,7 +239,7 @@ sfxcache_t *S_LoadSound (sfx_t *s) // Con_Printf ("loading %s\n",namebuffer); - data = COM_LoadStackFile(namebuffer, stackbuf, sizeof(stackbuf), false); + data = COM_LoadMallocFile(namebuffer, false); if (!data) { @@ -253,6 +252,7 @@ sfxcache_t *S_LoadSound (sfx_t *s) 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; } /* @@ -270,7 +270,10 @@ sfxcache_t *S_LoadSound (sfx_t *s) 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; @@ -280,6 +283,7 @@ sfxcache_t *S_LoadSound (sfx_t *s) ResampleSfx (s, sc->speed, data + info.dataofs); + qfree(data); return sc; } diff --git a/sys_linux.c b/sys_linux.c index 45c1e5db..8095fa9b 100644 --- a/sys_linux.c +++ b/sys_linux.c @@ -416,7 +416,7 @@ int main (int c, char **v) 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"); diff --git a/sys_win.c b/sys_win.c index d0f0d793..57492626 100644 --- a/sys_win.c +++ b/sys_win.c @@ -718,7 +718,7 @@ int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLin 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"); diff --git a/sys_wind.c b/sys_wind.c index ec32703f..6703846f 100644 --- a/sys_wind.c +++ b/sys_wind.c @@ -269,7 +269,7 @@ int main (int argc, char **argv) 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] == '\\') diff --git a/wad.c b/wad.c index 48c58114..64d35d82 100644 --- a/wad.c +++ b/wad.c @@ -206,7 +206,7 @@ void W_LoadTextureWadFile (char *filename, int complain) 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) @@ -232,7 +232,7 @@ void W_LoadTextureWadFile (char *filename, int complain) texwadlump[j].position = LittleLong(lump_p->filepos); texwadlump[j].size = LittleLong(lump_p->disksize); } - free(lumps); + qfree(lumps); // leaves the file open } @@ -323,7 +323,7 @@ byte *W_GetTexture(char *name, int matchwidth, int matchheight) {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; -- 2.39.2