-
Notifications
You must be signed in to change notification settings - Fork 1
/
array.c
47 lines (38 loc) · 900 Bytes
/
array.c
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
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "nooc.h"
#include "stack.h"
#include "ir.h"
#include "util.h"
#include "array.h"
int
_array_add(void **data, size_t *len, size_t *cap, const void *const new, const size_t size, const size_t count)
{
bool need_realloc;
while (*cap < *len + count) {
need_realloc = true;
*cap = *cap ? *cap * 2 : 1;
}
if (need_realloc)
*data = xrealloc(*data, size*(*cap));
memcpy((char *)*data + size*(*len), new, size*count);
*len += count;
return *len;
}
// should only be called when size is 1
int
_array_zero(void **data, size_t *len, size_t *cap, const size_t count)
{
bool need_realloc;
while (*cap < *len + count) {
need_realloc = true;
*cap = *cap ? *cap * 2 : 1;
}
if (need_realloc)
*data = xrealloc(*data, *cap);
memset((char *)*data + *len, 0, count);
*len += count;
return *len;
}