Compare commits

..

No commits in common. "c73dfc2c0060991de10addf27c79b5e0dfd44b41" and "620f779274688cae38bcbb8f7c4bb05301277dbe" have entirely different histories.

14 changed files with 274 additions and 301 deletions

6
.gitignore vendored
View file

@ -1,9 +1,7 @@
*
!.gitignore
!*.c
!*.h
!*.md
!LICENSE
!Makefile
!/src
!/src/*.c
!/include
!/include/*.h

View file

@ -1,45 +1,47 @@
CC = gcc
CFLAGS =
CPPFLAGS = -Iinclude -MMD -MP
CC = gcc
CFLAGS = -O3 -Wall -Wextra -Wpedantic
SRCDIR = src
SRCS = $(wildcard $(SRCDIR)/*.c)
OBJS = $(patsubst $(SRCDIR)/%.c,%.o,$(SRCS))
SRCS = main.c vm.c gc.c galloc.c
OBJS = $(SRCS:.c=.o)
EXE = gc
DBDIR = debug
DBEXE = $(DBDIR)/$(EXE)
DBOBJS = $(addprefix $(DBDIR)/, $(OBJS))
DBCFLAGS = -g -O0 -DDEBUG
DBDIR = debug
DBEXE = $(DBDIR)/$(EXE)
DBOBJS = $(addprefix $(DBDIR)/, $(OBJS))
DBCFLAGS = -g -O0 -DDEBUG
REDIR = bin
REEXE = $(REDIR)/$(EXE)
REOBJS = $(addprefix $(REDIR)/, $(OBJS))
RECFLAGS = -O3 -Wall -Wextra -Wpedantic -Werror
REDIR = build
REEXE = $(REDIR)/$(EXE)
REOBJS = $(addprefix $(REDIR)/, $(OBJS))
.PHONY: all clean debug prep release
.PHONY: all clean debug release prep
all: debug release
all: debug release gc
gc: $(REEXE)
@cp $< $@
debug: prep $(DBEXE)
$(DBEXE): $(DBOBJS)
$(CC) $(CFLAGS) $(DBCFLAGS) -o $@ $^
$(CC) $(CFLAGS) $(DBCFLAGS) -DTEST -o $@ $^
$(DBDIR)/%.o: $(SRCDIR)/%.c
$(CC) -c $(CPPFLAGS) $(CFLAGS) $(DBCFLAGS) -o $@ $<
$(DBDIR)/%.o: %.c
$(CC) -c $(CFLAGS) $(DBCFLAGS) -o $@ $<
release: prep $(REEXE)
$(REEXE): $(REOBJS)
$(CC) $(CFLAGS) $(RECFLAGS) -o $@ $^
$(CC) $(CFLAGS) -o $@ $^
$(REDIR)/%.o: $(SRCDIR)/%.c
$(CC) -c $(CPPFLAGS) $(CFLAGS) $(RECFLAGS) -o $@ $<
$(REDIR)/%.o: %.c
$(CC) -c $(CFLAGS) -o $@ $<
prep:
@mkdir -p $(DBDIR) $(REDIR)
clean:
rm -rf $(DBDIR) $(REDIR)
rm -rf gc $(DBDIR) $(REDIR)

View file

@ -1,44 +1,38 @@
# marCsweep
A mark-and-sweep garbage collector written in C.
Includes a simple virtual machine and a custom `malloc` implementation
for creating, tracing, and freeing garbage.
A mark-and-sweep garbage collector with a simple virtual machine to perform
allocations.
## Building
Compiling the project should be relatively easy, even if you aren't familiar
with `make`.
Dependencies: `git`, `make`, and `gcc`.
Dependencies: `git`, `make`, and `gcc`. Compiling with `clang` is also possible,
simply replace "gcc" with "clang" in the first line of the Makefile.
```
git clone https://codeberg.org/andyscott/marCsweep.git
cd marCsweep
make release # or debug
make release
```
That's it! Make will create the `bin` directory and place the finished
executable there.
Compiling with `clang` is also supported, just replace "gcc" with "clang" in the
first line of the `Makefile`. Also note that if you don't specify a target for
make (i.e. "release" or "debug") both will be built. Debug builds are placed in
the `debug` directory.
That's it! The Makefile will automatically create the build directory and place
the compiled executable there.
## Running
Once compiled, the program can be run with the following:
```
cd bin # or debug
cd build # or debug
./gc
```
The results of the test cases will be output to your terminal:
By default the program will output the results of the test cases:
> test_int_alloc: PASS
test_pair_alloc: PASS
@ -56,7 +50,3 @@ This is a personal project that I am writing in my free time to learn more about
garbage collection. Thank you to [Robert
Nystrom](https://journal.stuffwithstuff.com/2013/12/08/babys-first-garbage-collector/)
for the idea.
Sadly, it was this author's idea to extend the project by writing a custom
`malloc`. Perhaps I should have been lazy and used the preexisting definitions,
but where's the fun in that?

44
gc.c Normal file
View file

@ -0,0 +1,44 @@
#include <stdlib.h>
#include "gc.h"
#include "vm.h"
void mark(struct garbageObject *obj) {
if (obj->mark)
return;
++obj->mark;
if (obj->type == GARBAGE_PAIR) {
mark(obj->head);
mark(obj->tail);
}
}
void markAll(struct virtualMachine *vm) {
for (int i = 0; i < vm->stackSize; ++i) {
mark(vm->stack[i]);
}
}
void sweep(struct virtualMachine *vm) {
struct garbageObject **obj = &vm->head;
while (*obj) {
if (!(*obj)->mark) {
struct garbageObject *unreachable = *obj;
*obj = unreachable->next;
free(unreachable);
vm->refCount--;
} else {
(*obj)->mark = 0;
obj = &(*obj)->next;
}
}
}
void collect(struct virtualMachine *vm) {
markAll(vm);
sweep(vm);
vm->refMax = vm->refCount * 2 <= STACK_MAX ? vm->refCount * 2 : STACK_MAX;
}

View file

View file

@ -1,5 +1,5 @@
#ifndef GC_ALLOC_H
#define GC_ALLOC_H
#ifndef GALLOC_H
#define GALLOC_H
#include <stdio.h>
@ -23,4 +23,4 @@ void *galloc(size_t size);
// Frees memory allocated with galloc()
void gfree(void *ptr);
#endif /* GC_ALLOC_H */
#endif /* GALLOC_H */

