source: mainline/uspace/lib/bithenge/src/tree.c

Last change on this file was 7c3fb9b, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix block comment formatting (ccheck).

  • Property mode set to 100644
File size: 11.1 KB
Line 
1/*
2 * Copyright (c) 2012 Sean Bartell
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup bithenge
30 * @{
31 */
32/**
33 * @file
34 * Trees and nodes.
35 */
36
37#include <errno.h>
38#include <stdlib.h>
39#include <bithenge/blob.h>
40#include <bithenge/tree.h>
41#include "common.h"
42
43static void blob_destroy(bithenge_node_t *base)
44{
45 bithenge_blob_t *self = bithenge_node_as_blob(base);
46 assert(self->base.blob_ops);
47 self->base.blob_ops->destroy(self);
48}
49
50static void node_destroy(bithenge_node_t *self)
51{
52 switch (bithenge_node_type(self)) {
53 case BITHENGE_NODE_BLOB:
54 blob_destroy(self);
55 return;
56 case BITHENGE_NODE_STRING:
57 if (self->string_value.needs_free)
58 free((void *)self->string_value.ptr);
59 break;
60 case BITHENGE_NODE_INTERNAL:
61 self->internal_ops->destroy(self);
62 return;
63 case BITHENGE_NODE_BOOLEAN:
64 return; /* The boolean nodes are allocated statically below. */
65 case BITHENGE_NODE_INTEGER: /* pass-through */
66 break;
67 }
68 free(self);
69}
70
71/** Decrement a node's reference count and free it if appropriate.
72 * @memberof bithenge_node_t
73 * @param node The node to dereference, or NULL.
74 */
75void bithenge_node_dec_ref(bithenge_node_t *node)
76{
77 if (!node)
78 return;
79 assert (node->refs > 0);
80 if (--node->refs == 0)
81 node_destroy(node);
82}
83
84typedef struct {
85 bithenge_node_t *key;
86 bithenge_node_t **out;
87} get_for_each_data_t;
88
89static errno_t get_for_each_func(bithenge_node_t *key, bithenge_node_t *value,
90 void *raw_data)
91{
92 get_for_each_data_t *data = (get_for_each_data_t *)raw_data;
93 bool equal;
94 errno_t rc = bithenge_node_equal(&equal, key, data->key);
95 bithenge_node_dec_ref(key);
96 if (rc != EOK)
97 return rc;
98 if (equal) {
99 *data->out = value;
100 return EEXIST;
101 }
102 bithenge_node_dec_ref(value);
103 return EOK;
104}
105
106/** Get a child of a node. Takes ownership of the key. If the node does not
107 * provide this function, for_each will be used as an alternative, which may be
108 * very slow. Also works for blob nodes to find the byte value at a given
109 * index.
110 * @memberof bithenge_node_t
111 * @param self The internal/blob node to find a child of.
112 * @param key The key to search for.
113 * @param[out] out Holds the found node.
114 * @return EOK on success, ENOENT if not found, or another error code from
115 * errno.h.
116 */
117errno_t bithenge_node_get(bithenge_node_t *self, bithenge_node_t *key,
118 bithenge_node_t **out)
119{
120 if (self->type == BITHENGE_NODE_BLOB) {
121 if (bithenge_node_type(key) != BITHENGE_NODE_INTEGER) {
122 bithenge_node_dec_ref(key);
123 return ENOENT;
124 }
125 bithenge_int_t offset = bithenge_integer_node_value(key);
126 bithenge_node_dec_ref(key);
127 uint8_t byte;
128 aoff64_t size = 1;
129 errno_t rc = bithenge_blob_read(bithenge_node_as_blob(self),
130 offset, (char *)&byte, &size);
131 if (rc != EOK)
132 return rc;
133 if (size != 1)
134 return ENOENT;
135
136 return bithenge_new_integer_node(out, byte);
137 }
138
139 assert(self->type == BITHENGE_NODE_INTERNAL);
140 if (self->internal_ops->get)
141 return self->internal_ops->get(self, key, out);
142 *out = NULL;
143 get_for_each_data_t data = { key, out };
144 errno_t rc = bithenge_node_for_each(self, get_for_each_func, &data);
145 bithenge_node_dec_ref(key);
146 if (rc == EEXIST && *out)
147 return EOK;
148 if (rc == EOK)
149 rc = ENOENT;
150 bithenge_node_dec_ref(*out);
151 return rc;
152}
153
154/** Initialize an internal node.
155 * @memberof bithenge_node_t
156 * @param[out] self The node.
157 * @param[in] ops The operations provided.
158 * @return EOK on success or an error code from errno.h.
159 */
160errno_t bithenge_init_internal_node(bithenge_node_t *self,
161 const bithenge_internal_node_ops_t *ops)
162{
163 self->type = BITHENGE_NODE_INTERNAL;
164 self->refs = 1;
165 self->internal_ops = ops;
166 return EOK;
167}
168
169static void internal_node_indestructible(bithenge_node_t *self)
170{
171 assert(false);
172}
173
174static errno_t empty_internal_node_for_each(bithenge_node_t *base,
175 bithenge_for_each_func_t func, void *data)
176{
177 return EOK;
178}
179
180static errno_t empty_internal_node_get(bithenge_node_t *self, bithenge_node_t *key,
181 bithenge_node_t **out)
182{
183 return ENOENT;
184}
185
186static const bithenge_internal_node_ops_t empty_internal_node_ops = {
187 .for_each = empty_internal_node_for_each,
188 .get = empty_internal_node_get,
189 .destroy = internal_node_indestructible,
190};
191
192static bithenge_node_t empty_internal_node = {
193 BITHENGE_NODE_INTERNAL,
194 1,
195 { .internal_ops = &empty_internal_node_ops },
196};
197
198/** Create an empty internal node.
199 * @param[out] out Holds the created node.
200 * @return EOK on success or an error code from errno.h.
201 */
202errno_t bithenge_new_empty_internal_node(bithenge_node_t **out)
203{
204 if (bithenge_should_fail())
205 return ENOMEM;
206 bithenge_node_inc_ref(&empty_internal_node);
207 *out = &empty_internal_node;
208 return EOK;
209}
210
211typedef struct {
212 bithenge_node_t base;
213 bithenge_node_t **nodes;
214 bithenge_int_t len;
215 bool needs_free;
216} simple_internal_node_t;
217
218static simple_internal_node_t *node_as_simple(bithenge_node_t *node)
219{
220 return (simple_internal_node_t *)node;
221}
222
223static bithenge_node_t *simple_as_node(simple_internal_node_t *node)
224{
225 return &node->base;
226}
227
228static errno_t simple_internal_node_for_each(bithenge_node_t *base,
229 bithenge_for_each_func_t func, void *data)
230{
231 errno_t rc;
232 simple_internal_node_t *self = node_as_simple(base);
233 for (bithenge_int_t i = 0; i < self->len; i++) {
234 bithenge_node_inc_ref(self->nodes[2 * i + 0]);
235 bithenge_node_inc_ref(self->nodes[2 * i + 1]);
236 rc = func(self->nodes[2 * i + 0], self->nodes[2 * i + 1], data);
237 if (rc != EOK)
238 return rc;
239 }
240 return EOK;
241}
242
243static void simple_internal_node_destroy(bithenge_node_t *base)
244{
245 simple_internal_node_t *self = node_as_simple(base);
246 for (bithenge_int_t i = 0; i < 2 * self->len; i++)
247 bithenge_node_dec_ref(self->nodes[i]);
248 if (self->needs_free)
249 free(self->nodes);
250 free(self);
251}
252
253static bithenge_internal_node_ops_t simple_internal_node_ops = {
254 .for_each = simple_internal_node_for_each,
255 .destroy = simple_internal_node_destroy,
256};
257
258/** Create an internal node from a set of keys and values. This function takes
259 * ownership of a reference to the key and value nodes, and optionally the
260 * array @a nodes.
261 * @memberof bithenge_node_t
262 * @param[out] out Stores the created internal node.
263 * @param nodes The array of key-value pairs. Keys are stored at even indices
264 * and values are stored at odd indices.
265 * @param len The number of key-value pairs in the node array.
266 * @param needs_free If true, when the internal node is destroyed it will free
267 * the nodes array rather than just dereferencing each node inside it.
268 * @return EOK on success or an error code from errno.h.
269 */
270errno_t bithenge_new_simple_internal_node(bithenge_node_t **out,
271 bithenge_node_t **nodes, bithenge_int_t len, bool needs_free)
272{
273 errno_t rc;
274 assert(out);
275 simple_internal_node_t *self = malloc(sizeof(*self));
276 if (!self) {
277 rc = ENOMEM;
278 goto error;
279 }
280 rc = bithenge_init_internal_node(simple_as_node(self),
281 &simple_internal_node_ops);
282 if (rc != EOK)
283 goto error;
284 self->nodes = nodes;
285 self->len = len;
286 self->needs_free = needs_free;
287 *out = simple_as_node(self);
288 return EOK;
289error:
290 for (bithenge_int_t i = 0; i < 2 * len; i++)
291 bithenge_node_dec_ref(nodes[i]);
292 if (needs_free)
293 free(nodes);
294 free(self);
295 return rc;
296}
297
298static bithenge_node_t false_node = { BITHENGE_NODE_BOOLEAN, 1, .boolean_value = false };
299static bithenge_node_t true_node = { BITHENGE_NODE_BOOLEAN, 1, .boolean_value = true };
300
301/** Create a boolean node.
302 * @memberof bithenge_node_t
303 * @param[out] out Stores the created boolean node.
304 * @param value The value for the node to hold.
305 * @return EOK on success or an error code from errno.h.
306 */
307errno_t bithenge_new_boolean_node(bithenge_node_t **out, bool value)
308{
309 assert(out);
310 if (bithenge_should_fail())
311 return ENOMEM;
312 *out = value ? &true_node : &false_node;
313 (*out)->refs++;
314 return EOK;
315}
316
317/** Create an integer node.
318 * @memberof bithenge_node_t
319 * @param[out] out Stores the created integer node.
320 * @param value The value for the node to hold.
321 * @return EOK on success or an error code from errno.h.
322 */
323errno_t bithenge_new_integer_node(bithenge_node_t **out, bithenge_int_t value)
324{
325 assert(out);
326 bithenge_node_t *self = malloc(sizeof(*self));
327 if (!self)
328 return ENOMEM;
329 self->type = BITHENGE_NODE_INTEGER;
330 self->refs = 1;
331 self->integer_value = value;
332 *out = self;
333 return EOK;
334}
335
336/** Create a string node.
337 * @memberof bithenge_node_t
338 * @param[out] out Stores the created string node. On error, this is unchanged.
339 * @param value The value for the node to hold.
340 * @param needs_free Whether the string should be freed when the node is
341 * destroyed.
342 * @return EOK on success or an error code from errno.h.
343 */
344errno_t bithenge_new_string_node(bithenge_node_t **out, const char *value, bool needs_free)
345{
346 assert(out);
347 bithenge_node_t *self = malloc(sizeof(*self));
348 if (!self) {
349 if (needs_free)
350 free((void *)value);
351 return ENOMEM;
352 }
353 self->type = BITHENGE_NODE_STRING;
354 self->refs = 1;
355 self->string_value.ptr = value;
356 self->string_value.needs_free = needs_free;
357 *out = self;
358 return EOK;
359}
360
361/** Check whether the contents of two nodes are equal. Does not yet work for
362 * internal nodes. Takes ownership of nothing.
363 * @memberof bithenge_node_t
364 * @param[out] out Holds whether the nodes are equal.
365 * @param a, b Nodes to compare.
366 * @return EOK on success or an error code from errno.h.
367 * @todo Add support for internal nodes.
368 */
369errno_t bithenge_node_equal(bool *out, bithenge_node_t *a, bithenge_node_t *b)
370{
371 if (a->type != b->type) {
372 *out = false;
373 return EOK;
374 }
375 switch (a->type) {
376 case BITHENGE_NODE_INTERNAL:
377 *out = false;
378 return EOK;
379 case BITHENGE_NODE_BOOLEAN:
380 *out = a->boolean_value == b->boolean_value;
381 return EOK;
382 case BITHENGE_NODE_INTEGER:
383 *out = a->integer_value == b->integer_value;
384 return EOK;
385 case BITHENGE_NODE_STRING:
386 *out = !str_cmp(a->string_value.ptr, b->string_value.ptr);
387 return EOK;
388 case BITHENGE_NODE_BLOB:
389 return bithenge_blob_equal(out, bithenge_node_as_blob(a),
390 bithenge_node_as_blob(b));
391 }
392 return EINVAL;
393}
394
395/** @}
396 */
Note: See TracBrowser for help on using the repository browser.