Skip to content

Commit

Permalink
boehm gc example (#1301)
Browse files Browse the repository at this point in the history
* add boehm gc program compiling example

* add file

* update Makefile

* try to build the boehm example in CI

* install package

* try to fix package

* unify make file

* fix -l arg
  • Loading branch information
johnynek authored Dec 9, 2024
1 parent 2c08a6b commit b398375
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,15 @@ jobs:
- '8'
steps:
- uses: "actions/[email protected]"
- name: "install boehm"
run: "sudo apt-get update && sudo apt install -y libgc1 libgc-dev"
- name: "test runtime code"
run: |
cd c_runtime
rm -f test_exe
make && git diff --quiet || { git diff; false; }
./test_exe && cd ..
make boehm_example
./test_exe && ./boehm_example && cd ..
- name: "build assembly"
run: "sbt \"++${{matrix.scala}}; cli/assembly\""
- name: "generate c code"
Expand Down
20 changes: 19 additions & 1 deletion c_runtime/Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
OS := $(shell uname)

# Platform-specific settings
ifneq ($(wildcard /opt/homebrew/Cellar/bdw-gc),) # Check if the directory exists
LATEST_VERSION := $(shell ls -v /opt/homebrew/Cellar/bdw-gc | tail -n 1)
BOEHM_GC := /opt/homebrew/Cellar/bdw-gc/$(LATEST_VERSION)
endif

ifeq ($(OS), Darwin)
CFLAGS += -I$(BOEHM_GC)/include
LIBS += "$(BOEHM_GC)/lib/libgc.a"
else ifeq ($(OS), Linux)
LIBS += -lgc
endif

all: bosatsu_runtime.o test_out bosatsu_ext_Bosatsu_l_Predef.o bosatsu_ext_Bosatsu_l_Prog.o

bosatsu_generated.h: typegen.py
Expand All @@ -21,4 +36,7 @@ bosatsu_ext_Bosatsu_l_Predef.o: bosatsu_ext_Bosatsu_l_Predef.c bosatsu_runtime.o
gcc -Wall -Werror -c bosatsu_ext_Bosatsu_l_Predef.c

bosatsu_ext_Bosatsu_l_Prog.o: bosatsu_ext_Bosatsu_l_Prog.c bosatsu_runtime.o
gcc -Wall -Werror -c bosatsu_ext_Bosatsu_l_Prog.c
gcc -Wall -Werror -c bosatsu_ext_Bosatsu_l_Prog.c

boehm_example: boehm_example.c
gcc $(CFLAGS) boehm_example.c $(LIBS) -o boehm_example
19 changes: 19 additions & 0 deletions c_runtime/boehm_example.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "gc.h"
#include <assert.h>
#include <stdio.h>

int main(void) {
int i;

GC_INIT();
for (i = 0; i < 10000000; ++i) {
int **p = (int **) GC_MALLOC(sizeof(int *));
int *q = (int *) GC_MALLOC_ATOMIC(sizeof(int));
assert(*p == 0);
*p = (int *) GC_REALLOC(q, 2 * sizeof(int));
if (i % 100000 == 0)
printf("Heap size = %lu bytes\n",
(unsigned long)GC_get_heap_size());
}
return 0;
}

0 comments on commit b398375

Please sign in to comment.