-
Notifications
You must be signed in to change notification settings - Fork 0
/
vec.c
106 lines (81 loc) · 1.97 KB
/
vec.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <assert.h>
#include <errno.h>
#include <string.h>
#include <stdint.h>
#include "vec.h"
#include "mem.h"
Vec vec_init(const size_t esize,
const size_t chunk,
VecCB dtor) {
assert(esize > 0);
assert(chunk > 0);
Vec self;
(void) memset(&self, 0, sizeof(Vec));
self.ptr = NULL;
self.len = 0;
self.cap = 0;
self.chunk = chunk;
self.esize = esize;
self.dtor = dtor;
return (self);
}
void vec_free(Vec *const self) {
assert(self != NULL);
if (self->dtor != NULL)
vec_iter(self, self->dtor);
if (self->ptr == NULL) {
assert(self->len == 0);
assert(self->cap == 0);
} else {
assert(self->cap > 0);
free(self->ptr);
self->ptr = NULL;
self->len = 0;
self->cap = 0;
self->dtor = NULL;
}
}
void *vec_add(Vec *const self, const size_t n) {
assert(self != NULL);
assert(self->len <= self->cap);
void *new_ptr = NULL;
if (SIZE_MAX - n < self->len)
goto err_overflow;
if (self->len + n > self->cap) {
if (SIZE_MAX / 2 < self->cap)
goto err_overflow;
size_t new_cap = 0;
new_cap = (self->len + n) > (self->cap * 2) ?
(self->len + n) : (self->cap * 2);
new_cap = new_cap > self->chunk ?
new_cap : self->chunk;
assert(new_cap > self->cap);
assert(new_cap >= self->len + n);
new_ptr = xrealloc(&self->ptr,
new_cap,
self->esize);
if (new_ptr == NULL)
return (NULL);
self->ptr = new_ptr;
self->cap = new_cap;
}
self->len += n;
return ((uint8_t *) self->ptr +
(self->len - n) *
self->esize);
err_overflow:
errno = EOVERFLOW;
return (NULL);
}
void vec_rev(Vec *const self, const size_t n) {
assert(self != NULL);
assert(self->len >= n);
self->len -= n;
}
void vec_iter(Vec *const self, VecCB iter) {
assert(self != NULL);
assert(iter != NULL);
uint8_t *p = self->ptr;
for (size_t i = 0; i < self->len; i++, p += self->esize)
iter(p);
}