From f67a3fd05c1b2fe8c749fc2f58287b1b14b011d8 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Fri, 4 Aug 2023 22:38:48 +0100 Subject: move M to its own file --- Makefile | 4 ++-- mem.c | 18 ++++++++++++++++++ mem.h | 8 ++++++++ val.c | 3 ++- vm.c | 15 +-------------- vm.h | 2 -- 6 files changed, 31 insertions(+), 19 deletions(-) create mode 100644 mem.c create mode 100644 mem.h diff --git a/Makefile b/Makefile index 696587b..8d9c882 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ -CS=ast.c com.c dis.c prs.c read.c val.c vm.c -HS=ast.h dis.h prs.h read.h val.h vm.h +CS=ast.c com.c dis.c mem.c prs.c read.c val.c vm.c +HS=ast.h dis.h mem.h prs.h read.h val.h vm.h CFLAGS=-O3 -Wall -Wpedantic -Werror=implicit-function-declaration bþ: $(CS) $(HS) diff --git a/mem.c b/mem.c new file mode 100644 index 0000000..6e0bc1b --- /dev/null +++ b/mem.c @@ -0,0 +1,18 @@ +#include "mem.h" + +#include +#include + +void *M(void *p, size_t sz) { + if (sz == 0) { + free(p); + return NULL; + } else { + void *x = realloc(p, sz); + if (x == NULL) { + printf("out of memory! aaaaaaa!!!!!\n"); + exit(42); + } + return x; + } +} diff --git a/mem.h b/mem.h new file mode 100644 index 0000000..8f66fed --- /dev/null +++ b/mem.h @@ -0,0 +1,8 @@ +#ifndef _mem_h +#define _mem_h + +#include + +void *M(void *p, size_t sz); + +#endif diff --git a/val.c b/val.c index cd76595..c1b83c3 100644 --- a/val.c +++ b/val.c @@ -1,7 +1,8 @@ #include #include #include "val.h" -#include "vm.h" +#include "mem.h" + ObjString *objstring_new(char *src, size_t len) { char *d = M(NULL, (1 + len) * sizeof (char)); diff --git a/vm.c b/vm.c index c420774..a9610d2 100644 --- a/vm.c +++ b/vm.c @@ -6,22 +6,9 @@ #include "val.h" #include "vm.h" +#include "mem.h" #include "dis.h" -void *M(void *p, size_t sz) { - if (sz == 0) { - free(p); - return NULL; - } else { - void *x = realloc(p, sz); - if (x == NULL) { - printf("out of memory! aaaaaaa!!!!!\n"); - exit(42); - } - return x; - } -} - Chunk chunk_new() { return (Chunk){ diff --git a/vm.h b/vm.h index c710a4b..9a17f77 100644 --- a/vm.h +++ b/vm.h @@ -7,8 +7,6 @@ #include "val.h" -void *M(void *p, size_t sz); - typedef struct { // bytecode size_t blen; -- cgit v1.2.3