-
Notifications
You must be signed in to change notification settings - Fork 227
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}
- Loading branch information
Showing
3 changed files
with
25 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use std::alloc::{GlobalAlloc, Layout}; | ||
|
||
/// 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 { | ||
// TODO: Check the requested alignment. | ||
rustler_sys::enif_alloc(layout.size()) as *mut u8 | ||
} | ||
|
||
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) { | ||
rustler_sys::enif_free(ptr as *mut rustler_sys::c_void); | ||
} | ||
} |
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