1 /* src/client/array_append.c
3 * This file is part of PlomRogue. PlomRogue is licensed under the GPL version 3
4 * or any later version. For details on its copyright, license, and warranties,
5 * see the file NOTICE in the root directory of the PlomRogue source package.
8 #include "array_append.h"
9 #include <stddef.h> /* size_t */
10 #include <stdint.h> /* uint32_t, UINT32_MAX */
11 #include <stdlib.h> /* free() */
12 #include <string.h> /* memcpy() */
13 #include "../common/rexit.h" /* exit_trouble() */
14 #include "../common/try_malloc.h" /* try_malloc() */
18 extern void array_append(uint32_t old_n, size_t region_size, void * new_region,
19 void ** ptr_old_array)
21 exit_trouble(UINT32_MAX<(old_n+1)*region_size, __func__, "too large sizes");
22 uint32_t old_size = old_n * region_size;
23 uint32_t new_size = old_size + region_size;
24 char * new_array = try_malloc(new_size, __func__);
25 memcpy(new_array, * ptr_old_array, old_size);
26 memcpy(new_array + (old_n * region_size), new_region, region_size);
27 free(* ptr_old_array);
28 *ptr_old_array = new_array;