This repository has been archived by the owner on Oct 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchunk.c
407 lines (330 loc) · 7.86 KB
/
chunk.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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/* Monkey HTTP Daemon
* ------------------
* Copyright (C) 2012, Sonny Karlsson
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdlib.h>
#include <sys/uio.h> /* struct iovec, writev */
#include "dbg.h"
#include "chunk.h"
static void *(*mem_alloc)(const size_t) = &malloc;
static void (*mem_free)(void *) = &free;
static void *(*mem_realloc)(void *, const size_t) = &realloc;
void chunk_module_init(void *(*mem_alloc_f)(const size_t),
void *(*mem_realloc_f)(void *, const size_t),
void (*mem_free_f)(void *))
{
mem_alloc = mem_alloc_f;
mem_realloc = mem_realloc_f;
mem_free = mem_free_f;
}
struct chunk *chunk_new(size_t size)
{
struct chunk *tmp = NULL;
tmp = mem_alloc(size);
check_mem(tmp);
mk_list_init(&tmp->_head);
tmp->read = 0;
tmp->write = 0;
tmp->refs = 0;
tmp->size = CHUNK_SIZE(size);
return tmp;
error:
if (tmp) {
mem_free(tmp);
}
return NULL;
}
struct chunk_ptr chunk_read_ptr(struct chunk *c)
{
return (struct chunk_ptr){
.parent = c,
.len = c->write - c->read,
.data = c->data + c->read,
};
}
struct chunk_ptr chunk_write_ptr(struct chunk *c)
{
return (struct chunk_ptr){
.parent = c,
.len = c->size - c->write,
.data = c->data + c->write,
};
}
int chunk_set_read_ptr(struct chunk *c, struct chunk_ptr read)
{
check(read.parent == c,
"Pointer not from this chunk.");
check(read.data >= c->data && read.data <= c->data + c->size,
"Pointer out of range for this chunk.");
c->read = read.data - c->data;
return 0;
error:
return -1;
}
int chunk_set_write_ptr(struct chunk *c, struct chunk_ptr write)
{
check(write.parent == c,
"Pointer not from this chunk.");
check(write.data >= c->data && write.data <= c->data + c->size,
"Pointer out of range for this chunk.");
c->write = write.data - c->data;
return 0;
error:
return -1;
}
void chunk_free(struct chunk *c)
{
mk_list_del(&c->_head);
mem_free(c);
}
void chunk_retain(struct chunk *c)
{
c->refs += 1;
}
int chunk_release(struct chunk *c)
{
c->refs -= 1;
check_debug(c->refs > 0, "Free chunk.");
return 0;
error:
chunk_free(c);
return 1;
}
void chunk_list_init(struct chunk_list *cm)
{
mk_list_init(&cm->chunks._head);
}
struct chunk *chunk_list_current(struct chunk_list *cm)
{
check_debug(mk_list_is_empty(&cm->chunks._head), "No managed chunks.");
return mk_list_entry_last((&cm->chunks._head), struct chunk, _head);
error:
return NULL;
}
void chunk_list_add(struct chunk_list *cm, struct chunk *a)
{
mk_list_add(&a->_head, &cm->chunks._head);
}
void chunk_list_stats(struct chunk_list *cm)
{
struct mk_list *head;
struct chunk *c;
size_t used;
size_t free;
size_t total_used = 0;
size_t total_free = 0;
int chunks = 0;
log_info("# Chunk stats.");
mk_list_foreach(head, &cm->chunks._head) {
c = mk_list_entry(head, struct chunk, _head);
used = c->write;
free = c->size - used;
log_info("Chunk: %d, S: %zu B, U: %zu B, F: %zu B, R: %d",
chunks,
c->size,
used,
free,
c->refs);
total_used += used;
total_free += free;
chunks++;
}
log_info("Total");
log_info("Count: %d, Size: %zu B, Used: %zu B, Free: %zu B",
chunks,
total_used + total_free,
total_used,
total_free);
log_info("# Chunk stats.");
}
void chunk_list_free_chunks(struct chunk_list *cm)
{
struct mk_list *head, *tmp;
struct chunk *c;
if (!mk_list_is_empty(&cm->chunks._head)) {
log_info("No chunks to free in manager.");
return;
}
mk_list_foreach_safe(head, tmp, &cm->chunks._head) {
c = mk_list_entry(head, struct chunk, _head);
chunk_free(c);
}
}
int chunk_iov_init(struct chunk_iov *iov, int size)
{
check(size <= UIO_MAXIOV, "New iov size is larger then UIO_MAXIOV.");
iov->held_refs = mem_alloc(size * sizeof(*iov->held_refs));
check_mem(iov->held_refs);
iov->io = mem_alloc(size * sizeof(*iov->io));
check_mem(iov->io);
iov->size = size;
iov->index = 0;
return 0;
error:
return -1;
}
int chunk_iov_resize(struct chunk_iov *iov, int size)
{
struct iovec *tio = NULL;
struct chunk_ref *trefs = NULL;
check(iov->io, "iovec in iov not allocated.");
check(iov->held_refs, "held refs in iov is not allocated.");
check(size <= UIO_MAXIOV, "New iov size is larger then UIO_MAXIOV.");
tio = mem_realloc(iov->io, size * sizeof(*iov->io));
check(tio, "Failed to realloc iovec in iov.");
trefs = mem_realloc(iov->held_refs, size * sizeof(*iov->held_refs));
check(trefs, "Failed to realloc held refs in iov.");
iov->io = tio;
iov->held_refs = trefs;
iov->size = size;
return 0;
error:
if (tio) {
iov->io = tio;
}
if (trefs) {
iov->held_refs = trefs;
}
return -1;
}
size_t chunk_iov_length(struct chunk_iov *iov)
{
size_t s = 0;
int i;
for (i = 0; i < iov->index; i++) {
s += iov->io[i].iov_len;
}
return s;
}
ssize_t chunk_iov_sendv(int fd, struct chunk_iov *iov)
{
check_debug(iov->index > 0, "Tried sending empty chunk_iov.");
return writev(fd, iov->io, iov->index);
error:
return 0;
}
/*
* Use char* p for pointer arithmetic as it's undefined for void*.
*/
int chunk_iov_drop(struct chunk_iov *iov, size_t bytes)
{
struct iovec *io;
size_t length = chunk_iov_length(iov);
size_t drop;
int i;
char *p;
check(bytes > 0, "Tried dropping 0 bytes.");
check(length >= bytes, "Tried dropping more bytes then available.");
for (i = 0; bytes > 0 && i < iov->size; i++) {
io = iov->io + i;
if (io->iov_len < bytes) {
drop = io->iov_len;
io->iov_len = 0;
io->iov_base = NULL;
} else {
drop = bytes;
io->iov_len -= bytes;
p = io->iov_base;
p += bytes;
io->iov_base = p;
}
bytes -= drop;
}
return 0;
error:
return -1;
}
int chunk_iov_add(struct chunk_iov *iov, struct chunk_ptr cp)
{
struct chunk_ref *cr;
struct iovec *io;
check_debug(iov->index < iov->size, "chunk_iov is full.");
check(cp.len > 0, "tried to add empty chunk_ptr");
cr = iov->held_refs + iov->index;
io = iov->io + iov->index;
iov->index += 1;
chunk_retain(cp.parent);
cr->t = CHUNK_REF_CHUNK;
cr->u.chunk = cp.parent;
io->iov_len = cp.len;
io->iov_base = cp.data;
return 0;
error:
return -1;
}
int chunk_iov_add_ptr(struct chunk_iov *iov,
void *vptr,
size_t len,
int do_free)
{
struct chunk_ref *cr;
struct iovec *io;
uint8_t *ptr = vptr;
check(iov->index < iov->size, "chunk_iov is full.");
check(len > 0, "tried to add ptr with len = 0.");
cr = iov->held_refs + iov->index;
io = iov->io + iov->index;
iov->index += 1;
if (do_free) {
cr->t = CHUNK_REF_UINT8;
cr->u.ptr = ptr;
} else {
cr->t = CHUNK_REF_NULL;
cr->u.ptr = NULL;
}
io->iov_len = len;
io->iov_base = ptr;
return 0;
error:
return -1;
}
static void chunk_iov_free_refs(struct chunk_iov *iov)
{
int i;
struct chunk_ref *cr;
for (i = 0; i < iov->index; i++) {
cr = iov->held_refs + i;
if (cr->t == CHUNK_REF_CHUNK) {
chunk_release(cr->u.chunk);
cr->u.chunk = NULL;
}
else if (cr->t == CHUNK_REF_UINT8) {
mem_free(cr->u.ptr);
cr->u.ptr = NULL;
}
cr->t = CHUNK_REF_NULL;
}
}
void chunk_iov_reset(struct chunk_iov *iov)
{
chunk_iov_free_refs(iov);
iov->index = 0;
}
void chunk_iov_free(struct chunk_iov *iov)
{
chunk_iov_free_refs(iov);
if (iov->io) {
mem_free(iov->io);
iov->io = NULL;
}
if (iov->held_refs) {
mem_free(iov->held_refs);
iov->held_refs = NULL;
}
iov->index = 0;
iov->size = 0;
}