diff options
author | David Robillard <d@drobilla.net> | 2023-09-10 15:06:42 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2023-12-02 18:49:08 -0500 |
commit | 248a874d7425749d29cf900a1c3783c624ea8d8c (patch) | |
tree | aed59f5a484a815cd254506866e98a947858904d /include/serd/node.h | |
parent | 0bd10132c6707353dba80bd89cf0102ee7ca4e34 (diff) | |
download | serd-248a874d7425749d29cf900a1c3783c624ea8d8c.tar.gz serd-248a874d7425749d29cf900a1c3783c624ea8d8c.tar.bz2 serd-248a874d7425749d29cf900a1c3783c624ea8d8c.zip |
Add support for custom allocators
This makes it explicit in the API where memory is allocated, and allows the
user to provide a custom allocator to avoid the use of the default system
allocator for whatever reason.
Diffstat (limited to 'include/serd/node.h')
-rw-r--r-- | include/serd/node.h | 60 |
1 files changed, 42 insertions, 18 deletions
diff --git a/include/serd/node.h b/include/serd/node.h index b0b14a24..f42f07f1 100644 --- a/include/serd/node.h +++ b/include/serd/node.h @@ -5,6 +5,7 @@ #define SERD_NODE_H #include "serd/attributes.h" +#include "serd/memory.h" #include "serd/string_view.h" #include "serd/uri.h" #include "serd/write_result.h" @@ -117,13 +118,15 @@ typedef uint32_t SerdNodeFlags; to create URIs, blank nodes, CURIEs, and simple string literals. */ SERD_API SerdNode* ZIX_ALLOCATED -serd_new_token(SerdNodeType type, SerdStringView string); +serd_new_token(SerdAllocator* ZIX_NULLABLE allocator, + SerdNodeType type, + SerdStringView string); /** Create a new string literal node. */ SERD_API SerdNode* ZIX_ALLOCATED -serd_new_string(SerdStringView string); +serd_new_string(SerdAllocator* ZIX_NULLABLE allocator, SerdStringView string); /** Create a new literal node with optional datatype or language. @@ -132,6 +135,8 @@ serd_new_string(SerdStringView string); associated datatype URI or language tag, as well as control whether a literal should be written as a short or long (triple-quoted) string. + @param allocator Allocator for the returned node. + @param string The string value of the literal. @param flags Flags to describe the literal and its metadata. This must be a @@ -147,27 +152,28 @@ serd_new_string(SerdStringView string); serd_node_free(), or null if the arguments are invalid or allocation failed. */ SERD_API SerdNode* ZIX_ALLOCATED -serd_new_literal(SerdStringView string, - SerdNodeFlags flags, - SerdStringView meta); +serd_new_literal(SerdAllocator* ZIX_NULLABLE allocator, + SerdStringView string, + SerdNodeFlags flags, + SerdStringView meta); /** Create a new node from a blank node label. */ SERD_API SerdNode* ZIX_ALLOCATED -serd_new_blank(SerdStringView string); +serd_new_blank(SerdAllocator* ZIX_NULLABLE allocator, SerdStringView string); /** Create a new URI node from a parsed URI. */ SERD_API SerdNode* ZIX_ALLOCATED -serd_new_parsed_uri(SerdURIView uri); +serd_new_parsed_uri(SerdAllocator* ZIX_NULLABLE allocator, SerdURIView uri); /** Create a new URI node from a string. */ SERD_API SerdNode* ZIX_ALLOCATED -serd_new_uri(SerdStringView string); +serd_new_uri(SerdAllocator* ZIX_NULLABLE allocator, SerdStringView string); /** Create a new file URI node from a file system path and optional hostname. @@ -178,13 +184,15 @@ serd_new_uri(SerdStringView string); If `path` is relative, `hostname` is ignored. */ SERD_API SerdNode* ZIX_ALLOCATED -serd_new_file_uri(SerdStringView path, SerdStringView hostname); +serd_new_file_uri(SerdAllocator* ZIX_NULLABLE allocator, + SerdStringView path, + SerdStringView hostname); /** Create a new canonical xsd:boolean node. */ SERD_API SerdNode* ZIX_ALLOCATED -serd_new_boolean(bool b); +serd_new_boolean(SerdAllocator* ZIX_NULLABLE allocator, bool b); /** Create a new canonical xsd:decimal literal. @@ -196,11 +204,14 @@ serd_new_boolean(bool b); (a leading and/or trailing '0' will be added if necessary), for example, "1.0". It will never be in scientific notation. + @param allocator Allocator for the returned node. @param d The value for the new node. @param datatype Datatype of node, or NULL for xsd:decimal. */ SERD_API SerdNode* ZIX_ALLOCATED -serd_new_decimal(double d, const SerdNode* ZIX_NULLABLE datatype); +serd_new_decimal(SerdAllocator* ZIX_NULLABLE allocator, + double d, + const SerdNode* ZIX_NULLABLE datatype); /** Create a new canonical xsd:double literal. @@ -212,11 +223,12 @@ serd_new_decimal(double d, const SerdNode* ZIX_NULLABLE datatype); Uses the shortest possible representation that precisely describes the value, which has at most 17 significant digits (under 24 characters total). + @param allocator Allocator for the returned node. @param d Double value to write. @return A literal node with datatype xsd:double. */ SERD_API SerdNode* ZIX_ALLOCATED -serd_new_double(double d); +serd_new_double(SerdAllocator* ZIX_NULLABLE allocator, double d); /** Create a new canonical xsd:float literal. @@ -224,11 +236,12 @@ serd_new_double(double d); Uses identical formatting to serd_new_double(), except with at most 9 significant digits (under 14 characters total). + @param allocator Allocator for the returned node. @param f Float value of literal. @return A literal node with datatype xsd:float. */ SERD_API SerdNode* ZIX_ALLOCATED -serd_new_float(float f); +serd_new_float(SerdAllocator* ZIX_NULLABLE allocator, float f); /** Create a new canonical xsd:integer literal. @@ -236,10 +249,11 @@ serd_new_float(float f); The node will be an xsd:integer literal like "1234", with datatype xsd:integer. + @param allocator Allocator for the returned node. @param i Integer value of literal. */ SERD_API SerdNode* ZIX_ALLOCATED -serd_new_integer(int64_t i); +serd_new_integer(SerdAllocator* ZIX_NULLABLE allocator, int64_t i); /** Create a new canonical xsd:base64Binary literal. @@ -247,21 +261,31 @@ serd_new_integer(int64_t i); This function can be used to make a node out of arbitrary binary data, which can be decoded using serd_base64_decode(). + @param allocator Allocator for the returned node. @param buf Raw binary data to encode in node. @param size Size of `buf` in bytes. */ SERD_API SerdNode* ZIX_ALLOCATED -serd_new_base64(const void* ZIX_NONNULL buf, size_t size); +serd_new_base64(SerdAllocator* ZIX_NULLABLE allocator, + const void* ZIX_NONNULL buf, + size_t size); + +/** + Return a deep copy of `node`. -/// Return a deep copy of `node` + @param allocator Allocator for the returned node. + @param node The node to copyl +*/ SERD_API SerdNode* ZIX_ALLOCATED -serd_node_copy(const SerdNode* ZIX_NULLABLE node); +serd_node_copy(SerdAllocator* ZIX_NULLABLE allocator, + const SerdNode* ZIX_NULLABLE node); /** Free any data owned by `node`. */ SERD_API void -serd_node_free(SerdNode* ZIX_NULLABLE node); +serd_node_free(SerdAllocator* ZIX_NULLABLE allocator, + SerdNode* ZIX_NULLABLE node); /** @} |