2021-03-20 14:16:36 +01:00
|
|
|
module builtin
|
|
|
|
|
2022-04-18 09:50:21 +02:00
|
|
|
$if dynamic_boehm ? {
|
|
|
|
$if windows {
|
2021-07-24 15:04:26 +02:00
|
|
|
$if tinyc {
|
|
|
|
#flag -I@VEXEROOT/thirdparty/libgc/include
|
2022-04-18 09:50:21 +02:00
|
|
|
#flag -L@VEXEROOT/thirdparty/tcc/lib
|
2021-07-24 15:04:26 +02:00
|
|
|
#flag -lgc
|
2022-01-11 14:23:21 +01:00
|
|
|
} $else $if msvc {
|
|
|
|
#flag -DGC_BUILTIN_ATOMIC=1
|
|
|
|
#flag -I@VEXEROOT/thirdparty/libgc/include
|
2021-07-24 15:04:26 +02:00
|
|
|
} $else {
|
2022-04-18 09:50:21 +02:00
|
|
|
#flag -DGC_WIN32_THREADS=1
|
2021-07-24 15:04:26 +02:00
|
|
|
#flag -DGC_BUILTIN_ATOMIC=1
|
|
|
|
#flag -I@VEXEROOT/thirdparty/libgc
|
|
|
|
#flag @VEXEROOT/thirdparty/libgc/gc.o
|
|
|
|
}
|
2021-04-08 18:17:04 +02:00
|
|
|
} $else {
|
2022-04-18 09:50:21 +02:00
|
|
|
$if $pkgconfig('bdw-gc') {
|
|
|
|
#pkgconfig bdw-gc
|
|
|
|
} $else {
|
|
|
|
$if openbsd || freebsd {
|
|
|
|
#flag -I/usr/local/include
|
|
|
|
#flag -L/usr/local/lib
|
|
|
|
}
|
|
|
|
#flag -lgc
|
|
|
|
}
|
2021-04-08 18:17:04 +02:00
|
|
|
}
|
|
|
|
} $else {
|
2022-04-18 09:50:21 +02:00
|
|
|
#flag -DGC_THREADS=1
|
|
|
|
#flag -DGC_BUILTIN_ATOMIC=1
|
|
|
|
$if macos || linux {
|
|
|
|
#flag -DGC_PTHREADS=1
|
|
|
|
#flag -I@VEXEROOT/thirdparty/libgc/include
|
|
|
|
$if (prod && !tinyc && !debug) || !(amd64 || arm64 || i386 || arm32) {
|
|
|
|
// TODO: replace the architecture check with a `!$exists("@VEXEROOT/thirdparty/tcc/lib/libgc.a")` comptime call
|
|
|
|
#flag @VEXEROOT/thirdparty/libgc/gc.o
|
|
|
|
} $else {
|
|
|
|
#flag @VEXEROOT/thirdparty/tcc/lib/libgc.a
|
|
|
|
}
|
2022-04-20 23:12:17 +02:00
|
|
|
#flag -ldl
|
2022-05-30 14:17:01 +02:00
|
|
|
#flag -lpthread
|
2022-04-18 09:50:21 +02:00
|
|
|
} $else $if freebsd {
|
|
|
|
// Tested on FreeBSD 13.0-RELEASE-p3, with clang, gcc and tcc:
|
|
|
|
#flag -DBUS_PAGE_FAULT=T_PAGEFLT
|
|
|
|
#flag -DGC_PTHREADS=1
|
|
|
|
$if !tinyc {
|
2022-05-03 08:17:53 +02:00
|
|
|
#flag -I@VEXEROOT/thirdparty/libgc/include
|
2022-04-18 09:50:21 +02:00
|
|
|
#flag @VEXEROOT/thirdparty/libgc/gc.o
|
2022-05-03 08:17:53 +02:00
|
|
|
}
|
|
|
|
$if tinyc {
|
2022-04-18 09:50:21 +02:00
|
|
|
#flag -I/usr/local/include
|
|
|
|
#flag $first_existing("/usr/local/lib/libgc.a", "/usr/lib/libgc.a")
|
2022-05-03 08:17:53 +02:00
|
|
|
#flag -lgc
|
2022-04-18 09:50:21 +02:00
|
|
|
}
|
|
|
|
#flag -lpthread
|
|
|
|
} $else $if openbsd {
|
2021-06-01 21:28:30 +02:00
|
|
|
#flag -I/usr/local/include
|
2022-04-18 09:50:21 +02:00
|
|
|
#flag $first_existing("/usr/local/lib/libgc.a", "/usr/lib/libgc.a")
|
|
|
|
#flag -lpthread
|
|
|
|
} $else $if windows {
|
2022-05-22 20:25:46 +02:00
|
|
|
#flag -DGC_NOT_DLL=1
|
|
|
|
#flag -DGC_WIN32_THREADS=1
|
2021-07-24 15:04:26 +02:00
|
|
|
$if tinyc {
|
|
|
|
#flag -I@VEXEROOT/thirdparty/libgc/include
|
2022-05-22 20:25:46 +02:00
|
|
|
#flag @VEXEROOT/thirdparty/tcc/lib/libgc.a
|
|
|
|
#flag -luser32
|
2021-07-24 15:04:26 +02:00
|
|
|
} $else {
|
2022-04-18 09:50:21 +02:00
|
|
|
#flag -I@VEXEROOT/thirdparty/libgc/include
|
2021-07-24 15:04:26 +02:00
|
|
|
#flag @VEXEROOT/thirdparty/libgc/gc.o
|
|
|
|
}
|
2022-04-18 09:50:21 +02:00
|
|
|
} $else $if $pkgconfig('bdw-gc') {
|
|
|
|
#pkgconfig bdw-gc
|
2021-07-24 15:04:26 +02:00
|
|
|
} $else {
|
|
|
|
#flag -lgc
|
2021-04-08 18:17:04 +02:00
|
|
|
}
|
2021-03-25 06:28:30 +01:00
|
|
|
}
|
2021-04-08 18:17:04 +02:00
|
|
|
|
2021-03-25 16:52:33 +01:00
|
|
|
$if gcboehm_leak ? {
|
2021-07-24 15:04:26 +02:00
|
|
|
#flag -DGC_DEBUG=1
|
2021-03-25 16:52:33 +01:00
|
|
|
}
|
2021-03-20 14:16:36 +01:00
|
|
|
|
2021-04-08 18:17:04 +02:00
|
|
|
#include <gc.h>
|
2021-03-20 14:16:36 +01:00
|
|
|
|
2021-03-25 16:52:33 +01:00
|
|
|
// replacements for `malloc()/calloc()`, `realloc()` and `free()`
|
|
|
|
// for use with Boehm-GC
|
|
|
|
// Do not use them manually. They are automatically chosen when
|
|
|
|
// compiled with `-gc boehm` or `-gc boehm_leak`.
|
2021-09-08 12:09:32 +02:00
|
|
|
fn C.GC_MALLOC(n usize) voidptr
|
2021-03-20 14:16:36 +01:00
|
|
|
|
2021-09-08 12:09:32 +02:00
|
|
|
fn C.GC_MALLOC_ATOMIC(n usize) voidptr
|
2021-04-14 00:40:26 +02:00
|
|
|
|
2021-09-08 12:09:32 +02:00
|
|
|
fn C.GC_MALLOC_UNCOLLECTABLE(n usize) voidptr
|
2021-03-26 15:44:45 +01:00
|
|
|
|
2021-09-08 12:09:32 +02:00
|
|
|
fn C.GC_REALLOC(ptr voidptr, n usize) voidptr
|
2021-03-20 14:16:36 +01:00
|
|
|
|
|
|
|
fn C.GC_FREE(ptr voidptr)
|
2021-03-21 03:31:06 +01:00
|
|
|
|
2022-04-18 09:50:21 +02:00
|
|
|
// explicitly perform garbage collection now! Garbage collections
|
2021-04-07 15:25:11 +02:00
|
|
|
// are done automatically when needed, so this function is hardly needed
|
2021-03-21 03:31:06 +01:00
|
|
|
fn C.GC_gcollect()
|
2021-03-25 16:52:33 +01:00
|
|
|
|
|
|
|
// functions to temporarily suspend/resume garbage collection
|
|
|
|
fn C.GC_disable()
|
|
|
|
|
|
|
|
fn C.GC_enable()
|
|
|
|
|
|
|
|
// returns non-zero if GC is disabled
|
|
|
|
fn C.GC_is_disabled() int
|
|
|
|
|
2021-04-09 12:13:49 +02:00
|
|
|
// protect memory block from being freed before this call
|
|
|
|
fn C.GC_reachable_here(voidptr)
|
|
|
|
|
2021-03-25 16:52:33 +01:00
|
|
|
// for leak detection it is advisable to do explicit garbage collections
|
|
|
|
pub fn gc_check_leaks() {
|
|
|
|
$if gcboehm_leak ? {
|
|
|
|
C.GC_gcollect()
|
|
|
|
}
|
|
|
|
}
|