107
main.c Normal file
View file

@ -0,0 +1,107 @@
#include <assert.h>
#include <stdio.h>
#include "gc.h"
#include "test_gc.h"
#include "vm.h"
void test_int_alloc(void) {
struct virtualMachine *vm = initVM();
pushInt(vm, 100);
pushInt(vm, 1000);
pushInt(vm, 10000);
pushInt(vm, -100000);
assert(vm->refCount == 4 &&
"test_int_alloc: GARBAGE_INT allocation failure\n");
printf("test_int_alloc: PASS\n");
deinitVM(vm);
}
void test_pair_alloc(void) {
struct virtualMachine *vm = initVM();
pushInt(vm, 100);
pushInt(vm, 1000);
pushInt(vm, 10000);
pushInt(vm, -100000);
pushPair(vm);
pushPair(vm);
assert(vm->refCount == 6 &&
"test_pair_alloc: FAILED: GARBAGE_PAIR allocation failure\n");
printf("test_pair_alloc: PASS\n");
deinitVM(vm);
}
void test_obj_count(void) {
struct virtualMachine *vm = initVM();
pushInt(vm, 100);
pushInt(vm, 1000);
pushInt(vm, 10000);
pushInt(vm, -100000);
collect(vm);
assert(vm->refCount == 4 &&
"test_obj_count: FAILED: GC occurred when it shouldn't have\n");
printf("test_obj_count: PASS\n");
deinitVM(vm);
}
void test_nested_pair(void) {
struct virtualMachine *vm = initVM();
pushInt(vm, 100);
pushInt(vm, 1000);
pushPair(vm);
pushInt(vm, 10000);
pushInt(vm, -100000);
pushPair(vm);
pushPair(vm);
collect(vm);
assert(vm->refCount == 7 &&
"test_nested_pair: FAILED: GARBAGE_PAIR allocation failure\n");
printf("test_pair_alloc: PASS\n");
deinitVM(vm);
}
void test_unreachable(void) {
struct virtualMachine *vm = initVM();
pushInt(vm, 100);
pushInt(vm, 1000);
pop(vm);
pop(vm);
collect(vm);
assert(vm->refCount == 0 &&
"test_unreachable: FAILED: 2 GARBAGE_INT should have been freed\n");
printf("test_unreachable: PASS\n");
deinitVM(vm);
}
void test_auto_gc(void) {
struct virtualMachine *vm = initVM();
for (size_t i = 0; i < 505; ++i) {
pushInt(vm, 1);
}
for (size_t i = 0; i < 5; ++i) {
pop(vm);
}
for (size_t i = 0; i < 50; ++i) {
pushInt(vm, 2);
}
assert(vm->refCount == 550 &&
"test_auto_gc: FAILED: 5 references should have been freed\n");
printf("test_auto_gc: PASS\n");
deinitVM(vm);
}
int main(void) {
test_int_alloc();
test_pair_alloc();
test_obj_count();
test_nested_pair();
test_unreachable();
test_auto_gc();
return 0;
}

