4 Commits

Author SHA1 Message Date
Yota Toyama
e11647fab9 Merge branch 'main' into chore/pointer-mask 2023-09-28 15:55:11 +10:00
Yota Toyama
08d549315b Fix 2023-09-24 20:23:45 +10:00
Yota Toyama
a4bcbf2c7b Call memalign 2023-09-24 17:01:53 +10:00
Yota Toyama
5f41a2b8e2 Set pointer mask 2023-09-24 16:56:08 +10:00
26 changed files with 108 additions and 232 deletions

View File

@@ -3,9 +3,7 @@
"autogen",
"autotools",
"bdwgc",
"canonicalize",
"cflag",
"clippy",
"dealloc",
"finalizer",
"gcollect",
@@ -15,7 +13,6 @@
"repr",
"rustc",
"stackbottom",
"swatinem",
"unregisters"
]
}

11
.github/mergify.yml vendored Normal file
View 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: []

View File

@@ -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 }}

View File

@@ -8,25 +8,27 @@ jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: actions/checkout@v4
with:
submodules: true
- uses: swatinem/rust-cache@v2
- uses: Swatinem/rust-cache@v2
- run: cargo clippy -- -D warnings
format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: actions/checkout@v4
- run: cargo fmt -- --check
spell-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: actions/checkout@v4
- uses: streetsidesoftware/cspell-action@main
with:
files: "**/*.{md,rs}"
readme:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: lycheeverse/lychee-action@v2
- uses: actions/checkout@v4
- uses: lycheeverse/lychee-action@v1
with:
fail: true

View File

@@ -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 }}

View File

@@ -8,32 +8,32 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: actions/checkout@v4
with:
submodules: true
- uses: swatinem/rust-cache@v2
- uses: Swatinem/rust-cache@v2
- run: cargo build
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: actions/checkout@v4
with:
submodules: true
- uses: swatinem/rust-cache@v2
- uses: Swatinem/rust-cache@v2
- run: cargo test
examples:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: actions/checkout@v4
with:
submodules: true
- uses: swatinem/rust-cache@v2
- run: tools/example_test.sh
- uses: Swatinem/rust-cache@v2
- run: examples/test.sh
cmake:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: actions/checkout@v4
with:
submodules: true
- uses: swatinem/rust-cache@v2
- uses: Swatinem/rust-cache@v2
- run: cargo build --no-default-features --features cmake

2
.gitignore vendored
View File

@@ -1,2 +1,2 @@
.cargo_cache
*.lock
target

4
.gitmodules vendored
View File

@@ -1,6 +1,6 @@
[submodule "vendor/bdwgc"]
path = vendor/bdwgc
url = https://github.com/bdwgc/bdwgc
url = https://github.com/ivmai/bdwgc
[submodule "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
View File

@@ -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",
]

View File

@@ -1,29 +1,21 @@
[package]
name = "bdwgc-alloc"
version = "0.6.13"
edition = "2024"
description = "impl GlobalAlloc for bdwgc"
version = "0.6.7"
authors = [
"swgillespie <sean.william.g@gmail.com>",
"Yota Toyama <raviqqe@gmail.com>",
]
repository = "https://github.com/raviqqe/bdwgc-alloc"
edition = "2021"
license = "MIT"
categories = ["api-bindings", "memory-management", "no-std", "no-std::no-alloc"]
[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]
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"
[dependencies]
libc = "0.2"
[build-dependencies]
autotools = { version = "0.2", optional = true }
cmake = { version = "0.1", optional = true }

View File

