-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpool.c
More file actions
172 lines (155 loc) · 3.88 KB
/
Copy pathpool.c
File metadata and controls
172 lines (155 loc) · 3.88 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#ifndef _AMALGAMATE_
#include "basedefs.h"
#include "pool.h"
#include "utils.h"
#include "alloc.h"
#include <string.h>
#include <stdio.h>
#endif
#define _UNIT_ALIGN_SIZE 8UL
static int _extend(struct pool *pool, size_t siz);
struct pool* pool_create_empty(void) {
return xcalloc(1, sizeof(struct pool));
}
struct pool* pool_create_preallocated(size_t sz) {
struct pool *pool = pool_create_empty();
_extend(pool, sz);
return pool;
}
struct pool* pool_create(void (*on_pool_destroy)(struct pool*)) {
struct pool *pool = pool_create_empty();
pool->on_pool_destroy = on_pool_destroy;
return pool;
}
void pool_destroy(struct pool *pool) {
if (!pool) {
return;
}
if (pool->on_pool_destroy) {
pool->on_pool_destroy(pool);
}
for (struct pool_unit *u = pool->unit, *next; u; u = next) {
next = u->next;
free(u->heap);
free(u);
}
free(pool);
}
static int _extend(struct pool *pool, size_t siz) {
struct pool_unit *nunit = xmalloc(sizeof(*nunit));
siz = ROUNDUP(siz, _UNIT_ALIGN_SIZE);
nunit->heap = xmalloc(siz);
nunit->next = pool->unit;
pool->heap = nunit->heap;
pool->unit = nunit;
pool->usiz = 0;
pool->asiz = siz;
return 1;
}
void* pool_alloc(struct pool *pool, size_t siz) {
siz = ROUNDUP(siz, _UNIT_ALIGN_SIZE);
size_t usiz = pool->usiz + siz;
void *h = pool->heap;
if (usiz > pool->asiz) {
usiz = usiz + pool->asiz;
if (!_extend(pool, usiz)) {
return 0;
}
h = pool->heap;
}
pool->usiz += siz;
pool->heap += siz;
return h;
}
void* pool_calloc(struct pool *pool, size_t siz) {
void *p = pool_alloc(pool, siz);
memset(p, 0, siz);
return p;
}
char* pool_strdup(struct pool *pool, const char *str) {
if (str) {
size_t len = strlen(str);
char *ret = pool_alloc(pool, len + 1);
memcpy(ret, str, len);
ret[len] = '\0';
return ret;
} else {
return 0;
}
}
char* pool_strndup(struct pool *pool, const char *str, size_t len) {
if (str) {
len = utils_strnlen(str, len);
char *ret = pool_alloc(pool, len + 1);
memcpy(ret, str, len);
ret[len] = '\0';
return ret;
} else {
return 0;
}
}
static inline int _printf_estimate_size(const char *format, va_list ap) {
char buf[1];
return vsnprintf(buf, sizeof(buf), format, ap) + 1;
}
static char* _printf_va(struct pool *pool, unsigned size, const char *format, va_list ap) {
char *wbuf = pool_alloc(pool, size);
if (!wbuf) {
return 0;
}
vsnprintf(wbuf, size, format, ap);
return wbuf;
}
char* pool_printf_va(struct pool *pool, const char *format, va_list va) {
va_list cva;
va_copy(cva, va);
int size = _printf_estimate_size(format, va);
va_end(va);
char *res = _printf_va(pool, size, format, cva);
va_end(cva);
return res;
}
char* pool_printf(struct pool *pool, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
int size = _printf_estimate_size(fmt, ap);
va_end(ap);
va_start(ap, fmt);
char *res = _printf_va(pool, size, fmt, ap);
va_end(ap);
return res;
}
const char** pool_split_string(
struct pool *pool,
const char *haystack,
const char *split_chars,
int ignore_whitespace) {
size_t hsz = strlen(haystack);
const char **ret = (const char**) pool_alloc(pool, (hsz + 1) * sizeof(char*));
const char *sp = haystack;
const char *ep = sp;
int j = 0;
for (int i = 0; *ep; ++i, ++ep) {
const char ch = haystack[i];
const char *sch = strchr(split_chars, ch);
if ((ep >= sp) && (sch || (*(ep + 1) == '\0'))) {
if (!sch && (*(ep + 1) == '\0')) {
++ep;
}
if (ignore_whitespace) {
while (utils_char_is_space(*sp)) ++sp;
while (utils_char_is_space(*(ep - 1))) --ep;
}
if (ep >= sp) {
char *s = pool_alloc(pool, ep - sp + 1);
memcpy(s, sp, ep - sp);
s[ep - sp] = '\0';
ret[j++] = s;
ep = haystack + i;
}
sp = haystack + i + 1;
}
}
ret[j] = 0;
return ret;
}