Compare commits

..

2 commits

Author SHA1 Message Date
26ef0bc3ad
formatting/style 2024-09-06 11:05:16 -04:00
0747e78b15
Makefile: overhaul variables, std=gnu11 for now 2024-09-06 11:03:59 -04:00
7 changed files with 220 additions and 214 deletions

View file

@ -1,21 +1,22 @@
CC = gcc CC = gcc
CFLAGS = CFLAGS = -std=gnu11
CPPFLAGS = -Iinclude -MMD -MP CPPFLAGS = -Iinclude -MMD -MP
SRCDIR = src SRCDIR = src
SRCS = $(wildcard $(SRCDIR)/*.c) SRCS := $(wildcard $(SRCDIR)/*.c)
OBJS = $(patsubst $(SRCDIR)/%.c,%.o,$(SRCS)) OBJS := $(patsubst $(SRCDIR)/%.c,%.o,$(SRCS))
EXE = gc EXE = gc
DBDIR = debug DBDIR = debug
DBEXE = $(DBDIR)/$(EXE) DBEXE := $(DBDIR)/$(EXE)
DBOBJS = $(addprefix $(DBDIR)/, $(OBJS)) DBOBJS := $(addprefix $(DBDIR)/, $(OBJS))
DBCFLAGS = -g -O0 -DDEBUG DBCFLAGS = -g -O0 -DDEBUG
REDIR = bin REDIR = bin
REEXE = $(REDIR)/$(EXE) REEXE := $(REDIR)/$(EXE)
REOBJS = $(addprefix $(REDIR)/, $(OBJS)) REOBJS := $(addprefix $(REDIR)/, $(OBJS))
RECFLAGS = -O3 -Wall -Wextra -Wpedantic -Werror RECFLAGS = -O3 -Wall -Wextra -Wpedantic -Werror
.PHONY: all clean debug prep release .PHONY: all clean debug prep release
@ -43,3 +44,5 @@ prep:
clean: clean:
rm -rf $(DBDIR) $(REDIR) rm -rf $(DBDIR) $(REDIR)
-include $(OBJ:.o=.d)

View file

@ -43,5 +43,6 @@ void collect(struct virtualMachine *vm)
{ {
markAll(vm); markAll(vm);
sweep(vm); sweep(vm);
vm->refMax = vm->refCount * 2 <= STACK_MAX ? vm->refCount * 2 : STACK_MAX; vm->refMax = vm->refCount * 2 <= STACK_MAX ? vm->refCount * 2 :
STACK_MAX;
} }

View file

@ -5,7 +5,8 @@
static void *allocHead = NULL; static void *allocHead = NULL;
struct gcHeader *findFree(struct gcHeader **prev, size_t size) { struct gcHeader *findFree(struct gcHeader **prev, size_t size)
{
struct gcHeader *curr = allocHead; struct gcHeader *curr = allocHead;
while (curr && !(curr->free && curr->size >= size)) { while (curr && !(curr->free && curr->size >= size)) {
@ -16,7 +17,8 @@ struct gcHeader *findFree(struct gcHeader **prev, size_t size) {
return curr; return curr;
} }
struct gcHeader *requestMem(struct gcHeader *prev, size_t size) { struct gcHeader *requestMem(struct gcHeader *prev, size_t size)
{
struct gcHeader *block; struct gcHeader *block;
void *request; void *request;

View file

@ -12,8 +12,8 @@ void test_int_alloc(void)
pushInt(vm, 1000); pushInt(vm, 1000);
pushInt(vm, 10000); pushInt(vm, 10000);
pushInt(vm, -100000); pushInt(vm, -100000);
assert(vm->refCount == 4 assert(vm->refCount == 4 &&
&& "test_int_alloc: GARBAGE_INT allocation failure\n"); "test_int_alloc: GARBAGE_INT allocation failure\n");
printf("test_int_alloc: PASS\n"); printf("test_int_alloc: PASS\n");
deinitVM(vm); deinitVM(vm);
} }
@ -27,8 +27,8 @@ void test_pair_alloc(void)
pushInt(vm, -100000); pushInt(vm, -100000);
pushPair(vm); pushPair(vm);
pushPair(vm); pushPair(vm);
assert(vm->refCount == 6 assert(vm->refCount == 6 &&
&& "test_pair_alloc: FAILED: GARBAGE_PAIR allocation failure\n"); "test_pair_alloc: FAILED: GARBAGE_PAIR allocation failure\n");
printf("test_pair_alloc: PASS\n"); printf("test_pair_alloc: PASS\n");
deinitVM(vm); deinitVM(vm);
} }
@ -41,8 +41,8 @@ void test_obj_count(void)
pushInt(vm, 10000); pushInt(vm, 10000);
pushInt(vm, -100000); pushInt(vm, -100000);
collect(vm); collect(vm);
assert(vm->refCount == 4 assert(vm->refCount == 4 &&
&& "test_obj_count: FAILED: GC occurred when it shouldn't have\n"); "test_obj_count: FAILED: GC occurred when it shouldn't have\n");
printf("test_obj_count: PASS\n"); printf("test_obj_count: PASS\n");
deinitVM(vm); deinitVM(vm);
} }
@ -58,8 +58,8 @@ void test_nested_pair(void)
pushPair(vm); pushPair(vm);
pushPair(vm); pushPair(vm);
collect(vm); collect(vm);
assert(vm->refCount == 7 assert(vm->refCount == 7 &&
&& "test_nested_pair: FAILED: GARBAGE_PAIR allocation failure\n"); "test_nested_pair: FAILED: GARBAGE_PAIR allocation failure\n");
printf("test_pair_alloc: PASS\n"); printf("test_pair_alloc: PASS\n");
deinitVM(vm); deinitVM(vm);
} }
@ -72,9 +72,8 @@ void test_unreachable(void)
pop(vm); pop(vm);
pop(vm); pop(vm);
collect(vm); collect(vm);
assert( assert(vm->refCount == 0 &&
vm->refCount == 0 "test_unreachable: FAILED: 2 GARBAGE_INT should have been freed\n");
&& "test_unreachable: FAILED: 2 GARBAGE_INT should have been freed\n");
printf("test_unreachable: PASS\n"); printf("test_unreachable: PASS\n");
deinitVM(vm); deinitVM(vm);
} }
@ -95,8 +94,8 @@ void test_auto_gc(void)
pushInt(vm, 2); pushInt(vm, 2);
} }
assert(vm->refCount == 550 assert(vm->refCount == 550 &&
&& "test_auto_gc: FAILED: 5 references should have been freed\n"); "test_auto_gc: FAILED: 5 references should have been freed\n");
printf("test_auto_gc: PASS\n"); printf("test_auto_gc: PASS\n");
deinitVM(vm); deinitVM(vm);
} }

View file

@ -24,7 +24,8 @@ void deinitVM(struct virtualMachine *vm)
void push(struct virtualMachine *vm, struct garbageObject *value) void push(struct virtualMachine *vm, struct garbageObject *value)
{ {
if (vm->stackSize >= STACK_MAX) { if (vm->stackSize >= STACK_MAX) {
fprintf(stderr, "ERROR: push(): refusing to overflow the stack!\n"); fprintf(stderr,
"ERROR: push(): refusing to overflow the stack!\n");
return; return;
} }
vm->stack[vm->stackSize++] = value; vm->stack[vm->stackSize++] = value;