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 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) (limited to 'include/serd/serd.h') 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 -- cgit v1.2.1