comparison src/ploki/re_block.c.h @ 4223:ac0403686959

<oerjan> rm -rf src/ploki; mv ploki src
author HackBot
date Fri, 20 Dec 2013 22:18:50 +0000
parents
children
comparison
equal deleted inserted replaced
4222:b0f3e267bb1e 4223:ac0403686959
1 /* vi: set ft=c: */
2 enum {BLOCK_MAGIC = 32};
3
4 struct block_node {
5 struct block_node *next;
6 size_t used;
7 re_node nodes[BLOCK_MAGIC];
8 };
9
10 struct txt_node {
11 struct txt_node *next;
12 unsigned char *buf;
13 };
14
15 typedef struct {
16 struct block_node *root;
17 struct txt_node *txt_root;
18 } t_block;
19
20 static void bl_init(t_block *b) {
21 b->root = xmalloc(1, sizeof *b->root);
22 b->root->next = NULL;
23 b->root->used = 0;
24 b->txt_root = NULL;
25 }
26
27 static void bl_free(t_block *b) {
28 while (b->root) {
29 struct block_node *tmp = b->root->next;
30 xfree(b->root);
31 b->root = tmp;
32 }
33
34 while (b->txt_root) {
35 struct txt_node *tmp = b->txt_root->next;
36 xfree(b->txt_root->buf);
37 xfree(b->txt_root);
38 b->txt_root = tmp;
39 }
40 }
41
42 static re_node *bl_node(t_block *b) {
43 if (b->root->used >= sizeof b->root->nodes / sizeof b->root->nodes[0]) {
44 struct block_node *tmp = xmalloc(1, sizeof *tmp);
45 tmp->used = 0;
46 tmp->next = b->root;
47 b->root = tmp;
48 }
49
50 return b->root->nodes + b->root->used++;
51 }
52
53 static unsigned char *bl_string(t_block *b, size_t len) {
54 struct txt_node *tmp = xmalloc(1, sizeof *tmp);
55 tmp->buf = xmalloc(len, sizeof *tmp->buf);
56 tmp->next = b->txt_root;
57 b->txt_root = tmp;
58
59 return b->txt_root->buf;
60 }