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/stack.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/stack.h')
-rw-r--r-- | src/stack.h | 21 |
1 files changed, 11 insertions, 10 deletions
diff --git a/src/stack.h b/src/stack.h index 94e091a1..c11970c1 100644 --- a/src/stack.h +++ b/src/stack.h @@ -47,14 +47,14 @@ serd_stack_free(ZixAllocator* const allocator, SerdStack* stack) static inline void* serd_stack_push(SerdStack* stack, size_t n_bytes) { - const size_t new_size = stack->size + n_bytes; + const size_t old_size = stack->size; + const size_t new_size = old_size + n_bytes; if (stack->buf_size < new_size) { return NULL; } - char* const ret = (stack->buf + stack->size); - stack->size = new_size; - return ret; + stack->size = new_size; + return stack->buf + old_size; } static inline void @@ -73,20 +73,21 @@ serd_stack_pop_to(SerdStack* stack, size_t n_bytes) } static inline void* -serd_stack_push_aligned(SerdStack* stack, size_t n_bytes, size_t align) +serd_stack_push_pad(SerdStack* stack, size_t align) { // Push padding if necessary - const size_t pad = align - stack->size % align; - if (pad > 0) { - void* padding = serd_stack_push(stack, pad); + const size_t leftovers = stack->size % align; + if (leftovers) { + const size_t pad = align - leftovers; + void* const padding = serd_stack_push(stack, pad); if (!padding) { return NULL; } memset(padding, 0, pad); + return padding; } - // Push requested space at aligned location - return serd_stack_push(stack, n_bytes); + return stack->buf + stack->size; } #endif // SERD_SRC_STACK_H |