@@ -1,8 +1,8 @@
# bdwgc-rust
# bdwgc-alloc
[![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)
[![GitHub Action](https://img.shields.io/github/actions/workflow/status/raviqqe/bdwgc-alloc/test.yaml?branch=main&style=flat-square)](https://github.com/raviqqe/bdwgc-alloc/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)](LICENSE)
[![License](https://img.shields.io/github/license/raviqqe/bdwgc-alloc.svg?style=flat-square)](LICENSE)
[`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)
[bdwgc]: https://github.com/bdwgc/bdwgc
[bdwgc]: https://github.com/ivmai/bdwgc

View File

@@ -6,7 +6,7 @@ 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"))
.arg(format!("cd {} && ./autogen.sh", dir))
.output()
.unwrap();
}
@@ -23,8 +23,7 @@ fn main() {
let dst = autotools::Config::new(LIB_GC_DIR)
.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 -D POINTER_MASK=0x7",
dst.join("include").display()
))
.build();
@@ -38,7 +37,7 @@ fn main() {
for dir in &[LIB_ATOMIC_OPS_DIR, LIB_GC_DIR] {
std::process::Command::new("sh")
.arg("-c")
.arg(format!("cd {dir} && git clean -dfx"))
.arg(format!("cd {} && git clean -dfx", dir))
.output()
.unwrap();
}
@@ -59,8 +58,11 @@ fn main() {
let dst = Config::new(LIB_GC_DIR)
.profile("Release")
.define("GC_BUILD_SHARED_LIBS", "FALSE")
.cflag(format!("-I{}", libatomic_include_path))
.define("BUILD_SHARED_LIBS", "FALSE")
.cflag(format!(
"-I{} -DPOINTER_MASK=0x7",
libatomic_include_path, foo
))
.build();
println!(

8
examples/Cargo.toml Normal file
View File

@@ -0,0 +1,8 @@
[workspace]
resolver = "2"
members = [
"dynamic_threads",
"free_by_borrow",
"free_by_gc",
"static_threads",
]

View File

@@ -1,7 +1,8 @@
[package]
name = "dynamic_threads"
version = "0.1.0"
edition = "2024"
authors = ["Yota Toyama <raviqqe@gmail.com>"]
edition = "2021"
publish = false
[dependencies]

View File

@@ -1,7 +1,8 @@
[package]
name = "free_by_borrow"
version = "0.1.0"
edition = "2024"
authors = ["Yota Toyama <raviqqe@gmail.com>"]
edition = "2021"
publish = false
[dependencies]

View File

@@ -1,7 +1,8 @@
[package]
name = "free_by_gc"
version = "0.1.0"
edition = "2024"
authors = ["Yota Toyama <raviqqe@gmail.com>"]
edition = "2021"
publish = false
[dependencies]

View File

@@ -1,7 +1,8 @@
[package]
name = "static_threads"
version = "0.1.0"
edition = "2024"
authors = ["Yota Toyama <raviqqe@gmail.com>"]
edition = "2021"
publish = false
[dependencies]

12
examples/test.sh Executable file
View File

@@ -0,0 +1,12 @@
#!/bin/sh
set -ex
cd $(dirname $0)
for cargo_file in */Cargo.toml; do
cargo run --bin $(dirname $cargo_file) &
pid=$!
sleep 20
kill $pid
done

1
rust-toolchain Normal file
View File

@@ -0,0 +1 @@
stable

View File

@@ -1,3 +0,0 @@
[toolchain]
channel = "stable"
components = ["clippy", "rustfmt", "rust-analyzer", "rust-src"]

View File

@@ -1,4 +1,4 @@
use core::{
use std::{
error,
fmt::{self, Display, Formatter},
};
@@ -9,7 +9,7 @@ pub struct Error {
}
impl Error {
pub const fn new(description: &'static str) -> Error {
pub fn new(description: &'static str) -> Error {
Error { description }
}
}

View File

@@ -1,9 +1,10 @@
#![doc = include_str!("../README.md")]
#![no_std]
extern crate alloc;
mod error;
use core::alloc::{GlobalAlloc, Layout};
use alloc::alloc::{GlobalAlloc, Layout};
use core::ptr::null;
use libc::{c_int, c_void, size_t};
@@ -16,7 +17,7 @@ struct GcStackBase {
}
#[link(name = "gc", kind = "static")]
unsafe extern "C" {
extern "C" {
fn GC_allow_register_threads();
fn GC_alloc_lock();
fn GC_alloc_unlock();
@@ -24,6 +25,7 @@ unsafe 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);
@@ -57,11 +59,9 @@ impl Allocator {
///
/// This function must be called in a main thread.
pub unsafe fn initialize() {
unsafe {
GC_init();
GC_allow_register_threads();
}
}
/// Registers a current thread to a collector.
///
@@ -71,9 +71,9 @@ impl Allocator {
pub unsafe fn register_current_thread() -> Result<(), error::Error> {
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"));
} 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"));
}
@@ -89,7 +89,6 @@ impl Allocator {
///
/// The bottom address must be valid.
pub unsafe fn set_stack_bottom(bottom: *const u8) {
unsafe {
GC_set_stackbottom(
null(),
&GcStackBase {
@@ -97,7 +96,6 @@ impl Allocator {
},
)
}
}
/// Unregisters a current thread from a collector.
///
@@ -105,7 +103,7 @@ impl Allocator {
///
/// The thread must be registered already.
pub unsafe fn unregister_current_thread() {
unsafe { GC_unregister_my_thread() }
GC_unregister_my_thread()
}
/// Runs a garbage collection forcibly.
@@ -123,20 +121,20 @@ impl Allocator {
finalizer: extern "C" fn(*mut c_void, *mut c_void),
client_data: *const c_void,
) {
unsafe { GC_register_finalizer(ptr, finalizer, client_data, null(), null()) };
GC_register_finalizer(ptr, finalizer, client_data, null(), null());
}
}
unsafe impl GlobalAlloc for Allocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
(unsafe { GC_malloc(layout.size()) }) as *mut u8
GC_memalign(layout.align(), layout.size()) as *mut u8
}
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 { GC_realloc(ptr as *mut c_void, size) }) as *mut u8
GC_realloc(ptr as *mut c_void, size) as *mut u8
}
}

View File

@@ -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