2021-03-20 16:16:36 +03:00
|
|
|
module builtin
|
|
|
|
|
2022-07-30 14:59:12 +03:00
|
|
|
#flag -DGC_THREADS=1
|
|
|
|
|
2022-04-18 10:50:21 +03:00
|
|
|
$if dynamic_boehm ? {
|
|
|
|
$if windows {
|
2021-07-24 16:04:26 +03:00
|
|
|
$if tinyc {
|
2022-06-09 00:44:29 +03:00
|
|
|
#flag -I @VEXEROOT/thirdparty/libgc/include
|
|
|
|
#flag -L @VEXEROOT/thirdparty/tcc/lib
|
2021-07-24 16:04:26 +03:00
|
|
|
#flag -lgc
|
2022-01-11 16:23:21 +03:00
|
|
|
} $else $if msvc {
|
|
|
|
#flag -DGC_BUILTIN_ATOMIC=1
|
2022-06-09 00:44:29 +03:00
|
|
|
#flag -I @VEXEROOT/thirdparty/libgc/include
|
2021-07-24 16:04:26 +03:00
|
|
|
} $else {
|
2022-04-18 10:50:21 +03:00
|
|
|
#flag -DGC_WIN32_THREADS=1
|
2021-07-24 16:04:26 +03:00
|
|
|
#flag -DGC_BUILTIN_ATOMIC=1
|
2022-06-09 00:44:29 +03:00
|
|
|
#flag -I @VEXEROOT/thirdparty/libgc
|
2021-07-24 16:04:26 +03:00
|
|
|
#flag @VEXEROOT/thirdparty/libgc/gc.o
|
|
|
|
}
|
2021-04-08 19:17:04 +03:00
|
|
|
} $else {
|
2022-04-18 10:50:21 +03: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 19:17:04 +03:00
|
|
|
}
|
|
|
|
} $else {
|
2022-04-18 10:50:21 +03:00
|
|
|
$if macos || linux {
|
2022-08-14 13:16:52 +03:00
|
|
|
#flag -DGC_BUILTIN_ATOMIC=1
|
2022-06-09 00:44:29 +03:00
|
|
|
#flag -I @VEXEROOT/thirdparty/libgc/include
|
2022-09-21 12:19:02 +03:00
|
|
|
$if (prod && !tinyc && !debug) || !(amd64 || arm64 || i386 || arm32) {
|
2022-04-18 10:50:21 +03:00
|
|
|
// 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-09-21 12:19:02 +03:00
|
|
|
$if macos {
|
|
|
|
#flag -DMPROTECT_VDB=1
|
|
|
|
}
|
2022-04-21 00:12:17 +03:00
|
|
|
#flag -ldl
|
2022-05-30 15:17:01 +03:00
|
|
|
#flag -lpthread
|
2022-04-18 10:50:21 +03:00
|
|
|
} $else $if freebsd {
|
|
|
|
// Tested on FreeBSD 13.0-RELEASE-p3, with clang, gcc and tcc:
|
2022-08-14 13:16:52 +03:00
|
|
|
#flag -DGC_BUILTIN_ATOMIC=1
|
2022-04-18 10:50:21 +03:00
|
|
|
#flag -DBUS_PAGE_FAULT=T_PAGEFLT
|
|
|
|
$if !tinyc {
|
2022-06-09 00:44:29 +03:00
|
|
|
#flag -I @VEXEROOT/thirdparty/libgc/include
|
2022-04-18 10:50:21 +03:00
|
|
|
#flag @VEXEROOT/thirdparty/libgc/gc.o
|
2022-05-03 09:17:53 +03:00
|
|
|
}
|
|
|
|
$if tinyc {
|
2022-04-18 10:50:21 +03:00
|
|
|
#flag -I/usr/local/include
|
|
|
|
#flag $first_existing("/usr/local/lib/libgc.a", "/usr/lib/libgc.a")
|
2022-05-03 09:17:53 +03:00
|
|
|
#flag -lgc
|
2022-04-18 10:50:21 +03:00
|
|
|
}
|
|
|
|
#flag -lpthread
|
|
|
|
} $else $if openbsd {
|
2022-08-14 13:16:52 +03:00
|
|
|
#flag -DGC_BUILTIN_ATOMIC=1
|
2021-06-01 22:28:30 +03:00
|
|
|
#flag -I/usr/local/include
|
2022-04-18 10:50:21 +03:00
|
|
|
#flag $first_existing("/usr/local/lib/libgc.a", "/usr/lib/libgc.a")
|
|
|
|
#flag -lpthread
|
|
|
|
} $else $if windows {
|
2022-05-22 21:25:46 +03:00
|
|
|
#flag -DGC_NOT_DLL=1
|
|
|
|
#flag -DGC_WIN32_THREADS=1
|
2021-07-24 16:04:26 +03:00
|
|
|
$if tinyc {
|
2022-08-14 13:16:52 +03:00
|
|
|
#flag -DGC_BUILTIN_ATOMIC=1
|
2022-06-09 00:44:29 +03:00
|
|
|
#flag -I @VEXEROOT/thirdparty/libgc/include
|
2022-05-22 21:25:46 +03:00
|
|
|
#flag @VEXEROOT/thirdparty/tcc/lib/libgc.a
|
|
|
|
#flag -luser32
|
2022-08-14 13:16:52 +03:00
|
|
|
} $else $if msvc {
|
|
|
|
// Build libatomic_ops
|
|
|
|
#flag @VEXEROOT/thirdparty/libatomic_ops/atomic_ops.o
|
|
|
|
#flag -I @VEXEROOT/thirdparty/libatomic_ops
|
|
|
|
|
|
|
|
#flag -I @VEXEROOT/thirdparty/libgc/include
|
|
|
|
#flag @VEXEROOT/thirdparty/libgc/gc.o
|
2021-07-24 16:04:26 +03:00
|
|
|
} $else {
|
2022-08-14 13:16:52 +03:00
|
|
|
#flag -DGC_BUILTIN_ATOMIC=1
|
2022-06-09 00:44:29 +03:00
|
|
|
#flag -I @VEXEROOT/thirdparty/libgc/include
|
2021-07-24 16:04:26 +03:00
|
|
|
#flag @VEXEROOT/thirdparty/libgc/gc.o
|
|
|
|
}
|
2022-04-18 10:50:21 +03:00
|
|
|
} $else $if $pkgconfig('bdw-gc') {
|
2022-08-14 13:16:52 +03:00
|
|
|
#flag -DGC_BUILTIN_ATOMIC=1
|
2022-04-18 10:50:21 +03:00
|
|
|
#pkgconfig bdw-gc
|
2021-07-24 16:04:26 +03:00
|
|
|
} $else {
|
2022-08-14 13:16:52 +03:00
|
|
|
#flag -DGC_BUILTIN_ATOMIC=1
|
2021-07-24 16:04:26 +03:00
|
|
|
#flag -lgc
|
2021-04-08 19:17:04 +03:00
|
|
|
}
|
2021-03-25 08:28:30 +03:00
|
|
|
}
|
2021-04-08 19:17:04 +03:00
|
|
|
|
2021-03-25 18:52:33 +03:00
|
|
|
$if gcboehm_leak ? {
|
2021-07-24 16:04:26 +03:00
|
|
|
#flag -DGC_DEBUG=1
|
2021-03-25 18:52:33 +03:00
|
|
|
}
|
2021-03-20 16:16:36 +03:00
|
|
|
|
2021-04-08 19:17:04 +03:00
|
|
|
#include <gc.h>
|
2021-03-20 16:16:36 +03:00
|
|
|
|
2021-03-25 18:52:33 +03: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 13:09:32 +03:00
|
|
|
fn C.GC_MALLOC(n usize) voidptr
|
2021-03-20 16:16:36 +03:00
|
|
|
|
2021-09-08 13:09:32 +03:00
|
|
|
fn C.GC_MALLOC_ATOMIC(n usize) voidptr
|
2021-04-14 01:40:26 +03:00
|
|
|
|
2021-09-08 13:09:32 +03:00
|
|
|
fn C.GC_MALLOC_UNCOLLECTABLE(n usize) voidptr
|
2021-03-26 17:44:45 +03:00
|
|
|
|
2021-09-08 13:09:32 +03:00
|
|
|
fn C.GC_REALLOC(ptr voidptr, n usize) voidptr
|
2021-03-20 16:16:36 +03:00
|
|
|
|
|
|
|
fn C.GC_FREE(ptr voidptr)
|
2021-03-21 05:31:06 +03:00
|
|
|
|
2022-04-18 10:50:21 +03:00
|
|
|
// explicitly perform garbage collection now! Garbage collections
|
2021-04-07 16:25:11 +03:00
|
|
|
// are done automatically when needed, so this function is hardly needed
|
2021-03-21 05:31:06 +03:00
|
|
|
fn C.GC_gcollect()
|
2021-03-25 18:52:33 +03: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 13:13:49 +03:00
|
|
|
// protect memory block from being freed before this call
|
|
|
|
fn C.GC_reachable_here(voidptr)
|
|
|
|
|
2021-03-25 18:52:33 +03:00
|
|
|
// for leak detection it is advisable to do explicit garbage collections
|
|
|
|
pub fn gc_check_leaks() {
|
|
|
|
$if gcboehm_leak ? {
|
|
|
|
C.GC_gcollect()
|
|
|
|
}
|
|
|
|
}
|
2023-05-03 20:33:52 +03:00
|
|
|
|
|
|
|
fn C.GC_get_heap_usage_safe(pheap_size &usize, pfree_bytes &usize, punmapped_bytes &usize, pbytes_since_gc &usize, ptotal_bytes &usize)
|
|
|
|
fn C.GC_get_memory_use() usize
|