forked from GitHub/bdwgc-rust
Compare commits
6 Commits
chore/poin
...
feature/ru
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f52f6dcfaf | ||
|
|
3de34a77e9 | ||
|
|
81df12684f | ||
|
|
a3a13f061f | ||
|
|
085028a8e1 | ||
|
|
04f5f88b33 |
@@ -1,18 +1,11 @@
|
||||
{
|
||||
"words": [
|
||||
"autogen",
|
||||
"autotools",
|
||||
"bdwgc",
|
||||
"cflag",
|
||||
"dealloc",
|
||||
"finalizer",
|
||||
"gcollect",
|
||||
"libatomic",
|
||||
"libc",
|
||||
"realloc",
|
||||
"repr",
|
||||
"rustc",
|
||||
"stackbottom",
|
||||
"unregisters"
|
||||
"stackbottom"
|
||||
]
|
||||
}
|
||||
|
||||
8
.github/workflows/lint.yaml
vendored
8
.github/workflows/lint.yaml
vendored
@@ -8,7 +8,7 @@ jobs:
|
||||
lint:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v3
|
||||
with:
|
||||
submodules: true
|
||||
- uses: Swatinem/rust-cache@v2
|
||||
@@ -16,19 +16,19 @@ jobs:
|
||||
format:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v3
|
||||
- run: cargo fmt -- --check
|
||||
spell-check:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v3
|
||||
- uses: streetsidesoftware/cspell-action@main
|
||||
with:
|
||||
files: "**/*.{md,rs}"
|
||||
readme:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v3
|
||||
- uses: lycheeverse/lychee-action@v1
|
||||
with:
|
||||
fail: true
|
||||
|
||||
12
.github/workflows/test.yaml
vendored
12
.github/workflows/test.yaml
vendored
@@ -8,7 +8,7 @@ jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v3
|
||||
with:
|
||||
submodules: true
|
||||
- uses: Swatinem/rust-cache@v2
|
||||
@@ -16,7 +16,7 @@ jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v3
|
||||
with:
|
||||
submodules: true
|
||||
- uses: Swatinem/rust-cache@v2
|
||||
@@ -24,15 +24,17 @@ jobs:
|
||||
examples:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v3
|
||||
with:
|
||||
submodules: true
|
||||
- uses: Swatinem/rust-cache@v2
|
||||
- run: examples/test.sh
|
||||
- run: |
|
||||
cd examples
|
||||
./test.sh
|
||||
cmake:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v3
|
||||
with:
|
||||
submodules: true
|
||||
- uses: Swatinem/rust-cache@v2
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
[package]
|
||||
name = "bdwgc-alloc"
|
||||
description = "impl GlobalAlloc for bdwgc"
|
||||
version = "0.6.7"
|
||||
version = "0.6.5"
|
||||
authors = [
|
||||
"swgillespie <sean.william.g@gmail.com>",
|
||||
"Yota Toyama <raviqqe@gmail.com>",
|
||||
@@ -9,6 +9,7 @@ authors = [
|
||||
repository = "https://github.com/raviqqe/bdwgc-alloc"
|
||||
edition = "2021"
|
||||
license = "MIT"
|
||||
build = "build.rs"
|
||||
|
||||
[features]
|
||||
default = ["autotools"]
|
||||
|
||||
7
build.rs
7
build.rs
@@ -23,7 +23,7 @@ fn main() {
|
||||
|
||||
let dst = autotools::Config::new(LIB_GC_DIR)
|
||||
.cflag(format!(
|
||||
"-I{} -L/lib/x86_64-linux-gnu -lpthread -fPIC -D POINTER_MASK=0x7",
|
||||
"-I{} -L/lib/x86_64-linux-gnu -lpthread -fPIC",
|
||||
dst.join("include").display()
|
||||
))
|
||||
.build();
|
||||
@@ -59,10 +59,7 @@ fn main() {
|
||||
let dst = Config::new(LIB_GC_DIR)
|
||||
.profile("Release")
|
||||
.define("BUILD_SHARED_LIBS", "FALSE")
|
||||
.cflag(format!(
|
||||
"-I{} -DPOINTER_MASK=0x7",
|
||||
libatomic_include_path, foo
|
||||
))
|
||||
.cflag(format!("-I{}", libatomic_include_path))
|
||||
.build();
|
||||
|
||||
println!(
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
[workspace]
|
||||
resolver = "2"
|
||||
members = [
|
||||
"dynamic_threads",
|
||||
"free_by_borrow",
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
name = "dynamic_threads"
|
||||
version = "0.1.0"
|
||||
authors = ["Yota Toyama <raviqqe@gmail.com>"]
|
||||
edition = "2021"
|
||||
edition = "2018"
|
||||
publish = false
|
||||
|
||||
[dependencies]
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
extern crate bdwgc_alloc;
|
||||
|
||||
use bdwgc_alloc::Allocator;
|
||||
use std::alloc::Layout;
|
||||
|
||||
@@ -12,7 +14,7 @@ fn main() {
|
||||
unsafe { Allocator::register_current_thread().unwrap() }
|
||||
|
||||
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() }
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
name = "free_by_borrow"
|
||||
version = "0.1.0"
|
||||
authors = ["Yota Toyama <raviqqe@gmail.com>"]
|
||||
edition = "2021"
|
||||
edition = "2018"
|
||||
publish = false
|
||||
|
||||
[dependencies]
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
extern crate bdwgc_alloc;
|
||||
|
||||
use bdwgc_alloc::Allocator;
|
||||
|
||||
#[global_allocator]
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
name = "free_by_gc"
|
||||
version = "0.1.0"
|
||||
authors = ["Yota Toyama <raviqqe@gmail.com>"]
|
||||
edition = "2021"
|
||||
edition = "2018"
|
||||
publish = false
|
||||
|
||||
[dependencies]
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
extern crate bdwgc_alloc;
|
||||
|
||||
use bdwgc_alloc::Allocator;
|
||||
use std::alloc::Layout;
|
||||
|
||||
@@ -8,6 +10,6 @@ fn main() {
|
||||
unsafe { Allocator::initialize() }
|
||||
|
||||
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()) };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
name = "static_threads"
|
||||
version = "0.1.0"
|
||||
authors = ["Yota Toyama <raviqqe@gmail.com>"]
|
||||
edition = "2021"
|
||||
edition = "2018"
|
||||
publish = false
|
||||
|
||||
[dependencies]
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
extern crate bdwgc_alloc;
|
||||
|
||||
use bdwgc_alloc::Allocator;
|
||||
use std::alloc::Layout;
|
||||
|
||||
@@ -11,7 +13,7 @@ fn main() {
|
||||
unsafe { Allocator::register_current_thread().unwrap() }
|
||||
|
||||
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()) };
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -2,11 +2,12 @@
|
||||
|
||||
set -ex
|
||||
|
||||
cd $(dirname $0)
|
||||
cargo build
|
||||
|
||||
for cargo_file in */Cargo.toml; do
|
||||
cargo run --bin $(dirname $cargo_file) &
|
||||
pid=$!
|
||||
sleep 20
|
||||
kill $pid
|
||||
for cargo_file in */Cargo.toml
|
||||
do
|
||||
target/debug/$(dirname $cargo_file) &
|
||||
pid=$!
|
||||
sleep 10
|
||||
kill $pid
|
||||
done
|
||||
|
||||
51
src/lib.rs
51
src/lib.rs
@@ -25,7 +25,6 @@ extern "C" {
|
||||
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_memalign(align: size_t, 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);
|
||||
@@ -43,31 +42,19 @@ extern "C" {
|
||||
pub struct Allocator;
|
||||
|
||||
impl Allocator {
|
||||
/// Locks a collector.
|
||||
pub fn lock() {
|
||||
unsafe { GC_alloc_lock() }
|
||||
}
|
||||
|
||||
/// Unlocks a collector.
|
||||
pub fn unlock() {
|
||||
unsafe { GC_alloc_unlock() }
|
||||
}
|
||||
|
||||
/// Initializes a collector.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// This function must be called in a main thread.
|
||||
pub unsafe fn initialize() {
|
||||
GC_init();
|
||||
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> {
|
||||
let mut base = GcStackBase { mem_base: null() };
|
||||
|
||||
@@ -80,14 +67,6 @@ impl Allocator {
|
||||
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) {
|
||||
GC_set_stackbottom(
|
||||
null(),
|
||||
@@ -97,37 +76,35 @@ impl Allocator {
|
||||
)
|
||||
}
|
||||
|
||||
/// Unregisters a current thread from a collector.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// The thread must be registered already.
|
||||
pub unsafe fn unregister_current_thread() {
|
||||
GC_unregister_my_thread()
|
||||
}
|
||||
|
||||
/// Runs a garbage collection forcibly.
|
||||
pub fn force_collect() {
|
||||
unsafe { GC_gcollect() }
|
||||
}
|
||||
|
||||
/// Registers a finalizer of an object.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// The given finalizer must not be null and handle pointers properly.
|
||||
pub unsafe fn register_finalizer(
|
||||
pub unsafe fn register_finalizer<F: FnOnce(*mut c_void) + 'static>(
|
||||
ptr: *const c_void,
|
||||
finalizer: extern "C" fn(*mut c_void, *mut c_void),
|
||||
client_data: *const c_void,
|
||||
finalizer: F,
|
||||
) {
|
||||
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 fn alloc(&self, layout: Layout) -> *mut u8 {
|
||||
GC_memalign(layout.align(), layout.size()) as *mut u8
|
||||
GC_malloc(layout.size()) as *mut u8
|
||||
}
|
||||
|
||||
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
|
||||
|
||||
2
vendor/bdwgc
vendored
2
vendor/bdwgc
vendored
Submodule vendor/bdwgc updated: e8c073d786...71f838eb6e
2
vendor/libatomic_ops
vendored
2
vendor/libatomic_ops
vendored
Submodule vendor/libatomic_ops updated: 9f6bc3b928...e06dc9549f
Reference in New Issue
Block a user