-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement simple allocator using enif_{alloc,free}
Since we can only expect a 64bit alignment from the Erlang allocator, we have to ensure larger alignments by overallocating. The allocator for this case behaves the same way as the zigler "large beam allocator", see https://github.com/E-xyza/zigler/blob/main/priv/beam/allocator.zig. If the alignment is greater than 8, we allocate enough memory to store an additional pointer. The result of the initial allocation is then written immediately before the aligned pointer, which is returned from the allocator. When deallocating, we can retrieve the original pointer and pass it on to `enif_free`.
- Loading branch information
Showing
8 changed files
with
58 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use std::alloc::{GlobalAlloc, Layout}; | ||
|
||
const SIZEOF_USIZE: usize = std::mem::size_of::<usize>(); | ||
const MAX_ALIGN: usize = 8; | ||
|
||
#[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 | ||
let padded = layout.pad_to_align(); | ||
let total_size = SIZEOF_USIZE + padded.size(); | ||
let ptr = rustler_sys::enif_alloc(total_size) as *mut u8; | ||
|
||
let ptr1 = ptr.wrapping_add(SIZEOF_USIZE); | ||
let aligned_ptr = ptr1.wrapping_add(ptr1.align_offset(layout.align())); | ||
|
||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,8 @@ pub mod wrapper; | |
#[doc(hidden)] | ||
pub mod codegen_runtime; | ||
|
||
mod alloc; | ||
|
||
#[macro_use] | ||
pub mod types; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters