source: mainline/uspace/lib/bithenge/tree.c@ 3a7356dc

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3a7356dc was 681a985, checked in by Sean Bartell <wingedtachikoma@…>, 13 years ago

Bithenge: move library to uspace/lib

  • Property mode set to 100644
File size: 10.2 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>
[5c925ce]39#include "blob.h"
[da0fef6]40#include "os.h"
[11b9ad7]41#include "tree.h"
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
[978ccaf1]73 * @param node The node to dereference, or NULL. */
74void bithenge_node_dec_ref(bithenge_node_t *node)
[f2da0bb]75{
76 if (!node)
[978ccaf1]77 return;
[681a985]78 assert (node->refs > 0);
[f2da0bb]79 if (--node->refs == 0)
[978ccaf1]80 node_destroy(node);
[f2da0bb]81}
82
[e8e31d9]83typedef struct {
84 bithenge_node_t *key;
85 bithenge_node_t **out;
86} get_for_each_data_t;
87
88static int get_for_each_func(bithenge_node_t *key, bithenge_node_t *value,
89 void *raw_data)
90{
91 get_for_each_data_t *data = (get_for_each_data_t *)raw_data;
92 bool equal = bithenge_node_equal(key, data->key);
93 bithenge_node_dec_ref(key);
94 if (equal) {
95 *data->out = value;
96 return EEXIST;
97 }
98 bithenge_node_dec_ref(value);
99 return EOK;
100}
101
102/** Get a child of a node. Takes ownership of the key. If the node does not
103 * provide this function, for_each will be used as an alternative, which may be
104 * very slow.
105 * @memberof bithenge_node_t
106 * @param self The internal node to find a child of.
107 * @param key The key to search for.
108 * @param[out] out Holds the found node.
109 * @return EOK on success, ENOENT if not found, or another error code from
110 * errno.h. */
111int bithenge_node_get(bithenge_node_t *self, bithenge_node_t *key,
112 bithenge_node_t **out)
113{
114 assert(self->type == BITHENGE_NODE_INTERNAL);
115 if (self->internal_ops->get)
116 return self->internal_ops->get(self, key, out);
117 *out = NULL;
118 get_for_each_data_t data = {key, out};
119 int rc = bithenge_node_for_each(self, get_for_each_func, &data);
120 bithenge_node_dec_ref(key);
121 if (rc == EEXIST && *out)
122 return EOK;
123 if (rc == EOK)
124 rc = ENOENT;
125 bithenge_node_dec_ref(*out);
126 return rc;
127}
128
[10334c2e]129/** Initialize an internal node.
130 * @memberof bithenge_node_t
131 * @param[out] self The node.
132 * @param[in] ops The operations provided.
133 * @return EOK on success or an error code from errno.h. */
134int bithenge_init_internal_node(bithenge_node_t *self,
135 const bithenge_internal_node_ops_t *ops)
136{
137 self->type = BITHENGE_NODE_INTERNAL;
138 self->refs = 1;
139 self->internal_ops = ops;
140 return EOK;
141}
142
143static void internal_node_indestructible(bithenge_node_t *self)
144{
145 assert(false);
146}
147
148static int empty_internal_node_for_each(bithenge_node_t *base,
149 bithenge_for_each_func_t func, void *data)
150{
151 return EOK;
152}
153
154static int empty_internal_node_get(bithenge_node_t *self, bithenge_node_t *key,
155 bithenge_node_t **out)
156{
157 return ENOENT;
158}
159
160static const bithenge_internal_node_ops_t empty_internal_node_ops = {
161 .for_each = empty_internal_node_for_each,
162 .get = empty_internal_node_get,
163 .destroy = internal_node_indestructible,
164};
165
166static bithenge_node_t empty_internal_node = {
167 BITHENGE_NODE_INTERNAL,
168 1,
169 { .internal_ops = &empty_internal_node_ops },
170};
171
172/** Create an empty internal node.
173 * @param[out] out Holds the created node.
174 * @return EOK on success or an error code from errno.h. */
175int bithenge_new_empty_internal_node(bithenge_node_t **out)
176{
177 bithenge_node_inc_ref(&empty_internal_node);
178 *out = &empty_internal_node;
179 return EOK;
180}
181
[11b9ad7]182typedef struct
183{
[5f679702]184 bithenge_node_t base;
[11b9ad7]185 bithenge_node_t **nodes;
186 bithenge_int_t len;
187 bool needs_free;
188} simple_internal_node_t;
189
[5f679702]190static simple_internal_node_t *node_as_simple(bithenge_node_t *node)
[11b9ad7]191{
192 return (simple_internal_node_t *)node;
193}
194
[f2da0bb]195static bithenge_node_t *simple_as_node(simple_internal_node_t *node)
196{
197 return &node->base;
198}
199
[8375d0eb]200static int simple_internal_node_for_each(bithenge_node_t *base,
201 bithenge_for_each_func_t func, void *data)
[11b9ad7]202{
203 int rc;
[978ccaf1]204 simple_internal_node_t *self = node_as_simple(base);
205 for (bithenge_int_t i = 0; i < self->len; i++) {
206 bithenge_node_inc_ref(self->nodes[2*i+0]);
207 bithenge_node_inc_ref(self->nodes[2*i+1]);
208 rc = func(self->nodes[2*i+0], self->nodes[2*i+1], data);
[11b9ad7]209 if (rc != EOK)
210 return rc;
211 }
212 return EOK;
213}
214
[978ccaf1]215static void simple_internal_node_destroy(bithenge_node_t *base)
[5c925ce]216{
[978ccaf1]217 simple_internal_node_t *self = node_as_simple(base);
218 for (bithenge_int_t i = 0; i < 2 * self->len; i++)
219 bithenge_node_dec_ref(self->nodes[i]);
220 if (self->needs_free)
221 free(self->nodes);
222 free(self);
[5c925ce]223}
224
[11b9ad7]225static bithenge_internal_node_ops_t simple_internal_node_ops = {
[5c925ce]226 .for_each = simple_internal_node_for_each,
227 .destroy = simple_internal_node_destroy,
[11b9ad7]228};
229
[f2da0bb]230/** Create an internal node from a set of keys and values. This function takes
231 * ownership of a reference to the key and value nodes, and optionally the
232 * array @a nodes.
[8375d0eb]233 * @memberof bithenge_node_t
234 * @param[out] out Stores the created internal node.
235 * @param nodes The array of key-value pairs. Keys are stored at even indices
236 * and values are stored at odd indices.
237 * @param len The number of key-value pairs in the node array.
238 * @param needs_free If true, when the internal node is destroyed it will free
[f2da0bb]239 * the nodes array rather than just dereferencing each node inside it.
[8375d0eb]240 * @return EOK on success or an error code from errno.h. */
241int bithenge_new_simple_internal_node(bithenge_node_t **out,
242 bithenge_node_t **nodes, bithenge_int_t len, bool needs_free)
[11b9ad7]243{
[04a7435f]244 int rc;
[11b9ad7]245 assert(out);
[978ccaf1]246 simple_internal_node_t *self = malloc(sizeof(*self));
247 if (!self) {
[04a7435f]248 rc = ENOMEM;
249 goto error;
[f2da0bb]250 }
[978ccaf1]251 rc = bithenge_init_internal_node(simple_as_node(self),
[04a7435f]252 &simple_internal_node_ops);
253 if (rc != EOK)
254 goto error;
[978ccaf1]255 self->nodes = nodes;
256 self->len = len;
257 self->needs_free = needs_free;
258 *out = simple_as_node(self);
[11b9ad7]259 return EOK;
[04a7435f]260error:
261 for (bithenge_int_t i = 0; i < 2 * len; i++)
262 bithenge_node_dec_ref(nodes[i]);
263 if (needs_free)
264 free(nodes);
[978ccaf1]265 free(self);
[04a7435f]266 return rc;
[11b9ad7]267}
268
[f2da0bb]269static bithenge_node_t false_node = { BITHENGE_NODE_BOOLEAN, 1, .boolean_value = false };
270static bithenge_node_t true_node = { BITHENGE_NODE_BOOLEAN, 1, .boolean_value = true };
[11b9ad7]271
[f2da0bb]272/** Create a boolean node.
[8375d0eb]273 * @memberof bithenge_node_t
274 * @param[out] out Stores the created boolean node.
275 * @param value The value for the node to hold.
276 * @return EOK on success or an error code from errno.h. */
[11b9ad7]277int bithenge_new_boolean_node(bithenge_node_t **out, bool value)
278{
279 assert(out);
[5f679702]280 *out = value ? &true_node : &false_node;
[f2da0bb]281 (*out)->refs++;
[11b9ad7]282 return EOK;
283}
284
[f2da0bb]285/** Create an integer node.
[8375d0eb]286 * @memberof bithenge_node_t
287 * @param[out] out Stores the created integer node.
288 * @param value The value for the node to hold.
289 * @return EOK on success or an error code from errno.h. */
[11b9ad7]290int bithenge_new_integer_node(bithenge_node_t **out, bithenge_int_t value)
291{
292 assert(out);
[978ccaf1]293 bithenge_node_t *self = malloc(sizeof(*self));
294 if (!self)
[11b9ad7]295 return ENOMEM;
[978ccaf1]296 self->type = BITHENGE_NODE_INTEGER;
297 self->refs = 1;
298 self->integer_value = value;
299 *out = self;
[11b9ad7]300 return EOK;
301}
302
[f2da0bb]303/** Create a string node.
[8375d0eb]304 * @memberof bithenge_node_t
[04a7435f]305 * @param[out] out Stores the created string node. On error, this is unchanged.
[8375d0eb]306 * @param value The value for the node to hold.
307 * @param needs_free Whether the string should be freed when the node is
308 * destroyed.
309 * @return EOK on success or an error code from errno.h. */
[11b9ad7]310int bithenge_new_string_node(bithenge_node_t **out, const char *value, bool needs_free)
311{
312 assert(out);
[978ccaf1]313 bithenge_node_t *self = malloc(sizeof(*self));
[600f5d1]314 if (!self) {
315 if (needs_free)
316 free((void *)value);
[11b9ad7]317 return ENOMEM;
[600f5d1]318 }
[978ccaf1]319 self->type = BITHENGE_NODE_STRING;
320 self->refs = 1;
321 self->string_value.ptr = value;
322 self->string_value.needs_free = needs_free;
323 *out = self;
[11b9ad7]324 return EOK;
325}
[8375d0eb]326
[d5070ef]327/** Check whether the contents of two nodes are equal. Does not yet work for
[78d3a00]328 * internal nodes. Takes ownership of nothing.
[d5070ef]329 * @memberof bithenge_node_t
330 * @param a, b Nodes to compare.
331 * @return Whether the nodes are equal. If an error occurs, returns false.
332 * @todo Add support for internal nodes.
333 */
334bool bithenge_node_equal(bithenge_node_t *a, bithenge_node_t *b)
335{
336 if (a->type != b->type)
337 return false;
338 switch (a->type) {
339 case BITHENGE_NODE_INTERNAL:
340 return false;
341 case BITHENGE_NODE_BOOLEAN:
342 return a->boolean_value == b->boolean_value;
343 case BITHENGE_NODE_INTEGER:
344 return a->integer_value == b->integer_value;
345 case BITHENGE_NODE_STRING:
346 return !str_cmp(a->string_value.ptr, b->string_value.ptr);
347 case BITHENGE_NODE_BLOB:
348 return bithenge_blob_equal(bithenge_node_as_blob(a),
349 bithenge_node_as_blob(b));
350 }
351 return false;
352}
353
[8375d0eb]354/** @}
355 */
Note: See TracBrowser for help on using the repository browser.