-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynarr.h
57 lines (48 loc) · 1.06 KB
/
dynarr.h
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#ifndef DYNARR_H
#define DYNARR_H
#include "common.h"
typedef struct DynArr {
void **buffer;
u64 size;
u64 capacity;
} DynArr;
DynArr *da_sized_init(u64 capacity) {
DynArr *da = (DynArr *)malloc(sizeof(DynArr));
da->buffer = (void **)malloc(sizeof(void *) * capacity);
da->size = 0;
da->capacity = capacity;
return da;
}
DynArr *da_init() {
return da_sized_init(1);
}
bool da_insert(DynArr *da, void *data) {
if (data != NULL) {
if (da->capacity <= da->size) {
debug("[DA] growing capacity from %llu to %llu because size is %llu\n", da->capacity, da->size * 2, da->size);
da->capacity = da->size * 2;
da->buffer = (void **)realloc(da->buffer, sizeof(void *) * da->capacity);
}
da->buffer[da->size] = data;
da->size++;
return true;
}
return false;
}
void da_print(DynArr *da) {
for (u64 i = 0; i < da->size; i++) {
printf("\t%p\n", da->buffer[i]);
}
}
void da_free(DynArr *da) {
free(da->buffer);
free(da);
}
void da_free_data(DynArr *da) {
for (u64 i = 0; i < da->size; i++) {
free(da->buffer[i]);
}
free(da->buffer);
free(da);
}
#endif