forked from GitHub/bdwgc-rust
Compare commits
6 Commits
main
...
feature/ru
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f52f6dcfaf | ||
|
|
3de34a77e9 | ||
|
|
81df12684f | ||
|
|
a3a13f061f | ||
|
|
085028a8e1 | ||
|
|
04f5f88b33 |
11
.cspell.json
Normal file
11
.cspell.json
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"words": [
|
||||||
|
"dealloc",
|
||||||
|
"finalizer",
|
||||||
|
"gcollect",
|
||||||
|
"libc",
|
||||||
|
"realloc",
|
||||||
|
"repr",
|
||||||
|
"stackbottom"
|
||||||
|
]
|
||||||
|
}
|
||||||
11
.github/mergify.yml
vendored
Normal file
11
.github/mergify.yml
vendored
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
pull_request_rules:
|
||||||
|
- name: dependabot
|
||||||
|
conditions:
|
||||||
|
- author=dependabot[bot]
|
||||||
|
actions:
|
||||||
|
queue:
|
||||||
|
name: default
|
||||||
|
method: squash
|
||||||
|
queue_rules:
|
||||||
|
- name: default
|
||||||
|
conditions: []
|
||||||
13
.github/workflows/dependabot.yaml
vendored
13
.github/workflows/dependabot.yaml
vendored
@@ -1,13 +0,0 @@
|
|||||||
name: dependabot
|
|
||||||
on: pull_request
|
|
||||||
permissions:
|
|
||||||
contents: write
|
|
||||||
pull-requests: write
|
|
||||||
jobs:
|
|
||||||
merge:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
if: github.actor == 'dependabot[bot]'
|
|
||||||
steps:
|
|
||||||
- run: gh pr merge --auto --squash ${{ github.event.pull_request.html_url }}
|
|
||||||
env:
|
|
||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
14
.github/workflows/lint.yaml
vendored
14
.github/workflows/lint.yaml
vendored
@@ -8,25 +8,27 @@ jobs:
|
|||||||
lint:
|
lint:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v6
|
- uses: actions/checkout@v3
|
||||||
with:
|
with:
|
||||||
submodules: true
|
submodules: true
|
||||||
- uses: swatinem/rust-cache@v2
|
- uses: Swatinem/rust-cache@v2
|
||||||
- run: cargo clippy -- -D warnings
|
- run: cargo clippy -- -D warnings
|
||||||
format:
|
format:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v6
|
- uses: actions/checkout@v3
|
||||||
- run: cargo fmt -- --check
|
- run: cargo fmt -- --check
|
||||||
spell-check:
|
spell-check:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v6
|
- uses: actions/checkout@v3
|
||||||
- uses: streetsidesoftware/cspell-action@main
|
- uses: streetsidesoftware/cspell-action@main
|
||||||
with:
|
with:
|
||||||
files: "**/*.{md,rs}"
|
files: "**/*.{md,rs}"
|
||||||
readme:
|
readme:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v6
|
- uses: actions/checkout@v3
|
||||||
- uses: lycheeverse/lychee-action@v2
|
- uses: lycheeverse/lychee-action@v1
|
||||||
|
with:
|
||||||
|
fail: true
|
||||||
|
|||||||
29
.github/workflows/release.yaml
vendored
29
.github/workflows/release.yaml
vendored
@@ -1,29 +0,0 @@
|
|||||||
name: release
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- main
|
|
||||||
pull_request:
|
|
||||||
concurrency:
|
|
||||||
group: release-${{ github.ref }}
|
|
||||||
cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}
|
|
||||||
jobs:
|
|
||||||
release:
|
|
||||||
permissions:
|
|
||||||
id-token: write
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
environment: ${{ github.ref == 'refs/heads/main' && 'release' || '' }}
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v6
|
|
||||||
with:
|
|
||||||
submodules: true
|
|
||||||
- uses: swatinem/rust-cache@v2
|
|
||||||
- run: cargo install cargo-workspaces
|
|
||||||
- uses: rust-lang/crates-io-auth-action@v1
|
|
||||||
id: auth
|
|
||||||
if: github.ref == 'refs/heads/main'
|
|
||||||
- run: >
|
|
||||||
cargo workspaces publish -y --from-git --no-verify
|
|
||||||
${{ github.ref != 'refs/heads/main' && '--dry-run' || '' }}
|
|
||||||
env:
|
|
||||||
CARGO_REGISTRY_TOKEN: ${{ steps.auth.outputs.token }}
|
|
||||||
20
.github/workflows/test.yaml
vendored
20
.github/workflows/test.yaml
vendored
@@ -8,32 +8,34 @@ jobs:
|
|||||||
build:
|
build:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v6
|
- uses: actions/checkout@v3
|
||||||
with:
|
with:
|
||||||
submodules: true
|
submodules: true
|
||||||
- uses: swatinem/rust-cache@v2
|
- uses: Swatinem/rust-cache@v2
|
||||||
- run: cargo build
|
- run: cargo build
|
||||||
test:
|
test:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v6
|
- uses: actions/checkout@v3
|
||||||
with:
|
with:
|
||||||
submodules: true
|
submodules: true
|
||||||
- uses: swatinem/rust-cache@v2
|
- uses: Swatinem/rust-cache@v2
|
||||||
- run: cargo test
|
- run: cargo test
|
||||||
examples:
|
examples:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v6
|
- uses: actions/checkout@v3
|
||||||
with:
|
with:
|
||||||
submodules: true
|
submodules: true
|
||||||
- uses: swatinem/rust-cache@v2
|
- uses: Swatinem/rust-cache@v2
|
||||||
- run: tools/example_test.sh
|
- run: |
|
||||||
|
cd examples
|
||||||
|
./test.sh
|
||||||
cmake:
|
cmake:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v6
|
- uses: actions/checkout@v3
|
||||||
with:
|
with:
|
||||||
submodules: true
|
submodules: true
|
||||||
- uses: swatinem/rust-cache@v2
|
- uses: Swatinem/rust-cache@v2
|
||||||
- run: cargo build --no-default-features --features cmake
|
- run: cargo build --no-default-features --features cmake
|
||||||
|
|||||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,3 +1,2 @@
|
|||||||
.cargo_cache
|
*.lock
|
||||||
target
|
target
|
||||||
.direnv
|
|
||||||
|
|||||||
4
.gitmodules
vendored
4
.gitmodules
vendored
@@ -1,6 +1,6 @@
|
|||||||
[submodule "vendor/bdwgc"]
|
[submodule "vendor/bdwgc"]
|
||||||
path = vendor/bdwgc
|
path = vendor/bdwgc
|
||||||
url = https://github.com/bdwgc/bdwgc
|
url = https://github.com/ivmai/bdwgc
|
||||||
[submodule "vendor/libatomic_ops"]
|
[submodule "vendor/libatomic_ops"]
|
||||||
path = vendor/libatomic_ops
|
path = vendor/libatomic_ops
|
||||||
url = https://github.com/bdwgc/libatomic_ops
|
url = https://github.com/ivmai/libatomic_ops
|
||||||
|
|||||||
86
Cargo.lock
generated
86
Cargo.lock
generated
@@ -1,86 +0,0 @@
|
|||||||
# This file is automatically @generated by Cargo.
|
|
||||||
# It is not intended for manual editing.
|
|
||||||
version = 4
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "autotools"
|
|
||||||
version = "0.2.7"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "ef941527c41b0fc0dd48511a8154cd5fc7e29200a0ff8b7203c5d777dbc795cf"
|
|
||||||
dependencies = [
|
|
||||||
"cc",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "bdwgc-alloc"
|
|
||||||
version = "0.6.13"
|
|
||||||
dependencies = [
|
|
||||||
"autotools",
|
|
||||||
"cmake",
|
|
||||||
"libc",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "cc"
|
|
||||||
version = "1.2.49"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "90583009037521a116abf44494efecd645ba48b6622457080f080b85544e2215"
|
|
||||||
dependencies = [
|
|
||||||
"find-msvc-tools",
|
|
||||||
"shlex",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "cmake"
|
|
||||||
version = "0.1.58"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "c0f78a02292a74a88ac736019ab962ece0bc380e3f977bf72e376c5d78ff0678"
|
|
||||||
dependencies = [
|
|
||||||
"cc",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "dynamic_threads"
|
|
||||||
version = "0.1.0"
|
|
||||||
dependencies = [
|
|
||||||
"bdwgc-alloc",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "find-msvc-tools"
|
|
||||||
version = "0.1.5"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "3a3076410a55c90011c298b04d0cfa770b00fa04e1e3c97d3f6c9de105a03844"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "free_by_borrow"
|
|
||||||
version = "0.1.0"
|
|
||||||
dependencies = [
|
|
||||||
"bdwgc-alloc",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "free_by_gc"
|
|
||||||
version = "0.1.0"
|
|
||||||
dependencies = [
|
|
||||||
"bdwgc-alloc",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "libc"
|
|
||||||
version = "0.2.186"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "68ab91017fe16c622486840e4c83c9a37afeff978bd239b5293d61ece587de66"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "shlex"
|
|
||||||
version = "1.3.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "static_threads"
|
|
||||||
version = "0.1.0"
|
|
||||||
dependencies = [
|
|
||||||
"bdwgc-alloc",
|
|
||||||
]
|
|
||||||
33
Cargo.toml
33
Cargo.toml
@@ -1,29 +1,22 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "bdwgc-alloc"
|
name = "bdwgc-alloc"
|
||||||
version = "0.6.13"
|
|
||||||
edition = "2024"
|
|
||||||
description = "impl GlobalAlloc for bdwgc"
|
description = "impl GlobalAlloc for bdwgc"
|
||||||
|
version = "0.6.5"
|
||||||
|
authors = [
|
||||||
|
"swgillespie <sean.william.g@gmail.com>",
|
||||||
|
"Yota Toyama <raviqqe@gmail.com>",
|
||||||
|
]
|
||||||
repository = "https://github.com/raviqqe/bdwgc-alloc"
|
repository = "https://github.com/raviqqe/bdwgc-alloc"
|
||||||
|
edition = "2021"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
categories = ["api-bindings", "memory-management", "no-std", "no-std::no-alloc"]
|
build = "build.rs"
|
||||||
|
|
||||||
[workspace]
|
|
||||||
resolver = "3"
|
|
||||||
members = ["examples/*"]
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
libc = "0.2.186"
|
|
||||||
|
|
||||||
[build-dependencies]
|
|
||||||
autotools = { version = "0.2.7", optional = true }
|
|
||||||
cmake = { version = "0.1.58", optional = true }
|
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["autotools"]
|
default = ["autotools"]
|
||||||
|
|
||||||
[lints.clippy]
|
[dependencies]
|
||||||
alloc_instead_of_core = "deny"
|
libc = "0.2"
|
||||||
dbg_macro = "deny"
|
|
||||||
missing_const_for_fn = "deny"
|
[build-dependencies]
|
||||||
std_instead_of_alloc = "deny"
|
autotools = { version = "0.2", optional = true }
|
||||||
std_instead_of_core = "deny"
|
cmake = { version = "0.1", optional = true }
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
# bdwgc-rust
|
# bdwgc-alloc
|
||||||
|
|
||||||
[](https://github.com/bdwgc/bdwgc-rust/actions)
|
[](https://github.com/raviqqe/bdwgc-alloc/actions)
|
||||||
[](https://crates.io/crates/bdwgc-alloc)
|
[](https://crates.io/crates/bdwgc-alloc)
|
||||||
[](LICENSE)
|
[](LICENSE)
|
||||||
|
|
||||||
[`GlobalAlloc`](https://doc.rust-lang.org/std/alloc/trait.GlobalAlloc.html) implementation for [`bdwgc`][bdwgc], the conservative garbage collector.
|
[`GlobalAlloc`](https://doc.rust-lang.org/std/alloc/trait.GlobalAlloc.html) implementation for [`bdwgc`][bdwgc], the conservative garbage collector.
|
||||||
|
|
||||||
@@ -22,4 +22,4 @@ cargo build --no-default-features --features cmake
|
|||||||
|
|
||||||
[MIT](LICENSE)
|
[MIT](LICENSE)
|
||||||
|
|
||||||
[bdwgc]: https://github.com/bdwgc/bdwgc
|
[bdwgc]: https://github.com/ivmai/bdwgc
|
||||||
|
|||||||
13
build.rs
13
build.rs
@@ -6,7 +6,7 @@ fn main() {
|
|||||||
for dir in &[LIB_ATOMIC_OPS_DIR, LIB_GC_DIR] {
|
for dir in &[LIB_ATOMIC_OPS_DIR, LIB_GC_DIR] {
|
||||||
std::process::Command::new("sh")
|
std::process::Command::new("sh")
|
||||||
.arg("-c")
|
.arg("-c")
|
||||||
.arg(format!("cd {dir} && ./autogen.sh"))
|
.arg(format!("cd {} && ./autogen.sh", dir))
|
||||||
.output()
|
.output()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
@@ -17,13 +17,12 @@ fn main() {
|
|||||||
|
|
||||||
println!(
|
println!(
|
||||||
"cargo:rustc-link-search=native={}",
|
"cargo:rustc-link-search=native={}",
|
||||||
dst.join("lib64").display()
|
dst.join("lib").display()
|
||||||
);
|
);
|
||||||
println!("cargo:rustc-link-lib=static=atomic_ops");
|
println!("cargo:rustc-link-lib=static=atomic_ops");
|
||||||
|
|
||||||
let dst = autotools::Config::new(LIB_GC_DIR)
|
let dst = autotools::Config::new(LIB_GC_DIR)
|
||||||
.cflag(format!(
|
.cflag(format!(
|
||||||
// spell-checker: disable-next-line
|
|
||||||
"-I{} -L/lib/x86_64-linux-gnu -lpthread -fPIC",
|
"-I{} -L/lib/x86_64-linux-gnu -lpthread -fPIC",
|
||||||
dst.join("include").display()
|
dst.join("include").display()
|
||||||
))
|
))
|
||||||
@@ -31,14 +30,14 @@ fn main() {
|
|||||||
|
|
||||||
println!(
|
println!(
|
||||||
"cargo:rustc-link-search=native={}",
|
"cargo:rustc-link-search=native={}",
|
||||||
dst.join("lib64").display()
|
dst.join("lib").display()
|
||||||
);
|
);
|
||||||
println!("cargo:rustc-link-lib=static=gc");
|
println!("cargo:rustc-link-lib=static=gc");
|
||||||
|
|
||||||
for dir in &[LIB_ATOMIC_OPS_DIR, LIB_GC_DIR] {
|
for dir in &[LIB_ATOMIC_OPS_DIR, LIB_GC_DIR] {
|
||||||
std::process::Command::new("sh")
|
std::process::Command::new("sh")
|
||||||
.arg("-c")
|
.arg("-c")
|
||||||
.arg(format!("cd {dir} && git clean -dfx"))
|
.arg(format!("cd {} && git clean -dfx", dir))
|
||||||
.output()
|
.output()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
@@ -59,13 +58,13 @@ fn main() {
|
|||||||
|
|
||||||
let dst = Config::new(LIB_GC_DIR)
|
let dst = Config::new(LIB_GC_DIR)
|
||||||
.profile("Release")
|
.profile("Release")
|
||||||
.define("GC_BUILD_SHARED_LIBS", "FALSE")
|
.define("BUILD_SHARED_LIBS", "FALSE")
|
||||||
.cflag(format!("-I{}", libatomic_include_path))
|
.cflag(format!("-I{}", libatomic_include_path))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
println!(
|
println!(
|
||||||
"cargo:rustc-link-search=native={}",
|
"cargo:rustc-link-search=native={}",
|
||||||
dst.join("lib64").display()
|
dst.join("lib").display()
|
||||||
);
|
);
|
||||||
println!("cargo:rustc-link-lib=static=gc");
|
println!("cargo:rustc-link-lib=static=gc");
|
||||||
}
|
}
|
||||||
|
|||||||
21
cspell.json
21
cspell.json
@@ -1,21 +0,0 @@
|
|||||||
{
|
|
||||||
"words": [
|
|
||||||
"autogen",
|
|
||||||
"autotools",
|
|
||||||
"bdwgc",
|
|
||||||
"canonicalize",
|
|
||||||
"cflag",
|
|
||||||
"clippy",
|
|
||||||
"dealloc",
|
|
||||||
"finalizer",
|
|
||||||
"gcollect",
|
|
||||||
"libatomic",
|
|
||||||
"libc",
|
|
||||||
"realloc",
|
|
||||||
"repr",
|
|
||||||
"rustc",
|
|
||||||
"stackbottom",
|
|
||||||
"swatinem",
|
|
||||||
"unregisters"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
7
examples/Cargo.toml
Normal file
7
examples/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
[workspace]
|
||||||
|
members = [
|
||||||
|
"dynamic_threads",
|
||||||
|
"free_by_borrow",
|
||||||
|
"free_by_gc",
|
||||||
|
"static_threads",
|
||||||
|
]
|
||||||
@@ -1,7 +1,8 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "dynamic_threads"
|
name = "dynamic_threads"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2024"
|
authors = ["Yota Toyama <raviqqe@gmail.com>"]
|
||||||
|
edition = "2018"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
extern crate bdwgc_alloc;
|
||||||
|
|
||||||
use bdwgc_alloc::Allocator;
|
use bdwgc_alloc::Allocator;
|
||||||
use std::alloc::Layout;
|
use std::alloc::Layout;
|
||||||
|
|
||||||
@@ -12,7 +14,7 @@ fn main() {
|
|||||||
unsafe { Allocator::register_current_thread().unwrap() }
|
unsafe { Allocator::register_current_thread().unwrap() }
|
||||||
|
|
||||||
for _ in 0..100 {
|
for _ in 0..100 {
|
||||||
let _ = unsafe { std::alloc::alloc(Layout::from_size_align(2 ^ 8, 8).unwrap()) };
|
unsafe { std::alloc::alloc(Layout::from_size_align(2 ^ 8, 8).unwrap()) };
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe { Allocator::unregister_current_thread() }
|
unsafe { Allocator::unregister_current_thread() }
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "free_by_borrow"
|
name = "free_by_borrow"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2024"
|
authors = ["Yota Toyama <raviqqe@gmail.com>"]
|
||||||
|
edition = "2018"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
extern crate bdwgc_alloc;
|
||||||
|
|
||||||
use bdwgc_alloc::Allocator;
|
use bdwgc_alloc::Allocator;
|
||||||
|
|
||||||
#[global_allocator]
|
#[global_allocator]
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "free_by_gc"
|
name = "free_by_gc"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2024"
|
authors = ["Yota Toyama <raviqqe@gmail.com>"]
|
||||||
|
edition = "2018"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
extern crate bdwgc_alloc;
|
||||||
|
|
||||||
use bdwgc_alloc::Allocator;
|
use bdwgc_alloc::Allocator;
|
||||||
use std::alloc::Layout;
|
use std::alloc::Layout;
|
||||||
|
|
||||||
@@ -8,6 +10,6 @@ fn main() {
|
|||||||
unsafe { Allocator::initialize() }
|
unsafe { Allocator::initialize() }
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let _ = unsafe { std::alloc::alloc(Layout::from_size_align(2 ^ 8, 8).unwrap()) };
|
unsafe { std::alloc::alloc(Layout::from_size_align(2 ^ 8, 8).unwrap()) };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "static_threads"
|
name = "static_threads"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2024"
|
authors = ["Yota Toyama <raviqqe@gmail.com>"]
|
||||||
|
edition = "2018"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
extern crate bdwgc_alloc;
|
||||||
|
|
||||||
use bdwgc_alloc::Allocator;
|
use bdwgc_alloc::Allocator;
|
||||||
use std::alloc::Layout;
|
use std::alloc::Layout;
|
||||||
|
|
||||||
@@ -11,7 +13,7 @@ fn main() {
|
|||||||
unsafe { Allocator::register_current_thread().unwrap() }
|
unsafe { Allocator::register_current_thread().unwrap() }
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let _ = unsafe { std::alloc::alloc(Layout::from_size_align(2 ^ 8, 8).unwrap()) };
|
unsafe { std::alloc::alloc(Layout::from_size_align(2 ^ 8, 8).unwrap()) };
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
13
examples/test.sh
Executable file
13
examples/test.sh
Executable file
@@ -0,0 +1,13 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
set -ex
|
||||||
|
|
||||||
|
cargo build
|
||||||
|
|
||||||
|
for cargo_file in */Cargo.toml
|
||||||
|
do
|
||||||
|
target/debug/$(dirname $cargo_file) &
|
||||||
|
pid=$!
|
||||||
|
sleep 10
|
||||||
|
kill $pid
|
||||||
|
done
|
||||||
66
flake.lock
generated
66
flake.lock
generated
@@ -1,66 +0,0 @@
|
|||||||
{
|
|
||||||
"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
flake.nix
54
flake.nix
@@ -1,54 +0,0 @@
|
|||||||
{
|
|
||||||
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="
|
|
||||||
];
|
|
||||||
};
|
|
||||||
}
|
|
||||||
1
rust-toolchain
Normal file
1
rust-toolchain
Normal file
@@ -0,0 +1 @@
|
|||||||
|
stable
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
[toolchain]
|
|
||||||
channel = "stable"
|
|
||||||
components = ["clippy", "rustfmt", "rust-analyzer", "rust-src"]
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
use core::{
|
use std::{
|
||||||
error,
|
error,
|
||||||
fmt::{self, Display, Formatter},
|
fmt::{self, Display, Formatter},
|
||||||
};
|
};
|
||||||
@@ -9,7 +9,7 @@ pub struct Error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Error {
|
impl Error {
|
||||||
pub const fn new(description: &'static str) -> Error {
|
pub fn new(description: &'static str) -> Error {
|
||||||
Error { description }
|
Error { description }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
87
src/lib.rs
87
src/lib.rs
@@ -1,9 +1,10 @@
|
|||||||
#![doc = include_str!("../README.md")]
|
#![doc = include_str!("../README.md")]
|
||||||
#![no_std]
|
|
||||||
|
extern crate alloc;
|
||||||
|
|
||||||
mod error;
|
mod error;
|
||||||
|
|
||||||
use core::alloc::{GlobalAlloc, Layout};
|
use alloc::alloc::{GlobalAlloc, Layout};
|
||||||
use core::ptr::null;
|
use core::ptr::null;
|
||||||
use libc::{c_int, c_void, size_t};
|
use libc::{c_int, c_void, size_t};
|
||||||
|
|
||||||
@@ -16,7 +17,7 @@ struct GcStackBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[link(name = "gc", kind = "static")]
|
#[link(name = "gc", kind = "static")]
|
||||||
unsafe extern "C" {
|
extern "C" {
|
||||||
fn GC_allow_register_threads();
|
fn GC_allow_register_threads();
|
||||||
fn GC_alloc_lock();
|
fn GC_alloc_lock();
|
||||||
fn GC_alloc_unlock();
|
fn GC_alloc_unlock();
|
||||||
@@ -41,102 +42,76 @@ unsafe extern "C" {
|
|||||||
pub struct Allocator;
|
pub struct Allocator;
|
||||||
|
|
||||||
impl Allocator {
|
impl Allocator {
|
||||||
/// Locks a collector.
|
|
||||||
pub fn lock() {
|
pub fn lock() {
|
||||||
unsafe { GC_alloc_lock() }
|
unsafe { GC_alloc_lock() }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Unlocks a collector.
|
|
||||||
pub fn unlock() {
|
pub fn unlock() {
|
||||||
unsafe { GC_alloc_unlock() }
|
unsafe { GC_alloc_unlock() }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Initializes a collector.
|
|
||||||
///
|
|
||||||
/// # Safety
|
|
||||||
///
|
|
||||||
/// This function must be called in a main thread.
|
|
||||||
pub unsafe fn initialize() {
|
pub unsafe fn initialize() {
|
||||||
unsafe {
|
GC_init();
|
||||||
GC_init();
|
GC_allow_register_threads();
|
||||||
GC_allow_register_threads();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Registers a current thread to a collector.
|
|
||||||
///
|
|
||||||
/// # Safety
|
|
||||||
///
|
|
||||||
/// This function must not be called in a main thread.
|
|
||||||
pub unsafe fn register_current_thread() -> Result<(), error::Error> {
|
pub unsafe fn register_current_thread() -> Result<(), error::Error> {
|
||||||
let mut base = GcStackBase { mem_base: null() };
|
let mut base = GcStackBase { mem_base: null() };
|
||||||
|
|
||||||
if unsafe { GC_get_stack_base(&mut base) } != GC_SUCCESS {
|
if GC_get_stack_base(&mut base) != GC_SUCCESS {
|
||||||
return Err(error::Error::new("failed to get stack base"));
|
return Err(error::Error::new("failed to get stack base"));
|
||||||
} else if unsafe { GC_register_my_thread(&base) } != GC_SUCCESS {
|
} else if GC_register_my_thread(&base) != GC_SUCCESS {
|
||||||
return Err(error::Error::new("failed to register a thread for GC"));
|
return Err(error::Error::new("failed to register a thread for GC"));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets a bottom of a stack.
|
|
||||||
///
|
|
||||||
/// You do not have to call this function in most cases.
|
|
||||||
/// A collector detects the bottom on initialization automatically.
|
|
||||||
///
|
|
||||||
/// # Safety
|
|
||||||
///
|
|
||||||
/// The bottom address must be valid.
|
|
||||||
pub unsafe fn set_stack_bottom(bottom: *const u8) {
|
pub unsafe fn set_stack_bottom(bottom: *const u8) {
|
||||||
unsafe {
|
GC_set_stackbottom(
|
||||||
GC_set_stackbottom(
|
null(),
|
||||||
null(),
|
&GcStackBase {
|
||||||
&GcStackBase {
|
mem_base: bottom as *const libc::c_void,
|
||||||
mem_base: bottom as *const libc::c_void,
|
},
|
||||||
},
|
)
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Unregisters a current thread from a collector.
|
|
||||||
///
|
|
||||||
/// # Safety
|
|
||||||
///
|
|
||||||
/// The thread must be registered already.
|
|
||||||
pub unsafe fn unregister_current_thread() {
|
pub unsafe fn unregister_current_thread() {
|
||||||
unsafe { GC_unregister_my_thread() }
|
GC_unregister_my_thread()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Runs a garbage collection forcibly.
|
|
||||||
pub fn force_collect() {
|
pub fn force_collect() {
|
||||||
unsafe { GC_gcollect() }
|
unsafe { GC_gcollect() }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Registers a finalizer of an object.
|
pub unsafe fn register_finalizer<F: FnOnce(*mut c_void) + 'static>(
|
||||||
///
|
|
||||||
/// # Safety
|
|
||||||
///
|
|
||||||
/// The given finalizer must not be null and handle pointers properly.
|
|
||||||
pub unsafe fn register_finalizer(
|
|
||||||
ptr: *const c_void,
|
ptr: *const c_void,
|
||||||
finalizer: extern "C" fn(*mut c_void, *mut c_void),
|
finalizer: F,
|
||||||
client_data: *const c_void,
|
|
||||||
) {
|
) {
|
||||||
unsafe { GC_register_finalizer(ptr, finalizer, client_data, null(), null()) };
|
extern "C" fn finalize<F: FnOnce(*mut c_void)>(ptr: *mut c_void, data: *mut c_void) {
|
||||||
|
(unsafe { Box::from_raw(data as *mut F) })(ptr)
|
||||||
|
}
|
||||||
|
|
||||||
|
GC_register_finalizer(
|
||||||
|
ptr,
|
||||||
|
finalize::<F>,
|
||||||
|
Box::into_raw(Box::new(finalizer)) as *const _,
|
||||||
|
null(),
|
||||||
|
null(),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl GlobalAlloc for Allocator {
|
unsafe impl GlobalAlloc for Allocator {
|
||||||
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
|
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
|
||||||
(unsafe { GC_malloc(layout.size()) }) as *mut u8
|
GC_malloc(layout.size()) as *mut u8
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
|
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
|
||||||
unsafe { GC_free(ptr as *mut c_void) }
|
GC_free(ptr as *mut c_void)
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe fn realloc(&self, ptr: *mut u8, _layout: Layout, size: usize) -> *mut u8 {
|
unsafe fn realloc(&self, ptr: *mut u8, _layout: Layout, size: usize) -> *mut u8 {
|
||||||
(unsafe { GC_realloc(ptr as *mut c_void, size) }) as *mut u8
|
GC_realloc(ptr as *mut c_void, size) as *mut u8
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,20 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
|
|
||||||
set -ex
|
|
||||||
|
|
||||||
duration=20
|
|
||||||
|
|
||||||
cd $(dirname $0)/..
|
|
||||||
|
|
||||||
for directory in $(ls examples); do
|
|
||||||
(
|
|
||||||
cd examples/$directory
|
|
||||||
|
|
||||||
cargo build
|
|
||||||
cargo run &
|
|
||||||
|
|
||||||
pid=$!
|
|
||||||
sleep $duration
|
|
||||||
kill $pid
|
|
||||||
)
|
|
||||||
done
|
|
||||||
2
vendor/bdwgc
vendored
2
vendor/bdwgc
vendored
Submodule vendor/bdwgc updated: 82028767b4...71f838eb6e
2
vendor/libatomic_ops
vendored
2
vendor/libatomic_ops
vendored
Submodule vendor/libatomic_ops updated: d007246aa7...e06dc9549f
Reference in New Issue
Block a user