forked from cathugger/mkp224o
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vec.c
50 lines (48 loc) · 958 Bytes
/
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
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include "vec.h"
void vec_add1(struct vec_basestruct *ctl,size_t sz)
{
if (!ctl->alen) {
ctl->alen = 8;
if (SIZE_MAX / 8 < sz)
ctl->alen = 1;
ctl->buf = malloc(ctl->alen * sz);
if (!ctl->buf)
abort();
} else if (ctl->len >= ctl->alen) {
ctl->alen *= 2;
if (SIZE_MAX / ctl->alen < sz)
abort();
ctl->buf = realloc(ctl->buf,ctl->alen * sz);
if (!ctl->buf)
abort();
}
++ctl->len;
}
void vec_addn(struct vec_basestruct *ctl,size_t sz,size_t n)
{
if (!ctl->alen) {
if (SIZE_MAX / 8 >= sz)
ctl->alen = 8;
else
ctl->alen = 1;
}
size_t nlen = ctl->alen;
ctl->len += n;
while (ctl->len > nlen)
nlen *= 2;
if (nlen > ctl->alen) {
ctl->alen = nlen;
if (SIZE_MAX / nlen < sz)
abort();
ctl->buf = realloc(ctl->buf,nlen * sz);
if (!ctl->buf)
abort();
} else if (!ctl->buf) {
ctl->buf = malloc(ctl->alen * sz);
if (!ctl->buf)
abort();
}
}