source: mainline/uspace/lib/bithenge/tree.c@ 1c79996

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

Bithenge: fix issues and expand coverage for test.sh

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