-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy patharray.c
More file actions
35 lines (28 loc) · 681 Bytes
/
array.c
File metadata and controls
35 lines (28 loc) · 681 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdlib.h> // malloc, realloc, free.
#include "array.h"
Bool array_grow(void **array, Size expand, Size type_size) {
Array *meta = array_meta(*array);
Size count = 0;
void *data = NULL;
if (*array) {
count = 2 * meta->capacity + expand;
data = realloc(meta, type_size * count + sizeof *meta);
if (!data) {
return false;
}
} else {
count = expand + 1;
data = malloc(type_size * count + sizeof *meta);
if (!data) {
return false;
}
((Array*)data)->size = 0;
}
meta = (Array*)data;
meta->capacity = count;
*array = meta + 1;
return true;
}
void array_delete(void *array) {
free(array_meta(array));
}