home · contact · privacy
52bcf97f2599b0a9136c122a78ec672ed5e62812
[plomrogue] / src / client / array_append.c
1 /* src/client/array_append.c
2  *
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.
6  */
7
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() */
15
16
17
18 extern void array_append(uint32_t old_n, size_t region_size, void * new_region,
19                         void ** ptr_old_array)
20 {
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;
29 }