diff options
author | David Robillard <d@drobilla.net> | 2023-12-01 20:39:44 -0500 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2023-12-02 18:49:08 -0500 |
commit | 02d56e83931e53e1cde57247c64d56fda3804f77 (patch) | |
tree | 2d1ac467bc56f4f4f3570497427be32d7e36bd1a /src/node.h | |
parent | d094448c095a59117febc8bd4687df071ce9759a (diff) | |
download | serd-02d56e83931e53e1cde57247c64d56fda3804f77.tar.gz serd-02d56e83931e53e1cde57247c64d56fda3804f77.tar.bz2 serd-02d56e83931e53e1cde57247c64d56fda3804f77.zip |
[WIP] Tighten up reader node management
[WIP] Broken on 32-bit
This makes the reader stack manipulations stricter, to make the code more
regular and avoid redundant work and bad cache activity. Now, functions that
push node headers and their bodies are responsible for (more or less)
immediately pushing any trailing null bytes required for termination and
alignment.
This makes the writes to the node in the stack more local, ensures nodes are
terminated as early as possible (to reduce the risk of using non-terminated
strings), and avoids the need to calculate aligned stack allocations.
Diffstat (limited to 'src/node.h')
-rw-r--r-- | src/node.h | 21 |
1 files changed, 10 insertions, 11 deletions
@@ -25,14 +25,19 @@ static const size_t serd_node_align = 2 * sizeof(uint64_t); #if SIZE_MAX == UINT64_MAX +/** + Pad a node string length to the number of bytes it will occupy in a node. + + This returns a size that is at least one larger than `n_bytes` (to ensure + the string is null terminated), but possibly even larger (to align the node + size). +*/ static inline size_t serd_node_pad_length(const size_t n_bytes) { - const size_t align = sizeof(SerdNode); - - assert((align & (align - 1U)) == 0U); + assert((serd_node_align & (serd_node_align - 1U)) == 0U); - return (n_bytes + align + 2U) & ~(align - 1U); + return (n_bytes + serd_node_align) & ~(serd_node_align - 1U); } #else @@ -40,10 +45,7 @@ serd_node_pad_length(const size_t n_bytes) 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; + return (n_bytes + sizeof(SerdNode)) / sizeof(SerdNode) * sizeof(SerdNode); } #endif @@ -101,7 +103,4 @@ serd_node_set(ZixAllocator* ZIX_NULLABLE allocator, ZIX_PURE_FUNC size_t serd_node_total_size(const SerdNode* ZIX_NONNULL node); -void -serd_node_zero_pad(SerdNode* ZIX_NONNULL node); - #endif // SERD_SRC_NODE_H |