summaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
authorPeter Mikkelsen <petermikkelsen10@gmail.com>2025-11-12 21:49:50 +0100
committerPeter Mikkelsen <petermikkelsen10@gmail.com>2025-11-12 21:49:50 +0100
commit7de14dbc0b642ee492fe592ccb53e8d81a4dc599 (patch)
tree1c44602db0d917a3176da535cbfa327edeec4e4a /src/core
Initial commit
Diffstat (limited to 'src/core')
-rw-r--r--src/core/Makefile.am9
-rw-r--r--src/core/defs.h12
-rw-r--r--src/core/fns.h16
-rw-r--r--src/core/init.c75
-rw-r--r--src/core/types.h14
-rw-r--r--src/core/wrappers.c27
6 files changed, 153 insertions, 0 deletions
diff --git a/src/core/Makefile.am b/src/core/Makefile.am
new file mode 100644
index 0000000..a635e5d
--- /dev/null
+++ b/src/core/Makefile.am
@@ -0,0 +1,9 @@
+AM_CPPFLAGS = -I$(top_srcdir)/src/include
+
+lib_LTLIBRARIES = libaplwc.la
+libaplwc_la_SOURCES = \
+ init.c \
+ wrappers.c \
+ defs.h \
+ fns.h \
+ types.h
diff --git a/src/core/defs.h b/src/core/defs.h
new file mode 100644
index 0000000..5cb9803
--- /dev/null
+++ b/src/core/defs.h
@@ -0,0 +1,12 @@
+#ifndef DEFS_H
+#define DEFS_H
+
+#include <stdalign.h>
+#include <aplwc.h>
+#include "types.h"
+
+#define WS_START_SIZE (64*1024)
+#define WS_ALIGNMENT 64
+#define APLWC_ALIGNMENT alignof(APLWC)
+
+#endif /* DEFS_H */ \ No newline at end of file
diff --git a/src/core/fns.h b/src/core/fns.h
new file mode 100644
index 0000000..024dede
--- /dev/null
+++ b/src/core/fns.h
@@ -0,0 +1,16 @@
+#ifndef FNS_H
+#define FNS_H
+
+#define WRAPPER(fn, retn, ...) retn aplwc_wrapper_##fn(APLWC *aplwc, __VA_ARGS__)
+#define CALLFN(fn, ...) aplwc_wrapper_##fn(aplwc, __VA_ARGS__)
+
+WRAPPER(alloc, void *, size_t);
+WRAPPER(free, void, void *);
+WRAPPER(debug, void, char *);
+WRAPPER(fatal, void, char *);
+
+#define DEBUG(msg) CALLFN(debug, msg)
+#define FATAL(msg) CALLFN(fatal, msg)
+#define ASSERT(check, msg) do{if(!(check))FATAL(msg);}while(0)
+
+#endif /* FNS_H */ \ No newline at end of file
diff --git a/src/core/init.c b/src/core/init.c
new file mode 100644
index 0000000..9a28a13
--- /dev/null
+++ b/src/core/init.c
@@ -0,0 +1,75 @@
+#include <config.h>
+#include <aplwc.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include "defs.h"
+#include "fns.h"
+#include "types.h"
+
+static void *
+default_alloc(void *aux, size_t size)
+{
+ return malloc(size);
+}
+
+static void
+default_free(void *aux, void *p)
+{
+ free(p);
+}
+
+static void
+default_debug(void *aux, char *msg)
+{
+ printf("DEBUG: %s\n", msg);
+}
+
+static void
+default_fatal(void *aux, char *msg)
+{
+ printf("FATAL: %s\n", msg);
+ exit(EXIT_FAILURE);
+}
+
+void
+aplwc_init_fns(APLWCFunctions *fns)
+{
+ fns->alloc = default_alloc;
+ fns->free = default_free;
+ fns->debug = default_debug;
+ fns->fatal = default_fatal;
+}
+
+APLWC *
+aplwc_init(APLWCFunctions *fns, void *aux)
+{
+ size_t size = sizeof(APLWC) + (APLWC_ALIGNMENT - 1) + WS_START_SIZE + (WS_ALIGNMENT - 1);
+ intptr_t base = (intptr_t)fns->alloc(aux, size);
+ intptr_t aligned;
+
+ aligned = base;
+ if((aligned % APLWC_ALIGNMENT) != 0)
+ aligned += APLWC_ALIGNMENT - (aligned % APLWC_ALIGNMENT);
+
+ APLWC *aplwc = (APLWC *)aligned;
+ aplwc->base = (void*)base;
+ aplwc->fns = fns;
+ aplwc->aux = aux;
+
+ aligned += sizeof(APLWC);
+ if((aligned % WS_ALIGNMENT) != 0)
+ aligned += WS_ALIGNMENT - (aligned % WS_ALIGNMENT);
+ aplwc->ws = (void*)aligned;
+ aplwc->ws_size = size - (aligned - base);
+
+ DEBUG("initialized");
+ return aplwc;
+}
+
+void
+aplwc_exit(APLWC *aplwc)
+{
+ DEBUG("exiting");
+ CALLFN(free, aplwc->base);
+}
diff --git a/src/core/types.h b/src/core/types.h
new file mode 100644
index 0000000..40dc852
--- /dev/null
+++ b/src/core/types.h
@@ -0,0 +1,14 @@
+#ifndef TYPES_H
+#define TYPES_H
+
+struct APLWC
+{
+ APLWCFunctions *fns;
+ void *aux;
+ void *base;
+
+ void *ws;
+ size_t ws_size;
+};
+
+#endif /* TYPES_H */ \ No newline at end of file
diff --git a/src/core/wrappers.c b/src/core/wrappers.c
new file mode 100644
index 0000000..24a560e
--- /dev/null
+++ b/src/core/wrappers.c
@@ -0,0 +1,27 @@
+#include <aplwc.h>
+#include "fns.h"
+#include "types.h"
+
+#define CALL(fn, ...) aplwc->fns->fn(aplwc->aux, __VA_ARGS__)
+
+WRAPPER(alloc, void *, size_t size)
+{
+ void *r = CALL(alloc, size);
+ ASSERT(r != NULL, "Could not allocate memory");
+ return r;
+}
+
+WRAPPER(free, void, void *p)
+{
+ CALL(free, p);
+}
+
+WRAPPER(debug, void, char *msg)
+{
+ CALL(debug, msg);
+}
+
+WRAPPER(fatal, void, char *msg)
+{
+ CALL(fatal, msg);
+} \ No newline at end of file