-
Notifications
You must be signed in to change notification settings - Fork 0
/
vec.c
123 lines (111 loc) · 2.98 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <stdlib.h>
#include <security/pam_ext.h>
#include <syslog.h>
#include <string.h>
#include <errno.h>
#include "prelude.h"
#include "vec.h"
extern pam_handle_t* pamhandle;
INTERNAL struct ptrvec ptrvec_new(size_t cap) {
if (!cap)
return (struct ptrvec) {};
cap = (cap + 1) / 2 * 2; // only use multiple of 16 bytes (minimum granularity for glibc malloc)
void* ptr = malloc(cap * sizeof(void*));
if (!ptr) {
log_err("Could not allocate %zu bytes: %s", sizeof(void*) * cap, strerror(errno));
return (struct ptrvec) {
.cap = cap,
.error = 1,
};
}
return (struct ptrvec) {
.cap = cap,
.ptr = ptr,
};
}
INTERNAL int ptrvec_append(struct ptrvec* v, void* e) {
if (v->len + 1 > v->cap) {
// increment in steps of two, since glibc's malloc usually works on 16 byte granularity anyway
size_t new_cap = v->cap <= 2 ? v->cap + 2 : v->cap * 1.5;
new_cap = (new_cap + 1) / 2 * 2; // round up to multiple of two
void *new_ptr = realloc(v->ptr, new_cap * sizeof(void*));
if (!new_ptr) {
log_err("Could not reallocate to %zu bytes: %s", sizeof(void*) * new_cap, strerror(errno));
return -1;
}
v->cap = new_cap;
v->ptr = new_ptr;
}
v->ptr[v->len++] = e;
return 0;
}
INTERNAL int ptrvec_resize_to_fit(struct ptrvec* v) {
if (v->len + 1 != v->cap) {
void *new_ptr = realloc(v->ptr, (v->len + 1) * sizeof(void*));
if (!new_ptr) {
log_err("Could not reallocate to %zu bytes: %s", sizeof(void*) * (v->len + 1), strerror(errno));
return -1;
}
v->cap = v->len + 1;
v->ptr = new_ptr;
}
v->ptr[v->len] = NULL;
return 0;
}
INTERNAL void ptrvec_cleanup(struct ptrvec* v) {
if (!v->ptr)
return;
for (size_t i = 0; i < v->len; i++) {
free(v->ptr[i]);
}
free(v->ptr);
}
INTERNAL struct intvec intvec_new(size_t cap) {
if (!cap)
return (struct intvec) {};
cap = (cap + 3) / 4 * 4; // round up ti multiple for four
void* ptr = malloc(cap * sizeof(void*));
if (!ptr) {
log_err("Could not allocate %zu bytes: %s", sizeof(void*) * cap, strerror(errno));
return (struct intvec) {
.cap = cap,
.error = 1,
};
}
return (struct intvec) {
.cap = cap,
.ptr = ptr,
};
}
INTERNAL int intvec_append(struct intvec* v, unsigned e) {
if (v->len + 1 > v->cap) {
size_t new_cap = v->cap <= 4 ? v->cap + 1 : v->cap * 1.5;
new_cap = (new_cap + 3) / 4 * 4; // round up to multiple of four
void *new_ptr = realloc(v->ptr, new_cap * sizeof(void*));
if (!new_ptr) {
log_err("Could not reallocate to %zu bytes: %s", sizeof(void*) * new_cap, strerror(errno));
return -1;
}
v->cap = new_cap;
v->ptr = new_ptr;
}
v->ptr[v->len++] = e;
return 0;
}
INTERNAL int intvec_resize_to_fit(struct intvec* v) {
if (v->len != v->cap) {
void *new_ptr = realloc(v->ptr, (v->len) * sizeof(void*));
if (!new_ptr) {
log_err("Could not reallocate to %zu bytes: %s", sizeof(void*) * (v->len), strerror(errno));
return -1;
}
v->cap = v->len + 1;
v->ptr = new_ptr;
}
return 0;
}
INTERNAL void intvec_cleanup(struct intvec* v) {
if (!v->ptr)
return;
free(v->ptr);
}