write/alsa: Add btr support.
[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 #include "error.h"
9
10
11 struct btr_buffer {
12 char *buf;
13 size_t size;
14 /** The number of references to this buffer. */
15 int refcount;
16 };
17
18 struct btr_buffer_reference {
19 struct btr_buffer *btrb;
20 size_t consumed;
21 /* Each buffer reference belongs to the buffer queue list of some buffer tree node. */
22 struct list_head node;
23 };
24
25 struct btr_node {
26 char *name;
27 struct btr_node *parent;
28 /* The position of this btr node in the buffer tree. */
29 struct list_head node;
30 /* The children nodes of this btr node are linked together in a list. */
31 struct list_head children;
32 /**
33 * The input queue is a list of references to btr buffers. Each item on
34 * the list represents an input buffer which has not been completely
35 * used by this btr node.
36 */
37 struct list_head input_queue;
38 btr_command_handler execute;
39 };
40
41 #define FOR_EACH_CHILD(_tn, _btrn) list_for_each_entry((_tn), \
42 &((_btrn)->children), node)
43
44 #define FOR_EACH_BUFFER_REF(_br, _btrn) \
45 list_for_each_entry((_br), &(_btrn)->input_queue, node)
46 #define FOR_EACH_BUFFER_REF_SAFE(_br, _tmp, _btrn) \
47 list_for_each_entry_safe((_br), (_tmp), &(_btrn)->input_queue, node)
48
49 struct btr_node *btr_new_node(char *name, struct btr_node *parent,
50 btr_command_handler handler)
51 {
52 struct btr_node *btrn = para_malloc(sizeof(*btrn));
53
54 btrn->name = para_strdup(name);
55 btrn->parent = parent;
56 btrn->execute = handler;
57 if (parent)
58 list_add_tail(&btrn->node, &parent->children);
59 INIT_LIST_HEAD(&btrn->children);
60 INIT_LIST_HEAD(&btrn->input_queue);
61 return btrn;
62 }
63
64 /*
65 * Allocate a new btr buffer.
66 *
67 * The freshly allocated buffer will have a zero refcount.
68 */
69 static struct btr_buffer *new_btrb(char *buf, size_t size)
70 {
71 struct btr_buffer *btrb = para_malloc(sizeof(*btrb));
72
73 btrb->buf = buf;
74 btrb->size = size;
75 btrb->refcount = 0;
76 return btrb;
77 }
78
79 /*
80 * Deallocate the reference, release the resources if refcount drops to zero.
81 */
82 static void btr_drop_buffer_reference(struct btr_buffer_reference *br)
83 {
84 struct btr_buffer *btrb = br->btrb;
85
86 //PARA_CRIT_LOG("dropping buffer reference %p\n", br);
87 list_del(&br->node);
88 free(br);
89 btrb->refcount--;
90 if (btrb->refcount == 0) {
91 free(btrb->buf);
92 free(btrb);
93 }
94 }
95
96 static void add_btrb_to_children(struct btr_buffer *btrb, struct btr_node *btrn)
97 {
98 struct btr_node *ch;
99
100 FOR_EACH_CHILD(ch, btrn) {
101 struct btr_buffer_reference *br = para_malloc(sizeof(*br));
102 br->btrb = btrb;
103 br->consumed = 0;
104 list_add_tail(&br->node, &ch->input_queue);
105 btrb->refcount++;
106 }
107 }
108
109 void btr_add_output(char *buf, size_t size, struct btr_node *btrn)
110 {
111 struct btr_buffer *btrb;
112
113 btrb = new_btrb(buf, size);
114 add_btrb_to_children(btrb, btrn);
115 }
116
117 static void btr_pushdown_br(struct btr_buffer_reference *br, struct btr_node *btrn)
118 {
119 add_btrb_to_children(br->btrb, btrn);
120 btr_drop_buffer_reference(br);
121 }
122
123 void btr_pushdown(struct btr_node *btrn)
124 {
125 struct btr_buffer_reference *br, *tmp;
126
127 FOR_EACH_BUFFER_REF_SAFE(br, tmp, btrn)
128 btr_pushdown_br(br, btrn);
129 }
130
131 /* Return true if this node has no children. */
132 bool btr_no_children(struct btr_node *btrn)
133 {
134 return list_empty(&btrn->children);
135 }
136
137 bool btr_no_parent(struct btr_node *btrn)
138 {
139 return !btrn->parent;
140 }
141
142 bool btr_inplace_ok(struct btr_node *btrn)
143 {
144 if (!btrn->parent)
145 return true;
146 return list_is_singular(&btrn->parent->children);
147 }
148
149 static inline size_t br_available_bytes(struct btr_buffer_reference *br)
150 {
151 return br->btrb->size - br->consumed;
152 }
153
154 size_t btr_get_buffer_by_reference(struct btr_buffer_reference *br, char **buf)
155 {
156 *buf = br->btrb->buf + br->consumed;
157 return br_available_bytes(br);
158 }
159
160 size_t btr_next_buffer(struct btr_node *btrn, char **bufp)
161 {
162 struct btr_buffer_reference *br;
163
164 if (list_empty(&btrn->input_queue)) {
165 *bufp = NULL;
166 return 0;
167 }
168 br = list_first_entry(&btrn->input_queue, struct btr_buffer_reference, node);
169 return btr_get_buffer_by_reference(br, bufp);
170 }
171
172 void btr_consume(struct btr_node *btrn, size_t numbytes)
173 {
174 struct btr_buffer_reference *br;
175
176 assert(!list_empty(&btrn->input_queue));
177 br = list_first_entry(&btrn->input_queue, struct btr_buffer_reference, node);
178 assert(br->consumed + numbytes <= br->btrb->size);
179 br->consumed += numbytes;
180 if (br->consumed == br->btrb->size)
181 btr_drop_buffer_reference(br);
182 }
183
184 static void flush_input_queue(struct btr_node *btrn)
185 {
186 struct btr_buffer_reference *br, *tmp;
187 FOR_EACH_BUFFER_REF_SAFE(br, tmp, btrn)
188 btr_drop_buffer_reference(br);
189 }
190
191 void btr_del_node(struct btr_node *btrn)
192 {
193 struct btr_node *ch;
194
195 if (!btrn)
196 return;
197 PARA_NOTICE_LOG("deleting %s\n", btrn->name);
198 FOR_EACH_CHILD(ch, btrn)
199 ch->parent = NULL;
200 flush_input_queue(btrn);
201 if (btrn->parent)
202 list_del(&btrn->node);
203 free(btrn->name);
204 free(btrn);
205 }
206
207 size_t btr_get_input_queue_size(struct btr_node *btrn)
208 {
209 struct btr_buffer_reference *br;
210 size_t size = 0;
211
212 FOR_EACH_BUFFER_REF(br, btrn) {
213 //PARA_CRIT_LOG("size: %zu\n", size);
214 size += br_available_bytes(br);
215 }
216 return size;
217 }
218
219 /**
220 * Return the size of the largest input queue.
221 *
222 * Iterates over all children of the given node.
223 */
224 size_t btr_bytes_pending(struct btr_node *btrn)
225 {
226 size_t max_size = 0;
227 struct btr_node *ch;
228
229 FOR_EACH_CHILD(ch, btrn) {
230 size_t size = btr_get_input_queue_size(ch);
231 max_size = PARA_MAX(max_size, size);
232 }
233 return max_size;
234 }
235
236 int btr_exec(struct btr_node *btrn, const char *command, char **value_result)
237 {
238 if (!btrn)
239 return -ERRNO_TO_PARA_ERROR(EINVAL);
240 if (!btrn->execute)
241 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
242 return btrn->execute(command, value_result);
243 }
244
245 int btr_exec_up(struct btr_node *btrn, const char *command, char **value_result)
246 {
247 int ret;
248
249 for (; btrn; btrn = btrn->parent) {
250 struct btr_node *parent = btrn->parent;
251 PARA_CRIT_LOG("parent: %p\n", parent);
252 if (!parent)
253 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
254 if (!parent->execute)
255 continue;
256 ret = parent->execute(command, value_result);
257 if (ret == -ERRNO_TO_PARA_ERROR(ENOTSUP))
258 continue;
259 if (ret < 0)
260 return ret;
261 }
262 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
263 }