aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2018-10-28 14:00:47 +0100
committerDavid Robillard <d@drobilla.net>2018-11-25 22:12:47 +0100
commit01ce25ac77fb1d8109cb45796ad9b4da1f9e8349 (patch)
treea85bab75c089bd27488922e5f36885d248d48181 /src
parent6a95fc336970abdbc9583e6da55ae6c244fc6142 (diff)
downloadserd-01ce25ac77fb1d8109cb45796ad9b4da1f9e8349.tar.gz
serd-01ce25ac77fb1d8109cb45796ad9b4da1f9e8349.tar.bz2
serd-01ce25ac77fb1d8109cb45796ad9b4da1f9e8349.zip
Add SerdNodes class for storing a cache of nodes
Diffstat (limited to 'src')
-rw-r--r--src/node.c2
-rw-r--r--src/node.h1
-rw-r--r--src/nodes.c137
3 files changed, 139 insertions, 1 deletions
diff --git a/src/node.c b/src/node.c
index 9e437c5e..dc19c2c6 100644
--- a/src/node.c
+++ b/src/node.c
@@ -96,7 +96,7 @@ serd_node_check_padding(const SerdNode* node)
#endif
}
-static size_t
+size_t
serd_node_total_size(const SerdNode* node)
{
const size_t len = sizeof(SerdNode) + serd_node_pad_size(node->n_bytes);
diff --git a/src/node.h b/src/node.h
index dec63823..c5d0e56b 100644
--- a/src/node.h
+++ b/src/node.h
@@ -42,6 +42,7 @@ serd_node_buffer_c(const SerdNode* node)
SerdNode* serd_node_malloc(size_t n_bytes, SerdNodeFlags flags, SerdType type);
void serd_node_set(SerdNode** dst, const SerdNode* src);
+size_t serd_node_total_size(const SerdNode* node);
void serd_node_zero_pad(SerdNode* node);
SerdNode* serd_new_resolved_uri_i(const char* str, const SerdURI* base);
diff --git a/src/nodes.c b/src/nodes.c
new file mode 100644
index 00000000..7a200c9f
--- /dev/null
+++ b/src/nodes.c
@@ -0,0 +1,137 @@
+/*
+ Copyright 2011-2018 David Robillard <http://drobilla.net>
+
+ Permission to use, copy, modify, and/or distribute this software for any
+ purpose with or without fee is hereby granted, provided that the above
+ copyright notice and this permission notice appear in all copies.
+
+ THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+*/
+
+#include "node.h"
+
+#include "zix/common.h"
+#include "zix/digest.h"
+#include "zix/hash.h"
+
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+
+typedef struct {
+ size_t refs;
+ SerdNode* node;
+} NodesEntry;
+
+typedef struct {
+ size_t refs;
+ const SerdNode* node;
+} NodesSearchKey;
+
+struct SerdNodesImpl {
+ ZixHash* hash;
+};
+
+static uint32_t
+nodes_hash(const void* n)
+{
+ const SerdNode* node = ((const NodesEntry*)n)->node;
+ uint32_t hash = zix_digest_start();
+ hash = zix_digest_add(hash, node, serd_node_total_size(node));
+ return hash;
+}
+
+static bool
+nodes_equal(const void* a, const void* b)
+{
+ const SerdNode* a_node = ((const NodesEntry*)a)->node;
+ const SerdNode* b_node = ((const NodesEntry*)b)->node;
+ const size_t a_size = serd_node_total_size(a_node);
+ const size_t b_size = serd_node_total_size(b_node);
+ return ((a_node == b_node) ||
+ (a_size == b_size && !memcmp(a_node, b_node, a_size)));
+}
+
+static void
+free_entry(void* value, void* user_data)
+{
+ NodesEntry* entry = (NodesEntry*)value;
+ serd_node_free(entry->node);
+}
+
+SerdNodes*
+serd_nodes_new(void)
+{
+ SerdNodes* nodes = (SerdNodes*)calloc(1, sizeof(SerdNodes));
+ nodes->hash = zix_hash_new(nodes_hash, nodes_equal, sizeof(NodesEntry));
+ return nodes;
+}
+
+void
+serd_nodes_free(SerdNodes* nodes)
+{
+ zix_hash_foreach(nodes->hash, free_entry, nodes);
+ zix_hash_free(nodes->hash);
+ free(nodes);
+}
+
+const SerdNode*
+serd_nodes_intern(SerdNodes* nodes, const SerdNode* node)
+{
+ if (!node) {
+ return NULL;
+ }
+
+ NodesSearchKey key = { 1, node };
+ NodesEntry* inserted = NULL;
+ switch (zix_hash_insert(nodes->hash, &key, (const void**)&inserted)) {
+ case ZIX_STATUS_EXISTS:
+ assert(serd_node_equals(inserted->node, node));
+ ++inserted->refs;
+ break;
+ case ZIX_STATUS_SUCCESS:
+ inserted->node = serd_node_copy(node);
+ default: break;
+ }
+
+ return inserted ? inserted->node : NULL;
+}
+
+const SerdNode*
+serd_nodes_manage(SerdNodes* nodes, SerdNode* node)
+{
+ if (!node) {
+ return NULL;
+ }
+
+ NodesSearchKey key = { 1, node };
+ NodesEntry* inserted = NULL;
+ switch (zix_hash_insert(nodes->hash, &key, (const void**)&inserted)) {
+ case ZIX_STATUS_EXISTS:
+ assert(serd_node_equals(inserted->node, node));
+ serd_node_free(node);
+ ++inserted->refs;
+ break;
+ default: break;
+ }
+
+ return inserted ? inserted->node : NULL;
+}
+
+void
+serd_nodes_deref(SerdNodes* nodes, const SerdNode* node)
+{
+ NodesSearchKey key = { 1, node };
+ NodesEntry* entry = (NodesEntry*)zix_hash_find(nodes->hash, &key);
+ if (entry && --entry->refs == 0) {
+ SerdNode* const intern_node = entry->node;
+ zix_hash_remove(nodes->hash, entry);
+ serd_node_free(intern_node);
+ }
+}