From f5d445afa10ed7ba03b0ea9a5274f11904220700 Mon Sep 17 00:00:00 2001 From: peter Date: Thu, 20 Aug 2026 18:51:01 +0000 Subject: Move aplwc-repl into its own directory, and likewise for the aplwc library FossilOrigin-Name: 7e6b710fae494fadfe06bed16ae10bdda0f377067cb9411ed2b8bfc682d2fcbc --- src/lib/memory.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/lib/memory.c (limited to 'src/lib/memory.c') diff --git a/src/lib/memory.c b/src/lib/memory.c new file mode 100644 index 0000000..187137e --- /dev/null +++ b/src/lib/memory.c @@ -0,0 +1,55 @@ +#include "aplwc_internal.h" + +struct memory +memory_init(uint8_t *data , size_t size) +{ + struct memory mem; + + mem.data = data; + mem.size = size; + mem.offset = 0; + return mem; +} + +static void * +alloc(struct memory *m, size_t size, size_t align) +{ + uintptr_t ptr; + size_t alloc_size, adjust; + void *p; + + ptr = (uintptr_t)m->data; + alloc_size = size; + + if(NEVER(align == 0)) + return NULL; + adjust = ptr % align; + + if(adjust) + alloc_size += align - adjust; + + if((m->size - m->offset) >= alloc_size){ + p = m->data+m->offset+alloc_size-size; + m->offset += alloc_size; + memset(p, 0, size); + }else + p = NULL; + + return p; +} + +struct aplwc * +memory_alloc_aplwc(struct memory *m) +{ + struct aplwc *p; + + p = alloc(m, sizeof(struct aplwc), alignof(struct aplwc)); + return p; +} + +void +memory_free_aplwc(struct memory *m, struct aplwc *p) +{ + UNUSED_PARAMETER(m); + UNUSED_PARAMETER(p); +} -- cgit v1.2.3