summaryrefslogtreecommitdiff
path: root/src/core/init.c
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/init.c
Initial commit
Diffstat (limited to 'src/core/init.c')
-rw-r--r--src/core/init.c75
1 files changed, 75 insertions, 0 deletions
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);
+}