mirror of
https://github.com/bdwgc/bdwgc-rust.git
synced 2026-09-27 04:33:43 -06:00
Compare commits
7
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6136c4f3f7 | ||
|
|
76bf20d8e3 | ||
|
|
08ea694983 | ||
|
|
d93e42910d | ||
|
|
df593ee27c | ||
|
|
6e18b7b12b | ||
|
|
e7fc7e39bf |
@@ -0,0 +1,11 @@
|
|||||||
|
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
|
||||||
@@ -9,7 +9,7 @@ permissions:
|
|||||||
pages: write
|
pages: write
|
||||||
id-token: write
|
id-token: write
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
document:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ on:
|
|||||||
- main
|
- main
|
||||||
pull_request:
|
pull_request:
|
||||||
jobs:
|
jobs:
|
||||||
lint:
|
clippy:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||||
@@ -30,3 +30,13 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||||
- uses: lycheeverse/lychee-action@e7477775783ea5526144ba13e8db5eec57747ce8 # v2.9.0
|
- 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
|
||||||
|
|||||||
+48
-15
@@ -6,26 +6,59 @@ on:
|
|||||||
pull_request:
|
pull_request:
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
runs-on: ubuntu-latest
|
strategy:
|
||||||
|
fail-fast: false
|
||||||
|
matrix:
|
||||||
|
os:
|
||||||
|
- ubuntu-latest
|
||||||
|
- ubuntu-26.04-arm
|
||||||
|
- macos-latest
|
||||||
|
runs-on: ${{ matrix.os }}
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||||
with:
|
with:
|
||||||
submodules: true
|
submodules: true
|
||||||
- uses: swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2
|
- uses: ./.github/actions/setup
|
||||||
- run: cargo build
|
- 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:
|
test:
|
||||||
|
needs:
|
||||||
|
- build
|
||||||
|
- unit_test
|
||||||
|
- examples
|
||||||
|
if: always()
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
- run: for result in ${{ join(needs.*.result, ' ') }}; do [ $result = success ]; done
|
||||||
with:
|
|
||||||
submodules: true
|
|
||||||
- uses: swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2
|
|
||||||
- run: cargo test
|
|
||||||
cmake:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
|
||||||
with:
|
|
||||||
submodules: true
|
|
||||||
- uses: swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2
|
|
||||||
- run: cargo build --no-default-features --features cmake
|
|
||||||
|
|||||||
Generated
+28
@@ -106,12 +106,33 @@ dependencies = [
|
|||||||
"cc",
|
"cc",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "dynamic_threads"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"bdwgc-alloc",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "find-msvc-tools"
|
name = "find-msvc-tools"
|
||||||
version = "0.1.5"
|
version = "0.1.5"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "3a3076410a55c90011c298b04d0cfa770b00fa04e1e3c97d3f6c9de105a03844"
|
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]]
|
[[package]]
|
||||||
name = "glob"
|
name = "glob"
|
||||||
version = "0.3.4"
|
version = "0.3.4"
|
||||||
@@ -237,6 +258,13 @@ version = "2.0.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba"
|
checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "static_threads"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"bdwgc-alloc",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "syn"
|
name = "syn"
|
||||||
version = "3.0.6"
|
version = "3.0.6"
|
||||||
|
|||||||
+2
-1
@@ -1,6 +1,7 @@
|
|||||||
[workspace]
|
[workspace]
|
||||||
resolver = "3"
|
resolver = "3"
|
||||||
members = ["bdwgc-alloc", "bdwgc-alloc-sys"]
|
members = ["bdwgc-alloc", "bdwgc-alloc-sys", "examples/*"]
|
||||||
|
default-members = ["bdwgc-alloc", "bdwgc-alloc-sys"]
|
||||||
|
|
||||||
[workspace.package]
|
[workspace.package]
|
||||||
version = "0.6.15"
|
version = "0.6.15"
|
||||||
|
|||||||
@@ -12,31 +12,7 @@ The raw bindings to the C API of [`bdwgc`][bdwgc] that this crate is built on ar
|
|||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
```rust
|
See [`examples`](https://github.com/bdwgc/bdwgc-rust/tree/main/examples) directory.
|
||||||
use bdwgc_alloc::Allocator;
|
|
||||||
use std::thread::spawn;
|
|
||||||
|
|
||||||
#[global_allocator]
|
|
||||||
static GLOBAL_ALLOCATOR: Allocator = Allocator;
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
unsafe { Allocator::initialize() }
|
|
||||||
|
|
||||||
// Unreachable objects are collected automatically.
|
|
||||||
Box::leak(Box::new([0u8; 256]));
|
|
||||||
|
|
||||||
// Threads other than a main thread must be registered to a collector before allocation.
|
|
||||||
spawn(|| {
|
|
||||||
unsafe { Allocator::register_current_thread() }.unwrap();
|
|
||||||
|
|
||||||
Box::leak(Box::new([0u8; 256]));
|
|
||||||
|
|
||||||
unsafe { Allocator::unregister_current_thread() }
|
|
||||||
})
|
|
||||||
.join()
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
By default [`bdwgc`][bdwgc] is built with autotools. To build with cmake, enable the `cmake` feature:
|
By default [`bdwgc`][bdwgc] is built with autotools. To build with cmake, enable the `cmake` feature:
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,8 @@
|
|||||||
use core::error::Error;
|
use core::error::Error;
|
||||||
use std::{env, path::PathBuf};
|
use std::{env, path::PathBuf};
|
||||||
|
|
||||||
|
// cspell: ignore reconf
|
||||||
|
|
||||||
const LIB_ATOMIC_OPS_DIR: &str = "vendor/libatomic_ops";
|
const LIB_ATOMIC_OPS_DIR: &str = "vendor/libatomic_ops";
|
||||||
const LIB_GC_DIR: &str = "vendor/bdwgc";
|
const LIB_GC_DIR: &str = "vendor/bdwgc";
|
||||||
|
|
||||||
@@ -52,14 +54,8 @@ cfg_select! {
|
|||||||
}
|
}
|
||||||
feature = "autotools" => {
|
feature = "autotools" => {
|
||||||
fn build_library() -> Result<(), Box<dyn Error>> {
|
fn build_library() -> Result<(), Box<dyn Error>> {
|
||||||
for dir in &[LIB_ATOMIC_OPS_DIR, LIB_GC_DIR] {
|
|
||||||
std::process::Command::new("sh")
|
|
||||||
.arg("-c")
|
|
||||||
.arg(format!("cd {dir} && ./autogen.sh"))
|
|
||||||
.output()?;
|
|
||||||
}
|
|
||||||
|
|
||||||
let dst = autotools::Config::new(LIB_ATOMIC_OPS_DIR)
|
let dst = autotools::Config::new(LIB_ATOMIC_OPS_DIR)
|
||||||
|
.reconf("-i")
|
||||||
.cflag("-fPIC")
|
.cflag("-fPIC")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
@@ -70,6 +66,7 @@ cfg_select! {
|
|||||||
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)
|
||||||
|
.reconf("-i")
|
||||||
.cflag(format!(
|
.cflag(format!(
|
||||||
// spell-checker: disable-next-line
|
// spell-checker: disable-next-line
|
||||||
"-I{} -L/lib/x86_64-linux-gnu -lpthread -fPIC",
|
"-I{} -L/lib/x86_64-linux-gnu -lpthread -fPIC",
|
||||||
|
|||||||
@@ -18,7 +18,3 @@ cmake = ["bdwgc-alloc-sys/cmake"]
|
|||||||
|
|
||||||
[lints]
|
[lints]
|
||||||
workspace = true
|
workspace = true
|
||||||
|
|
||||||
[[test]]
|
|
||||||
name = "allocator"
|
|
||||||
harness = false
|
|
||||||
|
|||||||
@@ -1,53 +0,0 @@
|
|||||||
//! A smoke test of the allocator.
|
|
||||||
|
|
||||||
use bdwgc_alloc::Allocator;
|
|
||||||
use core::hint::black_box;
|
|
||||||
use std::thread::spawn;
|
|
||||||
|
|
||||||
const OBJECT_SIZE: usize = 1 << 8;
|
|
||||||
const TOTAL_COUNT: usize = 1 << 16;
|
|
||||||
const THREAD_COUNT: usize = 1 << 6;
|
|
||||||
|
|
||||||
#[global_allocator]
|
|
||||||
static GLOBAL_ALLOCATOR: Allocator = Allocator;
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
unsafe { Allocator::initialize() }
|
|
||||||
|
|
||||||
free_by_gc();
|
|
||||||
free_by_drop();
|
|
||||||
static_thread();
|
|
||||||
dynamic_threads();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn free_by_gc() {
|
|
||||||
for _ in 0..TOTAL_COUNT {
|
|
||||||
black_box(Box::leak(Box::new([0u8; OBJECT_SIZE])));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn free_by_drop() {
|
|
||||||
for _ in 0..TOTAL_COUNT {
|
|
||||||
drop(black_box(Box::new([0u8; OBJECT_SIZE])));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn static_thread() {
|
|
||||||
spawn_thread(free_by_gc);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn dynamic_threads() {
|
|
||||||
for _ in 0..THREAD_COUNT {
|
|
||||||
spawn_thread(free_by_gc);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn spawn_thread(allocate: fn()) {
|
|
||||||
spawn(move || {
|
|
||||||
unsafe { Allocator::register_current_thread() }.unwrap();
|
|
||||||
allocate();
|
|
||||||
unsafe { Allocator::unregister_current_thread() }
|
|
||||||
})
|
|
||||||
.join()
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
@@ -10,6 +10,7 @@
|
|||||||
"dealloc",
|
"dealloc",
|
||||||
"finalizer",
|
"finalizer",
|
||||||
"gcollect",
|
"gcollect",
|
||||||
|
"gitsubmodule",
|
||||||
"libatomic",
|
"libatomic",
|
||||||
"libc",
|
"libc",
|
||||||
"libclang",
|
"libclang",
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
[package]
|
||||||
|
name = "dynamic_threads"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
publish = false
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
bdwgc-alloc = { path = "../../bdwgc-alloc" }
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
use bdwgc_alloc::Allocator;
|
||||||
|
use std::alloc::Layout;
|
||||||
|
|
||||||
|
#[global_allocator]
|
||||||
|
static GLOBAL_ALLOCATOR: Allocator = Allocator;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
unsafe { Allocator::initialize() }
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let handle = std::thread::spawn(move || {
|
||||||
|
unsafe { Allocator::register_current_thread().unwrap() }
|
||||||
|
|
||||||
|
for _ in 0..100 {
|
||||||
|
let _ = unsafe { std::alloc::alloc(Layout::from_size_align(2 ^ 8, 8).unwrap()) };
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe { Allocator::unregister_current_thread() }
|
||||||
|
});
|
||||||
|
|
||||||
|
handle.join().unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
[package]
|
||||||
|
name = "free_by_borrow"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
publish = false
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
bdwgc-alloc = { path = "../../bdwgc-alloc" }
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
use bdwgc_alloc::Allocator;
|
||||||
|
|
||||||
|
#[global_allocator]
|
||||||
|
static GLOBAL_ALLOCATOR: Allocator = Allocator;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
unsafe { Allocator::initialize() }
|
||||||
|
|
||||||
|
let mut _n: Box<[u8; 2 ^ 8]> = Box::new([0; 2 ^ 8]);
|
||||||
|
|
||||||
|
loop {
|
||||||
|
_n = Box::new([0; 2 ^ 8]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
[package]
|
||||||
|
name = "free_by_gc"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
publish = false
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
bdwgc-alloc = { path = "../../bdwgc-alloc" }
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
use bdwgc_alloc::Allocator;
|
||||||
|
use std::alloc::Layout;
|
||||||
|
|
||||||
|
#[global_allocator]
|
||||||
|
static GLOBAL_ALLOCATOR: Allocator = Allocator;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
unsafe { Allocator::initialize() }
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let _ = unsafe { std::alloc::alloc(Layout::from_size_align(2 ^ 8, 8).unwrap()) };
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
[package]
|
||||||
|
name = "static_threads"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
publish = false
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
bdwgc-alloc = { path = "../../bdwgc-alloc" }
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
use bdwgc_alloc::Allocator;
|
||||||
|
use std::alloc::Layout;
|
||||||
|
|
||||||
|
#[global_allocator]
|
||||||
|
static GLOBAL_ALLOCATOR: Allocator = Allocator;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
unsafe { Allocator::initialize() }
|
||||||
|
|
||||||
|
let handle = std::thread::spawn(move || {
|
||||||
|
unsafe { Allocator::register_current_thread().unwrap() }
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let _ = unsafe { std::alloc::alloc(Layout::from_size_align(2 ^ 8, 8).unwrap()) };
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
handle.join().unwrap();
|
||||||
|
}
|
||||||
Executable
+20
@@ -0,0 +1,20 @@
|
|||||||
|
#!/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
|
||||||
Reference in New Issue
Block a user