source: mainline/uspace/lib/bithenge/src/tree.c@ 4e2d387

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4e2d387 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
RevLine 
[11b9ad7]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>
[8fc0f47c]39#include <bithenge/blob.h>
40#include <bithenge/tree.h>
[6cd10ac]41#include "common.h"
[11b9ad7]42
[978ccaf1]43static void blob_destroy(bithenge_node_t *base)
[5c925ce]44{
[978ccaf1]45 bithenge_blob_t *self = bithenge_node_as_blob(base);
46 assert(self->base.blob_ops);
47 self->base.blob_ops->destroy(self);
[5c925ce]48}
49
[978ccaf1]50static void node_destroy(bithenge_node_t *self)
[11b9ad7]51{
[978ccaf1]52 switch (bithenge_node_type(self)) {
[5c925ce]53 case BITHENGE_NODE_BLOB:
[978ccaf1]54 blob_destroy(self);
55 return;
[11b9ad7]56 case BITHENGE_NODE_STRING:
[978ccaf1]57 if (self->string_value.needs_free)
58 free((void *)self->string_value.ptr);
[11b9ad7]59 break;
60 case BITHENGE_NODE_INTERNAL:
[978ccaf1]61 self->internal_ops->destroy(self);
62 return;
[11b9ad7]63 case BITHENGE_NODE_BOOLEAN:
[978ccaf1]64 return; /* The boolean nodes are allocated statically below. */
[11b9ad7]65 case BITHENGE_NODE_INTEGER: /* pass-through */
66 break;
67 }
[978ccaf1]68 free(self);
[11b9ad7]69}
70
[f2da0bb]71/** Decrement a node's reference count and free it if appropriate.
72 * @memberof bithenge_node_t
[7c3fb9b]73 * @param node The node to dereference, or NULL.
74 */
[978ccaf1]75void bithenge_node_dec_ref(bithenge_node_t *node)
[f2da0bb]76{
77 if (!node)
[978ccaf1]78 return;
[681a985]79 assert (node->refs > 0);
[f2da0bb]80 if (--node->refs == 0)
[978ccaf1]81 node_destroy(node);
[f2da0bb]82}
83
[e8e31d9]84typedef struct {
85 bithenge_node_t *key;
86 bithenge_node_t **out;
87} get_for_each_data_t;
88
[b7fd2a0]89static errno_t get_for_each_func(bithenge_node_t *key, bithenge_node_t *value,
[e8e31d9]90 void *raw_data)
91{
92 get_for_each_data_t *data = (get_for_each_data_t *)raw_data;
[a42d7d8]93 bool equal;
[b7fd2a0]94 errno_t rc = bithenge_node_equal(&equal, key, data->key);
[e8e31d9]95 bithenge_node_dec_ref(key);
[a42d7d8]96 if (rc != EOK)
97 return rc;
[e8e31d9]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
[1c79996]108 * very slow. Also works for blob nodes to find the byte value at a given
109 * index.
[e8e31d9]110 * @memberof bithenge_node_t
[1c79996]111 * @param self The internal/blob node to find a child of.
[e8e31d9]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
[7c3fb9b]115 * errno.h.
116 */
[b7fd2a0]117errno_t bithenge_node_get(bithenge_node_t *self, bithenge_node_t *key,
[e8e31d9]118 bithenge_node_t **out)
119{
[1c79996]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;
[b7fd2a0]129 errno_t rc = bithenge_blob_read(bithenge_node_as_blob(self),
[1c79996]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
[e8e31d9]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;
[1433ecda]143 get_for_each_data_t data = { key, out };
[b7fd2a0]144 errno_t rc = bithenge_node_for_each(self, get_for_each_func, &data);
[e8e31d9]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
[10334c2e]154/** Initialize an internal node.
155 * @memberof bithenge_node_t
156 * @param[out] self The node.
157 * @param[in] ops The operations provided.
[7c3fb9b]158 * @return EOK on success or an error code from errno.h.
159 */
[b7fd2a0]160errno_t bithenge_init_internal_node(bithenge_node_t *self,
[10334c2e]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
[b7fd2a0]174static errno_t empty_internal_node_for_each(bithenge_node_t *base,
[10334c2e]175 bithenge_for_each_func_t func, void *data)
176{
177 return EOK;
178}
179
[b7fd2a0]180static errno_t empty_internal_node_get(bithenge_node_t *self, bithenge_node_t *key,
[10334c2e]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.
[7c3fb9b]200 * @return EOK on success or an error code from errno.h.
201 */
[b7fd2a0]202errno_t bithenge_new_empty_internal_node(bithenge_node_t **out)
[10334c2e]203{
[1a3b953]204 if (bithenge_should_fail())
205 return ENOMEM;
[10334c2e]206 bithenge_node_inc_ref(&empty_internal_node);
207 *out = &empty_internal_node;
208 return EOK;
209}
210
[1433ecda]211typedef struct {
[5f679702]212 bithenge_node_t base;
[11b9ad7]213 bithenge_node_t **nodes;
214 bithenge_int_t len;
215 bool needs_free;
216} simple_internal_node_t;
217
[5f679702]218static simple_internal_node_t *node_as_simple(bithenge_node_t *node)
[11b9ad7]219{
220 return (simple_internal_node_t *)node;
221}
222
[f2da0bb]223static bithenge_node_t *simple_as_node(simple_internal_node_t *node)
224{
225 return &node->base;
226}
227
[b7fd2a0]228static errno_t simple_internal_node_for_each(bithenge_node_t *base,
[8375d0eb]229 bithenge_for_each_func_t func, void *data)
[11b9ad7]230{
[b7fd2a0]231 errno_t rc;
[978ccaf1]232 simple_internal_node_t *self = node_as_simple(base);
233 for (bithenge_int_t i = 0; i < self->len; i++) {
[1433ecda]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);
[11b9ad7]237 if (rc != EOK)
238 return rc;
239 }
240 return EOK;
241}
242
[978ccaf1]243static void simple_internal_node_destroy(bithenge_node_t *base)
[5c925ce]244{
[978ccaf1]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);
[5c925ce]251}
252
[11b9ad7]253static bithenge_internal_node_ops_t simple_internal_node_ops = {
[5c925ce]254 .for_each = simple_internal_node_for_each,
255 .destroy = simple_internal_node_destroy,
[11b9ad7]256};
257
[f2da0bb]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.
[8375d0eb]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
[f2da0bb]267 * the nodes array rather than just dereferencing each node inside it.
[7c3fb9b]268 * @return EOK on success or an error code from errno.h.
269 */
[b7fd2a0]270errno_t bithenge_new_simple_internal_node(bithenge_node_t **out,
[8375d0eb]271 bithenge_node_t **nodes, bithenge_int_t len, bool needs_free)
[11b9ad7]272{
[b7fd2a0]273 errno_t rc;
[11b9ad7]274 assert(out);
[978ccaf1]275 simple_internal_node_t *self = malloc(sizeof(*self));
276 if (!self) {
[04a7435f]277 rc = ENOMEM;
278 goto error;
[f2da0bb]279 }
[978ccaf1]280 rc = bithenge_init_internal_node(simple_as_node(self),
[04a7435f]281 &simple_internal_node_ops);
282 if (rc != EOK)
283 goto error;
[978ccaf1]284 self->nodes = nodes;
285 self->len = len;
286 self->needs_free = needs_free;
287 *out = simple_as_node(self);
[11b9ad7]288 return EOK;
[04a7435f]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);
[978ccaf1]294 free(self);
[04a7435f]295 return rc;
[11b9ad7]296}
297
[f2da0bb]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 };
[11b9ad7]300
[f2da0bb]301/** Create a boolean node.
[8375d0eb]302 * @memberof bithenge_node_t
303 * @param[out] out Stores the created boolean node.
304 * @param value The value for the node to hold.
[7c3fb9b]305 * @return EOK on success or an error code from errno.h.
306 */
[b7fd2a0]307errno_t bithenge_new_boolean_node(bithenge_node_t **out, bool value)
[11b9ad7]308{
309 assert(out);
[1a3b953]310 if (bithenge_should_fail())
311 return ENOMEM;
[5f679702]312 *out = value ? &true_node : &false_node;
[f2da0bb]313 (*out)->refs++;
[11b9ad7]314 return EOK;
315}
316
[f2da0bb]317/** Create an integer node.
[8375d0eb]318 * @memberof bithenge_node_t
319 * @param[out] out Stores the created integer node.
320 * @param value The value for the node to hold.
[7c3fb9b]321 * @return EOK on success or an error code from errno.h.
322 */
[b7fd2a0]323errno_t bithenge_new_integer_node(bithenge_node_t **out, bithenge_int_t value)
[11b9ad7]324{
325 assert(out);
[978ccaf1]326 bithenge_node_t *self = malloc(sizeof(*self));
327 if (!self)
[11b9ad7]328 return ENOMEM;
[978ccaf1]329 self->type = BITHENGE_NODE_INTEGER;
330 self->refs = 1;
331 self->integer_value = value;
332 *out = self;
[11b9ad7]333 return EOK;
334}
335
[f2da0bb]336/** Create a string node.
[8375d0eb]337 * @memberof bithenge_node_t
[04a7435f]338 * @param[out] out Stores the created string node. On error, this is unchanged.
[8375d0eb]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.
[7c3fb9b]342 * @return EOK on success or an error code from errno.h.
343 */
[b7fd2a0]344errno_t bithenge_new_string_node(bithenge_node_t **out, const char *value, bool needs_free)
[11b9ad7]345{
346 assert(out);
[978ccaf1]347 bithenge_node_t *self = malloc(sizeof(*self));
[600f5d1]348 if (!self) {
349 if (needs_free)
350 free((void *)value);
[11b9ad7]351 return ENOMEM;
[600f5d1]352 }
[978ccaf1]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;
[11b9ad7]358 return EOK;
359}
[8375d0eb]360
[d5070ef]361/** Check whether the contents of two nodes are equal. Does not yet work for
[78d3a00]362 * internal nodes. Takes ownership of nothing.
[d5070ef]363 * @memberof bithenge_node_t
[a42d7d8]364 * @param[out] out Holds whether the nodes are equal.
[d5070ef]365 * @param a, b Nodes to compare.
[a42d7d8]366 * @return EOK on success or an error code from errno.h.
[7c3fb9b]367 * @todo Add support for internal nodes.
368 */
[b7fd2a0]369errno_t bithenge_node_equal(bool *out, bithenge_node_t *a, bithenge_node_t *b)
[d5070ef]370{
[a42d7d8]371 if (a->type != b->type) {
372 *out = false;
373 return EOK;
374 }
[d5070ef]375 switch (a->type) {
376 case BITHENGE_NODE_INTERNAL:
[a42d7d8]377 *out = false;
378 return EOK;
[d5070ef]379 case BITHENGE_NODE_BOOLEAN:
[a42d7d8]380 *out = a->boolean_value == b->boolean_value;
381 return EOK;
[d5070ef]382 case BITHENGE_NODE_INTEGER:
[a42d7d8]383 *out = a->integer_value == b->integer_value;
384 return EOK;
[d5070ef]385 case BITHENGE_NODE_STRING:
[a42d7d8]386 *out = !str_cmp(a->string_value.ptr, b->string_value.ptr);
387 return EOK;
[d5070ef]388 case BITHENGE_NODE_BLOB:
[a42d7d8]389 return bithenge_blob_equal(out, bithenge_node_as_blob(a),
[d5070ef]390 bithenge_node_as_blob(b));
391 }
[a42d7d8]392 return EINVAL;
[d5070ef]393}
394
[8375d0eb]395/** @}
396 */
Note: See TracBrowser for help on using the repository browser.