From 3a81615601bf37ffc4f7b570030709ac0782e2ed Mon Sep 17 00:00:00 2001 From: peter Date: Mon, 10 Aug 2026 09:18:23 +0000 Subject: Avoid calling malloc/free in the library code. FossilOrigin-Name: 3e7c2c1b4beda2a729c880caf1a41c441e0a8387d40b666ac7163eb1346653b5 --- src/memory.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/memory.c (limited to 'src/memory.c') diff --git a/src/memory.c b/src/memory.c new file mode 100644 index 0000000..c2216b2 --- /dev/null +++ b/src/memory.c @@ -0,0 +1,43 @@ +#include + +Memory +memory_init(uint8_t *data , size_t size) +{ + Memory mem = { + .data = data, + .size = size, + .offset = 0, + }; + return mem; +} + +static void * +alloc(Memory *m, size_t size, size_t align) +{ + uintptr_t ptr = (uintptr_t)m->data; + size_t alloc_size = size; + if((ptr % align) != 0) + alloc_size += align - (ptr % align); + + void *p = NULL; + if((m->size - m->offset) >= alloc_size){ + p = m->data+m->offset+alloc_size-size; + m->offset += alloc_size; + memset(p, 0, size); + } + return p; +} + +Aplwc * +memory_alloc_aplwc(Memory *m) +{ + Aplwc *p = alloc(m, sizeof(Aplwc), alignof(Aplwc)); + return p; +} + +void +memory_free_aplwc(Memory *m, Aplwc *p) +{ + UNUSED_PARAMETER(m); + UNUSED_PARAMETER(p); +} -- cgit v1.2.3