Skip to content

Commit

Permalink
Merge pull request #108 from jacob-hughes/custom_boehm_path
Browse files Browse the repository at this point in the history
Add env var for specifying custom BDWGC path
  • Loading branch information
ltratt authored Jan 18, 2024
2 parents 27b2b31 + 6cfff56 commit 2a214a6
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 38 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ dependencies = [
name = "alloc"
version = "0.0.0"
dependencies = [
"boehm",
"bdwgc",
"compiler_builtins",
"core",
"rand",
Expand Down Expand Up @@ -305,7 +305,7 @@ dependencies = [
]

[[package]]
name = "boehm"
name = "bdwgc"
version = "0.1.0"
dependencies = [
"cmake",
Expand Down Expand Up @@ -5010,7 +5010,7 @@ version = "0.0.0"
dependencies = [
"addr2line",
"alloc",
"boehm",
"bdwgc",
"cfg-if",
"compiler_builtins",
"core",
Expand Down
2 changes: 1 addition & 1 deletion library/alloc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ autobenches = false
edition = "2021"

[dependencies]
boehm = { path = "../boehm"}
bdwgc = { path = "../bdwgc"}
core = { path = "../core" }
compiler_builtins = { version = "0.1.40", features = ['rustc-dep-of-std'] }

Expand Down
18 changes: 9 additions & 9 deletions library/alloc/src/gc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ unsafe impl GlobalAlloc for GcAllocator {
#[inline]
unsafe fn gc_malloc(layout: Layout) -> *mut u8 {
if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() {
unsafe { boehm::GC_malloc(layout.size()) as *mut u8 }
unsafe { bdwgc::GC_malloc(layout.size()) as *mut u8 }
} else {
let mut out = ptr::null_mut();
// posix_memalign requires that the alignment be a multiple of `sizeof(void*)`.
// Since these are all powers of 2, we can just use max.
unsafe {
let align = layout.align().max(core::mem::size_of::<usize>());
let ret = boehm::GC_posix_memalign(&mut out, align, layout.size());
let ret = bdwgc::GC_posix_memalign(&mut out, align, layout.size());
if ret != 0 { ptr::null_mut() } else { out as *mut u8 }
}
}
Expand All @@ -70,7 +70,7 @@ unsafe fn gc_malloc(layout: Layout) -> *mut u8 {
#[inline]
unsafe fn gc_realloc(ptr: *mut u8, old_layout: Layout, new_size: usize) -> *mut u8 {
if old_layout.align() <= MIN_ALIGN && old_layout.align() <= new_size {
unsafe { boehm::GC_realloc(ptr, new_size) as *mut u8 }
unsafe { bdwgc::GC_realloc(ptr, new_size) as *mut u8 }
} else {
unsafe {
let new_layout = Layout::from_size_align_unchecked(new_size, old_layout.align());
Expand All @@ -90,11 +90,11 @@ unsafe fn gc_realloc(ptr: *mut u8, old_layout: Layout, new_size: usize) -> *mut
unsafe fn gc_free(ptr: *mut u8, layout: Layout) {
if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() {
unsafe {
boehm::GC_free(ptr);
bdwgc::GC_free(ptr);
}
} else {
unsafe {
boehm::GC_free(boehm::GC_base(ptr));
bdwgc::GC_free(bdwgc::GC_base(ptr));
}
}
}
Expand All @@ -117,18 +117,18 @@ unsafe impl Allocator for GcAllocator {

impl GcAllocator {
pub fn force_gc() {
unsafe { boehm::GC_gcollect() }
unsafe { bdwgc::GC_gcollect() }
}
}

pub fn init() {
unsafe { boehm::GC_init() }
unsafe { bdwgc::GC_init() }
}

pub fn suppress_warnings() {
unsafe { boehm::GC_set_warn_proc(&boehm::GC_ignore_warn_proc as *const _ as *mut u8) };
unsafe { bdwgc::GC_set_warn_proc(&bdwgc::GC_ignore_warn_proc as *const _ as *mut u8) };
}

pub fn thread_registered() -> bool {
unsafe { boehm::GC_thread_is_registered() != 0 }
unsafe { bdwgc::GC_thread_is_registered() != 0 }
}
2 changes: 1 addition & 1 deletion library/alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ mod testing;
#[allow(unused_extern_crates)]
#[allow(missing_docs)]
#[unstable(feature = "gc", issue = "none")]
pub extern crate boehm;
pub extern crate bdwgc;

// Module with internal macros used by other modules (needs to be included before other modules).
#[macro_use]
Expand Down
2 changes: 1 addition & 1 deletion library/alloc/tests/gc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn large_alignment() {
}

#[test]
fn boehm_issue_589() {
fn bdwgc_issue_589() {
// Test the specific size / alignment problem raised in [1].
//
// [1]: https://github.com/ivmai/bdwgc/issues/589
Expand Down
2 changes: 1 addition & 1 deletion library/boehm/Cargo.toml → library/bdwgc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cargo-features = ["public-dependency"]

[package]
name = "boehm"
name = "bdwgc"
version = "0.1.0"
authors = ["Jake Hughes <jh@jakehughes.uk>"]
edition = "2021"
Expand Down
27 changes: 16 additions & 11 deletions library/boehm/build.rs → library/bdwgc/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use std::env;
use std::path::PathBuf;
use std::process::Command;

const BOEHM_REPO: &str = "https://github.com/softdevteam/bdwgc.git";
const BOEHM_ATOMICS_REPO: &str = "https://github.com/ivmai/libatomic_ops.git";
const BOEHM_DIR: &str = "bdwgc";
const BUILD_DIR: &str = "lib";
const BDWGC_REPO: &str = "https://github.com/softdevteam/bdwgc.git";
const BDWGC_ATOMICS_REPO: &str = "https://github.com/ivmai/libatomic_ops.git";
const BDWGC_DEFAULT_SRC_DIR: &str = "bdwgc";
const BDWGC_BUILD_DIR: &str = "lib";

#[cfg(not(all(target_pointer_width = "64", target_arch = "x86_64")))]
compile_error!("Requires x86_64 with 64 bit pointer width.");
Expand All @@ -24,17 +24,22 @@ where

fn main() {
let out_dir = env::var("OUT_DIR").unwrap();
let mut boehm_src = PathBuf::from(&out_dir);
boehm_src.push(BOEHM_DIR);
let mut bdwgc_src = PathBuf::from(&out_dir);

match env::var("BDWGC") {
Ok(path) => bdwgc_src.push(path),
Err(_) => bdwgc_src.push(BDWGC_DEFAULT_SRC_DIR),
}

let mut build_dir = PathBuf::from(&out_dir);
build_dir.push(BUILD_DIR);
build_dir.push(BDWGC_BUILD_DIR);

if !boehm_src.exists() {
run("git", |cmd| cmd.arg("clone").arg(BOEHM_REPO).arg(&boehm_src));
run("git", |cmd| cmd.arg("clone").arg(BOEHM_ATOMICS_REPO).current_dir(&boehm_src));
if !bdwgc_src.exists() && env::var("BDWGC").is_err() {
run("git", |cmd| cmd.arg("clone").arg(BDWGC_REPO).arg(&bdwgc_src));
run("git", |cmd| cmd.arg("clone").arg(BDWGC_ATOMICS_REPO).current_dir(&bdwgc_src));
}

let mut build = cmake::Config::new(&boehm_src);
let mut build = cmake::Config::new(&bdwgc_src);
build
.pic(true)
.define("BUILD_SHARED_LIBS", "OFF")
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion library/std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ crate-type = ["dylib", "rlib"]
[dependencies]
alloc = { path = "../alloc", public = true }
cfg-if = { version = "1.0", features = ['rustc-dep-of-std'] }
boehm = { path = "../boehm" }
bdwgc = { path = "../bdwgc" }
panic_unwind = { path = "../panic_unwind", optional = true }
panic_abort = { path = "../panic_abort" }
core = { path = "../core", public = true }
Expand Down
4 changes: 2 additions & 2 deletions library/std/src/gc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ impl<T> Gc<T> {
}

unsafe {
boehm::GC_register_finalizer_no_order(
bdwgc::GC_register_finalizer_no_order(
self.ptr.as_ptr() as *mut u8,
Some(finalizer::<T>),
null_mut(),
Expand All @@ -281,7 +281,7 @@ impl<T> Gc<T> {
pub fn unregister_finalizer(&mut self) {
let ptr = self.ptr.as_ptr() as *mut GcBox<T> as *mut u8;
unsafe {
boehm::GC_register_finalizer(
bdwgc::GC_register_finalizer(
ptr,
None,
::core::ptr::null_mut(),
Expand Down
8 changes: 4 additions & 4 deletions library/std/src/sys/common/thread_local/os_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use crate::cell::Cell;
use crate::sys_common::thread_local_key::StaticKey as OsStaticKey;
use crate::{fmt, marker, panic, ptr};

use alloc::boehm;
use alloc::bdwgc;

/// A buffer of pointers to each thread local variable.
///
/// The Boehm GC can't locate GC pointers stored inside POSIX thread locals, so
/// The BDWGC can't locate GC pointers stored inside POSIX thread locals, so
/// this struct keeps track of pointers to thread local data, which the GC then
/// uses as part of its marking rootset.
///
Expand All @@ -31,11 +31,11 @@ impl TLSRoots {
/// Push a root to the current thread's TLS rootset. This lazily
/// initialises the backing vector.
fn push(root: *mut u8) {
let mut rootset = unsafe { boehm::GC_tls_rootset() as *mut Vec<*mut u8> };
let mut rootset = unsafe { bdwgc::GC_tls_rootset() as *mut Vec<*mut u8> };
if rootset.is_null() {
let v = Vec::new();
let buf: *mut Vec<*mut u8> = Box::into_raw(Box::new(v));
unsafe { boehm::GC_init_tls_rootset(buf as *mut u8) };
unsafe { bdwgc::GC_init_tls_rootset(buf as *mut u8) };
rootset = buf
}
unsafe { (&mut *rootset).push(root) };
Expand Down
6 changes: 3 additions & 3 deletions library/std/src/sys/unix/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl Thread {
};
}

let ret = alloc::boehm::GC_pthread_create(&mut native, &attr, thread_start, p as *mut _);
let ret = alloc::bdwgc::GC_pthread_create(&mut native, &attr, thread_start, p as *mut _);
// Note: if the thread creation fails and this assert fails, then p will
// be leaked. However, an alternative design could cause double-free
// which is clearly worse.
Expand Down Expand Up @@ -267,7 +267,7 @@ impl Thread {

pub fn join(self) {
unsafe {
let ret = alloc::boehm::GC_pthread_join(self.id, ptr::null_mut());
let ret = alloc::bdwgc::GC_pthread_join(self.id, ptr::null_mut());
mem::forget(self);
assert!(ret == 0, "failed to join thread: {}", io::Error::from_raw_os_error(ret));
}
Expand All @@ -286,7 +286,7 @@ impl Thread {

impl Drop for Thread {
fn drop(&mut self) {
let ret = unsafe { alloc::boehm::GC_pthread_detach(self.id) };
let ret = unsafe { alloc::bdwgc::GC_pthread_detach(self.id) };
debug_assert_eq!(ret, 0);
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/runtime/gc/thread_local.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ignore-test
// run-pass
// ignore-tidy-linelength
// no-prefer-dynamic
#![feature(allocator_api)]
Expand Down

0 comments on commit 2a214a6

Please sign in to comment.