Skip to content

Commit

Permalink
Use fjall as global_allocator for rustc_driver and C allocator …
Browse files Browse the repository at this point in the history
…for `rustc-main`
  • Loading branch information
Zoxc committed Mar 24, 2024
1 parent 8f5e879 commit 755d9c8
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 10 deletions.
19 changes: 19 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,17 @@ dependencies = [
"windows-sys 0.52.0",
]

[[package]]
name = "fjall"
version = "0.1.0"
source = "git+https://github.com/Zoxc/fjall.git#06440ceeb3dfd4b6c2c4191f16651fbf02e7a983"
dependencies = [
"bitflags 2.4.2",
"libc",
"sptr",
"windows-sys 0.52.0",
]

[[package]]
name = "flate2"
version = "1.0.28"
Expand Down Expand Up @@ -3387,6 +3398,7 @@ checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
name = "rustc-main"
version = "0.0.0"
dependencies = [
"fjall",
"jemalloc-sys",
"rustc_codegen_ssa",
"rustc_driver",
Expand Down Expand Up @@ -3777,6 +3789,7 @@ dependencies = [
name = "rustc_driver"
version = "0.0.0"
dependencies = [
"fjall",
"rustc_driver_impl",
]

Expand Down Expand Up @@ -5176,6 +5189,12 @@ dependencies = [
"uuid",
]

[[package]]
name = "sptr"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b9b39299b249ad65f3b7e96443bad61c02ca5cd3589f46cb6d610a0fd6c0d6a"

[[package]]
name = "stable_deref_trait"
version = "1.2.0"
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ edition = "2021"
[dependencies]
# tidy-alphabetical-start

fjall = { git = "https://github.com/Zoxc/fjall.git" }

# Make sure rustc_codegen_ssa ends up in the sysroot, because this
# crate is intended to be used by codegen backends, which may not be in-tree.
rustc_codegen_ssa = { path = "../rustc_codegen_ssa" }
Expand Down
49 changes: 40 additions & 9 deletions compiler/rustc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,38 @@
// https://github.com/rust-lang/rust/commit/b90cfc887c31c3e7a9e6d462e2464db1fe506175#diff-43914724af6e464c1da2171e4a9b6c7e607d5bc1203fa95c0ab85be4122605ef
// for an example of how to do so.

use std::os::raw::{c_int, c_void};

#[no_mangle]
unsafe extern "C" fn calloc(items: usize, size: usize) -> *mut c_void {
fjall::c::calloc(items, size)
}

#[no_mangle]
unsafe extern "C" fn posix_memalign(ptr: *mut *mut c_void, size: usize, align: usize) -> c_int {
fjall::c::posix_memalign(ptr, size, align)
}

#[no_mangle]
unsafe extern "C" fn aligned_alloc(size: usize, align: usize) -> *mut c_void {
fjall::c::aligned_alloc(size, align)
}

#[no_mangle]
unsafe extern "C" fn malloc(size: usize) -> *mut c_void {
fjall::c::malloc(size)
}

#[no_mangle]
unsafe extern "C" fn realloc(ptr: *mut c_void, size: usize) -> *mut c_void {
fjall::c::realloc(ptr, size)
}

#[no_mangle]
unsafe extern "C" fn free(ptr: *mut c_void) {
fjall::c::free(ptr);
}

#[unix_sigpipe = "sig_dfl"]
fn main() {
// See the comment at the top of this file for an explanation of this.
Expand All @@ -44,19 +76,18 @@ fn main() {
use std::os::raw::{c_int, c_void};

#[used]
static _F1: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::calloc;
static _F1: unsafe extern "C" fn(usize, usize) -> *mut c_void = calloc;
#[used]
static _F2: unsafe extern "C" fn(*mut *mut c_void, usize, usize) -> c_int =
jemalloc_sys::posix_memalign;
static _F2: unsafe extern "C" fn(*mut *mut c_void, usize, usize) -> c_int = posix_memalign;
#[used]
static _F3: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::aligned_alloc;
static _F3: unsafe extern "C" fn(usize, usize) -> *mut c_void = aligned_alloc;
#[used]
static _F4: unsafe extern "C" fn(usize) -> *mut c_void = jemalloc_sys::malloc;
static _F4: unsafe extern "C" fn(usize) -> *mut c_void = malloc;
#[used]
static _F5: unsafe extern "C" fn(*mut c_void, usize) -> *mut c_void = jemalloc_sys::realloc;
static _F5: unsafe extern "C" fn(*mut c_void, usize) -> *mut c_void = realloc;
#[used]
static _F6: unsafe extern "C" fn(*mut c_void) = jemalloc_sys::free;

static _F6: unsafe extern "C" fn(*mut c_void) = free;
/*
// On OSX, jemalloc doesn't directly override malloc/free, but instead
// registers itself with the allocator's zone APIs in a ctor. However,
// the linker doesn't seem to consider ctors as "used" when statically
Expand All @@ -69,7 +100,7 @@ fn main() {
#[used]
static _F7: unsafe extern "C" fn() = _rjem_je_zone_register;
}
}*/
}

rustc_driver::main()
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_driver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ crate-type = ["dylib"]

[dependencies]
# tidy-alphabetical-start
fjall = { git = "https://github.com/Zoxc/fjall.git" }
rustc_driver_impl = { path = "../rustc_driver_impl" }
# tidy-alphabetical-end
4 changes: 4 additions & 0 deletions compiler/rustc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@
#![feature(rustdoc_internals)]
#![doc(rust_logo)]

#[cfg(not(bootstrap))]
#[global_allocator]
static GLOBAL: fjall::Alloc = fjall::Alloc;

pub use rustc_driver_impl::*;
2 changes: 2 additions & 0 deletions src/tools/tidy/src/deps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
"fallible-iterator", // dependency of `thorin`
"fastrand",
"field-offset",
"fjall",
"flate2",
"fluent-bundle",
"fluent-langneg",
Expand Down Expand Up @@ -336,6 +337,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
"shlex",
"smallvec",
"snap",
"sptr",
"stable_deref_trait",
"stacker",
"static_assertions",
Expand Down
5 changes: 4 additions & 1 deletion src/tools/tidy/src/extdeps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use std::fs;
use std::path::Path;

/// List of allowed sources for packages.
const ALLOWED_SOURCES: &[&str] = &["\"registry+https://github.com/rust-lang/crates.io-index\""];
const ALLOWED_SOURCES: &[&str] = &[
"\"registry+https://github.com/rust-lang/crates.io-index\"",
"\"git+https://github.com/Zoxc/fjall.git#06440ceeb3dfd4b6c2c4191f16651fbf02e7a983\"",
];

/// Checks for external package sources. `root` is the path to the directory that contains the
/// workspace `Cargo.toml`.
Expand Down

0 comments on commit 755d9c8

Please sign in to comment.