View file

@ -1,47 +0,0 @@
#include <stdlib.h>
#include "gc.h"
void mark(struct garbageObject *obj)
{
if (obj->mark)
return;
++obj->mark;
if (obj->type == GARBAGE_PAIR) {
mark(obj->head);
mark(obj->tail);
}
}
void markAll(struct virtualMachine *vm)
{
for (int i = 0; i < vm->stackSize; ++i) {
mark(vm->stack[i]);
}
}
void sweep(struct virtualMachine *vm)
{
struct garbageObject **obj = &vm->head;
while (*obj) {
if (!(*obj)->mark) {
struct garbageObject *unreachable = *obj;
*obj = unreachable->next;
free(unreachable);
vm->refCount--;
} else {
(*obj)->mark = 0;
obj = &(*obj)->next;
}
}
}
void collect(struct virtualMachine *vm)
{
markAll(vm);
sweep(vm);
vm->refMax = vm->refCount * 2 <= STACK_MAX ? vm->refCount * 2 : STACK_MAX;
}

View file

@ -1,114 +0,0 @@
#include <assert.h>
#include <stdio.h>
#include "gc.h"
#include "test_gc.h"
#include "vm.h"
void test_int_alloc(void)
{
struct virtualMachine *vm = initVM();
pushInt(vm, 100);
pushInt(vm, 1000);
pushInt(vm, 10000);
pushInt(vm, -100000);
assert(vm->refCount == 4
&& "test_int_alloc: GARBAGE_INT allocation failure\n");
printf("test_int_alloc: PASS\n");
deinitVM(vm);
}
void test_pair_alloc(void)
{
struct virtualMachine *vm = initVM();
pushInt(vm, 100);
pushInt(vm, 1000);
pushInt(vm, 10000);
pushInt(vm, -100000);
pushPair(vm);
pushPair(vm);
assert(vm->refCount == 6
&& "test_pair_alloc: FAILED: GARBAGE_PAIR allocation failure\n");
printf("test_pair_alloc: PASS\n");
deinitVM(vm);
}
void test_obj_count(void)
{
struct virtualMachine *vm = initVM();
pushInt(vm, 100);
pushInt(vm, 1000);
pushInt(vm, 10000);
pushInt(vm, -100000);
collect(vm);
assert(vm->refCount == 4
&& "test_obj_count: FAILED: GC occurred when it shouldn't have\n");
printf("test_obj_count: PASS\n");
deinitVM(vm);
}
void test_nested_pair(void)
{
struct virtualMachine *vm = initVM();
pushInt(vm, 100);
pushInt(vm, 1000);
pushPair(vm);
pushInt(vm, 10000);
pushInt(vm, -100000);
pushPair(vm);
pushPair(vm);
collect(vm);
assert(vm->refCount == 7
&& "test_nested_pair: FAILED: GARBAGE_PAIR allocation failure\n");
printf("test_pair_alloc: PASS\n");
deinitVM(vm);
}
void test_unreachable(void)
{
struct virtualMachine *vm = initVM();
pushInt(vm, 100);
pushInt(vm, 1000);
pop(vm);
pop(vm);
collect(vm);
assert(
vm->refCount == 0
&& "test_unreachable: FAILED: 2 GARBAGE_INT should have been freed\n");
printf("test_unreachable: PASS\n");
deinitVM(vm);
}
void test_auto_gc(void)
{
struct virtualMachine *vm = initVM();
for (size_t i = 0; i < 505; ++i) {
pushInt(vm, 1);
}
for (size_t i = 0; i < 5; ++i) {
pop(vm);
}
for (size_t i = 0; i < 50; ++i) {
pushInt(vm, 2);
}
assert(vm->refCount == 550
&& "test_auto_gc: FAILED: 5 references should have been freed\n");
printf("test_auto_gc: PASS\n");
deinitVM(vm);
}
int main(void)
{
test_int_alloc();
test_pair_alloc();
test_obj_count();
test_nested_pair();
test_unreachable();
test_auto_gc();
return 0;
}

