From 800f2d0d4960061a1739a36d8b608a13896b321e Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 28 Oct 2018 14:00:47 +0100 Subject: Add SerdNodes class for storing a cache of nodes --- include/serd/serd.h | 60 +++++++++++++++++++++ meson.build | 5 +- src/node.c | 1 - src/node.h | 4 ++ src/nodes.c | 142 ++++++++++++++++++++++++++++++++++++++++++++++++++ test/meson.build | 1 + test/test_free_null.c | 1 + test/test_nodes.c | 93 +++++++++++++++++++++++++++++++++ 8 files changed, 305 insertions(+), 2 deletions(-) create mode 100644 src/nodes.c create mode 100644 test/test_nodes.c diff --git a/include/serd/serd.h b/include/serd/serd.h index 09017175..7c1ff057 100644 --- a/include/serd/serd.h +++ b/include/serd/serd.h @@ -84,6 +84,9 @@ extern "C" { /// Global library state typedef struct SerdWorldImpl SerdWorld; +/// Hashing node container for interning and simplified memory management +typedef struct SerdNodesImpl SerdNodes; + /// A subject, predicate, and object, with optional graph context typedef struct SerdStatementImpl SerdStatement; @@ -1337,6 +1340,63 @@ SERD_API SerdStatus serd_writer_finish(SerdWriter* SERD_NONNULL writer); +/** + @} + @defgroup serd_nodes Nodes + @{ +*/ + +/// Create a new node set +SERD_API +SerdNodes* SERD_ALLOCATED +serd_nodes_new(void); + +/** + Free `nodes` and all nodes that are stored in it. + + Note that this invalidates any pointers previously returned from + `serd_nodes_intern()` or `serd_nodes_manage()` calls on `nodes`. +*/ +SERD_API +void +serd_nodes_free(SerdNodes* SERD_NULLABLE nodes); + +/** + Intern `node`. + + Multiple calls with equivalent nodes will return the same pointer. + + @return A node that is different than, but equivalent to, `node`. +*/ +SERD_API +const SerdNode* SERD_ALLOCATED +serd_nodes_intern(SerdNodes* SERD_NONNULL nodes, + const SerdNode* SERD_NULLABLE node); + +/** + Manage `node`. + + Like `serd_nodes_intern`, but takes ownership of `node`, freeing it and + returning a previously interned/managed equivalent node if necessary. + + @return A node that is equivalent to `node`. +*/ +SERD_API +const SerdNode* SERD_ALLOCATED +serd_nodes_manage(SerdNodes* SERD_NONNULL nodes, SerdNode* SERD_NULLABLE node); + +/** + Dereference `node`. + + Decrements the reference count of `node`, and frees the internally stored + equivalent node if this was the last reference. Does nothing if no node + equivalent to `node` is stored in `nodes`. +*/ +SERD_API +void +serd_nodes_deref(SerdNodes* SERD_NONNULL nodes, + const SerdNode* SERD_NONNULL node); + /** @} @defgroup serd_statement Statement diff --git a/meson.build b/meson.build index 16fce995..078b36a6 100644 --- a/meson.build +++ b/meson.build @@ -98,6 +98,7 @@ sources = [ 'src/env.c', 'src/n3.c', 'src/node.c', + 'src/nodes.c', 'src/reader.c', 'src/sink.c', 'src/statement.c', @@ -107,6 +108,8 @@ sources = [ 'src/uri.c', 'src/world.c', 'src/writer.c', + 'src/zix/digest.c', + 'src/zix/hash.c', ] # System libraries @@ -140,7 +143,7 @@ libserd = build_target( versioned_name, sources, version: meson.project_version(), - include_directories: include_directories(['include']), + include_directories: include_directories('include', 'src'), c_args: c_warnings + library_args, dependencies: [exess_dep, m_dep], gnu_symbol_visibility: 'hidden', diff --git a/src/node.c b/src/node.c index f071fc97..0322dd64 100644 --- a/src/node.c +++ b/src/node.c @@ -85,7 +85,6 @@ serd_node_check_padding(const SerdNode* node) #endif } -static SERD_PURE_FUNC size_t serd_node_total_size(const SerdNode* node) { diff --git a/src/node.h b/src/node.h index 3d01bae7..04452c98 100644 --- a/src/node.h +++ b/src/node.h @@ -47,6 +47,10 @@ void serd_node_set(SerdNode* SERD_NONNULL* SERD_NONNULL dst, const SerdNode* SERD_NONNULL src); +SERD_PURE_FUNC +size_t +serd_node_total_size(const SerdNode* SERD_NONNULL node); + void serd_node_zero_pad(SerdNode* SERD_NONNULL node); diff --git a/src/nodes.c b/src/nodes.c new file mode 100644 index 00000000..e60834bc --- /dev/null +++ b/src/nodes.c @@ -0,0 +1,142 @@ +/* + Copyright 2011-2020 David Robillard + + 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 "serd/serd.h" +#include "zix/common.h" +#include "zix/digest.h" +#include "zix/hash.h" + +#include +#include +#include +#include +#include + +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; + + return zix_digest_add_64( + zix_digest_start(), node, serd_node_total_size(node)); +} + +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) +{ + (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) +{ + if (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; + + const ZixStatus st = zix_hash_insert(nodes->hash, &key, (void**)&inserted); + if (st == ZIX_STATUS_SUCCESS) { + inserted->node = serd_node_copy(node); + } else if (st == ZIX_STATUS_EXISTS) { + assert(serd_node_equals(inserted->node, node)); + ++inserted->refs; + } + + 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; + + const ZixStatus st = zix_hash_insert(nodes->hash, &key, (void**)&inserted); + if (st == ZIX_STATUS_EXISTS) { + assert(serd_node_equals(inserted->node, node)); + serd_node_free(node); + ++inserted->refs; + } + + 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); + } +} diff --git a/test/meson.build b/test/meson.build index e46e75ab..94477318 100644 --- a/test/meson.build +++ b/test/meson.build @@ -7,6 +7,7 @@ unit_tests = [ 'env', 'free_null', 'node', + 'nodes', 'read_chunk', 'reader_writer', 'string', diff --git a/test/test_free_null.c b/test/test_free_null.c index f629a285..62615a02 100644 --- a/test/test_free_null.c +++ b/test/test_free_null.c @@ -31,6 +31,7 @@ main(void) serd_sink_free(NULL); serd_reader_free(NULL); serd_writer_free(NULL); + serd_nodes_free(NULL); serd_cursor_free(NULL); return 0; diff --git a/test/test_nodes.c b/test/test_nodes.c new file mode 100644 index 00000000..f515edd0 --- /dev/null +++ b/test/test_nodes.c @@ -0,0 +1,93 @@ +/* + Copyright 2018 David Robillard + + 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. +*/ + +#undef NDEBUG + +#include "serd/serd.h" + +#include +#include + +static int +test_intern(void) +{ + SerdNodes* nodes = serd_nodes_new(); + SerdNode* node = serd_new_string(SERD_STATIC_STRING("node")); + + const SerdNode* interned1 = serd_nodes_intern(nodes, node); + assert(serd_node_equals(node, interned1)); + + const SerdNode* interned2 = serd_nodes_intern(nodes, node); + assert(serd_node_equals(node, interned2)); + assert(interned1 == interned2); + + serd_node_free(node); + serd_nodes_free(nodes); + return 0; +} + +static int +test_manage(void) +{ + SerdNodes* nodes = serd_nodes_new(); + SerdNode* node = serd_new_string(SERD_STATIC_STRING("node")); + + assert(!serd_nodes_manage(nodes, NULL)); + + const SerdNode* managed1 = serd_nodes_manage(nodes, node); + assert(managed1 == node); + + SerdNode* equal = serd_new_string(SERD_STATIC_STRING("node")); + const SerdNode* managed2 = serd_nodes_manage(nodes, equal); + assert(managed2 == node); + + serd_nodes_free(nodes); + return 0; +} + +static int +test_deref(void) +{ + SerdNodes* nodes = serd_nodes_new(); + const SerdNode* managed = + serd_nodes_manage(nodes, serd_new_string(SERD_STATIC_STRING("node"))); + + serd_nodes_deref(nodes, managed); + + SerdNode* node = serd_new_string(SERD_STATIC_STRING("node")); + const SerdNode* interned = serd_nodes_intern(nodes, node); + + assert(interned != node); + + serd_node_free(node); + serd_nodes_free(nodes); + return 0; +} + +int +main(void) +{ + typedef int (*TestFunc)(void); + + const TestFunc tests[] = {test_intern, test_manage, test_deref, NULL}; + + int ret = 0; + for (const TestFunc* t = tests; *t; ++t) { + ret += (*t)(); + } + + return ret; +} -- cgit v1.2.1