Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement simple allocator using enif_{alloc,free} #580

Merged
merged 1 commit into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ versions.
## [unreleased]

### Added

- Add optional support for using Erlang's allocator as Rust's global allocator
(#580).

### Fixed
### Changed

Expand Down
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ members = [
"rustler_tests/native/rustler_compile_tests",
"rustler_benchmarks/native/benchmark",
]
default-members = [
"rustler",
"rustler_codegen",
"rustler_sys",
]
1 change: 1 addition & 0 deletions rustler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ big_integer = ["dep:num-bigint"]
default = ["derive", "nif_version_2_15"]
derive = ["rustler_codegen"]
alternative_nif_init_name = []
allocator = []
nif_version_2_14 = ["rustler_sys/nif_version_2_14"]
nif_version_2_15 = ["nif_version_2_14", "rustler_sys/nif_version_2_15"]
nif_version_2_16 = ["nif_version_2_15", "rustler_sys/nif_version_2_16"]
Expand Down
55 changes: 55 additions & 0 deletions rustler/src/alloc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use std::alloc::{GlobalAlloc, Layout};

const SIZEOF_USIZE: usize = std::mem::size_of::<usize>();
const MAX_ALIGN: usize = 8;
filmor marked this conversation as resolved.
Show resolved Hide resolved

#[cfg(feature = "allocator")]
#[global_allocator]
static ALLOCATOR: EnifAllocator = EnifAllocator;

/// Allocator implementation that forwards all allocation calls to Erlang's allocator. Allows the
/// memory usage to be tracked by the BEAM.
pub struct EnifAllocator;

unsafe impl GlobalAlloc for EnifAllocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
if layout.align() > MAX_ALIGN {
// Overallocate and store the original pointer in memory immediately before the aligned
// section.
//
// The requested size is chosen such that we can always get an aligned buffer of size
// `layout.size()`: Ignoring `SIZEOF_USIZE`, there must always be an aligned pointer in
// the interval `[ptr, layout.align())`, so in the worst case, we have to pad with
// `layout.align() - 1`. The requirement for an additional `usize` just shifts the
// problem without changing the padding requirement.
let total_size = SIZEOF_USIZE + layout.size() + layout.align() - 1;
let ptr = rustler_sys::enif_alloc(total_size) as *mut u8;

// Shift the returned pointer to make space for the original pointer
let ptr1 = ptr.wrapping_add(SIZEOF_USIZE);

// Align the result to the requested alignment
let aligned_ptr = ptr1.wrapping_add(ptr1.align_offset(layout.align()));

// Write the original pointer immediately in front of the aligned pointer
let header = aligned_ptr.wrapping_sub(SIZEOF_USIZE);
*(header as *mut usize) = ptr as usize;

aligned_ptr
} else {
rustler_sys::enif_alloc(layout.size()) as *mut u8
}
}

unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
let ptr = if layout.align() > MAX_ALIGN {
// Retrieve the original pointer
let header = ptr.wrapping_sub(SIZEOF_USIZE);
let ptr = *(header as *mut usize);
ptr as *mut rustler_sys::c_void
} else {
ptr as *mut rustler_sys::c_void
};
rustler_sys::enif_free(ptr);
}
}
2 changes: 2 additions & 0 deletions rustler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub mod wrapper;
#[doc(hidden)]
pub mod codegen_runtime;

mod alloc;

#[macro_use]
pub mod types;

Expand Down
2 changes: 1 addition & 1 deletion rustler_tests/native/dynamic_load/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ path = "src/lib.rs"
crate-type = ["cdylib"]

[dependencies]
rustler = { path = "../../../rustler", features = ["big_integer"] }
rustler = { path = "../../../rustler", features = ["big_integer", "allocator"] }
2 changes: 1 addition & 1 deletion rustler_tests/native/rustler_bigint_test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ path = "src/lib.rs"
crate-type = ["cdylib"]

[dependencies]
rustler = { path = "../../../rustler", features = ["big_integer"] }
rustler = { path = "../../../rustler", features = ["big_integer", "allocator"] }
2 changes: 1 addition & 1 deletion rustler_tests/native/rustler_compile_tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ path = "src/lib.rs"
crate-type = ["cdylib"]

[dependencies]
rustler = { path = "../../../rustler" }
rustler = { path = "../../../rustler", features = ["allocator"] }
2 changes: 1 addition & 1 deletion rustler_tests/native/rustler_test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ nif_version_2_16 = ["nif_version_2_15", "rustler/nif_version_2_16"]
nif_version_2_17 = ["nif_version_2_16", "rustler/nif_version_2_17"]

[dependencies]
rustler = { path = "../../../rustler" }
rustler = { path = "../../../rustler", features = ["allocator"] }
Loading