rustler/alloc.rs
1use std::alloc::{GlobalAlloc, Layout};
2
3use crate::sys::{c_void, enif_alloc, enif_free};
4
5const SIZEOF_USIZE: usize = std::mem::size_of::<usize>();
6const MAX_ALIGN: usize = 8;
7
8#[cfg(feature = "allocator")]
9#[global_allocator]
10static ALLOCATOR: EnifAllocator = EnifAllocator;
11
12/// Allocator implementation that forwards all allocation calls to Erlang's allocator. Allows the
13/// memory usage to be tracked by the BEAM.
14pub struct EnifAllocator;
15
16unsafe impl GlobalAlloc for EnifAllocator {
17 unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
18 if layout.align() > MAX_ALIGN {
19 // Overallocate and store the original pointer in memory immediately before the aligned
20 // section.
21 //
22 // The requested size is chosen such that we can always get an aligned buffer of size
23 // `layout.size()`: Ignoring `SIZEOF_USIZE`, there must always be an aligned pointer in
24 // the interval `[ptr, layout.align())`, so in the worst case, we have to pad with
25 // `layout.align() - 1`. The requirement for an additional `usize` just shifts the
26 // problem without changing the padding requirement.
27 let total_size = SIZEOF_USIZE + layout.size() + layout.align() - 1;
28 let ptr = enif_alloc(total_size) as *mut u8;
29
30 // Shift the returned pointer to make space for the original pointer
31 let ptr1 = ptr.wrapping_add(SIZEOF_USIZE);
32
33 // Align the result to the requested alignment
34 let aligned_ptr = ptr1.wrapping_add(ptr1.align_offset(layout.align()));
35
36 // Write the original pointer immediately in front of the aligned pointer
37 let header = aligned_ptr.wrapping_sub(SIZEOF_USIZE);
38 *(header as *mut usize) = ptr as usize;
39
40 aligned_ptr
41 } else {
42 enif_alloc(layout.size()) as *mut u8
43 }
44 }
45
46 unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
47 let ptr = if layout.align() > MAX_ALIGN {
48 // Retrieve the original pointer
49 let header = ptr.wrapping_sub(SIZEOF_USIZE);
50 let ptr = *(header as *mut usize);
51 ptr as *mut c_void
52 } else {
53 ptr as *mut c_void
54 };
55 enif_free(ptr);
56 }
57}