diff options
author | David Robillard <d@drobilla.net> | 2021-01-13 19:34:11 +0100 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2021-03-08 23:23:05 -0500 |
commit | 2e25fe1e64f487fd91ff6f636bf3249b761ffcdc (patch) | |
tree | 73dc6d1731442acd26ea5b1f11de000c8dd31967 /src/stack.h | |
parent | e6abc21f9bde66d8f60078493354ba3713f9fcd8 (diff) | |
download | serd-2e25fe1e64f487fd91ff6f636bf3249b761ffcdc.tar.gz serd-2e25fe1e64f487fd91ff6f636bf3249b761ffcdc.tar.bz2 serd-2e25fe1e64f487fd91ff6f636bf3249b761ffcdc.zip |
Use a fixed-size reader stack
This improves performance, and makes the reader more suitable for embedded or
network-facing applications, at the cost of requiring the user to specify a
maximum stack size.
Diffstat (limited to 'src/stack.h')
-rw-r--r-- | src/stack.h | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/src/stack.h b/src/stack.h index bcf1434b..c2335b3a 100644 --- a/src/stack.h +++ b/src/stack.h @@ -66,9 +66,9 @@ serd_stack_push(SerdStack* stack, size_t n_bytes) { const size_t new_size = stack->size + n_bytes; if (stack->buf_size < new_size) { - stack->buf_size += (stack->buf_size >> 1); // *= 1.5 - stack->buf = (char*)realloc(stack->buf, stack->buf_size); + return NULL; } + char* const ret = (stack->buf + stack->size); stack->size = new_size; return ret; @@ -85,12 +85,16 @@ 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 - serd_stack_push(stack, 1); + if (!serd_stack_push(stack, 1)) { + return NULL; + } // Push padding if necessary const size_t pad = align - stack->size % align; if (pad > 0) { - serd_stack_push(stack, pad); + if (!serd_stack_push(stack, pad)) { + return NULL; + } } // Set top of stack to pad count so we can properly pop later |