First implementation of the buffer tree code.
[paraslash.git] / buffer_tree.c
1 #include <regex.h>
2 #include <stdbool.h>
3
4 #include "para.h"
5 #include "list.h"
6 #include "string.h"
7 //#include "buffer_tree.h"
8
9
10 struct btr_buffer {
11         char *buf;
12         size_t size;
13         /** The number of references to this buffer. */
14         int refcount;
15 };
16
17 struct btr_buffer_reference {
18         struct btr_buffer *btrb;
19         size_t consumed;
20         /* Each buffer reference belongs to the buffer queue list of some buffer tree node. */
21         struct list_head node;
22 };
23
24 struct btr_node {
25         char *name;
26         struct btr_node *parent;
27         /* The position of this btr node in the buffer tree. */
28         struct list_head node;
29         /* The children nodes of this btr node are linked together in a list. */
30         struct list_head children;
31         /**
32          * The input queue is a list of references to btr buffers. Each item on
33          * the list represents an input buffer which has not been completely
34          * used by this btr node.
35          */
36         struct list_head buffer_queue;
37 };
38
39 #define FOR_EACH_TARGET_NODE(_tn, _btrn) list_for_each_entry((_tn), \
40         &((_btrn)->children), node)
41
42 #define FOR_EACH_BUFFER_REF(_br, _btrn) \
43         list_for_each_entry((_br), &(_btrn)->buffer_queue, node)
44 #define FOR_EACH_BUFFER_REF_SAFE(_br, _tmp, _btrn) \
45         list_for_each_entry_safe((_br), (_tmp), &(_btrn)->buffer_queue, node)
46
47 INIT_STDERR_LOGGING(0);
48
49 struct btr_node *btr_new_node(char *name, struct btr_node *parent)
50 {
51         struct btr_node *btrn = para_malloc(sizeof(*btrn));
52
53         btrn->name = para_strdup(name);
54         btrn->parent = parent;
55         if (parent)
56                 list_add_tail(&btrn->node, &parent->children);
57         INIT_LIST_HEAD(&btrn->children);
58         INIT_LIST_HEAD(&btrn->buffer_queue);
59         return btrn;
60 }
61
62 static struct btr_buffer *new_btrb(char *buf, size_t size)
63 {
64         struct btr_buffer *btrb = para_malloc(sizeof(*btrb));
65
66         btrb->buf = buf;
67         btrb->size = size;
68         btrb->refcount = 0;
69         return btrb;
70 }
71
72 void btr_drop_buffer_reference(struct btr_buffer_reference *br)
73 {
74         struct btr_buffer *btrb = br->btrb;
75
76         list_del(&br->node);
77         free(br);
78         btrb->refcount--;
79         if (btrb->refcount == 0) {
80                 free(btrb->buf);
81                 free(btrb);
82         }
83 }
84
85 static void add_btrb_to_targets(struct btr_buffer *btrb, struct btr_node *btrn)
86 {
87         struct btr_node *tn; /* target node */
88
89         FOR_EACH_TARGET_NODE(tn, btrn) {
90                 struct btr_buffer_reference *br = para_malloc(sizeof(*br));
91                 br->btrb = btrb;
92                 br->consumed = 0;
93                 list_add_tail(&br->node, &tn->buffer_queue);
94                 btrb->refcount++;
95         }
96 }
97
98 void btr_add_output(char *buf, size_t size, struct btr_node *btrn)
99 {
100         struct btr_buffer *btrb;
101
102         btrb = new_btrb(buf, size);
103         add_btrb_to_targets(btrb, btrn);
104 }
105
106 void btr_pushdown_br(struct btr_buffer_reference *br, struct btr_node *btrn)
107 {
108         add_btrb_to_targets(br->btrb, btrn);
109         btr_drop_buffer_reference(br);
110 }
111
112 void btr_pushdown(struct btr_node *btrn)
113 {
114         struct btr_buffer_reference *br, *tmp;
115
116         FOR_EACH_BUFFER_REF_SAFE(br, tmp, btrn)
117                 btr_pushdown_br(br, btrn);
118 }
119
120 bool btr_no_children(struct btr_node *btrn)
121 {
122         return list_empty(&btrn->children);
123 }
124
125 bool btr_no_parent(struct btr_node *btrn)
126 {
127         return !btrn->parent;
128 }
129
130 bool btr_inplace_ok(struct btr_node *btrn)
131 {
132         if (!btrn->parent)
133                 return true;
134         return list_is_singular(&btrn->parent->children);
135 }
136
137 struct btr_buffer_reference *btr_next_br(struct btr_node *btrn)
138 {
139         if (list_empty(&btrn->buffer_queue))
140                 return NULL;
141         return list_first_entry(&btrn->buffer_queue, struct btr_buffer_reference, node);
142 }
143
144 static inline size_t br_available_bytes(struct btr_buffer_reference *br)
145 {
146         return br->btrb->size - br->consumed;
147 }
148
149 size_t btr_get_buffer_by_reference(struct btr_buffer_reference *br, char **buf)
150 {
151         *buf = br->btrb->buf + br->consumed;
152         return br_available_bytes(br);
153 }
154
155 void btr_increase_used_bytes(struct btr_buffer_reference *br, size_t consumed)
156 {
157         br->consumed += consumed;
158         if (br->consumed == br->btrb->size)
159                 btr_drop_buffer_reference(br);
160 }
161
162 static void flush_input_queue(struct btr_node *btrn)
163 {
164         struct btr_buffer_reference *br, *tmp;
165         FOR_EACH_BUFFER_REF_SAFE(br, tmp, btrn)
166                 btr_drop_buffer_reference(br);
167 }
168
169 void btr_del_node(struct btr_node *btrn)
170 {
171         struct btr_node *tn;
172
173         FOR_EACH_TARGET_NODE(tn, btrn)
174                 tn->parent = NULL;
175         flush_input_queue(btrn);
176         if (btrn->parent)
177                 list_del(&btrn->node);
178         free(btrn->name);
179         free(btrn);
180 }
181
182 size_t btr_get_input_queue_size(struct btr_node *btrn)
183 {
184         struct btr_buffer_reference *br;
185         size_t size = 0;
186
187         FOR_EACH_BUFFER_REF(br, btrn)
188                 size += br_available_bytes(br);
189         return size;
190 }
191
192 int main(void)
193 {
194         return 1;
195 }