31 changed files with 309 additions and 626 deletions
+1
View File
@@ -0,0 +1 @@
use flake
-11
View File
@@ -1,11 +0,0 @@
name: setup
description: Sets up a build environment
inputs: {}
outputs: {}
runs:
using: composite
steps:
- uses: swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2
- uses: homebrew/actions/setup-homebrew@e99025eb970cfa124b8284a35463d87426917478 # main
- run: brew install autoconf automake libtool
shell: sh
-26
View File
@@ -1,26 +0,0 @@
name: document
on:
push:
branches:
- main
pull_request:
permissions:
contents: read
pages: write
id-token: write
jobs:
document:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
submodules: true
- uses: actions/configure-pages@45bfe0192ca1faeb007ade9deae92b16b8254a0d # v6.0.0
- uses: swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2
- run: cargo doc --no-deps
- run: rm target/doc/.lock
- uses: actions/upload-pages-artifact@fc324d3547104276b827a68afc52ff2a11cc49c9 # v5.0.0
with:
path: target/doc
- uses: actions/deploy-pages@368f82528645a54fb793d4d04e342629a3f51346 # v5.0.1
if: github.ref == 'refs/heads/main'
+9 -19
View File
@@ -5,38 +5,28 @@ on:
- main
pull_request:
jobs:
clippy:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- uses: actions/checkout@v6
with:
submodules: true
- uses: swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2
- uses: swatinem/rust-cache@v2
- run: cargo clippy -- -D warnings
format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- run: cargo fmt --all -- --check
- uses: actions/checkout@v6
- run: cargo fmt -- --check
spell-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- uses: streetsidesoftware/cspell-action@6f3c77c1406bc930f944ba97e9801a22e42caf58 # v9.1.0
- uses: actions/checkout@v6
- uses: streetsidesoftware/cspell-action@main
with:
files: "**/*.{md,rs}"
readme:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- uses: lycheeverse/lychee-action@e7477775783ea5526144ba13e8db5eec57747ce8 # v2.9.0
lint:
needs:
- clippy
- format
- spell-check
- readme
if: always()
runs-on: ubuntu-latest
steps:
- run: for result in ${{ join(needs.*.result, ' ') }}; do [ $result = success ]; done
- uses: actions/checkout@v6
- uses: lycheeverse/lychee-action@v2
+3 -3
View File
@@ -14,12 +14,12 @@ jobs:
runs-on: ubuntu-latest
environment: ${{ github.ref == 'refs/heads/main' && 'release' || '' }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- uses: actions/checkout@v6
with:
submodules: true
- uses: swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2
- uses: swatinem/rust-cache@v2
- run: cargo install cargo-workspaces
- uses: rust-lang/crates-io-auth-action@c6f97d42243bad5fab37ca0427f495c86d5b1a18 # v1.0.5
- uses: rust-lang/crates-io-auth-action@v1
id: auth
if: github.ref == 'refs/heads/main'
- run: >
+29 -54
View File
@@ -6,59 +6,34 @@ on:
pull_request:
jobs:
build:
strategy:
fail-fast: false
matrix:
os:
- ubuntu-latest
- ubuntu-26.04-arm
- macos-latest
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
submodules: true
- uses: ./.github/actions/setup
- run: cargo build
unit_test:
strategy:
fail-fast: false
matrix:
os:
- ubuntu-latest
- ubuntu-26.04-arm
- macos-latest
options:
-
- --no-default-features --features cmake
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
submodules: true
- uses: ./.github/actions/setup
- run: cargo test ${{ matrix.options }}
examples:
strategy:
fail-fast: false
matrix:
os:
- ubuntu-latest
- ubuntu-26.04-arm
- macos-latest
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
submodules: true
- uses: ./.github/actions/setup
- run: tools/example_test.sh
test:
needs:
- build
- unit_test
- examples
if: always()
runs-on: ubuntu-latest
steps:
- run: for result in ${{ join(needs.*.result, ' ') }}; do [ $result = success ]; done
- uses: actions/checkout@v6
with:
submodules: true
- uses: swatinem/rust-cache@v2
- run: cargo build
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
with:
submodules: true
- uses: swatinem/rust-cache@v2
- run: cargo test
examples:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
with:
submodules: true
- uses: swatinem/rust-cache@v2
- run: tools/example_test.sh
cmake:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
with:
submodules: true
- uses: swatinem/rust-cache@v2
- run: cargo build --no-default-features --features cmake
+1
View File
@@ -1,2 +1,3 @@
.cargo_cache
target
.direnv
+2 -2
View File
@@ -1,6 +1,6 @@
[submodule "vendor/bdwgc"]
path = bdwgc-alloc-sys/vendor/bdwgc
path = vendor/bdwgc
url = https://github.com/bdwgc/bdwgc
[submodule "vendor/libatomic_ops"]
path = bdwgc-alloc-sys/vendor/libatomic_ops
path = vendor/libatomic_ops
url = https://github.com/bdwgc/libatomic_ops
Generated
+5 -208
View File
@@ -2,15 +2,6 @@
# It is not intended for manual editing.
version = 4
[[package]]
name = "aho-corasick"
version = "1.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c982642fa9e8606056828ee9a8505737230110bb1099153c79efe865c59d12ba"
dependencies = [
"memchr",
]
[[package]]
name = "autotools"
version = "0.2.7"
@@ -22,45 +13,13 @@ dependencies = [
[[package]]
name = "bdwgc-alloc"
version = "0.6.15"
dependencies = [
"bdwgc-alloc-sys",
]
[[package]]
name = "bdwgc-alloc-sys"
version = "0.6.15"
version = "0.6.13"
dependencies = [
"autotools",
"bindgen",
"cmake",
"libc",
]
[[package]]
name = "bindgen"
version = "0.73.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "787ef8ef523575546b106a58213d6e6b06198a05c2f757258c68a74273670cfa"
dependencies = [
"bitflags",
"cexpr",
"clang-sys",
"log",
"prettyplease",
"proc-macro2",
"quote",
"regex",
"rustc-hash",
"shlex 2.0.1",
"syn",
]
[[package]]
name = "bitflags"
version = "2.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06"
[[package]]
name = "cc"
version = "1.2.49"
@@ -68,33 +27,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90583009037521a116abf44494efecd645ba48b6622457080f080b85544e2215"
dependencies = [
"find-msvc-tools",
"shlex 1.3.0",
]
[[package]]
name = "cexpr"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766"
dependencies = [
"nom",
]
[[package]]
name = "cfg-if"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600"
[[package]]
name = "clang-sys"
version = "1.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "157a8ba7b480713b56f4c09fd13fc3e0a22a5dfab8097ba61cbc5feef950788a"
dependencies = [
"glob",
"libc",
"libloading",
"shlex",
]
[[package]]
@@ -133,118 +66,11 @@ dependencies = [
"bdwgc-alloc",
]
[[package]]
name = "glob"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e4eba85ea1d0a966a983acd07deee566e67395d2d96b6fb39e62b5a833f1eb0b"
[[package]]
name = "libc"
version = "0.2.189"
version = "0.2.186"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3eaf3ede3fee6db1a4c2ee091bf8a8b4dccdc6d17f656fb07896ee72867612f2"
[[package]]
name = "libloading"
version = "0.8.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7c4b02199fee7c5d21a5ae7d8cfa79a6ef5bb2fc834d6e9058e89c825efdc55"
dependencies = [
"cfg-if",
"windows-link",
]
[[package]]
name = "log"
version = "0.4.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f9f8bd3e56ce4dfc153cf470fffbfa98c7620958b312ca5c3a4b8d5181fd13c6"
[[package]]
name = "memchr"
version = "2.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"
[[package]]
name = "minimal-lexical"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
[[package]]
name = "nom"
version = "7.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a"
dependencies = [
"memchr",
"minimal-lexical",
]
[[package]]
name = "prettyplease"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2bfe0f4c752e450fc2faf62654f1c134747922825d5b04ca717b8874f41a40c0"
dependencies = [
"proc-macro2",
"syn",
]
[[package]]
name = "proc-macro2"
version = "1.0.107"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.47"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001"
dependencies = [
"proc-macro2",
]
[[package]]
name = "regex"
version = "1.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f020237b6c8eed93db2e2cb53c00c60a8e1bc73da7d073199a1180401450218d"
dependencies = [
"aho-corasick",
"memchr",
"regex-automata",
"regex-syntax",
]
[[package]]
name = "regex-automata"
version = "0.4.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ad8553b9b26413251cbf30e620595c7a41b3887f03da04579c0e6b0d6a06b4b2"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.8.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d6f6ff9a378485b298a5286656da665ba74413d36db0979633275d2e708145d4"
[[package]]
name = "rustc-hash"
version = "2.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6b1e7f9a428571be2dc5bc0505c13fb6bf936822b894ec87abf8a08a4e51742d"
checksum = "68ab91017fe16c622486840e4c83c9a37afeff978bd239b5293d61ece587de66"
[[package]]
name = "shlex"
@@ -252,38 +78,9 @@ version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]]
name = "shlex"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba"
[[package]]
name = "static_threads"
version = "0.1.0"
dependencies = [
"bdwgc-alloc",
]
[[package]]
name = "syn"
version = "3.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8593e8e72159ed2257d083c7a454a85cbf854f37a0966d8d483aff8c8a3ebcee"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d245f478577f809a851594d02313b640fb437e0bb33866753cff937863096954"
[[package]]
name = "windows-link"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
+20 -12
View File
@@ -1,21 +1,29 @@
[package]
name = "bdwgc-alloc"
version = "0.6.13"
edition = "2024"
description = "impl GlobalAlloc for bdwgc"
repository = "https://github.com/raviqqe/bdwgc-alloc"
license = "MIT"
categories = ["api-bindings", "memory-management", "no-std", "no-std::no-alloc"]
[workspace]
resolver = "3"
members = ["bdwgc-alloc", "bdwgc-alloc-sys", "examples/*"]
default-members = ["bdwgc-alloc", "bdwgc-alloc-sys"]
members = ["examples/*"]
[workspace.package]
version = "0.6.15"
edition = "2024"
repository = "https://github.com/bdwgc/bdwgc-rust"
license = "MIT"
[dependencies]
libc = "0.2.186"
[workspace.lints.clippy]
[build-dependencies]
autotools = { version = "0.2.7", optional = true }
cmake = { version = "0.1.58", optional = true }
[features]
default = ["autotools"]
[lints.clippy]
alloc_instead_of_core = "deny"
dbg_macro = "deny"
missing_const_for_fn = "deny"
std_instead_of_alloc = "deny"
std_instead_of_core = "deny"
[workspace.lints.rust]
missing_docs = "deny"
warnings = "deny"
+3 -5
View File
@@ -2,17 +2,15 @@
[![GitHub Action](https://img.shields.io/github/actions/workflow/status/bdwgc/bdwgc-rust/test.yaml?branch=main&style=flat-square)](https://github.com/bdwgc/bdwgc-rust/actions)
[![Crate](https://img.shields.io/crates/v/bdwgc-alloc.svg?style=flat-square)](https://crates.io/crates/bdwgc-alloc)
[![License](https://img.shields.io/github/license/bdwgc/bdwgc-rust.svg?style=flat-square)](https://github.com/bdwgc/bdwgc-rust/blob/main/LICENSE)
[![License](https://img.shields.io/github/license/bdwgc/bdwgc-rust.svg?style=flat-square)](LICENSE)
[`GlobalAlloc`](https://doc.rust-lang.org/std/alloc/trait.GlobalAlloc.html) implementation for [`bdwgc`][bdwgc], the conservative garbage collector.
This crate is for use cases in which developers need to integrate [`bdwgc`][bdwgc] into their programs written in Rust (e.g. writing a runtime library in Rust for their own programming language whose GC is done by [`bdwgc`][bdwgc].)
The raw bindings to the C API of [`bdwgc`][bdwgc] that this crate is built on are available as the `bdwgc-alloc-sys` crate.
## Usage
See [`examples`](https://github.com/bdwgc/bdwgc-rust/tree/main/examples) directory.
See [`examples`](examples) directory.
By default [`bdwgc`][bdwgc] is built with autotools. To build with cmake, enable the `cmake` feature:
@@ -22,6 +20,6 @@ cargo build --no-default-features --features cmake
## License
[MIT](https://github.com/bdwgc/bdwgc-rust/blob/main/LICENSE)
[MIT](LICENSE)
[bdwgc]: https://github.com/bdwgc/bdwgc
-25
View File
@@ -1,25 +0,0 @@
[package]
name = "bdwgc-alloc-sys"
version.workspace = true
edition.workspace = true
description = "Rust bindings to the bdwgc C API"
documentation = "https://bdwgc.github.io/bdwgc-rust/bdwgc_alloc_sys/"
repository.workspace = true
license.workspace = true
categories = ["external-ffi-bindings", "memory-management", "no-std", "no-std::no-alloc"]
links = "gc"
[build-dependencies]
autotools = { version = "0.2.7", optional = true }
bindgen = "0.73.2"
cmake = { version = "0.1.58", optional = true }
[features]
default = ["autotools"]
[lints]
workspace = true
[[test]]
name = "collector"
harness = false
-28
View File
@@ -1,28 +0,0 @@
# bdwgc-alloc-sys
[![GitHub Action](https://img.shields.io/github/actions/workflow/status/bdwgc/bdwgc-rust/test.yaml?branch=main&style=flat-square)](https://github.com/bdwgc/bdwgc-rust/actions)
[![License](https://img.shields.io/github/license/bdwgc/bdwgc-rust.svg?style=flat-square)](https://github.com/bdwgc/bdwgc-rust/blob/main/LICENSE)
Rust bindings to the C API of [`bdwgc`][bdwgc], the conservative garbage collector.
This crate builds [`bdwgc`][bdwgc] from its vendored source and links it statically. The bindings are generated from its headers at build time by [`bindgen`](https://github.com/rust-lang/rust-bindgen), which requires `libclang`.
See the [`bdwgc-alloc`](https://crates.io/crates/bdwgc-alloc) crate for a [`GlobalAlloc`](https://doc.rust-lang.org/std/alloc/trait.GlobalAlloc.html) implementation on top of these bindings.
## Install
```sh
cargo add bdwgc-alloc-sys
```
By default [`bdwgc`][bdwgc] is built with autotools. To build with cmake, enable the `cmake` feature:
```sh
cargo add bdwgc-alloc-sys --no-default-features --features cmake
```
## License
[MIT](https://github.com/bdwgc/bdwgc-rust/blob/main/LICENSE)
[bdwgc]: https://github.com/bdwgc/bdwgc
-93
View File
@@ -1,93 +0,0 @@
//! A build script.
use core::error::Error;
use std::{env, path::PathBuf};
// cspell: ignore reconf
const LIB_ATOMIC_OPS_DIR: &str = "vendor/libatomic_ops";
const LIB_GC_DIR: &str = "vendor/bdwgc";
fn main() -> Result<(), Box<dyn Error>> {
build_library()?;
bindgen::builder()
.header(format!("{LIB_GC_DIR}/include/gc.h"))
.clang_arg(format!("-I{LIB_GC_DIR}/include"))
.clang_arg("-DGC_THREADS")
.use_core()
.allowlist_item("GC_.*")
.default_macro_constant_type(bindgen::MacroTypeVariation::Signed)
.generate()?
.write_to_file(PathBuf::from(env::var("OUT_DIR")?).join("bindings.rs"))?;
Ok(())
}
cfg_select! {
feature = "cmake" => {
fn build_library() -> Result<(), Box<dyn Error>> {
use cmake::Config;
use std::path::Path;
let libatomic_include_path = Path::new(LIB_ATOMIC_OPS_DIR)
.join("src")
.canonicalize()?
.display()
.to_string()
.replace(r"\\?\", "");
let dst = Config::new(LIB_GC_DIR)
.profile("Release")
.define("GC_BUILD_SHARED_LIBS", "FALSE")
.cflag(format!("-I{}", libatomic_include_path))
.build();
println!(
"cargo:rustc-link-search=native={}",
dst.join("lib").display()
);
println!("cargo:rustc-link-lib=static=gc");
Ok(())
}
}
feature = "autotools" => {
fn build_library() -> Result<(), Box<dyn Error>> {
let dst = autotools::Config::new(LIB_ATOMIC_OPS_DIR)
.reconf("-i")
.cflag("-fPIC")
.build();
println!(
"cargo:rustc-link-search=native={}",
dst.join("lib").display()
);
println!("cargo:rustc-link-lib=static=atomic_ops");
let dst = autotools::Config::new(LIB_GC_DIR)
.reconf("-i")
.cflag(format!(
// spell-checker: disable-next-line
"-I{} -L/lib/x86_64-linux-gnu -lpthread -fPIC",
dst.join("include").display()
))
.build();
println!(
"cargo:rustc-link-search=native={}",
dst.join("lib").display()
);
println!("cargo:rustc-link-lib=static=gc");
for dir in &[LIB_ATOMIC_OPS_DIR, LIB_GC_DIR] {
std::process::Command::new("sh")
.arg("-c")
.arg(format!("cd {dir} && git clean -dfx"))
.output()?;
}
Ok(())
}
}
}
-8
View File
@@ -1,8 +0,0 @@
#![doc = include_str!("../README.md")]
#![no_std]
#![allow(missing_docs)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
-76
View File
@@ -1,76 +0,0 @@
//! A smoke test of the bindings.
use bdwgc_alloc_sys::{
GC_SUCCESS, GC_allow_register_threads, GC_free, GC_gcollect, GC_get_stack_base, GC_init,
GC_malloc, GC_realloc, GC_register_finalizer, GC_register_my_thread, GC_stack_base,
GC_unregister_my_thread,
};
use core::{
ffi::c_void,
ptr::null_mut,
sync::atomic::{AtomicUsize, Ordering},
};
use std::thread::spawn;
const TOTAL_COUNT: usize = 1 << 10;
static FINALIZED_COUNT: AtomicUsize = AtomicUsize::new(0);
fn main() {
unsafe {
GC_init();
GC_allow_register_threads();
}
allocate();
finalize();
register_thread();
}
fn allocate() {
let ptr = unsafe { GC_malloc(42) };
assert!(!ptr.is_null());
let ptr = unsafe { GC_realloc(ptr, 84) };
assert!(!ptr.is_null());
unsafe { GC_free(ptr) };
}
fn finalize() {
extern "C" fn count(_object: *mut c_void, _client_data: *mut c_void) {
FINALIZED_COUNT.fetch_add(1, Ordering::Relaxed);
}
for _ in 0..TOTAL_COUNT {
unsafe {
GC_register_finalizer(
GC_malloc(42),
Some(count),
null_mut(),
null_mut(),
null_mut(),
);
}
}
unsafe { GC_gcollect() };
// TODO Is there any way to collect all?
assert!(FINALIZED_COUNT.load(Ordering::Relaxed) > TOTAL_COUNT / 2);
}
fn register_thread() {
spawn(|| {
let mut base = GC_stack_base {
mem_base: null_mut(),
};
assert_eq!(unsafe { GC_get_stack_base(&mut base) }, GC_SUCCESS);
assert_eq!(unsafe { GC_register_my_thread(&base) }, GC_SUCCESS);
assert!(!unsafe { GC_malloc(42) }.is_null());
assert_eq!(unsafe { GC_unregister_my_thread() }, GC_SUCCESS);
})
.join()
.unwrap();
}
-20
View File
@@ -1,20 +0,0 @@
[package]
name = "bdwgc-alloc"
version.workspace = true
edition.workspace = true
description = "impl GlobalAlloc for bdwgc"
documentation = "https://bdwgc.github.io/bdwgc-rust/bdwgc_alloc/"
repository.workspace = true
license.workspace = true
categories = ["api-bindings", "memory-management", "no-std", "no-std::no-alloc"]
[dependencies]
bdwgc-alloc-sys = { version = "0.6.15", path = "../bdwgc-alloc-sys", default-features = false }
[features]
default = ["autotools"]
autotools = ["bdwgc-alloc-sys/autotools"]
cmake = ["bdwgc-alloc-sys/cmake"]
[lints]
workspace = true
-1
View File
@@ -1 +0,0 @@
../README.md
+71
View File
@@ -0,0 +1,71 @@
const LIB_ATOMIC_OPS_DIR: &str = "vendor/libatomic_ops";
const LIB_GC_DIR: &str = "vendor/bdwgc";
#[cfg(feature = "autotools")]
fn main() {
for dir in &[LIB_ATOMIC_OPS_DIR, LIB_GC_DIR] {
std::process::Command::new("sh")
.arg("-c")
.arg(format!("cd {dir} && ./autogen.sh"))
.output()
.unwrap();
}
let dst = autotools::Config::new(LIB_ATOMIC_OPS_DIR)
.cflag("-fPIC")
.build();
println!(
"cargo:rustc-link-search=native={}",
dst.join("lib64").display()
);
println!("cargo:rustc-link-lib=static=atomic_ops");
let dst = autotools::Config::new(LIB_GC_DIR)
.cflag(format!(
// spell-checker: disable-next-line
"-I{} -L/lib/x86_64-linux-gnu -lpthread -fPIC",
dst.join("include").display()
))
.build();
println!(
"cargo:rustc-link-search=native={}",
dst.join("lib64").display()
);
println!("cargo:rustc-link-lib=static=gc");
for dir in &[LIB_ATOMIC_OPS_DIR, LIB_GC_DIR] {
std::process::Command::new("sh")
.arg("-c")
.arg(format!("cd {dir} && git clean -dfx"))
.output()
.unwrap();
}
}
#[cfg(feature = "cmake")]
fn main() {
use cmake::Config;
use std::path::Path;
let libatomic_include_path = Path::new(LIB_ATOMIC_OPS_DIR)
.join("src")
.canonicalize()
.unwrap()
.display()
.to_string()
.replace(r"\\?\", "");
let dst = Config::new(LIB_GC_DIR)
.profile("Release")
.define("GC_BUILD_SHARED_LIBS", "FALSE")
.cflag(format!("-I{}", libatomic_include_path))
.build();
println!(
"cargo:rustc-link-search=native={}",
dst.join("lib64").display()
);
println!("cargo:rustc-link-lib=static=gc");
}
-3
View File
@@ -3,17 +3,14 @@
"autogen",
"autotools",
"bdwgc",
"bindgen",
"canonicalize",
"cflag",
"clippy",
"dealloc",
"finalizer",
"gcollect",
"gitsubmodule",
"libatomic",
"libc",
"libclang",
"realloc",
"repr",
"rustc",
+1 -1
View File
@@ -5,4 +5,4 @@ edition = "2024"
publish = false
[dependencies]
bdwgc-alloc = { path = "../../bdwgc-alloc" }
bdwgc-alloc = { path = "../.." }
+1 -1
View File
@@ -5,4 +5,4 @@ edition = "2024"
publish = false
[dependencies]
bdwgc-alloc = { path = "../../bdwgc-alloc" }
bdwgc-alloc = { path = "../.." }
+1 -1
View File
@@ -5,4 +5,4 @@ edition = "2024"
publish = false
[dependencies]
bdwgc-alloc = { path = "../../bdwgc-alloc" }
bdwgc-alloc = { path = "../.." }
+1 -1
View File
@@ -5,4 +5,4 @@ edition = "2024"
publish = false
[dependencies]
bdwgc-alloc = { path = "../../bdwgc-alloc" }
bdwgc-alloc = { path = "../.." }
Generated
+66
View File
@@ -0,0 +1,66 @@
{
"nodes": {
"fenix": {
"inputs": {
"nixpkgs": [
"nixpkgs"
],
"rust-analyzer-src": "rust-analyzer-src"
},
"locked": {
"lastModified": 1779185128,
"narHash": "sha256-Kl2bkmwZJD3n2KWDxuIlturZ7emqRK+anpD1LmDwpmY=",
"owner": "nix-community",
"repo": "fenix",
"rev": "b7bd9323fe26a3b4f4bddbb2c2a1dacabced2f88",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "fenix",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1778869304,
"narHash": "sha256-30sZNZoA1cqF5JNO9fVX+wgiQYjB7HJqqJ4ztCDeBZE=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "d233902339c02a9c334e7e593de68855ad26c4cb",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"fenix": "fenix",
"nixpkgs": "nixpkgs"
}
},
"rust-analyzer-src": {
"flake": false,
"locked": {
"lastModified": 1779074864,
"narHash": "sha256-0M3WqsWmtXmv9Ev/vnFfCHosWvISDwiuuhQ104UO3CI=",
"owner": "rust-lang",
"repo": "rust-analyzer",
"rev": "cdfe408d4b436e806ff525cb3e67588a6a009ed1",
"type": "github"
},
"original": {
"owner": "rust-lang",
"ref": "nightly",
"repo": "rust-analyzer",
"type": "github"
}
}
},
"root": "root",
"version": 7
}
+54
View File
@@ -0,0 +1,54 @@
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
fenix = {
url = "github:nix-community/fenix";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = { self, nixpkgs, fenix, ... }:
let
supportedSystems = [
"aarch64-darwin" "aarch64-linux"
"x86_64-darwin" "x86_64-linux"
];
each-system = f: nixpkgs.lib.genAttrs supportedSystems (system: f rec {
pkgs = import nixpkgs {
inherit system overlays;
};
inherit (pkgs) lib;
inherit system;
});
overlays = [
fenix.overlays.default
];
in {
_pkgs = each-system ({ pkgs, ... }: pkgs);
devShells = each-system ({ pkgs, lib, ... }: {
default = pkgs.mkShell {
packages = [
pkgs.pkg-config
(pkgs.fenix.complete.withComponents [
"cargo" "clippy" "rust-src" "rustc" "rustfmt"
])
pkgs.rust-analyzer-nightly
pkgs.cmake
];
};
});
};
nixConfig = {
extra-substituters = [
"https://fenix.cachix.org"
];
extra-trusted-public-keys = [
"fenix.cachix.org-1:ecJhr+RdYEdcVgUkjruiYhjbBloIEGov7bos90cZi0Q="
];
};
}
+40 -27
View File
@@ -3,18 +3,41 @@
mod error;
use bdwgc_alloc_sys::{
GC_SUCCESS, GC_alloc_lock, GC_alloc_unlock, GC_allow_register_threads, GC_free, GC_gcollect,
GC_get_stack_base, GC_init, GC_malloc, GC_realloc, GC_register_finalizer,
GC_register_my_thread, GC_set_stackbottom, GC_stack_base, GC_unregister_my_thread,
};
use core::{
alloc::{GlobalAlloc, Layout},
ffi::c_void,
ptr::null_mut,
};
use core::alloc::{GlobalAlloc, Layout};
use core::ptr::null;
use libc::{c_int, c_void, size_t};
const GC_SUCCESS: c_int = 0;
#[repr(C)]
struct GcStackBase {
mem_base: *const c_void,
// TODO: Add reg_base field to support IA64.
}
#[link(name = "gc", kind = "static")]
unsafe extern "C" {
fn GC_allow_register_threads();
fn GC_alloc_lock();
fn GC_alloc_unlock();
fn GC_free(ptr: *mut c_void);
fn GC_get_stack_base(stack_base: *mut GcStackBase) -> c_int;
fn GC_init();
fn GC_malloc(size: size_t) -> *mut c_void;
fn GC_realloc(ptr: *mut c_void, size: size_t) -> *mut c_void;
fn GC_register_my_thread(stack_base: *const GcStackBase) -> c_int;
fn GC_set_stackbottom(thread: *const c_void, stack_bottom: *const GcStackBase);
fn GC_unregister_my_thread();
fn GC_gcollect();
fn GC_register_finalizer(
ptr: *const c_void,
finalizer: extern "C" fn(*mut c_void, *mut c_void),
client_data: *const c_void,
opt_old_finalizer: *const c_void,
opt_old_client_data: *const c_void,
) -> *mut c_void;
}
/// An allocator.
pub struct Allocator;
impl Allocator {
@@ -46,9 +69,7 @@ impl Allocator {
///
/// This function must not be called in a main thread.
pub unsafe fn register_current_thread() -> Result<(), error::Error> {
let mut base = GC_stack_base {
mem_base: null_mut(),
};
let mut base = GcStackBase { mem_base: null() };
if unsafe { GC_get_stack_base(&mut base) } != GC_SUCCESS {
return Err(error::Error::new("failed to get stack base"));
@@ -70,9 +91,9 @@ impl Allocator {
pub unsafe fn set_stack_bottom(bottom: *const u8) {
unsafe {
GC_set_stackbottom(
null_mut(),
&GC_stack_base {
mem_base: bottom.cast_mut().cast(),
null(),
&GcStackBase {
mem_base: bottom as *const libc::c_void,
},
)
}
@@ -84,7 +105,7 @@ impl Allocator {
///
/// The thread must be registered already.
pub unsafe fn unregister_current_thread() {
unsafe { GC_unregister_my_thread() };
unsafe { GC_unregister_my_thread() }
}
/// Runs a garbage collection forcibly.
@@ -102,15 +123,7 @@ impl Allocator {
finalizer: extern "C" fn(*mut c_void, *mut c_void),
client_data: *const c_void,
) {
unsafe {
GC_register_finalizer(
ptr.cast_mut(),
Some(finalizer),
client_data.cast_mut(),
null_mut(),
null_mut(),
)
};
unsafe { GC_register_finalizer(ptr, finalizer, client_data, null(), null()) };
}
}
Vendored Submodule
+1
Submodule vendor/bdwgc added at 82028767b4