View file

@ -1,89 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include "gc.h"
#include "vm.h"
struct virtualMachine *initVM(void)
{
struct virtualMachine *vm = malloc(sizeof(struct virtualMachine));
vm->stackSize = 0;
vm->refCount = 0;
vm->refMax = GC_THRESHOLD;
vm->head = NULL;
return vm;
}
void deinitVM(struct virtualMachine *vm)
{
vm->stackSize = 0;
collect(vm);
free(vm);
}
void push(struct virtualMachine *vm, struct garbageObject *value)
{
if (vm->stackSize >= STACK_MAX) {
fprintf(stderr, "ERROR: push(): refusing to overflow the stack!\n");
return;
}
vm->stack[vm->stackSize++] = value;
}
struct garbageObject *pop(struct virtualMachine *vm)
{
if (vm->stackSize < 0) {
fprintf(stderr, "ERROR: pop(): Stack size cannot be negative!");
return NULL;
}
return vm->stack[--vm->stackSize];
}
struct garbageObject *initGarbage(struct virtualMachine *vm,
enum garbageData type)
{
if (vm->refCount >= vm->refMax) {
collect(vm);
}
struct garbageObject *obj = malloc(sizeof(struct garbageObject));
if (obj == NULL) {
fprintf(stderr, "initGarbage(): Allocation failure!");
return obj;
}
obj->type = type;
obj->mark = 0;
obj->next = vm->head;
vm->head = obj;
vm->refCount++;
return obj;
}
void pushInt(struct virtualMachine *vm, int value)
{
struct garbageObject *obj = initGarbage(vm, GARBAGE_INT);
if (obj == NULL) {
fprintf(stderr,
"pushInt(): Unable to create GARBAGE_INT - out of memory?");
}
obj->value = value;
push(vm, obj);
}
struct garbageObject *pushPair(struct virtualMachine *vm)
{
struct garbageObject *obj = initGarbage(vm, GARBAGE_PAIR);
if (obj == NULL) {
fprintf(stderr,
"pushPair(): Unable to create GARBAGE_PAIR - out of memory?");
}
obj->tail = pop(vm);
obj->head = pop(vm);
push(vm, obj);
return obj;
}

82
vm.c Normal file
View file

@ -0,0 +1,82 @@
#include <stdio.h>
#include <stdlib.h>
#include "gc.h"
#include "vm.h"
struct virtualMachine *initVM(void) {
struct virtualMachine *vm = malloc(sizeof(struct virtualMachine));
vm->stackSize = 0;
vm->refCount = 0;
vm->refMax = GC_THRESHOLD;
vm->head = NULL;
return vm;
}
void deinitVM(struct virtualMachine *vm) {
vm->stackSize = 0;
collect(vm);
free(vm);
}
void push(struct virtualMachine *vm, struct garbageObject *value) {
if (vm->stackSize >= STACK_MAX) {
fprintf(stderr, "ERROR: push(): refusing to overflow the stack!\n");
return;
}
vm->stack[vm->stackSize++] = value;
}
struct garbageObject *pop(struct virtualMachine *vm) {
if (vm->stackSize < 0) {
fprintf(stderr, "ERROR: pop(): Stack size cannot be negative!");
return NULL;
}
return vm->stack[--vm->stackSize];
}
struct garbageObject *initGarbage(struct virtualMachine *vm,
enum garbageData type) {
if (vm->refCount >= vm->refMax) {
collect(vm);
}
struct garbageObject *obj = malloc(sizeof(struct garbageObject));
if (obj == NULL) {
fprintf(stderr, "initGarbage(): Allocation failure!");
return obj;
}
obj->type = type;
obj->mark = 0;
obj->next = vm->head;
vm->head = obj;
vm->refCount++;
return obj;
}
void pushInt(struct virtualMachine *vm, int value) {
struct garbageObject *obj = initGarbage(vm, GARBAGE_INT);
if (obj == NULL) {
fprintf(stderr, "pushInt(): Unable to create GARBAGE_INT - out of memory?");
}
obj->value = value;
push(vm, obj);
}
struct garbageObject *pushPair(struct virtualMachine *vm) {
struct garbageObject *obj = initGarbage(vm, GARBAGE_PAIR);
if (obj == NULL) {
fprintf(stderr,
"pushPair(): Unable to create GARBAGE_PAIR - out of memory?");
}
obj->tail = pop(vm);
obj->head = pop(vm);
push(vm, obj);
return obj;
}

View file