summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/cmd3.cc9
-rw-r--r--src/cmd4.cc10
-rw-r--r--src/externs.h34
-rw-r--r--src/files.cc4
-rw-r--r--src/generate.cc4
-rw-r--r--src/init2.cc68
-rw-r--r--src/maid-x11.c5
-rw-r--r--src/main-gtk2.c77
-rw-r--r--src/main-win.c44
-rw-r--r--src/main-x11.c11
-rw-r--r--src/main.c34
-rw-r--r--src/modules.cc28
-rw-r--r--src/quark.cc4
-rw-r--r--src/string_list.cc13
-rw-r--r--src/string_list.h2
-rw-r--r--src/util.cc6
-rw-r--r--src/variable.cc34
-rw-r--r--src/z-term.c136
-rw-r--r--src/z-virt.c139
-rw-r--r--src/z-virt.h141
20 files changed, 279 insertions, 524 deletions
diff --git a/src/cmd3.cc b/src/cmd3.cc
index f5ab201e..610b24fa 100644
--- a/src/cmd3.cc
+++ b/src/cmd3.cc
@@ -2072,16 +2072,19 @@ void cli_add(cptr active, cptr trigger, cptr descr)
}
if (*t == '\0') break;
}
- cli_ptr->comm = string_make(temp);
+ cli_ptr->comm = strdup(temp);
}
else
{
- cli_ptr->comm = string_make(trigger);
+ cli_ptr->comm = strdup(trigger);
}
/* First try copying everything across. */
cli_ptr->key = num;
- cli_ptr->descrip = string_make(descr);
+ cli_ptr->descrip = nullptr;
+ if (descr) {
+ cli_ptr->descrip = strdup(descr);
+ }
/* Take description for the previous record if appropriate. */
if ((cli_total > 0) && (old_ptr->key == cli_ptr->key) && (cli_ptr->descrip == 0))
diff --git a/src/cmd4.cc b/src/cmd4.cc
index 7a6fbe7c..c71c28f3 100644
--- a/src/cmd4.cc
+++ b/src/cmd4.cc
@@ -2004,11 +2004,9 @@ void do_cmd_macros(void)
/* Convert to ascii */
text_to_ascii(macro__buf, tmp);
- /* Free old keymap */
- string_free(keymap_act[mode][(byte)(buf[0])]);
-
/* Make new keymap */
- keymap_act[mode][(byte)(buf[0])] = string_make(macro__buf);
+ free(keymap_act[mode][(byte)(buf[0])]);
+ keymap_act[mode][(byte)(buf[0])] = strdup(macro__buf);
/* Prompt */
msg_print("Added a keymap.");
@@ -2027,10 +2025,8 @@ void do_cmd_macros(void)
/* Get a keymap trigger */
do_cmd_macro_aux_keymap(buf);
- /* Free old keymap */
- string_free(keymap_act[mode][(byte)(buf[0])]);
-
/* Make new keymap */
+ free(keymap_act[mode][(byte)(buf[0])]);
keymap_act[mode][(byte)(buf[0])] = NULL;
/* Prompt */
diff --git a/src/externs.h b/src/externs.h
index ea4b68b7..940f2eea 100644
--- a/src/externs.h
+++ b/src/externs.h
@@ -300,8 +300,8 @@ extern s16b temp_n;
extern byte temp_y[TEMP_MAX];
extern byte temp_x[TEMP_MAX];
extern s16b macro__num;
-extern cptr *macro__pat;
-extern cptr *macro__act;
+extern char **macro__pat;
+extern char **macro__act;
extern bool_ *macro__cmd;
extern char *macro__buf;
extern u32b option_flag[8];
@@ -328,7 +328,7 @@ extern byte misc_to_attr[256];
extern char misc_to_char[256];
extern byte tval_to_attr[128];
extern char tval_to_char[128];
-extern cptr keymap_act[KEYMAP_MODES][256];
+extern char *keymap_act[KEYMAP_MODES][256];
extern player_type p_body;
extern player_type *p_ptr;
extern player_sex *sp_ptr;
@@ -427,20 +427,20 @@ extern char *set_text;
extern cptr ANGBAND_SYS;
extern cptr ANGBAND_KEYBOARD;
extern cptr ANGBAND_GRAF;
-extern cptr ANGBAND_DIR;
-extern cptr ANGBAND_DIR_CORE;
-extern cptr ANGBAND_DIR_DNGN;
-extern cptr ANGBAND_DIR_DATA;
-extern cptr ANGBAND_DIR_EDIT;
-extern cptr ANGBAND_DIR_FILE;
-extern cptr ANGBAND_DIR_HELP;
-extern cptr ANGBAND_DIR_INFO;
-extern cptr ANGBAND_DIR_MODULES;
-extern cptr ANGBAND_DIR_NOTE;
-extern cptr ANGBAND_DIR_SAVE;
-extern cptr ANGBAND_DIR_PREF;
-extern cptr ANGBAND_DIR_USER;
-extern cptr ANGBAND_DIR_XTRA;
+extern char *ANGBAND_DIR;
+extern char *ANGBAND_DIR_MODULES;
+extern char *ANGBAND_DIR_SAVE;
+extern char *ANGBAND_DIR_CORE;
+extern char *ANGBAND_DIR_DNGN;
+extern char *ANGBAND_DIR_DATA;
+extern char *ANGBAND_DIR_EDIT;
+extern char *ANGBAND_DIR_FILE;
+extern char *ANGBAND_DIR_HELP;
+extern char *ANGBAND_DIR_INFO;
+extern char *ANGBAND_DIR_NOTE;
+extern char *ANGBAND_DIR_PREF;
+extern char *ANGBAND_DIR_USER;
+extern char *ANGBAND_DIR_XTRA;
extern char pref_tmp_value[8];
extern bool_ item_tester_full;
extern byte item_tester_tval;
diff --git a/src/files.cc b/src/files.cc
index 26687148..04066bbd 100644
--- a/src/files.cc
+++ b/src/files.cc
@@ -432,9 +432,9 @@ errr process_pref_file_aux(char *buf)
if (!tmp[0] || tmp[1]) return (1);
i = (byte)(tmp[0]);
- string_free(keymap_act[mode][i]);
+ free(keymap_act[mode][i]);
- keymap_act[mode][i] = string_make(macro__buf);
+ keymap_act[mode][i] = strdup(macro__buf);
return (0);
}
diff --git a/src/generate.cc b/src/generate.cc
index fb8cb66e..4b8e1f3f 100644
--- a/src/generate.cc
+++ b/src/generate.cc
@@ -295,9 +295,11 @@ static level_generator_type *level_generators = NULL;
*/
void add_level_generator(cptr name, bool_ (*generator)(), bool_ stairs, bool_ monsters, bool_ objects, bool_ miscs)
{
+ assert(name != nullptr);
+
level_generator_type *g = new level_generator_type;
- g->name = string_make(name);
+ g->name = strdup(name);
g->generator = generator;
g->default_stairs = stairs;
diff --git a/src/init2.cc b/src/init2.cc
index 4d458561..4a3fc4d6 100644
--- a/src/init2.cc
+++ b/src/init2.cc
@@ -72,25 +72,27 @@ void init_file_paths(char *path)
char *tail;
int pathlen;
+ assert(path != nullptr);
+
/*** Free everything ***/
/* Free the main path */
- string_free(ANGBAND_DIR);
+ free(ANGBAND_DIR);
/* Free the sub-paths */
- string_free(ANGBAND_DIR_CORE);
- string_free(ANGBAND_DIR_DNGN);
- string_free(ANGBAND_DIR_DATA);
- string_free(ANGBAND_DIR_EDIT);
- string_free(ANGBAND_DIR_FILE);
- string_free(ANGBAND_DIR_HELP);
- string_free(ANGBAND_DIR_INFO);
- string_free(ANGBAND_DIR_MODULES);
- string_free(ANGBAND_DIR_NOTE);
- string_free(ANGBAND_DIR_SAVE);
- string_free(ANGBAND_DIR_PREF);
- string_free(ANGBAND_DIR_USER);
- string_free(ANGBAND_DIR_XTRA);
+ free(ANGBAND_DIR_CORE);
+ free(ANGBAND_DIR_DNGN);
+ free(ANGBAND_DIR_DATA);
+ free(ANGBAND_DIR_EDIT);
+ free(ANGBAND_DIR_FILE);
+ free(ANGBAND_DIR_HELP);
+ free(ANGBAND_DIR_INFO);
+ free(ANGBAND_DIR_MODULES);
+ free(ANGBAND_DIR_NOTE);
+ free(ANGBAND_DIR_SAVE);
+ free(ANGBAND_DIR_PREF);
+ free(ANGBAND_DIR_USER);
+ free(ANGBAND_DIR_XTRA);
/*** Prepare the "path" ***/
@@ -105,17 +107,17 @@ void init_file_paths(char *path)
if (strcmp(path + pathlen - seplen, PATH_SEP) == 0)
{
path[pathlen - seplen] = '\0';
- ANGBAND_DIR = string_make(path);
+ ANGBAND_DIR = strdup(path);
path[pathlen - seplen] = *PATH_SEP;
}
else
{
- ANGBAND_DIR = string_make(path);
+ ANGBAND_DIR = strdup(path);
}
}
else
{
- ANGBAND_DIR = string_make(path);
+ ANGBAND_DIR = strdup(path);
}
/* Prepare to append to the Base Path */
@@ -127,39 +129,39 @@ void init_file_paths(char *path)
/* Build a path name */
strcpy(tail, "core");
- ANGBAND_DIR_CORE = string_make(path);
+ ANGBAND_DIR_CORE = strdup(path);
/* Build a path name */
strcpy(tail, "dngn");
- ANGBAND_DIR_DNGN = string_make(path);
+ ANGBAND_DIR_DNGN = strdup(path);
/* Build a path name */
strcpy(tail, "data");
- ANGBAND_DIR_DATA = string_make(path);
+ ANGBAND_DIR_DATA = strdup(path);
/* Build a path name */
strcpy(tail, "edit");
- ANGBAND_DIR_EDIT = string_make(path);
+ ANGBAND_DIR_EDIT = strdup(path);
/* Build a path name */
strcpy(tail, "file");
- ANGBAND_DIR_FILE = string_make(path);
+ ANGBAND_DIR_FILE = strdup(path);
/* Build a path name */
strcpy(tail, "help");
- ANGBAND_DIR_HELP = string_make(path);
+ ANGBAND_DIR_HELP = strdup(path);
/* Build a path name */
strcpy(tail, "info");
- ANGBAND_DIR_INFO = string_make(path);
+ ANGBAND_DIR_INFO = strdup(path);
/* Build a path name */
strcpy(tail, "mods");
- ANGBAND_DIR_MODULES = string_make(path);
+ ANGBAND_DIR_MODULES = strdup(path);
/* Build a path name */
strcpy(tail, "pref");
- ANGBAND_DIR_PREF = string_make(path);
+ ANGBAND_DIR_PREF = strdup(path);
/* synchronize with module_reset_dir */
{
@@ -168,17 +170,17 @@ void init_file_paths(char *path)
/* Get an absolute path from the file name */
path_parse(user_path, 1024, PRIVATE_USER_PATH);
strcat(user_path, USER_PATH_VERSION);
- ANGBAND_DIR_USER = string_make(user_path);
- ANGBAND_DIR_NOTE = string_make(user_path);
+ ANGBAND_DIR_USER = strdup(user_path);
+ ANGBAND_DIR_NOTE = strdup(user_path);
/* Savefiles are in user directory */
strcat(user_path, "/save");
- ANGBAND_DIR_SAVE = string_make(user_path);
+ ANGBAND_DIR_SAVE = strdup(user_path);
}
/* Build a path name */
strcpy(tail, "xtra");
- ANGBAND_DIR_XTRA = string_make(path);
+ ANGBAND_DIR_XTRA = strdup(path);
}
@@ -1617,11 +1619,9 @@ errr init_v_info(void)
*/
static void init_basic()
{
- int i;
-
/* Macro variables */
- macro__pat = make_array<cptr>(MACRO_MAX);
- macro__act = make_array<cptr>(MACRO_MAX);
+ macro__pat = make_array<char *>(MACRO_MAX);
+ macro__act = make_array<char *>(MACRO_MAX);
macro__cmd = make_array<bool_>(MACRO_MAX);
/* Macro action buffer */
diff --git a/src/maid-x11.c b/src/maid-x11.c
index 86df2119..0e1e3320 100644
--- a/src/maid-x11.c
+++ b/src/maid-x11.c
@@ -264,7 +264,7 @@ static XImage *ReadBMP(Display *dpy, char *Name)
total = infoheader.biWidth * infoheader.biHeight * i;
/* Allocate image memory */
- C_MAKE(Data, total, char);
+ Data = safe_calloc(total, sizeof(char));
Res = XCreateImage(dpy, visual, depth, ZPixmap, 0 /*offset*/,
Data, infoheader.biWidth, infoheader.biHeight,
@@ -273,7 +273,8 @@ static XImage *ReadBMP(Display *dpy, char *Name)
/* Failure */
if (Res == NULL)
{
- C_KILL(Data, total, char);
+ free(Data);
+ Data = NULL;
fclose(f);
return (NULL);
}
diff --git a/src/main-gtk2.c b/src/main-gtk2.c
index 07b86430..fbcfe79b 100644
--- a/src/main-gtk2.c
+++ b/src/main-gtk2.c
@@ -35,18 +35,6 @@
/*
* Activate variant-specific features
- *
- * Angband 2.9.3 and close variants don't require any.
- *
- * Angband 2.9.4 alpha and later removed the short-lived
- * can_save flag, so please #define can_save TRUE, or remove
- * all the references to it. They also changed long-lived
- * z-virt macro names. Find C_FREE/C_KILL and replace them
- * with FREE/KILL, which takes one pointer parameter.
- *
- * ZAngband has its own enhanced main-gtk.c as mentioned above, and
- * you *should* use it :-)
- *
*/
#define USE_DOUBLE_TILES /* Mogami's bigtile patch */
@@ -146,7 +134,7 @@ struct term_data
#endif /* USE_GRAPHICS */
- cptr name;
+ char *name;
};
@@ -254,32 +242,6 @@ static bool_ use_transparency = TRUE;
-/**** Low level routines - memory allocation ****/
-
-/*
- * Hook to "release" memory
- */
-static vptr hook_rnfree(vptr v, huge size)
-{
- /* Dispose */
- g_free(v);
-
- /* Success */
- return (NULL);
-}
-
-
-/*
- * Hook to "allocate" memory
- */
-static vptr hook_ralloc(huge size)
-{
- /* Make a new pointer */
- return (g_malloc(size));
-}
-
-
-
/**** Low level routines - colours and graphics ****/
@@ -370,7 +332,7 @@ static int tile_cols;
/*
* Directory name(s)
*/
-static cptr ANGBAND_DIR_XTRA_GRAF;
+static char *ANGBAND_DIR_XTRA_GRAF;
/*
@@ -1088,7 +1050,7 @@ static GdkRGBImage *resize_tiles_fast(
*/
/* Set up x offset table */
- C_MAKE(xoffsets, new_wid, int);
+ xoffsets = safe_calloc(new_wid, sizeof(int));
/* Initialize line parameters */
add = old_wid / new_wid;
@@ -1147,7 +1109,7 @@ static GdkRGBImage *resize_tiles_fast(
}
/* Free offset table */
- C_FREE(xoffsets, new_wid, int);
+ free(xoffsets);
return (new_image);
}
@@ -1346,7 +1308,7 @@ static GdkRGBImage *load_xpm(cptr filename)
}
/* Allocate palette */
- C_MAKE(pal, colours, pal_type);
+ pal = safe_calloc(colours, sizeof(pal_type));
/* Initialise hash table */
for (i = 0; i < HASH_SIZE; i++) head[i] = NULL;
@@ -1447,7 +1409,7 @@ static GdkRGBImage *load_xpm(cptr filename)
buflen = width * chars + 1;
/* Allocate line buffer */
- C_MAKE(lin, buflen, char);
+ lin = safe_calloc(buflen, sizeof(char));
/* For each row */
for (i = 0; i < height; i++)
@@ -1512,10 +1474,10 @@ static GdkRGBImage *load_xpm(cptr filename)
my_fclose(f);
/* Free line buffer */
- C_FREE(lin, buflen, char);
+ free(lin);
/* Free palette */
- C_FREE(pal, colours, pal_type);
+ free(pal);
/* Return result */
return (img);
@@ -1529,10 +1491,10 @@ oops:
if (img) gdk_rgb_image_destroy(img);
/* Free line buffer */
- if (lin) C_FREE(lin, buflen, char);
+ if (lin) free(lin);
/* Free palette */
- if (pal) C_FREE(pal, colours, pal_type);
+ if (pal) free(pal);
/* Failure */
return (NULL);
@@ -2139,7 +2101,7 @@ static void Term_nuke_gtk(term *t)
/* Free name */
- if (td->name) string_free(td->name);
+ if (td->name) free(td->name);
/* Forget it */
td->name = NULL;
@@ -3620,7 +3582,8 @@ static errr term_data_init(term_data *td, int i)
term_init(t, td->cols, td->rows, 1024);
/* Store the name of the term */
- td->name = string_make(angband_term_name[i]);
+ assert(angband_term_name[i] != NULL);
+ td->name = strdup(angband_term_name[i]);
/* Instance names should start with a lowercase letter XXX */
for (p = (char *)td->name; *p; p++) *p = tolower(*p);
@@ -3808,13 +3771,13 @@ static void setup_menu_paths(void)
strnfmt(buf, 64, "/Terms/%s", angband_term_name[i]);
/* XXX XXX Store it in the menu definition */
- term_entry[i].path = (gchar*)string_make(buf);
+ term_entry[i].path = (gchar*) strdup(buf);
/* XXX XXX Build the real path name to the entry */
strnfmt(buf, 64, "/Options/Font/%s", angband_term_name[i]);
/* XXX XXX Store it in the menu definition */
- font_entry[i].path = (gchar*)string_make(buf);
+ font_entry[i].path = (gchar*) strdup(buf);
}
}
@@ -3860,10 +3823,10 @@ static void free_menu_paths(void)
for (i = 0; i < MAX_TERM_DATA; i++)
{
/* XXX XXX Free Term menu path */
- if (term_entry[i].path) string_free((cptr)term_entry[i].path);
+ if (term_entry[i].path) free(term_entry[i].path);
/* XXX XXX Free Font menu path */
- if (font_entry[i].path) string_free((cptr)font_entry[i].path);
+ if (font_entry[i].path) free(font_entry[i].path);
}
}
@@ -4348,7 +4311,7 @@ static void hook_quit(cptr str)
# ifdef USE_GRAPHICS
/* Free pathname string */
- if (ANGBAND_DIR_XTRA_GRAF) string_free(ANGBAND_DIR_XTRA_GRAF);
+ if (ANGBAND_DIR_XTRA_GRAF) free(ANGBAND_DIR_XTRA_GRAF);
# endif /* USE_GRAPHICS */
@@ -4369,8 +4332,6 @@ errr init_gtk2(int argc, char **argv)
gtk_init(&argc, &argv);
/* Activate hooks - Use gtk/glib interface throughout */
- ralloc_aux = hook_ralloc;
- rnfree_aux = hook_rnfree;
quit_aux = hook_quit;
core_aux = hook_quit;
@@ -4451,7 +4412,7 @@ errr init_gtk2(int argc, char **argv)
path_build(path, 1024, ANGBAND_DIR_XTRA, "graf");
/* Allocate the path */
- ANGBAND_DIR_XTRA_GRAF = string_make(path);
+ ANGBAND_DIR_XTRA_GRAF = strdup(path);
}
#endif /* USE_GRAPHICS */
diff --git a/src/main-win.c b/src/main-win.c
index 776c3e15..454f3866 100644
--- a/src/main-win.c
+++ b/src/main-win.c
@@ -75,7 +75,6 @@
#include "angband.h"
-
#ifdef WINDOWS
@@ -936,7 +935,7 @@ static void load_prefs_aux(term_data *td, cptr sec_name)
td->bizarre = (GetPrivateProfileInt(sec_name, "Bizarre", td->bizarre, ini_file) != 0);
/* Analyze font, save desired font name */
- td->font_want = string_make(analyze_font(tmp, &wid, &hgt));
+ td->font_want = strdup(analyze_font(tmp, &wid, &hgt));
/* Tile size */
td->tile_wid = GetPrivateProfileInt(sec_name, "TileWid", wid, ini_file);
@@ -1030,7 +1029,8 @@ static int new_palette(void)
if (hBmPal)
{
lppeSize = 256 * sizeof(PALETTEENTRY);
- lppe = (LPPALETTEENTRY)ralloc(lppeSize);
+ lppe = (LPPALETTEENTRY) safe_calloc(1, lppeSize);
+
nEntries = GetPaletteEntries(hBmPal, 0, 255, lppe);
if ((nEntries == 0) || (nEntries > 220))
{
@@ -1038,7 +1038,8 @@ static int new_palette(void)
plog_fmt("Unusable bitmap palette (%d entries)", nEntries);
/* Cleanup */
- rnfree(lppe, lppeSize);
+ free(lppe);
+ lppe = NULL;
/* Fail */
return (FALSE);
@@ -1051,7 +1052,7 @@ static int new_palette(void)
pLogPalSize = sizeof(LOGPALETTE) + (nEntries + 16) * sizeof(PALETTEENTRY);
/* Allocate palette */
- pLogPal = (LPLOGPALETTE)ralloc(pLogPalSize);
+ pLogPal = (LPLOGPALETTE) safe_calloc(1, pLogPalSize);
/* Version */
pLogPal->palVersion = 0x300;
@@ -1083,14 +1084,19 @@ static int new_palette(void)
}
/* Free something */
- if (lppe) rnfree(lppe, lppeSize);
+ if (lppe)
+ {
+ free(lppe);
+ lppe = NULL;
+ }
/* Create a new palette, or fail */
hNewPal = CreatePalette(pLogPal);
if (!hNewPal) quit("Cannot create palette!");
/* Free the palette */
- rnfree(pLogPal, pLogPalSize);
+ free(pLogPal);
+ pLogPal = NULL;
/* Main window */
td = &data[0];
@@ -1225,7 +1231,7 @@ static bool_ init_sound()
path_build(buf, 1024, ANGBAND_DIR_XTRA_SOUND, wav);
/* Save the sound filename, if it exists */
- if (check_file(buf)) sound_file[i] = string_make(buf);
+ if (check_file(buf)) sound_file[i] = strdup(buf);
}
/* Sound available */
@@ -1301,9 +1307,7 @@ static errr term_force_font(term_data *td, cptr path)
if (!used) RemoveFontResource(td->font_file);
/* Free the old name */
- string_free(td->font_file);
-
- /* Forget it */
+ free(td->font_file);
td->font_file = NULL;
}
@@ -1328,7 +1332,7 @@ static errr term_force_font(term_data *td, cptr path)
if (!AddFontResource(buf)) return (1);
/* Save new font name */
- td->font_file = string_make(base);
+ td->font_file = strdup(base);
/* Remove the "suffix" */
base[strlen(base) - 4] = '\0';
@@ -3798,7 +3802,11 @@ static void hook_quit(cptr str)
for (i = MAX_TERM_DATA - 1; i >= 0; --i)
{
term_force_font(&data[i], NULL);
- if (data[i].font_want) string_free(data[i].font_want);
+ if (data[i].font_want)
+ {
+ free(data[i].font_want);
+ data[i].font_want = NULL;
+ }
if (data[i].w) DestroyWindow(data[i].w);
data[i].w = 0;
}
@@ -3837,7 +3845,7 @@ static void init_stuff(void)
strcpy(path + strlen(path) - 4, ".INI");
/* Save the the name of the ini-file */
- ini_file = string_make(path);
+ ini_file = strdup(path);
/* Validate the ini-file */
validate_file(ini_file);
@@ -3870,7 +3878,7 @@ static void init_stuff(void)
path_build(path, 1024, ANGBAND_DIR_XTRA, "font");
/* Allocate the path */
- ANGBAND_DIR_XTRA_FONT = string_make(path);
+ ANGBAND_DIR_XTRA_FONT = strdup(path);
/*** Validate the paths to ensure we have a working install ***/
@@ -3901,7 +3909,7 @@ static void init_stuff(void)
path_build(path, 1024, ANGBAND_DIR_XTRA, "graf");
/* Allocate the path */
- ANGBAND_DIR_XTRA_GRAF = string_make(path);
+ ANGBAND_DIR_XTRA_GRAF = strdup(path);
/* Validate the "graf" directory */
validate_dir(ANGBAND_DIR_XTRA_GRAF);
@@ -3921,7 +3929,7 @@ static void init_stuff(void)
path_build(path, 1024, ANGBAND_DIR_XTRA, "sound");
/* Allocate the path */
- ANGBAND_DIR_XTRA_SOUND = string_make(path);
+ ANGBAND_DIR_XTRA_SOUND = strdup(path);
/* Validate the "sound" directory */
validate_dir(ANGBAND_DIR_XTRA_SOUND);
@@ -3933,7 +3941,7 @@ static void init_stuff(void)
path_build(path, 1024, ANGBAND_DIR_XTRA, "help");
/* Allocate the path */
- ANGBAND_DIR_XTRA_HELP = string_make(path);
+ ANGBAND_DIR_XTRA_HELP = strdup(path);
/* Validate the "help" directory */
/* validate_dir(ANGBAND_DIR_XTRA_HELP); */
diff --git a/src/main-x11.c b/src/main-x11.c
index d15415f2..e20451a7 100644
--- a/src/main-x11.c
+++ b/src/main-x11.c
@@ -1059,7 +1059,7 @@ static errr Infofnt_init_data(cptr name)
}
/* Save a copy of the font name */
- Infofnt->name = string_make(name);
+ Infofnt->name = strdup(name);
/* Mark it as nukable */
Infofnt->nuke = 1;
@@ -2494,7 +2494,7 @@ static errr term_data_init(term_data *td, int i)
/* Prepare the standard font */
- MAKE(td->fnt, infofnt);
+ td->fnt = safe_calloc(1, sizeof(struct infofnt));
Infofnt_set(td->fnt);
Infofnt_init_data(font);
@@ -2506,7 +2506,7 @@ static errr term_data_init(term_data *td, int i)
hgt = rows * td->fnt->hgt + (oy + oy);
/* Create a top-window */
- MAKE(td->win, infowin);
+ td->win = safe_calloc(1, sizeof(struct infowin));
Infowin_set(td->win);
Infowin_init_top(x, y, wid, hgt, 0,
Metadpy->fg, Metadpy->bg);
@@ -2684,7 +2684,7 @@ errr init_x11(int argc, char *argv[])
/* Prepare cursor color */
- MAKE(xor, infoclr);
+ xor = safe_calloc(1, sizeof(struct infoclr));
Infoclr_set(xor);
Infoclr_init_ppn(Metadpy->fg, Metadpy->bg, "xor", 0);
@@ -2694,8 +2694,7 @@ errr init_x11(int argc, char *argv[])
{
Pixell pixel;
- MAKE(clr[i], infoclr);
-
+ clr[i] = safe_calloc(1, sizeof(struct infoclr));
Infoclr_set(clr[i]);
/* Acquire Angband colors */
diff --git a/src/main.c b/src/main.c
index 0e6a5907..394d08d1 100644
--- a/src/main.c
+++ b/src/main.c
@@ -130,57 +130,57 @@ static void change_path(cptr info)
{
case 'f':
{
- string_free(ANGBAND_DIR_FILE);
- ANGBAND_DIR_FILE = string_make(s + 1);
+ free(ANGBAND_DIR_FILE);
+ ANGBAND_DIR_FILE = strdup(s + 1);
break;
}
case 'h':
{
- string_free(ANGBAND_DIR_HELP);
- ANGBAND_DIR_HELP = string_make(s + 1);
+ free(ANGBAND_DIR_HELP);
+ ANGBAND_DIR_HELP = strdup(s + 1);
break;
}
case 'i':
{
- string_free(ANGBAND_DIR_INFO);
- ANGBAND_DIR_INFO = string_make(s + 1);
+ free(ANGBAND_DIR_INFO);
+ ANGBAND_DIR_INFO = strdup(s + 1);
break;
}
case 'u':
{
- string_free(ANGBAND_DIR_USER);
- ANGBAND_DIR_USER = string_make(s + 1);
+ free(ANGBAND_DIR_USER);
+ ANGBAND_DIR_USER = strdup(s + 1);
break;
}
case 'x':
{
- string_free(ANGBAND_DIR_XTRA);
- ANGBAND_DIR_XTRA = string_make(s + 1);
+ free(ANGBAND_DIR_XTRA);
+ ANGBAND_DIR_XTRA = strdup(s + 1);
break;
}
case 'd':
{
- string_free(ANGBAND_DIR_DATA);
- ANGBAND_DIR_DATA = string_make(s + 1);
+ free(ANGBAND_DIR_DATA);
+ ANGBAND_DIR_DATA = strdup(s + 1);
break;
}
case 'e':
{
- string_free(ANGBAND_DIR_EDIT);
- ANGBAND_DIR_EDIT = string_make(s + 1);
+ free(ANGBAND_DIR_EDIT);
+ ANGBAND_DIR_EDIT = strdup(s + 1);
break;
}
case 's':
{
- string_free(ANGBAND_DIR_SAVE);
- ANGBAND_DIR_SAVE = string_make(s + 1);
+ free(ANGBAND_DIR_SAVE);
+ ANGBAND_DIR_SAVE = strdup(s + 1);
break;
}
@@ -328,7 +328,7 @@ int main(int argc, char *argv[])
case 'M':
{
if (!argv[i][2]) goto usage;
- force_module = string_make(&argv[i][2]);
+ force_module = &argv[i][2];
break;
}
diff --git a/src/modules.cc b/src/modules.cc
index edfc2ee2..059e9200 100644
--- a/src/modules.cc
+++ b/src/modules.cc
@@ -9,15 +9,15 @@
#include "angband.h"
#include "hooks.h"
-static void module_reset_dir_aux(cptr *dir, cptr new_path)
+static void module_reset_dir_aux(char **dir, cptr new_path)
{
char buf[1024];
/* Build the new path */
strnfmt(buf, sizeof (buf), "%s%s%s", *dir, PATH_SEP, new_path);
- string_free(*dir);
- *dir = string_make(buf);
+ free(*dir);
+ *dir = strdup(buf);
/* Make it if needed */
if (!private_check_user_directory(*dir))
@@ -26,7 +26,7 @@ static void module_reset_dir_aux(cptr *dir, cptr new_path)
static void module_reset_dir(cptr dir, cptr new_path)
{
- cptr *d = 0;
+ char **d = 0;
char buf[1025];
if (!strcmp(dir, "core")) d = &ANGBAND_DIR_CORE;
@@ -40,18 +40,20 @@ static void module_reset_dir(cptr dir, cptr new_path)
if (!strcmp(dir, "xtra")) d = &ANGBAND_DIR_XTRA;
if (!strcmp(dir, "user")) d = &ANGBAND_DIR_USER;
if (!strcmp(dir, "note")) d = &ANGBAND_DIR_NOTE;
+
if (
!strcmp(dir, "user") ||
- !strcmp(dir, "note") ||
- !strcmp(dir, "cmov"))
+ !strcmp(dir, "note"))
{
char user_path[1024];
/* copied from init_file_paths */
path_parse(user_path, 1024, PRIVATE_USER_PATH);
strcat(user_path, USER_PATH_VERSION);
strnfmt(buf, 1024, "%s%s%s", user_path, PATH_SEP, new_path);
- string_free(*d);
- *d = string_make(buf);
+
+ free(*d);
+ *d = strdup(buf);
+
// Make it if needed */
if (!private_check_user_directory(*d))
{
@@ -67,8 +69,8 @@ static void module_reset_dir(cptr dir, cptr new_path)
/* Build the new path */
strnfmt(buf, 1024, "%s%s%s%s%s", ANGBAND_DIR_MODULES, PATH_SEP, new_path, PATH_SEP, dir);
- string_free(*d);
- *d = string_make(buf);
+ free(*d);
+ *d = strdup(buf);
}
}
@@ -207,7 +209,8 @@ bool_ select_module()
{
/* Process the module */
init_module(&modules[sel]);
- game_module = string_make(modules[sel].meta.name);
+
+ game_module = modules[sel].meta.name;
activate_module(sel);
@@ -277,7 +280,8 @@ bool_ select_module()
/* Process the module */
init_module(&modules[x]);
- game_module = string_make(modules[x].meta.name);
+
+ game_module = modules[x].meta.name;
activate_module(x);
diff --git a/src/quark.cc b/src/quark.cc
index 1c8619fa..8a78b6f8 100644
--- a/src/quark.cc
+++ b/src/quark.cc
@@ -49,6 +49,8 @@ void quark_init()
*/
s16b quark_add(cptr str)
{
+ assert(str != nullptr);
+
int i;
/* Look for an existing quark */
@@ -65,7 +67,7 @@ s16b quark_add(cptr str)
quark__num = i + 1;
/* Add a new quark */
- quark__str[i] = string_make(str);
+ quark__str[i] = strdup(str);
/* Return the index */
return (i);
diff --git a/src/string_list.cc b/src/string_list.cc
index 03080e46..41a1feaf 100644
--- a/src/string_list.cc
+++ b/src/string_list.cc
@@ -19,7 +19,12 @@ void string_list_init(string_list *sl, cptr s)
{
assert(sl != NULL);
- sl->s = string_make(s);
+ sl->s = nullptr;
+ if (s)
+ {
+ sl->s = strdup(s);
+ }
+
sl->next = NULL;
}
@@ -31,10 +36,8 @@ void string_list_destroy(string_list *sl)
assert(sl != NULL);
/* Free contained string */
- if (sl->s) {
- string_free(sl->s);
- sl->s = NULL;
- }
+ free(sl->s);
+ sl->s = NULL;
/* We do NOT free the rest of the list. */
sl->next = NULL;
diff --git a/src/string_list.h b/src/string_list.h
index 1d9c2a36..8dfe0b83 100644
--- a/src/string_list.h
+++ b/src/string_list.h
@@ -13,7 +13,7 @@ extern "C" {
typedef struct string_list string_list;
struct string_list {
/* The string list owns the string */
- cptr s;
+ char *s;
/* Next */
string_list *next;
};
diff --git a/src/util.cc b/src/util.cc
index 9714b374..ce2d5ef0 100644
--- a/src/util.cc
+++ b/src/util.cc
@@ -1408,7 +1408,7 @@ errr macro_add(cptr pat, cptr act)
if (n >= 0)
{
/* Free the old macro action */
- string_free(macro__act[n]);
+ free(macro__act[n]);
}
/* Create a new macro */
@@ -1418,11 +1418,11 @@ errr macro_add(cptr pat, cptr act)
n = macro__num++;
/* Save the pattern */
- macro__pat[n] = string_make(pat);
+ macro__pat[n] = strdup(pat);
}
/* Save the action */
- macro__act[n] = string_make(act);
+ macro__act[n] = strdup(act);
/* Efficiency */
macro__use[(byte)(pat[0])] = TRUE;
diff --git a/src/variable.cc b/src/variable.cc
index 579d3fd9..8b2fa8ad 100644
--- a/src/variable.cc
+++ b/src/variable.cc
@@ -413,12 +413,12 @@ s16b macro__num;
/*
* Array of macro patterns [MACRO_MAX]
*/
-cptr *macro__pat;
+char **macro__pat;
/*
* Array of macro actions [MACRO_MAX]
*/
-cptr *macro__act;
+char **macro__act;
/*
* Array of macro types [MACRO_MAX]
@@ -643,7 +643,7 @@ char tval_to_char[128];
/*
* Keymaps for each "mode" associated with each keypress.
*/
-cptr keymap_act[KEYMAP_MODES][256];
+char *keymap_act[KEYMAP_MODES][256];
@@ -878,84 +878,84 @@ cptr ANGBAND_GRAF = "old";
* Path name: The main "lib" directory
* This variable is not actually used anywhere in the code
*/
-cptr ANGBAND_DIR;
+char *ANGBAND_DIR;
/*
* Core lua system
* These files are portable between platforms
*/
-cptr ANGBAND_DIR_CORE;
+char *ANGBAND_DIR_CORE;
/*
* Textual dungeon level definition files
* These files are portable between platforms
*/
-cptr ANGBAND_DIR_DNGN;
+char *ANGBAND_DIR_DNGN;
/*
* Binary image files for the "*_info" arrays (binary)
* These files are not portable between platforms
*/
-cptr ANGBAND_DIR_DATA;
+char *ANGBAND_DIR_DATA;
/*
* Textual template files for the "*_info" arrays (ascii)
* These files are portable between platforms
*/
-cptr ANGBAND_DIR_EDIT;
+char *ANGBAND_DIR_EDIT;
/*
* Various extra files (ascii)
* These files may be portable between platforms
*/
-cptr ANGBAND_DIR_FILE;
+char *ANGBAND_DIR_FILE;
/*
* Help files (normal) for the online help (ascii)
* These files are portable between platforms
*/
-cptr ANGBAND_DIR_HELP;
+char *ANGBAND_DIR_HELP;
/*
* Help files (spoilers) for the online help (ascii)
* These files are portable between platforms
*/
-cptr ANGBAND_DIR_INFO;
+char *ANGBAND_DIR_INFO;
/*
* Modules, those subdirectories are half-mirrors of lib/
*/
-cptr ANGBAND_DIR_MODULES;
+char *ANGBAND_DIR_MODULES;
/*
* Textual template files for the plot files (ascii)
* These files are portable between platforms
*/
-cptr ANGBAND_DIR_NOTE;
+char *ANGBAND_DIR_NOTE;
/*
* Savefiles for current characters (binary)
* These files are portable between platforms
*/
-cptr ANGBAND_DIR_SAVE;
+char *ANGBAND_DIR_SAVE;
/*
* Default "preference" files (ascii)
* These files are rarely portable between platforms
*/
-cptr ANGBAND_DIR_PREF;
+char *ANGBAND_DIR_PREF;
/*
* User "preference" files (ascii)
* These files are rarely portable between platforms
*/
-cptr ANGBAND_DIR_USER;
+char *ANGBAND_DIR_USER;
/*
* Various extra files (binary)
* These files are rarely portable between platforms
*/
-cptr ANGBAND_DIR_XTRA;
+char *ANGBAND_DIR_XTRA;
/*
* Some variables values are created on the fly XXX XXX
diff --git a/src/z-term.c b/src/z-term.c
index fafd0b95..ae8fc7ea 100644
--- a/src/z-term.c
+++ b/src/z-term.c
@@ -269,28 +269,43 @@ term *Term = NULL;
static errr term_win_nuke(term_win *s, int w, int h)
{
/* Free the window access arrays */
- C_KILL(s->a, h, byte*);
- C_KILL(s->c, h, char*);
+ free(s->a);
+ s->a = NULL;
+
+ free(s->c);
+ s->c = NULL;
/* Free the window content arrays */
- C_KILL(s->va, h * w, byte);
- C_KILL(s->vc, h * w, char);
+ free(s->va);
+ s->va = NULL;
+
+ free(s->vc);
+ s->vc = NULL;
/* Free the terrain access arrays */
- C_KILL(s->ta, h, byte*);
- C_KILL(s->tc, h, char*);
+ free(s->ta);
+ s->ta = NULL;
+
+ free(s->tc);
+ s->tc = NULL;
/* Free the terrain content arrays */
- C_KILL(s->vta, h * w, byte);
- C_KILL(s->vtc, h * w, char);
+ free(s->vta);
+ s->vta = NULL;
+ free(s->vtc);
+ s->vtc = NULL;
/* Free the ego graphics access arrays */
- C_KILL(s->ea, h, byte*);
- C_KILL(s->ec, h, char*);
+ free(s->ea);
+ s->ea = NULL;
+ free(s->ec);
+ s->ec = NULL;
/* Free the ego graphics content arrays */
- C_KILL(s->vea, h * w, byte);
- C_KILL(s->vec, h * w, char);
+ free(s->vea);
+ s->vea = NULL;
+ free(s->vec);
+ s->vec = NULL;
/* Success */
return (0);
@@ -305,28 +320,28 @@ static errr term_win_init(term_win *s, int w, int h)
int y;
/* Make the window access arrays */
- C_MAKE(s->a, h, byte*);
- C_MAKE(s->c, h, char*);
+ s->a = safe_calloc(h, sizeof(byte*));
+ s->c = safe_calloc(h, sizeof(char*));
/* Make the window content arrays */
- C_MAKE(s->va, h * w, byte);
- C_MAKE(s->vc, h * w, char);
+ s->va = safe_calloc(h * w, sizeof(byte));
+ s->vc = safe_calloc(h * w, sizeof(char));
/* Make the terrain access arrays */
- C_MAKE(s->ta, h, byte*);
- C_MAKE(s->tc, h, char*);
+ s->ta = safe_calloc(h, sizeof(byte*));
+ s->tc = safe_calloc(h, sizeof(char*));
/* Make the terrain content arrays */
- C_MAKE(s->vta, h * w, byte);
- C_MAKE(s->vtc, h * w, char);
+ s->vta = safe_calloc(h * w, sizeof(byte));
+ s->vtc = safe_calloc(h * w, sizeof(char));
/* Make the ego graphics access arrays */
- C_MAKE(s->ea, h, byte*);
- C_MAKE(s->ec, h, char*);
+ s->ea = safe_calloc(h, sizeof(byte*));
+ s->ec = safe_calloc(h, sizeof(char*));
/* Make the ego graphics content arrays */
- C_MAKE(s->vea, h * w, byte);
- C_MAKE(s->vec, h * w, char);
+ s->vea = safe_calloc(h * w, sizeof(byte));
+ s->vec = safe_calloc(h * w, sizeof(char));
/* Prepare the window access arrays */
@@ -2154,7 +2169,7 @@ errr Term_save(void)
if (!Term->mem)
{
/* Allocate window */
- MAKE(Term->mem, term_win);
+ Term->mem = safe_calloc(1, sizeof(struct term_win));
/* Initialize window */
term_win_init(Term->mem, w, h);
@@ -2177,7 +2192,7 @@ term_win* Term_save_to(void)
term_win *save;
/* Allocate window */
- MAKE(save, term_win);
+ save = safe_calloc(1, sizeof(struct term_win));
/* Initialize window */
term_win_init(save, w, h);
@@ -2205,7 +2220,7 @@ errr Term_load(void)
if (!Term->mem)
{
/* Allocate window */
- MAKE(Term->mem, term_win);
+ Term->mem = safe_calloc(1, sizeof(struct term_win));
/* Initialize window */
term_win_init(Term->mem, w, h);
@@ -2263,7 +2278,9 @@ errr Term_load_from(term_win *save, bool_ final)
/* Free is requested */
if (final)
- FREE(save, term_win);
+ {
+ free(save);
+ }
/* Success */
return (0);
@@ -2286,7 +2303,7 @@ errr Term_exchange(void)
if (!Term->tmp)
{
/* Allocate window */
- MAKE(Term->tmp, term_win);
+ Term->tmp = safe_calloc(1, sizeof(struct term_win));
/* Initialize window */
term_win_init(Term->tmp, w, h);
@@ -2362,11 +2379,11 @@ errr Term_resize(int w, int h)
hold_tmp = Term->tmp;
/* Create new scanners */
- C_MAKE(Term->x1, h, byte);
- C_MAKE(Term->x2, h, byte);
+ Term->x1 = safe_calloc(h, sizeof(byte));
+ Term->x2 = safe_calloc(h, sizeof(byte));
/* Create new window */
- MAKE(Term->old, term_win);
+ Term->old = safe_calloc(1, sizeof(struct term_win));
/* Initialize new window */
term_win_init(Term->old, w, h);
@@ -2375,7 +2392,7 @@ errr Term_resize(int w, int h)
term_win_copy(Term->old, hold_old, wid, hgt);
/* Create new window */
- MAKE(Term->scr, term_win);
+ Term->scr = safe_calloc(1, sizeof(struct term_win));
/* Initialize new window */
term_win_init(Term->scr, w, h);
@@ -2387,7 +2404,7 @@ errr Term_resize(int w, int h)
if (hold_mem)
{
/* Create new window */
- MAKE(Term->mem, term_win);
+ Term->mem = safe_calloc(1, sizeof(struct term_win));
/* Initialize new window */
term_win_init(Term->mem, w, h);
@@ -2400,7 +2417,7 @@ errr Term_resize(int w, int h)
if (hold_tmp)
{
/* Create new window */
- MAKE(Term->tmp, term_win);
+ Term->tmp = safe_calloc(1, sizeof(struct term_win));
/* Initialize new window */
term_win_init(Term->tmp, w, h);
@@ -2410,14 +2427,17 @@ errr Term_resize(int w, int h)
}
/* Free some arrays */
- C_KILL(hold_x1, Term->hgt, byte);
- C_KILL(hold_x2, Term->hgt, byte);
+ free(hold_x1);
+ hold_x1 = NULL;
+ free(hold_x2);
+ hold_x2 = NULL;
/* Nuke */
term_win_nuke(hold_old, Term->wid, Term->hgt);
/* Kill */
- KILL(hold_old, term_win);
+ free(hold_old);
+ hold_old = NULL;
/* Illegal cursor */
if (Term->old->cx >= w) Term->old->cu = 1;
@@ -2427,7 +2447,8 @@ errr Term_resize(int w, int h)
term_win_nuke(hold_scr, Term->wid, Term->hgt);
/* Kill */
- KILL(hold_scr, term_win);
+ free(hold_scr);
+ hold_scr = NULL;
/* Illegal cursor */
if (Term->scr->cx >= w) Term->scr->cu = 1;
@@ -2440,7 +2461,8 @@ errr Term_resize(int w, int h)
term_win_nuke(hold_mem, Term->wid, Term->hgt);
/* Kill */
- KILL(hold_mem, term_win);
+ free(hold_mem);
+ hold_mem = NULL;
/* Illegal cursor */
if (Term->mem->cx >= w) Term->mem->cu = 1;
@@ -2454,7 +2476,8 @@ errr Term_resize(int w, int h)
term_win_nuke(hold_tmp, Term->wid, Term->hgt);
/* Kill */
- KILL(hold_tmp, term_win);
+ free(hold_tmp);
+ hold_tmp = NULL;
/* Illegal cursor */
if (Term->tmp->cx >= w) Term->tmp->cu = 1;
@@ -2560,13 +2583,15 @@ errr term_nuke(term *t)
term_win_nuke(t->old, w, h);
/* Kill "displayed" */
- KILL(t->old, term_win);
+ free(t->old);
+ t->old = NULL;
/* Nuke "requested" */
term_win_nuke(t->scr, w, h);
/* Kill "requested" */
- KILL(t->scr, term_win);
+ free(t->scr);
+ t->scr = NULL;
/* If needed */
if (t->mem)
@@ -2575,7 +2600,8 @@ errr term_nuke(term *t)
term_win_nuke(t->mem, w, h);
/* Kill "memorized" */
- KILL(t->mem, term_win);
+ free(t->mem);
+ t->mem = NULL;
}
/* If needed */
@@ -2585,15 +2611,19 @@ errr term_nuke(term *t)
term_win_nuke(t->tmp, w, h);
/* Kill "temporary" */
- KILL(t->tmp, term_win);
+ free(t->tmp);
+ t->tmp = NULL;
}
/* Free some arrays */
- C_KILL(t->x1, h, byte);
- C_KILL(t->x2, h, byte);
+ free(t->x1);
+ t->x1 = NULL;
+ free(t->x2);
+ t->x2 = NULL;
/* Free the input queue */
- C_KILL(t->key_queue, t->key_size, char);
+ free(t->key_queue);
+ t->key_queue = NULL;
/* Success */
return (0);
@@ -2621,7 +2651,7 @@ errr term_init(term *t, int w, int h, int k)
t->key_size = k;
/* Allocate the input queue */
- C_MAKE(t->key_queue, t->key_size, char);
+ t->key_queue = safe_calloc(t->key_size, sizeof(char));
/* Save the size */
@@ -2629,19 +2659,19 @@ errr term_init(term *t, int w, int h, int k)
t->hgt = h;
/* Allocate change arrays */
- C_MAKE(t->x1, h, byte);
- C_MAKE(t->x2, h, byte);
+ t->x1 = safe_calloc(h, sizeof(byte));
+ t->x2 = safe_calloc(h, sizeof(byte));
/* Allocate "displayed" */
- MAKE(t->old, term_win);
+ t->old = safe_calloc(1, sizeof(struct term_win));
/* Initialize "displayed" */
term_win_init(t->old, w, h);
/* Allocate "requested" */
- MAKE(t->scr, term_win);
+ t->scr = safe_calloc(1, sizeof(struct term_win));
/* Initialize "requested" */
term_win_init(t->scr, w, h);
diff --git a/src/z-virt.c b/src/z-virt.c
index 92632080..e6b545e2 100644
--- a/src/z-virt.c
+++ b/src/z-virt.c
@@ -1,5 +1,3 @@
-/* File: z-virt.c */
-
/*
* Copyright (c) 1997 Ben Harrison
*
@@ -8,138 +6,17 @@
* are included in all such copies.
*/
-/* Purpose: Memory management routines -BEN- */
-
#include "z-virt.h"
-#include "z-util.h"
-
-
/*
- * Optional auxiliary "rnfree" function
+ * Calloc wrapper which aborts if NULL is returned by calloc
*/
-vptr (*rnfree_aux)(vptr, huge) = NULL;
-
-/*
- * Free some memory (allocated by ralloc), return NULL
- */
-vptr rnfree(vptr p, huge len)
+extern void *safe_calloc(size_t nmemb, size_t size)
{
- /* Easy to free zero bytes */
- if (len == 0) return (NULL);
-
- /* Use the "aux" function */
- if (rnfree_aux) return ((*rnfree_aux)(p, len));
-
- /* Use "free" */
- free ((char*)(p));
-
- /* Done */
- return (NULL);
+ void *p = calloc(nmemb, size);
+ if ((nmemb > 0) && (p == NULL))
+ {
+ abort();
+ }
+ return p;
}
-
-
-/*
- * Optional auxiliary "rpanic" function
- */
-vptr (*rpanic_aux)(huge) = NULL;
-
-/*
- * The system is out of memory, so panic. If "rpanic_aux" is set,
- * it can be used to free up some memory and do a new "ralloc()",
- * or if not, it can be used to save things, clean up, and exit.
- * By default, this function simply crashes the computer.
- */
-vptr rpanic(huge len)
-{
- /* Hopefully, we have a real "panic" function */
- if (rpanic_aux) return ((*rpanic_aux)(len));
-
- /* Attempt to crash before icky things happen */
- core("Out of Memory!");
-
- /* Paranoia */
- return ((vptr)(NULL));
-}
-
-
-/*
- * Optional auxiliary "ralloc" function
- */
-vptr (*ralloc_aux)(huge) = NULL;
-
-
-/*
- * Allocate some memory
- */
-vptr ralloc(huge len)
-{
- vptr mem;
-
- /* Allow allocation of "zero bytes" */
- if (len == 0) return ((vptr)(NULL));
-
- /* Use the aux function if set */
- if (ralloc_aux) mem = (*ralloc_aux)(len);
-
- /* Use malloc() to allocate some memory */
- else mem = ((vptr)(malloc((size_t)(len))));
-
- /* We were able to acquire memory */
- if (!mem) mem = rpanic(len);
-
- /* Return the memory, if any */
- return (mem);
-}
-
-
-
-
-/*
- * Allocate a constant string, containing the same thing as 'str'
- */
-cptr string_make(cptr str)
-{
- huge len = 0;
- cptr t = str;
- char *s, *res;
-
- /* Simple sillyness */
- if (!str) return (str);
-
- /* Get the number of chars in the string, including terminator */
- while (str[len++]) /* loop */;
-
- /* Allocate space for the string */
- s = res = (char*)(ralloc(len));
-
- /* Copy the string (with terminator) */
- while ((*s++ = *t++) != 0) /* loop */;
-
- /* Return the allocated, initialized, string */
- return (res);
-}
-
-
-/*
- * Un-allocate a string allocated above.
- * Depends on no changes being made to the string.
- */
-errr string_free(cptr str)
-{
- huge len = 0;
-
- /* Succeed on non-strings */
- if (!str) return (0);
-
- /* Count the number of chars in 'str' plus the terminator */
- while (str[len++]) /* loop */;
-
- /* Kill the buffer of chars we must have allocated above */
- rnfree((vptr)(str), len);
-
- /* Success */
- return (0);
-}
-
-
diff --git a/src/z-virt.h b/src/z-virt.h
index 04944d9d..52696518 100644
--- a/src/z-virt.h
+++ b/src/z-virt.h
@@ -1,5 +1,3 @@
-/* File: z-virt.h */
-
/*
* Copyright (c) 1997 Ben Harrison
*
@@ -8,148 +6,19 @@
* are included in all such copies.
*/
-#ifndef INCLUDED_Z_VIRT_H
-#define INCLUDED_Z_VIRT_H
+#pragma once
+
+#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif
-#include "h-basic.h"
-
/*
- * Memory management routines.
- *
- * Set ralloc_aux to modify the memory allocation routine.
- * Set rnfree_aux to modify the memory de-allocation routine.
- * Set rpanic_aux to let the program react to memory failures.
- *
- * These routines work best as a *replacement* for malloc/free.
- *
- * The string_make() and string_free() routines handle dynamic strings.
- * A dynamic string is a string allocated at run-time, which should not
- * be modified once it has been created.
- *
- * Note the macros below which simplify the details of allocation,
- * deallocation, setting, clearing, casting, size extraction, etc.
- *
- * The macros MAKE/C_MAKE and KILL/C_KILL have a "procedural" metaphor,
- * and they actually modify their arguments.
- *
- * Note that, for some reason, some allocation macros may disallow
- * "stars" in type names, but you can use typedefs to circumvent
- * this. For example, instead of "type **p; MAKE(p,type*);" you
- * can use "typedef type *type_ptr; type_ptr *p; MAKE(p,type_ptr)".
- *
- * Note that it is assumed that "memset()" will function correctly,
- * in particular, that it returns its first argument.
+ * Calloc wrapper which aborts if NULL is returned by calloc
*/
-
-
-
-/**** Available macros ****/
-
-
-/* Size of 'N' things of type 'T' */
-#define C_SIZE(N,T) \
- ((huge)((N)*(sizeof(T))))
-
-/* Size of one thing of type 'T' */
-#define SIZE(T) \
- ((huge)(sizeof(T)))
-
-
-/* Wipe an array of type T[N], at location P, and return P */
-#define C_WIPE(P,N,T) \
- (memset((char*)(P),0,C_SIZE(N,T)))
-
-/* Wipe a thing of type T, at location P, and return P */
-#define WIPE(P,T) \
- (memset((char*)(P),0,SIZE(T)))
-
-
-/* Free an array of N things of type T at P, return NULL */
-#define C_FREE(P,N,T) \
- (rnfree(P,C_SIZE(N,T)))
-
-/* Free one thing of type T at P, return NULL */
-#define FREE(P,T) \
- (rnfree(P,SIZE(T)))
-
-
-/* Allocate, and return, an array of type T[N] */
-#define C_RNEW(N,T) \
- (ralloc(C_SIZE(N,T)))
-
-/* Allocate, and return, a thing of type T */
-#define RNEW(T) \
- (ralloc(SIZE(T)))
-
-
-/* Allocate, wipe, and return an array of type T[N] */
-#define C_ZNEW(N,T) \
- (C_WIPE(C_RNEW(N,T),N,T))
-
-/* Allocate, wipe, and return a thing of type T */
-#define ZNEW(T) \
- (WIPE(RNEW(T),T))
-
-
-/* Allocate a wiped array of type T[N], assign to pointer P */
-#define C_MAKE(P,N,T) \
- ((P)=C_ZNEW(N,T))
-
-/* Allocate a wiped thing of type T, assign to pointer P */
-#define MAKE(P,T) \
- ((P)=ZNEW(T))
-
-
-/* Free an array of type T[N], at location P, and set P to NULL */
-#define C_KILL(P,N,T) \
- ((P)=C_FREE(P,N,T))
-
-/* Free a thing of type T, at location P, and set P to NULL */
-#define KILL(P,T) \
- ((P)=FREE(P,T))
-
-
-
-/**** Available variables ****/
-
-/* Replacement hook for "rnfree()" */
-extern vptr (*rnfree_aux)(vptr, huge);
-
-/* Replacement hook for "rpanic()" */
-extern vptr (*rpanic_aux)(huge);
-
-/* Replacement hook for "ralloc()" */
-extern vptr (*ralloc_aux)(huge);
-
-
-/**** Available functions ****/
-
-/* De-allocate a given amount of memory */
-extern vptr rnfree(vptr p, huge len);
-
-/* Panic, attempt to Allocate 'len' bytes */
-extern vptr rpanic(huge len);
-
-/* Allocate (and return) 'len', or dump core */
-extern vptr ralloc(huge len);
-
-/* Create a "dynamic string" */
-extern cptr string_make(cptr str);
-
-/* Free a string allocated with "string_make()" */
-extern errr string_free(cptr str);
-
-
-
+extern void *safe_calloc(size_t nmemb, size_t size);
#ifdef __cplusplus
} /* extern "C" */
#endif
-
-
-
-#endif