diff options
author | David Robillard <d@drobilla.net> | 2018-05-12 22:44:36 +0200 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2018-05-27 21:10:21 +0200 |
commit | 3e28f23571213b3d92001e9e9aa6d6fe23073573 (patch) | |
tree | 96a547ebabb3a2b7133c932de12008382c6c576c /src/stack.h | |
parent | c4b5d8a5f6973952b510ebd321cbac8aa5fb5267 (diff) | |
download | serd-3e28f23571213b3d92001e9e9aa6d6fe23073573.tar.gz serd-3e28f23571213b3d92001e9e9aa6d6fe23073573.tar.bz2 serd-3e28f23571213b3d92001e9e9aa6d6fe23073573.zip |
Simplify stack management by popping in bulk at higher levels
Since all memory used by the reader is POD in the stack, there is no benefit to
forcing code to explicitly pop everything pushed to the stack, since any
function can record an offset and pop back down to it regardless of what its
callers pushed if it knows that it does not need those items.
Diffstat (limited to 'src/stack.h')
-rw-r--r-- | src/stack.h | 33 |
1 files changed, 11 insertions, 22 deletions
diff --git a/src/stack.h b/src/stack.h index 3848af66..466a6027 100644 --- a/src/stack.h +++ b/src/stack.h @@ -20,6 +20,7 @@ #include <assert.h> #include <stddef.h> #include <stdlib.h> +#include <string.h> /** An offset to start the stack at. Note 0 is reserved for NULL. */ #define SERD_STACK_BOTTOM sizeof(void*) @@ -75,40 +76,28 @@ serd_stack_pop(SerdStack* stack, size_t n_bytes) stack->size -= n_bytes; } +static inline void +serd_stack_pop_to(SerdStack* stack, size_t n_bytes) +{ + assert(stack->size >= n_bytes); + stack->size = n_bytes; +} + static inline void* serd_stack_push_aligned(SerdStack* stack, size_t n_bytes, size_t align) { - // Push one byte to ensure space for a pad count - if (!serd_stack_push(stack, 1)) { - return NULL; - } - // Push padding if necessary const uint8_t pad = align - stack->size % align; if (pad > 0) { - if (!serd_stack_push(stack, pad)) { + void* padding = serd_stack_push(stack, pad); + if (!padding) { return NULL; } + memset(padding, 0, pad); } - // Set top of stack to pad count so we can properly pop later - stack->buf[stack->size - 1] = pad; - // Push requested space at aligned location return serd_stack_push(stack, n_bytes); } -static inline void -serd_stack_pop_aligned(SerdStack* stack, size_t n_bytes) -{ - // Pop requested space down to aligned location - serd_stack_pop(stack, n_bytes); - - // Get amount of padding from top of stack - const uint8_t pad = stack->buf[stack->size - 1]; - - // Pop padding and pad count - serd_stack_pop(stack, pad + 1); -} - #endif // SERD_STACK_H |