| 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 "blob.h"
|
|---|
| 40 | #include "os.h"
|
|---|
| 41 | #include "tree.h"
|
|---|
| 42 |
|
|---|
| 43 | static 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 |
|
|---|
| 50 | static 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 | void bithenge_node_dec_ref(bithenge_node_t *node)
|
|---|
| 75 | {
|
|---|
| 76 | if (!node)
|
|---|
| 77 | return;
|
|---|
| 78 | if (--node->refs == 0)
|
|---|
| 79 | node_destroy(node);
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | typedef struct
|
|---|
| 83 | {
|
|---|
| 84 | bithenge_node_t base;
|
|---|
| 85 | bithenge_node_t **nodes;
|
|---|
| 86 | bithenge_int_t len;
|
|---|
| 87 | bool needs_free;
|
|---|
| 88 | } simple_internal_node_t;
|
|---|
| 89 |
|
|---|
| 90 | static simple_internal_node_t *node_as_simple(bithenge_node_t *node)
|
|---|
| 91 | {
|
|---|
| 92 | return (simple_internal_node_t *)node;
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | static bithenge_node_t *simple_as_node(simple_internal_node_t *node)
|
|---|
| 96 | {
|
|---|
| 97 | return &node->base;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | static int simple_internal_node_for_each(bithenge_node_t *base,
|
|---|
| 101 | bithenge_for_each_func_t func, void *data)
|
|---|
| 102 | {
|
|---|
| 103 | int rc;
|
|---|
| 104 | simple_internal_node_t *self = node_as_simple(base);
|
|---|
| 105 | for (bithenge_int_t i = 0; i < self->len; i++) {
|
|---|
| 106 | bithenge_node_inc_ref(self->nodes[2*i+0]);
|
|---|
| 107 | bithenge_node_inc_ref(self->nodes[2*i+1]);
|
|---|
| 108 | rc = func(self->nodes[2*i+0], self->nodes[2*i+1], data);
|
|---|
| 109 | if (rc != EOK)
|
|---|
| 110 | return rc;
|
|---|
| 111 | }
|
|---|
| 112 | return EOK;
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | static void simple_internal_node_destroy(bithenge_node_t *base)
|
|---|
| 116 | {
|
|---|
| 117 | simple_internal_node_t *self = node_as_simple(base);
|
|---|
| 118 | for (bithenge_int_t i = 0; i < 2 * self->len; i++)
|
|---|
| 119 | bithenge_node_dec_ref(self->nodes[i]);
|
|---|
| 120 | if (self->needs_free)
|
|---|
| 121 | free(self->nodes);
|
|---|
| 122 | free(self);
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | static bithenge_internal_node_ops_t simple_internal_node_ops = {
|
|---|
| 126 | .for_each = simple_internal_node_for_each,
|
|---|
| 127 | .destroy = simple_internal_node_destroy,
|
|---|
| 128 | };
|
|---|
| 129 |
|
|---|
| 130 | /** Initialize an internal node.
|
|---|
| 131 | * @memberof bithenge_node_t
|
|---|
| 132 | * @param[out] self The node.
|
|---|
| 133 | * @param[in] ops The operations provided.
|
|---|
| 134 | * @return EOK on success or an error code from errno.h. */
|
|---|
| 135 | int bithenge_init_internal_node(bithenge_node_t *self,
|
|---|
| 136 | const bithenge_internal_node_ops_t *ops)
|
|---|
| 137 | {
|
|---|
| 138 | self->type = BITHENGE_NODE_INTERNAL;
|
|---|
| 139 | self->refs = 1;
|
|---|
| 140 | self->internal_ops = ops;
|
|---|
| 141 | return EOK;
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | /** Create an internal node from a set of keys and values. This function takes
|
|---|
| 145 | * ownership of a reference to the key and value nodes, and optionally the
|
|---|
| 146 | * array @a nodes.
|
|---|
| 147 | * @memberof bithenge_node_t
|
|---|
| 148 | * @param[out] out Stores the created internal node.
|
|---|
| 149 | * @param nodes The array of key-value pairs. Keys are stored at even indices
|
|---|
| 150 | * and values are stored at odd indices.
|
|---|
| 151 | * @param len The number of key-value pairs in the node array.
|
|---|
| 152 | * @param needs_free If true, when the internal node is destroyed it will free
|
|---|
| 153 | * the nodes array rather than just dereferencing each node inside it.
|
|---|
| 154 | * @return EOK on success or an error code from errno.h. */
|
|---|
| 155 | int bithenge_new_simple_internal_node(bithenge_node_t **out,
|
|---|
| 156 | bithenge_node_t **nodes, bithenge_int_t len, bool needs_free)
|
|---|
| 157 | {
|
|---|
| 158 | int rc;
|
|---|
| 159 | assert(out);
|
|---|
| 160 | simple_internal_node_t *self = malloc(sizeof(*self));
|
|---|
| 161 | if (!self) {
|
|---|
| 162 | rc = ENOMEM;
|
|---|
| 163 | goto error;
|
|---|
| 164 | }
|
|---|
| 165 | rc = bithenge_init_internal_node(simple_as_node(self),
|
|---|
| 166 | &simple_internal_node_ops);
|
|---|
| 167 | if (rc != EOK)
|
|---|
| 168 | goto error;
|
|---|
| 169 | self->nodes = nodes;
|
|---|
| 170 | self->len = len;
|
|---|
| 171 | self->needs_free = needs_free;
|
|---|
| 172 | *out = simple_as_node(self);
|
|---|
| 173 | return EOK;
|
|---|
| 174 | error:
|
|---|
| 175 | for (bithenge_int_t i = 0; i < 2 * len; i++)
|
|---|
| 176 | bithenge_node_dec_ref(nodes[i]);
|
|---|
| 177 | if (needs_free)
|
|---|
| 178 | free(nodes);
|
|---|
| 179 | free(self);
|
|---|
| 180 | return rc;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | static bithenge_node_t false_node = { BITHENGE_NODE_BOOLEAN, 1, .boolean_value = false };
|
|---|
| 184 | static bithenge_node_t true_node = { BITHENGE_NODE_BOOLEAN, 1, .boolean_value = true };
|
|---|
| 185 |
|
|---|
| 186 | /** Create a boolean node.
|
|---|
| 187 | * @memberof bithenge_node_t
|
|---|
| 188 | * @param[out] out Stores the created boolean node.
|
|---|
| 189 | * @param value The value for the node to hold.
|
|---|
| 190 | * @return EOK on success or an error code from errno.h. */
|
|---|
| 191 | int bithenge_new_boolean_node(bithenge_node_t **out, bool value)
|
|---|
| 192 | {
|
|---|
| 193 | assert(out);
|
|---|
| 194 | *out = value ? &true_node : &false_node;
|
|---|
| 195 | (*out)->refs++;
|
|---|
| 196 | return EOK;
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | /** Create an integer node.
|
|---|
| 200 | * @memberof bithenge_node_t
|
|---|
| 201 | * @param[out] out Stores the created integer node.
|
|---|
| 202 | * @param value The value for the node to hold.
|
|---|
| 203 | * @return EOK on success or an error code from errno.h. */
|
|---|
| 204 | int bithenge_new_integer_node(bithenge_node_t **out, bithenge_int_t value)
|
|---|
| 205 | {
|
|---|
| 206 | assert(out);
|
|---|
| 207 | bithenge_node_t *self = malloc(sizeof(*self));
|
|---|
| 208 | if (!self)
|
|---|
| 209 | return ENOMEM;
|
|---|
| 210 | self->type = BITHENGE_NODE_INTEGER;
|
|---|
| 211 | self->refs = 1;
|
|---|
| 212 | self->integer_value = value;
|
|---|
| 213 | *out = self;
|
|---|
| 214 | return EOK;
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | /** Create a string node.
|
|---|
| 218 | * @memberof bithenge_node_t
|
|---|
| 219 | * @param[out] out Stores the created string node. On error, this is unchanged.
|
|---|
| 220 | * @param value The value for the node to hold.
|
|---|
| 221 | * @param needs_free Whether the string should be freed when the node is
|
|---|
| 222 | * destroyed.
|
|---|
| 223 | * @return EOK on success or an error code from errno.h. */
|
|---|
| 224 | int bithenge_new_string_node(bithenge_node_t **out, const char *value, bool needs_free)
|
|---|
| 225 | {
|
|---|
| 226 | assert(out);
|
|---|
| 227 | bithenge_node_t *self = malloc(sizeof(*self));
|
|---|
| 228 | if (!self)
|
|---|
| 229 | return ENOMEM;
|
|---|
| 230 | self->type = BITHENGE_NODE_STRING;
|
|---|
| 231 | self->refs = 1;
|
|---|
| 232 | self->string_value.ptr = value;
|
|---|
| 233 | self->string_value.needs_free = needs_free;
|
|---|
| 234 | *out = self;
|
|---|
| 235 | return EOK;
|
|---|
| 236 | }
|
|---|
| 237 |
|
|---|
| 238 | /** Check whether the contents of two nodes are equal. Does not yet work for
|
|---|
| 239 | * internal nodes.
|
|---|
| 240 | * @memberof bithenge_node_t
|
|---|
| 241 | * @param a, b Nodes to compare.
|
|---|
| 242 | * @return Whether the nodes are equal. If an error occurs, returns false.
|
|---|
| 243 | * @todo Add support for internal nodes.
|
|---|
| 244 | */
|
|---|
| 245 | bool bithenge_node_equal(bithenge_node_t *a, bithenge_node_t *b)
|
|---|
| 246 | {
|
|---|
| 247 | if (a->type != b->type)
|
|---|
| 248 | return false;
|
|---|
| 249 | switch (a->type) {
|
|---|
| 250 | case BITHENGE_NODE_INTERNAL:
|
|---|
| 251 | return false;
|
|---|
| 252 | case BITHENGE_NODE_BOOLEAN:
|
|---|
| 253 | return a->boolean_value == b->boolean_value;
|
|---|
| 254 | case BITHENGE_NODE_INTEGER:
|
|---|
| 255 | return a->integer_value == b->integer_value;
|
|---|
| 256 | case BITHENGE_NODE_STRING:
|
|---|
| 257 | return !str_cmp(a->string_value.ptr, b->string_value.ptr);
|
|---|
| 258 | case BITHENGE_NODE_BLOB:
|
|---|
| 259 | return bithenge_blob_equal(bithenge_node_as_blob(a),
|
|---|
| 260 | bithenge_node_as_blob(b));
|
|---|
| 261 | }
|
|---|
| 262 | return false;
|
|---|
| 263 | }
|
|---|
| 264 |
|
|---|
| 265 | /** @}
|
|---|
| 266 | */
|
|---|