changeset 227:befa2e855d3b

core: reinterface the alloc module
author David Demelier <markand@malikania.fr>
date Thu, 19 Nov 2020 10:48:46 +0100
parents dd7c8d4321a3
children 2734223d3daf
files examples/example-battle/main.c examples/example-battle/spell-fire.c examples/example-drawable/main.c examples/example-map/main.c libadventure/adventure/state/mainmenu.c libadventure/adventure/state/panic.c libadventure/adventure/state/splashscreen.c libcore/core/alloc.c libcore/core/alloc.h libcore/core/util.c librpg/rpg/battle-state-check.c librpg/rpg/battle-state-closing.c librpg/rpg/battle-state-lost.c librpg/rpg/battle-state-opening.c librpg/rpg/battle-state-selection.c librpg/rpg/battle-state-victory.c librpg/rpg/battle.c librpg/rpg/map-file.c librpg/rpg/tileset-file.c
diffstat 19 files changed, 122 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/examples/example-battle/main.c	Thu Nov 19 10:17:04 2020 +0100
+++ b/examples/example-battle/main.c	Thu Nov 19 10:48:46 2020 +0100
@@ -167,7 +167,7 @@
 static void
 prepare_to_fight(void)
 {
-	struct battle *bt = alloc_zero(1, sizeof (*bt));
+	struct battle *bt = alloc_new0(sizeof (*bt));
 
 //	bt->enemies[0].ch = &haunted_wood;
 	bt->team[0].ch = &team[0];
--- a/examples/example-battle/spell-fire.c	Thu Nov 19 10:17:04 2020 +0100
+++ b/examples/example-battle/spell-fire.c	Thu Nov 19 10:48:46 2020 +0100
@@ -89,7 +89,7 @@
 
 	(void)owner;
 
-	data = alloc_zero(1, sizeof (*data));
+	data = alloc_new0(sizeof (*data));
 	data->battle = bt;
 	data->selection = selection;
 	data->action.data = data;
--- a/examples/example-drawable/main.c	Thu Nov 19 10:17:04 2020 +0100
+++ b/examples/example-drawable/main.c	Thu Nov 19 10:48:46 2020 +0100
@@ -108,7 +108,7 @@
 static void
 spawn(int x, int y)
 {
-	struct explosion *ex = alloc_zero(1, sizeof (*ex));
+	struct explosion *ex = alloc_new0(sizeof (*ex));
 
 	animation_init(&ex->anim, &explosion_sprite, 15);
 
--- a/examples/example-map/main.c	Thu Nov 19 10:17:04 2020 +0100
+++ b/examples/example-map/main.c	Thu Nov 19 10:48:46 2020 +0100
@@ -115,7 +115,7 @@
 	char path[1024];
 	struct map_state *ms;
 
-	ms = alloc_zero(1, sizeof (*ms));
+	ms = alloc_new0(sizeof (*ms));
 	ms->loader.load_action = load_action;
 
 	snprintf(path, sizeof (path), "%s/assets/maps/%s.map", BINDIR, name);
@@ -177,7 +177,7 @@
 {
 	struct teleport_effect *fx;
 
-	fx = alloc_zero(1, sizeof (*fx));
+	fx = alloc_new0(sizeof (*fx));
 	fx->tp.state = map_state_new(name, ox, oy);
 
 	fx->act.data = fx;
@@ -243,7 +243,7 @@
 	if (sscanf(def, "%255[^:]:%d:%d", name, &ox, &oy) < 1)
 		panicf("could not parse teleport destination");
 
-	touch = alloc_zero(1, sizeof (*touch));
+	touch = alloc_new0(sizeof (*touch));
 	touch->map = map;
 	touch->ox = ox;
 	touch->oy = oy;
--- a/libadventure/adventure/state/mainmenu.c	Thu Nov 19 10:17:04 2020 +0100
+++ b/libadventure/adventure/state/mainmenu.c	Thu Nov 19 10:48:46 2020 +0100
@@ -136,7 +136,7 @@
 	struct font fonts[2];
 
 	/* Allocate the main menu data. */
-	main = (state->data = alloc_zero(1, sizeof (*main)));
+	main = (state->data = alloc_new0(sizeof (*main)));
 
 	if (!font_openmem(&fonts[0], fonts_teutonic, sizeof (fonts_teutonic), 130) ||
 	    !font_openmem(&fonts[1], fonts_pirata_one, sizeof (fonts_pirata_one), 30))
--- a/libadventure/adventure/state/panic.c	Thu Nov 19 10:17:04 2020 +0100
+++ b/libadventure/adventure/state/panic.c	Thu Nov 19 10:48:46 2020 +0100
@@ -96,7 +96,7 @@
 
 	theme = theme_default();
 	font = theme->fonts[THEME_FONT_INTERFACE];
-	view = alloc_zero(1, sizeof (*view));
+	view = alloc_new0(sizeof (*view));
 
 	if (!font_render(font, &view->texts[0].tex, "An unrecoverable error occured and the game cannot continue.", FOREGROUND) ||
 	    !font_render(font, &view->texts[1].tex, "Please report the detailed error as provided below.", FOREGROUND) ||
--- a/libadventure/adventure/state/splashscreen.c	Thu Nov 19 10:17:04 2020 +0100
+++ b/libadventure/adventure/state/splashscreen.c	Thu Nov 19 10:48:46 2020 +0100
@@ -54,7 +54,7 @@
 	struct splashscreen *splash;
 	struct font font;
 
-	splash = alloc_zero(1, sizeof (*splash));
+	splash = alloc_new0(sizeof (*splash));
 	splash->next = next;
 
 	if (!font_openmem(&font, fonts_cubic, sizeof (fonts_cubic), 80))
--- a/libcore/core/alloc.c	Thu Nov 19 10:17:04 2020 +0100
+++ b/libcore/core/alloc.c	Thu Nov 19 10:48:46 2020 +0100
@@ -54,7 +54,28 @@
 };
 
 void *
-alloc(size_t n, size_t size)
+alloc_new(size_t size)
+{
+	assert(size != 0);
+
+	return allocator.alloc(size);
+}
+
+void *
+alloc_new0(size_t size)
+{
+	assert(size != 0);
+
+	void *ptr;
+
+	if ((ptr = allocator.alloc(size)))
+		memset(ptr, 0, size);
+
+	return ptr;
+}
+
+void *
+alloc_array(size_t n, size_t size)
 {
 	assert(n != 0);
 	assert(size != 0);
@@ -68,7 +89,7 @@
 }
 
 void *
-alloc_zero(size_t n, size_t size)
+alloc_array0(size_t n, size_t size)
 {
 	assert(n != 0);
 	assert(size != 0);
@@ -86,6 +107,23 @@
 }
 
 void *
+alloc_renew(void *ptr, size_t amount)
+{
+	return allocator.realloc(ptr, amount);
+}
+
+void *
+alloc_rearray(void *ptr, size_t n, size_t size)
+{
+	size_t total = n * size;
+
+	if (total / n != size)
+		return errorf("%s", strerror(ENOMEM)), NULL;
+
+	return allocator.realloc(ptr, total);
+}
+
+void *
 alloc_dup(const void *ptr, size_t size)
 {
 	assert(ptr);
@@ -98,3 +136,17 @@
 
 	return mem;
 }
+
+char *
+alloc_sdup(const char *src)
+{
+	assert(src);
+
+	char *ret;
+	size_t length = strlen(src) + 1;
+
+	if ((ret = allocator.alloc(length)))
+		memcpy(ret, src, length + 1);
+
+	return ret;
+}
--- a/libcore/core/alloc.h	Thu Nov 19 10:17:04 2020 +0100
+++ b/libcore/core/alloc.h	Thu Nov 19 10:48:46 2020 +0100
@@ -81,16 +81,25 @@
 /**
  * Shortcut for allocator->alloc.
  *
- * \pre n != 0 && size != 0
- * \param n the number of objects to allocate
- * \param size the size of each object
+ * \pre size != 0
+ * \param size the amount of bytes to allocate
  * \return The result of allocator->alloc.
  */
 void *
-alloc(size_t n, size_t size);
+alloc_new(size_t size);
 
 /**
- * Shortcut for allocator->alloc and then use memset to clear memory.
+ * Similar to alloc_new but zero-initialize the memory.
+ *
+ * \pre size != 0
+ * \param size the amount of bytes to allocate
+ * \return The result of allocator->alloc.
+ */
+void *
+alloc_new0(size_t size);
+
+/**
+ * Shortcut for allocator->alloc but specialized for arrays.
  *
  * \pre n != 0 && size != 0
  * \param n the number of objects to allocate
@@ -98,7 +107,39 @@
  * \return The result of allocator->alloc.
  */
 void *
-alloc_zero(size_t n, size_t size);
+alloc_array(size_t n, size_t size);
+
+/**
+ * Similar to alloc_array but zero-initialize the memory.
+ *
+ * \pre n != 0 && size != 0
+ * \param n the number of objects to allocate
+ * \param size the size of each object
+ * \return The result of allocator->alloc.
+ */
+void *
+alloc_array0(size_t n, size_t size);
+
+/**
+ * Shortcut for allocator->realloc.
+ *
+ * \param ptr the old pointer (maybe NULL)
+ * \param amount the new amount (maybe 0)
+ * \return The result of allocator->realloc
+ */
+void *
+alloc_renew(void *ptr, size_t amount);
+
+/**
+ * Shortcut for allocator->realloc but specialized for arrays.
+ *
+ * \param ptr the old pointer (maybe NULL)
+ * \param n the number of element
+ * \param size the size of each object
+ * \return The result of allocator->realloc
+ */
+void *
+alloc_rearray(void *ptr, size_t n, size_t size);
 
 /**
  * Duplicate region pointer by ptr.
--- a/libcore/core/util.c	Thu Nov 19 10:17:04 2020 +0100
+++ b/libcore/core/util.c	Thu Nov 19 10:48:46 2020 +0100
@@ -45,7 +45,7 @@
 
 	if ((fd = open(path, O_RDONLY)) < 0 || fstat(fd, &st) < 0)
 		goto io_error;
-	if (!(str = alloc_zero(1, st.st_size + 1)))
+	if (!(str = alloc_new0(st.st_size + 1)))
 		goto alloc_error;
 	if ((nr = read(fd, str, st.st_size)) != st.st_size)
 		goto io_error;
--- a/librpg/rpg/battle-state-check.c	Thu Nov 19 10:17:04 2020 +0100
+++ b/librpg/rpg/battle-state-check.c	Thu Nov 19 10:48:46 2020 +0100
@@ -82,7 +82,7 @@
 {
 	struct fadeout *fade;
 
-	if (!(fade = alloc_zero(1, sizeof (*fade))))
+	if (!(fade = alloc_new0(sizeof (*fade))))
 		panic();
 
 	fade->ch = et->ch;
--- a/librpg/rpg/battle-state-closing.c	Thu Nov 19 10:17:04 2020 +0100
+++ b/librpg/rpg/battle-state-closing.c	Thu Nov 19 10:48:46 2020 +0100
@@ -88,7 +88,7 @@
 
 	struct closing *closing;
 
-	if (!(closing = alloc_zero(1, sizeof (*closing))) ||
+	if (!(closing = alloc_new0(sizeof (*closing))) ||
 	    !texture_new(&closing->texture, window.w, window.h))
 		panic();
 
--- a/librpg/rpg/battle-state-lost.c	Thu Nov 19 10:17:04 2020 +0100
+++ b/librpg/rpg/battle-state-lost.c	Thu Nov 19 10:48:46 2020 +0100
@@ -75,7 +75,7 @@
 
 	struct lost *lost;
 
-	if (!(lost = alloc_zero(1, sizeof (*lost))))
+	if (!(lost = alloc_new0(sizeof (*lost))))
 		panic();
 
 	lost->self.data = lost;
--- a/librpg/rpg/battle-state-opening.c	Thu Nov 19 10:17:04 2020 +0100
+++ b/librpg/rpg/battle-state-opening.c	Thu Nov 19 10:48:46 2020 +0100
@@ -91,7 +91,7 @@
 
 	struct opening *opening;
 
-	if (!(opening = alloc_zero(1, sizeof (*opening))))
+	if (!(opening = alloc_new0(sizeof (*opening))))
 		panic();
 
 	opening->self.data = opening;
--- a/librpg/rpg/battle-state-selection.c	Thu Nov 19 10:17:04 2020 +0100
+++ b/librpg/rpg/battle-state-selection.c	Thu Nov 19 10:48:46 2020 +0100
@@ -207,7 +207,7 @@
 
 	struct select *select;
 
-	if (!(select = alloc_zero(1, sizeof (*select))))
+	if (!(select = alloc_new0(sizeof (*select))))
 		panic();
 
 	select->type = type;
--- a/librpg/rpg/battle-state-victory.c	Thu Nov 19 10:17:04 2020 +0100
+++ b/librpg/rpg/battle-state-victory.c	Thu Nov 19 10:48:46 2020 +0100
@@ -75,7 +75,7 @@
 
 	struct victory *vic;
 
-	if (!(vic = alloc_zero(1, sizeof (*vic))))
+	if (!(vic = alloc_new0(sizeof (*vic))))
 		panic();
 
 	/* TODO: compute money, xp and drop. */
--- a/librpg/rpg/battle.c	Thu Nov 19 10:17:04 2020 +0100
+++ b/librpg/rpg/battle.c	Thu Nov 19 10:48:46 2020 +0100
@@ -358,7 +358,7 @@
 	assert(target);
 
 	const struct battle_entity *et = find(bt, target);
-	struct indicator *id = alloc_zero(1, sizeof (*id));
+	struct indicator *id = alloc_new0(sizeof (*id));
 
 	id->bti.color = BATTLE_INDICATOR_HP_COLOR;
 	id->bti.amount = amount;
--- a/librpg/rpg/map-file.c	Thu Nov 19 10:17:04 2020 +0100
+++ b/librpg/rpg/map-file.c	Thu Nov 19 10:48:46 2020 +0100
@@ -66,7 +66,7 @@
 	 * The next line after a layer declaration is a list of plain integer
 	 * that fill the layer tiles.
 	 */
-	if (!(ctx->mf->layers[layer_type].tiles = alloc_zero(amount, sizeof (unsigned short))))
+	if (!(ctx->mf->layers[layer_type].tiles = alloc_array0(amount, sizeof (unsigned short))))
 		return false;
 
 	for (int tile; fscanf(ctx->fp, "%d\n", &tile) && current < amount; ++current)
--- a/librpg/rpg/tileset-file.c	Thu Nov 19 10:17:04 2020 +0100
+++ b/librpg/rpg/tileset-file.c	Thu Nov 19 10:48:46 2020 +0100
@@ -120,7 +120,7 @@
 	size_t tiledefsz = 0;
 
 	while (fscanf(ctx->fp, "%hu|%hd|%hd|%hu|%hu\n", &id, &x, &y, &w, &h) == 5) {
-		tiledefs = allocator.realloc(tiledefs, ++tiledefsz * sizeof (*tiledefs));
+		tiledefs = alloc_rearray(tiledefs, ++tiledefsz, sizeof (*tiledefs));
 		tiledefs[tiledefsz - 1].id = id;
 		tiledefs[tiledefsz - 1].x = x;
 		tiledefs[tiledefsz - 1].y = y;
@@ -155,11 +155,11 @@
 		 * one animation reference.
 		 */
 		tf->tfasz++;
-		tf->tfas = allocator.realloc(tf->tfas, tf->tfasz * sizeof (*tf->tfas));
+		tf->tfas = alloc_rearray(tf->tfas, tf->tfasz, sizeof (*tf->tfas));
 		tfa = &tf->tfas[tf->tfasz - 1];
 
 		/* This is the real user-side tileset array of animations. */
-		tf->anims = allocator.realloc(tf->anims, tf->tfasz * sizeof (*tf->anims));
+		tf->anims = alloc_rearray(tf->anims, tf->tfasz, sizeof (*tf->anims));
 
 		snprintf(path, sizeof (path), "%s/%s", ctx->basedir, filename);