aboutsummaryrefslogtreecommitdiffstats
path: root/src/stack.h
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2018-11-17 21:01:59 +0100
committerDavid Robillard <d@drobilla.net>2023-12-02 18:49:07 -0500
commit2d43bf2b5c301ca1e4766b5048650ca1e29e38c0 (patch)
tree46d05984f9ece93ce5c5d1fabd2b5d849cc835bf /src/stack.h
parent31ef2936a1549cdc125c387a8f541d8b70e60c21 (diff)
downloadserd-2d43bf2b5c301ca1e4766b5048650ca1e29e38c0.tar.gz
serd-2d43bf2b5c301ca1e4766b5048650ca1e29e38c0.tar.bz2
serd-2d43bf2b5c301ca1e4766b5048650ca1e29e38c0.zip
Align nodes on the reader stack
Diffstat (limited to 'src/stack.h')
-rw-r--r--src/stack.h19
1 files changed, 8 insertions, 11 deletions
diff --git a/src/stack.h b/src/stack.h
index cdf2b0f6..20bd561b 100644
--- a/src/stack.h
+++ b/src/stack.h
@@ -4,15 +4,13 @@
#ifndef SERD_SRC_STACK_H
#define SERD_SRC_STACK_H
+#include "system.h"
+
#include <assert.h>
#include <stdbool.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*)
-
/** A dynamic stack in memory. */
typedef struct {
char* buf; ///< Stack memory
@@ -20,23 +18,22 @@ typedef struct {
size_t size; ///< Conceptual size of stack in buf
} SerdStack;
-/** An offset to start the stack at. Note 0 is reserved for NULL. */
-#define SERD_STACK_BOTTOM sizeof(void*)
-
static inline SerdStack
-serd_stack_new(size_t size)
+serd_stack_new(size_t size, size_t align)
{
+ const size_t aligned_size = (size + (align - 1)) / align * align;
+
SerdStack stack;
- stack.buf = (char*)calloc(size, 1);
+ stack.buf = (char*)serd_calloc_aligned(align, aligned_size);
stack.buf_size = size;
- stack.size = SERD_STACK_BOTTOM;
+ stack.size = align; // 0 is reserved for null
return stack;
}
static inline void
serd_stack_free(SerdStack* stack)
{
- free(stack->buf);
+ serd_free_aligned(stack->buf);
stack->buf = NULL;
stack->buf_size = 0;
stack->size = 0;