memory allocation checking cleanup

This commit is contained in:
Bill Currie 2002-05-14 06:37:28 +00:00
parent 1e0611e156
commit 6a5bbf6f0a
24 changed files with 38 additions and 78 deletions

View file

@ -91,8 +91,8 @@ void Sys_DebugLog(const char *file, const char *fmt, ...) __attribute__((format(
#define SYS_CHECKMEM(x) \ #define SYS_CHECKMEM(x) \
do { \ do { \
if (!(x)) \ if (!(x)) \
Sys_Error ("%s: Failed to reallocate memory.", __func__); \ Sys_Error ("%s: Failed to allocate memory.", __func__); \
} while (0) } while (0)
#endif // __sys_h #endif // __sys_h

View file

@ -72,8 +72,7 @@ Mod_Fullbright (byte * skin, int width, int height, char *name)
// ptexels = Hunk_Alloc(s); // ptexels = Hunk_Alloc(s);
ptexels = malloc (pixels); ptexels = malloc (pixels);
if (!ptexels) SYS_CHECKMEM (ptexels);
Sys_Error ("Mod_Fullbright: Memory Allocation Failure");
if (Mod_CalcFullbright (skin, ptexels, pixels)) { if (Mod_CalcFullbright (skin, ptexels, pixels)) {
Sys_DPrintf ("FB Model ID: '%s'\n", name); Sys_DPrintf ("FB Model ID: '%s'\n", name);
texnum = GL_LoadTexture (name, width, height, ptexels, true, true, 1); texnum = GL_LoadTexture (name, width, height, ptexels, true, true, 1);

View file

@ -658,8 +658,7 @@ Cmd_StuffCmds_f (void)
// pull out the commands // pull out the commands
build = malloc (s + 1); build = malloc (s + 1);
if (!build) SYS_CHECKMEM (build);
Sys_Error ("Cmd_StuffCmds_f: Memory Allocation Failure");
build[0] = 0; build[0] = 0;
for (i = 0; i < s - 1; i++) { for (i = 0; i < s - 1; i++) {
@ -1557,8 +1556,7 @@ Cmd_AddCommand (const char *cmd_name, xcommand_t function,
} }
cmd = calloc (1, sizeof (cmd_function_t)); cmd = calloc (1, sizeof (cmd_function_t));
if (!cmd) SYS_CHECKMEM (cmd);
Sys_Error ("Cmd_AddCommand: Memory Allocation Failure");
cmd->name = cmd_name; cmd->name = cmd_name;
cmd->function = function; cmd->function = function;
cmd->description = description; cmd->description = description;
@ -1699,8 +1697,7 @@ Cmd_CompleteBuildList (const char *partial)
len = strlen (partial); len = strlen (partial);
buf = malloc (sizeofbuf + sizeof (char *)); buf = malloc (sizeofbuf + sizeof (char *));
if (!buf) SYS_CHECKMEM (buf);
Sys_Error ("Cmd_CompleteBuildList: Memory Allocation Failure");
// Loop through the alias list and print all matches // Loop through the alias list and print all matches
for (cmd = cmd_functions; cmd; cmd = cmd->next) for (cmd = cmd_functions; cmd; cmd = cmd->next)
if (!strncasecmp (partial, cmd->name, len)) if (!strncasecmp (partial, cmd->name, len))
@ -1788,8 +1785,7 @@ Cmd_CompleteAliasBuildList (const char *partial)
len = strlen (partial); len = strlen (partial);
buf = malloc (sizeofbuf + sizeof (char *)); buf = malloc (sizeofbuf + sizeof (char *));
if (!buf) SYS_CHECKMEM (buf);
Sys_Error ("Cmd_CompleteAliasBuildList: Memory Allocation Failure");
// Loop through the alias list and print all matches // Loop through the alias list and print all matches
for (alias = cmd_alias; alias; alias = alias->next) for (alias = cmd_alias; alias; alias = alias->next)
if (!strncasecmp (partial, alias->name, len)) if (!strncasecmp (partial, alias->name, len))
@ -1927,8 +1923,7 @@ Cmd_Alias_f (void)
cmdalias_t **a; cmdalias_t **a;
alias = calloc (1, sizeof (cmdalias_t)); alias = calloc (1, sizeof (cmdalias_t));
if (!alias) SYS_CHECKMEM (alias);
Sys_Error ("Cmd_Alias_f: Memory Allocation Failure");
alias->name = strdup (s); alias->name = strdup (s);
Hash_Add (cmd_alias_hash, alias); Hash_Add (cmd_alias_hash, alias);
for (a = &cmd_alias; *a; a = &(*a)->next) for (a = &cmd_alias; *a; a = &(*a)->next)
@ -1939,8 +1934,7 @@ Cmd_Alias_f (void)
} }
// copy the rest of the command line // copy the rest of the command line
cmd = malloc (strlen (Cmd_Args (1)) + 2); // can never be longer cmd = malloc (strlen (Cmd_Args (1)) + 2); // can never be longer
if (!cmd) SYS_CHECKMEM (cmd);
Sys_Error ("Cmd_Alias_f: Memory Allocation Failure");
cmd[0] = 0; // start out with a null string cmd[0] = 0; // start out with a null string
c = Cmd_Argc (); c = Cmd_Argc ();
for (i = 2; i < c; i++) { for (i = 2; i < c; i++) {

View file

@ -216,8 +216,7 @@ Cvar_CompleteBuildList (const char *partial)
len = strlen(partial); len = strlen(partial);
buf = malloc(sizeofbuf + sizeof (char *)); buf = malloc(sizeofbuf + sizeof (char *));
if (!buf) SYS_CHECKMEM (buf);
Sys_Error ("Cvar_CompleteBuildList: Memory Allocation Failure");
// Loop through the alias list and print all matches // Loop through the alias list and print all matches
for (cvar = cvar_vars; cvar; cvar = cvar->next) for (cvar = cvar_vars; cvar; cvar = cvar->next)
if (!strncasecmp(partial, cvar->name, len)) if (!strncasecmp(partial, cvar->name, len))

View file

@ -149,15 +149,11 @@ LoadTGA (VFile *fin)
switch (targa->pixel_size) { switch (targa->pixel_size) {
case 24: case 24:
tex = Hunk_TempAlloc (field_offset (tex_t, data[numPixels * 3])); tex = Hunk_TempAlloc (field_offset (tex_t, data[numPixels * 3]));
if (!tex)
Sys_Error ("LoadTGA: Memory Allocation Failure");
tex->format = tex_rgb; tex->format = tex_rgb;
break; break;
default: default:
case 32: case 32:
tex = Hunk_TempAlloc (field_offset (tex_t, data[numPixels * 4])); tex = Hunk_TempAlloc (field_offset (tex_t, data[numPixels * 4]));
if (!tex)
Sys_Error ("LoadTGA: Memory Allocation Failure");
tex->format = tex_rgba; tex->format = tex_rgba;
break; break;
} }

View file

@ -603,8 +603,7 @@ SCR_ScreenShot_f (void)
return; return;
} }
buffer = malloc (glwidth * glheight * 3); buffer = malloc (glwidth * glheight * 3);
if (!buffer) SYS_CHECKMEM (buffer);
Sys_Error ("SCR_ScreenShot_f: Memory Allocation Failure");
qfglReadPixels (glx, gly, glwidth, glheight, GL_BGR_EXT, GL_UNSIGNED_BYTE, qfglReadPixels (glx, gly, glwidth, glheight, GL_BGR_EXT, GL_UNSIGNED_BYTE,
buffer); buffer);
WriteTGAfile (pcxname, buffer, glwidth, glheight); WriteTGAfile (pcxname, buffer, glwidth, glheight);

View file

@ -508,8 +508,7 @@ GL_Upload8 (byte * data, int width, int height, qboolean mipmap, qboolean alpha)
s = width * height; s = width * height;
trans = malloc (s * sizeof (unsigned int)); trans = malloc (s * sizeof (unsigned int));
if (!trans) SYS_CHECKMEM (trans);
Sys_Error ("GL_Upload8: Memory Allocation Failure");
// if there are no transparent pixels, make it a 3 component // if there are no transparent pixels, make it a 3 component
// texture even if it was specified as otherwise // texture even if it was specified as otherwise

View file

@ -407,8 +407,7 @@ AddVideoMode (const struct VideoMode *vmode)
Die("%s:%d: Duplicate mode name `%s'\n", Opt_modedb, line, Die("%s:%d: Duplicate mode name `%s'\n", Opt_modedb, line,
vmode->name); vmode->name);
vmode2 = malloc(sizeof(struct VideoMode)); vmode2 = malloc(sizeof(struct VideoMode));
if (!vmode2) SYS_CHECKMEM (vmode2);
Sys_Error ("AddVideoMode: Memory Allocation Failure");
*vmode2 = *vmode; *vmode2 = *vmode;
if (!FillScanRates(vmode2)) if (!FillScanRates(vmode2))
Die("%s:%d: Bad video mode `%s'\n", Opt_modedb, line, vmode2->name); Die("%s:%d: Bad video mode `%s'\n", Opt_modedb, line, vmode2->name);

View file

@ -197,8 +197,7 @@ VID_MakeColormaps (int fullbrights, byte *pal)
vid.colormap8 = malloc (256*VID_GRADES * sizeof (byte)); vid.colormap8 = malloc (256*VID_GRADES * sizeof (byte));
vid.colormap16 = malloc (256*VID_GRADES * sizeof (short)); vid.colormap16 = malloc (256*VID_GRADES * sizeof (short));
vid.colormap32 = malloc (256*VID_GRADES * sizeof (int)); vid.colormap32 = malloc (256*VID_GRADES * sizeof (int));
if (!vid.colormap8 || !vid.colormap16 || !vid.colormap32) SYS_CHECKMEM (vid.colormap8 && vid.colormap16 && vid.colormap32);
Sys_Error ("VID_MakeColormaps: Memory Allocation Failure");
VID_MakeColormap8(vid.colormap8, pal); VID_MakeColormap8(vid.colormap8, pal);
VID_MakeColormap16(vid.colormap16, pal); VID_MakeColormap16(vid.colormap16, pal);
VID_MakeColormap32(vid.colormap32, pal); VID_MakeColormap32(vid.colormap32, pal);

View file

@ -266,8 +266,7 @@ VID_InitModes (void)
/* Get complete information on all modes */ /* Get complete information on all modes */
num_modes = vga_lastmodenumber () + 1; num_modes = vga_lastmodenumber () + 1;
modes = malloc (num_modes * sizeof (vga_modeinfo)); modes = malloc (num_modes * sizeof (vga_modeinfo));
if (!modes) SYS_CHECKMEM (modes);
Sys_Error ("VID_InitModes: Memory Allocation Failure");
for (i = 0; i < num_modes; i++) { for (i = 0; i < num_modes; i++) {
if (vga_hasmode (i)) { if (vga_hasmode (i)) {
memcpy (&modes[i], vga_getmodeinfo (i), sizeof (vga_modeinfo)); memcpy (&modes[i], vga_getmodeinfo (i), sizeof (vga_modeinfo));

View file

@ -301,8 +301,7 @@ ResetFrameBuffer (void)
pwidth = 4; pwidth = 4;
mem = ((vid.width * pwidth + 7) & ~7) * vid.height; mem = ((vid.width * pwidth + 7) & ~7) * vid.height;
buf = malloc (mem); buf = malloc (mem);
if (!buf) SYS_CHECKMEM (buf);
Sys_Error ("ResetFrameBuffer: Memory Allocation Failure");
// allocate new screen buffer // allocate new screen buffer
x_framebuffer[0] = XCreateImage (x_disp, x_vis, x_visinfo->depth, x_framebuffer[0] = XCreateImage (x_disp, x_vis, x_visinfo->depth,

View file

@ -743,16 +743,14 @@ Host_InitVCR (quakeparms_t *parms)
Qread (vcrFile, &com_argc, sizeof (int)); Qread (vcrFile, &com_argc, sizeof (int));
com_argv = malloc (com_argc * sizeof (char *)); com_argv = malloc (com_argc * sizeof (char *));
if (!com_argv) SYS_CHECKMEM (com_argv);
Sys_Error ("Host_InitVCR: Memory Allocation Failure");
com_argv[0] = parms->argv[0]; com_argv[0] = parms->argv[0];
for (i = 0; i < com_argc; i++) { for (i = 0; i < com_argc; i++) {
Qread (vcrFile, &len, sizeof (int)); Qread (vcrFile, &len, sizeof (int));
p = malloc (len); p = malloc (len);
if (!p) SYS_CHECKMEM (p);
Sys_Error ("Host_InitVCR: Memory Allocation Failure");
Qread (vcrFile, p, len); Qread (vcrFile, p, len);
com_argv[i + 1] = p; com_argv[i + 1] = p;
} }

View file

@ -97,8 +97,7 @@ locs_add (const vec3_t location, const char *name)
num = locations_count - 1; num = locations_count - 1;
locations[num] = malloc (sizeof (location_t)); locations[num] = malloc (sizeof (location_t));
if (!locations[num]) SYS_CHECKMEM (locations[num]);
Sys_Error ("locs_add: Memory Allocation Failure");
locations[num]->loc[0] = location[0]; locations[num]->loc[0] = location[0];
locations[num]->loc[1] = location[1]; locations[num]->loc[1] = location[1];

View file

@ -128,8 +128,7 @@ SL_Add (server_entry_t *start, char *ip, char *desc)
start->next = 0; start->next = 0;
start->server = malloc (strlen (ip) + 1); start->server = malloc (strlen (ip) + 1);
start->desc = malloc (strlen (desc ? desc : ip) + 1); start->desc = malloc (strlen (desc ? desc : ip) + 1);
if (!start->server || !start->desc) SYS_CHECKMEM (start->server && start->desc);
Sys_Error ("SL_Add: Memory Allocation Failure");
strcpy (start->server, ip); strcpy (start->server, ip);
strcpy (start->desc, desc ? desc : ip); strcpy (start->desc, desc ? desc : ip);
start->status = NULL; start->status = NULL;
@ -145,8 +144,7 @@ SL_Add (server_entry_t *start, char *ip, char *desc)
p->next->prev = p; p->next->prev = p;
p->next->server = malloc (strlen (ip) + 1); p->next->server = malloc (strlen (ip) + 1);
p->next->desc = malloc (strlen (desc ? desc : ip) + 1); p->next->desc = malloc (strlen (desc ? desc : ip) + 1);
if (!p->next->server || !p->next->desc) SYS_CHECKMEM (p->next->server && p->next->desc);
Sys_Error ("SL_Add: Memory Allocation Failure");
strcpy (p->next->server, ip); strcpy (p->next->server, ip);
strcpy (p->next->desc, desc ? desc : ip); strcpy (p->next->desc, desc ? desc : ip);
@ -190,8 +188,7 @@ SL_InsB (server_entry_t *start, server_entry_t *place, char *ip, char *desc)
new->server = malloc (strlen (ip) + 1); new->server = malloc (strlen (ip) + 1);
new->desc = malloc (strlen (desc) + 1); new->desc = malloc (strlen (desc) + 1);
if (!new->server || !new->desc) SYS_CHECKMEM (new->server && new->desc);
Sys_Error ("SL_InsB: Memory Allocation Failure");
strcpy (new->server, ip); strcpy (new->server, ip);
strcpy (new->desc, desc); strcpy (new->desc, desc);
other = place->prev; other = place->prev;
@ -636,8 +633,7 @@ SL_LoadF (VFile *f, server_entry_t *start)
if ((st = gettokstart (line, 1, ' ')) != NULL) { if ((st = gettokstart (line, 1, ' ')) != NULL) {
len = gettoklen (line, 1, ' '); len = gettoklen (line, 1, ' ');
addr = malloc (len + 1); addr = malloc (len + 1);
if (!addr) SYS_CHECKMEM (addr);
Sys_Error ("SL_LoadF: Memory Allocation Failure");
strncpy (addr, &line[0], len); strncpy (addr, &line[0], len);
addr[len] = '\0'; addr[len] = '\0';
if ((st = gettokstart (line, 2, ' '))) { if ((st = gettokstart (line, 2, ' '))) {

View file

@ -98,8 +98,7 @@ locs_add (const vec3_t location, const char *name)
num = locations_count - 1; num = locations_count - 1;
locations[num] = malloc (sizeof (location_t)); locations[num] = malloc (sizeof (location_t));
if (!locations[num]) SYS_CHECKMEM (locations[num]);
Sys_Error ("locs_add: Memory Allocation Failure");
locations[num]->loc[0] = location[0]; locations[num]->loc[0] = location[0];
locations[num]->loc[1] = location[1]; locations[num]->loc[1] = location[1];

View file

@ -224,8 +224,7 @@ LoadFile (char *filename, void **bufferptr)
f = SafeOpenRead (filename); f = SafeOpenRead (filename);
length = FileLength (f); length = FileLength (f);
buffer = malloc (length + 1); buffer = malloc (length + 1);
if (!buffer) SYS_CHECKMEM (buffer);
Sys_Error ("LoadFile: Memory Allocation Failure");
((char *) buffer)[length] = 0; ((char *) buffer)[length] = 0;
SafeRead (f, buffer, length); SafeRead (f, buffer, length);
fclose (f); fclose (f);

View file

@ -348,8 +348,7 @@ new_label_name (void)
int len = 1 + strlen (fname) + 1 + num_digits (lnum) + 1; int len = 1 + strlen (fname) + 1 + num_digits (lnum) + 1;
char *lname = malloc (len); char *lname = malloc (len);
if (!lname) SYS_CHECKMEM (lname);
Sys_Error ("new_label_expr: Memory Allocation Failure");
snprintf (lname, len, "$%s_%d", fname, lnum); snprintf (lname, len, "$%s_%d", fname, lnum);
return lname; return lname;
} }
@ -601,8 +600,7 @@ do_op_string (int op, expr_t *e1, expr_t *e2)
case '+': case '+':
len = strlen (s1) + strlen (s2) + 1; len = strlen (s1) + strlen (s2) + 1;
buf = malloc (len); buf = malloc (len);
if (!buf) SYS_CHECKMEM (buf);
Sys_Error ("do_op_string: Memory Allocation Failure");
strcpy (buf, s1); strcpy (buf, s1);
strcat (buf, s2); strcat (buf, s2);
e1->e.string_val = buf; e1->e.string_val = buf;
@ -1570,8 +1568,7 @@ function_expr (expr_t *e1, expr_t *e2)
ret->type = ex_def; ret->type = ex_def;
ret->e.def = memcpy (malloc (sizeof (def_t)), &def_ret, sizeof (def_t)); ret->e.def = memcpy (malloc (sizeof (def_t)), &def_ret, sizeof (def_t));
if (!ret->e.def) SYS_CHECKMEM (ret->e.def);
Sys_Error ("function_expr: Memory Allocation Failure");
ret->e.def->type = ftype->aux_type; ret->e.def->type = ftype->aux_type;
call->e.block.result = ret; call->e.block.result = ret;
} }

View file

@ -268,8 +268,7 @@ PR_FreeLocation (def_t *def)
if (!free_free_locs) { if (!free_free_locs) {
free_free_locs = malloc (256 * sizeof (locref_t)); free_free_locs = malloc (256 * sizeof (locref_t));
if (!free_free_locs) SYS_CHECKMEM (free_free_locs);
Sys_Error ("PR_FreeLocation: Memory Allocation Failure");
for (loc = free_free_locs; loc - free_free_locs < 255; loc++) for (loc = free_free_locs; loc - free_free_locs < 255; loc++)
loc->next = loc + 1; loc->next = loc + 1;
loc->next = 0; loc->next = 0;

View file

@ -376,8 +376,7 @@ void
add_frame_macro (char *token) add_frame_macro (char *token)
{ {
frame_t *frame = malloc (sizeof (frame_t)); frame_t *frame = malloc (sizeof (frame_t));
if (!frame) SYS_CHECKMEM (frame);
Sys_Error ("add_frame_macro: Memory Allocation Failure");
frame->name = strdup (token); frame->name = strdup (token);
frame->num = frame_number++; frame->num = frame_number++;
@ -403,8 +402,7 @@ make_string (char *token)
int quote; int quote;
s = str = malloc (strlen (token) + 1); s = str = malloc (strlen (token) + 1);
if (!str) SYS_CHECKMEM (str);
Sys_Error ("make_string: Memory Allocation Failure");
mask = 0x00; mask = 0x00;
boldnext = 0; boldnext = 0;

View file

@ -1274,8 +1274,7 @@ scan_scope (hashtab_t *tab, def_t *scope)
for (def = scope->scope_next; def; def = def->scope_next) { for (def = scope->scope_next; def; def = def->scope_next) {
if (def->name && !def->removed) { if (def->name && !def->removed) {
def_state_t *ds = malloc (sizeof (def_state_t)); def_state_t *ds = malloc (sizeof (def_state_t));
if (!ds) SYS_CHECKMEM (ds);
Sys_Error ("scan_scope: Memory Allocation Failure");
ds->def = def; ds->def = def;
ds->state = def->initialized; ds->state = def->initialized;
Hash_Add (tab, ds); Hash_Add (tab, ds);
@ -1305,8 +1304,7 @@ merge_local_inits (hashtab_t *dl_1, hashtab_t *dl_2)
(*ds)->def->initialized = (*ds)->state; (*ds)->def->initialized = (*ds)->state;
nds = malloc (sizeof (def_state_t)); nds = malloc (sizeof (def_state_t));
if (!nds) SYS_CHECKMEM (nds);
Sys_Error ("merge_local_inits: Memory Allocation Failure");
nds->def = (*ds)->def; nds->def = (*ds)->def;
nds->state = (*ds)->state && d->state; nds->state = (*ds)->state && d->state;
Hash_Add (tab, nds); Hash_Add (tab, nds);

View file

@ -74,8 +74,7 @@ case_label_expr (switch_block_t *switch_block, expr_t *value)
{ {
case_label_t *cl = malloc (sizeof (case_label_t)); case_label_t *cl = malloc (sizeof (case_label_t));
if (!cl) SYS_CHECKMEM (cl);
Sys_Error ("case_label_expr: Memory Allocation Failure");
if (value) if (value)
convert_name (value); convert_name (value);
@ -105,8 +104,7 @@ new_switch_block (void)
{ {
switch_block_t *switch_block = malloc (sizeof (switch_block_t)); switch_block_t *switch_block = malloc (sizeof (switch_block_t));
if (!switch_block) SYS_CHECKMEM (switch_block);
Sys_Error ("new_switch_block: Memory Allocation Failure");
switch_block->labels = Hash_NewTable (127, 0, 0, 0); switch_block->labels = Hash_NewTable (127, 0, 0, 0);
Hash_SetHashCompare (switch_block->labels, get_hash, compare); Hash_SetHashCompare (switch_block->labels, get_hash, compare);
switch_block->test = 0; switch_block->test = 0;

View file

@ -121,8 +121,7 @@ find_type (type_t *type)
// allocate a new one // allocate a new one
check = malloc (sizeof (*check)); check = malloc (sizeof (*check));
if (!check) SYS_CHECKMEM (check);
Sys_Error ("find_type: Memory Allocation Failure");
*check = *type; *check = *type;
chain_type (check); chain_type (check);

View file

@ -105,8 +105,7 @@ fix_missing_globals (progs_t *pr, def_t *globals)
} }
progs = malloc (pr->progs_size + strings_size); progs = malloc (pr->progs_size + strings_size);
if (!progs) SYS_CHECKMEM (progs);
Sys_Error ("fix_missing_globals: Memory Allocation Failure");
memcpy (progs, pr->progs, pr->progs_size); memcpy (progs, pr->progs, pr->progs_size);
offs = progs->ofs_strings + progs->numstrings; offs = progs->ofs_strings + progs->numstrings;

View file

@ -64,8 +64,7 @@ main ()
Cvar_Init_Hash (); Cvar_Init_Hash ();
Cmd_Init_Hash (); Cmd_Init_Hash ();
membase = malloc (memsize); membase = malloc (memsize);
if (!membase) SYS_CHECKMEM (membase);
Sys_Error ("Memory Allocation Failure");
Memory_Init (membase, memsize); Memory_Init (membase, memsize);
Cvar_Init (); Cvar_Init ();
Cbuf_Init (); Cbuf_Init ();