diff options
author | David Robillard <d@drobilla.net> | 2021-04-15 10:52:11 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2023-12-02 16:27:02 -0500 |
commit | e80312a5ac9cdeaaa0d4f81e7aa258572d695c74 (patch) | |
tree | 02950f6df39488c79de5f5f2572bdd1270ae84ca /src/node.c | |
parent | c644f404054e6d68f3bb645cf93b8ad1501b8938 (diff) | |
download | serd-e80312a5ac9cdeaaa0d4f81e7aa258572d695c74.tar.gz serd-e80312a5ac9cdeaaa0d4f81e7aa258572d695c74.tar.bz2 serd-e80312a5ac9cdeaaa0d4f81e7aa258572d695c74.zip |
Align node allocations
Diffstat (limited to 'src/node.c')
-rw-r--r-- | src/node.c | 33 |
1 files changed, 27 insertions, 6 deletions
@@ -5,7 +5,9 @@ #include "base64.h" #include "string_utils.h" +#include "system.h" +#include "serd/attributes.h" #include "serd/buffer.h" #include "serd/node.h" #include "serd/string.h" @@ -29,6 +31,8 @@ # endif #endif +static const size_t serd_node_align = 2 * sizeof(uint64_t); + static size_t serd_uri_string_length(const SerdURIView* const uri) { @@ -57,12 +61,26 @@ string_sink(const void* const buf, const size_t len, void* const stream) return len; } +static size_t +serd_node_pad_size(const size_t n_bytes) +{ + const size_t pad = sizeof(SerdNode) - (n_bytes + 2) % sizeof(SerdNode); + return n_bytes + 2 + pad; +} + +static SERD_PURE_FUNC size_t +serd_node_total_size(const SerdNode* const node) +{ + return node ? (sizeof(SerdNode) + serd_node_pad_size(node->length)) : 0; +} + SerdNode* serd_node_malloc(const size_t length, const SerdNodeFlags flags, const SerdNodeType type) { - SerdNode* node = (SerdNode*)calloc(1, sizeof(SerdNode) + length + 1); + const size_t size = sizeof(SerdNode) + serd_node_pad_size(length); + SerdNode* node = (SerdNode*)serd_calloc_aligned(serd_node_align, size); node->length = 0; node->flags = flags; @@ -77,10 +95,13 @@ serd_node_set(SerdNode** const dst, const SerdNode* const src) assert(dst); assert(src); - if (!*dst || (*dst)->length < src->length) { - (*dst) = (SerdNode*)realloc(*dst, sizeof(SerdNode) + src->length + 1); + const size_t size = serd_node_total_size(src); + if (!*dst || serd_node_total_size(*dst) < size) { + serd_free_aligned(*dst); + *dst = (SerdNode*)serd_calloc_aligned(serd_node_align, size); } + assert(*dst); memcpy(*dst, src, sizeof(SerdNode) + src->length + 1); } @@ -115,8 +136,8 @@ serd_node_copy(const SerdNode* node) return NULL; } - const size_t size = sizeof(SerdNode) + node->length + 1; - SerdNode* copy = (SerdNode*)malloc(size); + const size_t size = serd_node_total_size(node); + SerdNode* copy = (SerdNode*)serd_calloc_aligned(serd_node_align, size); memcpy(copy, node, size); return copy; } @@ -437,5 +458,5 @@ serd_node_flags(const SerdNode* const node) void serd_node_free(SerdNode* const node) { - free(node); + serd_free_aligned(node); } |