summaryrefslogtreecommitdiff
path: root/src/memory.c
diff options
context:
space:
mode:
authorpeter <peter@pmikkelsen.com>2026-08-10 09:18:23 +0000
committerpeter <peter@pmikkelsen.com>2026-08-10 09:18:23 +0000
commit3a81615601bf37ffc4f7b570030709ac0782e2ed (patch)
treefc5a4d59b9bb62521d37a878c1fa7bb34d3d2bb5 /src/memory.c
parente30299f2cd59c46c2d733e38139da96c93f9acf4 (diff)
Avoid calling malloc/free in the library code.
FossilOrigin-Name: 3e7c2c1b4beda2a729c880caf1a41c441e0a8387d40b666ac7163eb1346653b5
Diffstat (limited to 'src/memory.c')
-rw-r--r--src/memory.c43
1 files changed, 43 insertions, 0 deletions
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 <aplwc_internal.h>
+
+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);
+}