-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_slab.c
69 lines (60 loc) · 1.65 KB
/
string_slab.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
#include <string.h> // strlen, memcpy
#include <stdlib.h> // malloc, free
#define SLAB_SIZE (64*1024)
struct slab {
struct slab *prev;
char *buffer;
int capacity;
int cursor;
};
char *allocate(struct slab **slab, int size) {
int remaining = (*slab)->capacity - (*slab)->cursor;
if (remaining < size) {
int capacity = SLAB_SIZE * ((size + SLAB_SIZE + 1) / SLAB_SIZE);
struct slab *next = malloc(sizeof next[0] + capacity);
next->prev = *slab;
next->buffer = (char *)(next + 1);
next->capacity = capacity;
next->cursor = 0;
*slab = next;
}
char *result = (*slab)->buffer + (*slab)->cursor;
(*slab)->cursor += size;
return result;
}
char *copy_string(struct slab **slab, const char *string) {
int size = 1 + (int)strlen(string);
char *copy = allocate(slab, size);
memcpy(copy, string, (size_t)size);
return copy;
}
void deallocate_all(struct slab **slab) {
for (;;) {
struct slab *prev = (*slab)->prev;
if ((*slab)->capacity)
free(*slab);
if (!prev)
return;
*slab = prev;
}
}
#include <assert.h>
int main(void) {
struct slab *slab = &(struct slab) { 0 };
assert(strcmp(copy_string(&slab, "Hello, sailor!"), "Hello, sailor!") == 0);
assert(strcmp(copy_string(&slab, ""), "") == 0);
char *large_string = malloc(2 * SLAB_SIZE + 1);
memset(large_string, 'A', 2 * SLAB_SIZE);
large_string[2 * SLAB_SIZE] = 0;
assert(strcmp(copy_string(&slab, large_string), large_string) == 0);
assert(slab->prev);
deallocate_all(&slab);
assert(!slab->prev);
// This shouldn't leak.
for (int i = 0; i < 10000; ++i) {
slab = &(struct slab) { 0 };
for (int j = 0; j < 10000; ++j)
copy_string(&slab, "ABCDEFGHIJKLMOP");
deallocate_all(&slab);
}
}