summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorubq323 <ubq323@ubq323.website>2023-08-04 22:38:48 +0100
committerubq323 <ubq323@ubq323.website>2023-08-04 22:38:48 +0100
commitf67a3fd05c1b2fe8c749fc2f58287b1b14b011d8 (patch)
tree5566db459c3414ad1c9c18bcc61a21e0ab034f37
parent7c9790d7726e0bb3f44bec15eff7631080d45b03 (diff)
move M to its own file
-rw-r--r--Makefile4
-rw-r--r--mem.c18
-rw-r--r--mem.h8
-rw-r--r--val.c3
-rw-r--r--vm.c15
-rw-r--r--vm.h2
6 files changed, 31 insertions, 19 deletions
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 <stdio.h>
+#include <stdlib.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;
+ }
+}
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 <stddef.h>
+
+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 <stdio.h>
#include <string.h>
#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;