home · contact · privacy
Refactored similar array append activities into array_append().
[plomrogue] / src / client / misc.c
index a8cf0532e21c3aa767033e99124a3b8a2134b391..4f967dfdcbf764b6e5854f687664fcbe7426e05c 100644 (file)
@@ -22,6 +22,7 @@
                       */
 #include "world.h" /* global world */
 
+#include "../common/try_malloc.h" /* try_malloc() */
 
 
 extern void obey_argv(int argc, char * argv[])
@@ -136,3 +137,18 @@ extern void nav_inventory(char dir)
     world.player_inventory_select = world.player_inventory_select
                                     + (world.player_inventory_select < n_elems);
 }
+
+
+
+extern void array_append(uint32_t old_n, size_t region_size, void * new_region,
+                        void ** ptr_old_array)
+{
+    char * f_name = "array_append()";
+    uint32_t old_size = old_n * region_size;
+    uint32_t new_size = old_size + region_size;
+    void * new_array = try_malloc(new_size, f_name);
+    memcpy(new_array, * ptr_old_array, old_size);
+    memcpy(new_array + (old_n * region_size), new_region, region_size);
+    free(* ptr_old_array);
+    * ptr_old_array = new_array;
+}