From b398375a7001accf9534021ac97029f5c620a048 Mon Sep 17 00:00:00 2001 From: "P. Oscar Boykin" Date: Mon, 9 Dec 2024 09:00:04 -1000 Subject: [PATCH] boehm gc example (#1301) * 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 --- .github/workflows/ci.yml | 5 ++++- c_runtime/Makefile | 20 +++++++++++++++++++- c_runtime/boehm_example.c | 19 +++++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 c_runtime/boehm_example.c diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0b116e814..dedb84b66 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -118,12 +118,15 @@ jobs: - '8' steps: - uses: "actions/checkout@v2.1.0" + - 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" diff --git a/c_runtime/Makefile b/c_runtime/Makefile index d36d6a353..158305aca 100644 --- a/c_runtime/Makefile +++ b/c_runtime/Makefile @@ -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 @@ -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 \ No newline at end of file + gcc -Wall -Werror -c bosatsu_ext_Bosatsu_l_Prog.c + +boehm_example: boehm_example.c + gcc $(CFLAGS) boehm_example.c $(LIBS) -o boehm_example \ No newline at end of file diff --git a/c_runtime/boehm_example.c b/c_runtime/boehm_example.c new file mode 100644 index 000000000..d365fc68f --- /dev/null +++ b/c_runtime/boehm_example.c @@ -0,0 +1,19 @@ +#include "gc.h" +#include +#include + +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; +} \ No newline at end of file