diff options
author | David Robillard <d@drobilla.net> | 2023-05-05 09:43:57 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2023-12-02 18:49:08 -0500 |
commit | 4711fdf527f416faee8ff19e15f050d4b48dcfb2 (patch) | |
tree | 6b18712ec44cce5713ddef1a21aec5f12651d901 /src/node.h | |
parent | 248a874d7425749d29cf900a1c3783c624ea8d8c (diff) | |
download | serd-4711fdf527f416faee8ff19e15f050d4b48dcfb2.tar.gz serd-4711fdf527f416faee8ff19e15f050d4b48dcfb2.tar.bz2 serd-4711fdf527f416faee8ff19e15f050d4b48dcfb2.zip |
[WIP] Generalize node construction API
Diffstat (limited to 'src/node.h')
-rw-r--r-- | src/node.h | 66 |
1 files changed, 50 insertions, 16 deletions
@@ -4,12 +4,13 @@ #ifndef SERD_SRC_NODE_H #define SERD_SRC_NODE_H -#include "exess/exess.h" #include "serd/memory.h" #include "serd/node.h" #include "serd/status.h" +#include "serd/write_result.h" #include "zix/attributes.h" +#include <assert.h> #include <stdbool.h> #include <stddef.h> #include <stdint.h> @@ -22,19 +23,57 @@ struct SerdNodeImpl { static const size_t serd_node_align = 2 * sizeof(uint64_t); -static inline char* ZIX_NONNULL +#if SIZE_MAX == UINT64_MAX + +static inline size_t +serd_node_pad_length(const size_t n_bytes) +{ + const size_t align = sizeof(SerdNode); + + assert((align & (align - 1U)) == 0U); + + return (n_bytes + align + 2U) & ~(align - 1U); +} + +#else + +static inline size_t +serd_node_pad_length(const size_t n_bytes) +{ + const size_t pad = sizeof(SerdNode) - (n_bytes + 2) % sizeof(SerdNode); + const size_t size = n_bytes + 2 + pad; + assert(size % sizeof(SerdNode) == 0); + return size; +} + +#endif + +ZIX_CONST_FUNC static inline char* ZIX_NONNULL serd_node_buffer(SerdNode* ZIX_NONNULL node) { return (char*)(node + 1); } -static inline const char* ZIX_NONNULL +ZIX_PURE_FUNC static inline const char* ZIX_NONNULL serd_node_buffer_c(const SerdNode* ZIX_NONNULL node) { return (const char*)(node + 1); } -static inline const char* ZIX_NONNULL +ZIX_PURE_FUNC static inline SerdNode* ZIX_NONNULL +serd_node_meta(SerdNode* const ZIX_NONNULL node) +{ + return node + 1 + (serd_node_pad_length(node->length) / sizeof(SerdNode)); +} + +ZIX_PURE_FUNC static inline const SerdNode* ZIX_NONNULL +serd_node_meta_c(const SerdNode* const ZIX_NONNULL node) +{ + assert(node->flags & (SERD_HAS_DATATYPE | SERD_HAS_LANGUAGE)); + return node + 1 + (serd_node_pad_length(node->length) / sizeof(SerdNode)); +} + +ZIX_CONST_FUNC static inline const char* ZIX_NONNULL serd_node_string_i(const SerdNode* const ZIX_NONNULL node) { return (const char*)(node + 1); @@ -47,11 +86,12 @@ serd_node_pattern_match(const SerdNode* ZIX_NULLABLE a, return !a || !b || serd_node_equals(a, b); } -SerdNode* ZIX_ALLOCATED -serd_node_malloc(SerdAllocator* ZIX_NULLABLE allocator, - size_t length, - SerdNodeFlags flags, - SerdNodeType type); +ZIX_MALLOC_FUNC SerdNode* ZIX_ALLOCATED +serd_node_malloc(SerdAllocator* ZIX_NULLABLE allocator, size_t size); + +ZIX_MALLOC_FUNC SerdNode* ZIX_ALLOCATED +serd_node_try_malloc(SerdAllocator* ZIX_NULLABLE allocator, + SerdWriteResult result); SerdStatus serd_node_set(SerdAllocator* ZIX_NULLABLE allocator, @@ -59,15 +99,9 @@ serd_node_set(SerdAllocator* ZIX_NULLABLE allocator, const SerdNode* ZIX_NONNULL src); ZIX_PURE_FUNC size_t -serd_node_total_size(const SerdNode* ZIX_NULLABLE node); +serd_node_total_size(const SerdNode* ZIX_NONNULL node); void serd_node_zero_pad(SerdNode* ZIX_NONNULL node); -ExessResult -serd_node_get_value_as(const SerdNode* ZIX_NONNULL node, - ExessDatatype value_type, - size_t value_size, - void* ZIX_NONNULL value); - #endif // SERD_SRC_NODE_H |