aboutsummaryrefslogtreecommitdiffstats
path: root/src/stack.h
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2021-01-13 23:47:44 +0100
committerDavid Robillard <d@drobilla.net>2023-12-02 16:27:02 -0500
commit3fc450d7f586388135028ac83870d288834c34a1 (patch)
treea80cbe021ff2b946b1126a4b79c835d98fecbea3 /src/stack.h
parentfb95ceca3b744e47e973585aa682515365ac9eb0 (diff)
downloadserd-3fc450d7f586388135028ac83870d288834c34a1.tar.gz
serd-3fc450d7f586388135028ac83870d288834c34a1.tar.bz2
serd-3fc450d7f586388135028ac83870d288834c34a1.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. This is slightly more efficient (due to avoiding many pop calls), but also more resilient since "leaks" at deeper levels of recursion get nuked by some caller regardless of what was pushed. This should help prevent future regressions like f6437f606 (Fix memory consumption when reading documents).
Diffstat (limited to 'src/stack.h')
-rw-r--r--src/stack.h34
1 files changed, 11 insertions, 23 deletions
diff --git a/src/stack.h b/src/stack.h
index e6e46372..4592705e 100644
--- a/src/stack.h
+++ b/src/stack.h
@@ -7,8 +7,8 @@
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
-#include <stdint.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*)
@@ -68,40 +68,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 size_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] = (char)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 = (uint8_t)stack->buf[stack->size - 1];
-
- // Pop padding and pad count
- serd_stack_pop(stack, pad + 1U);
-}
-
#endif // SERD_SRC_STACK_H