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