Skip to content

Commit 266e052

Browse files
committed
Implement enif_alloc/free in case these are used by the lib's allocator
1 parent a0fbad3 commit 266e052

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

rustler_tool/src/fake_symbols.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
1-
#[no_mangle]
2-
pub static enif_alloc: usize = 0;
1+
use std::alloc::{Layout, alloc, dealloc};
2+
3+
const HEADER: usize = 8;
4+
const ALIGNMENT: usize = 8;
5+
6+
#[no_mangle]
7+
pub unsafe extern "C" fn enif_alloc(size: usize) -> *mut u8 {
8+
if let Ok(layout) = Layout::from_size_align(size + HEADER, ALIGNMENT) {
9+
let ptr = alloc(layout);
10+
*(ptr as *mut usize) = size;
11+
return ptr.wrapping_add(HEADER);
12+
}
13+
14+
std::ptr::null_mut()
15+
}
16+
17+
#[no_mangle]
18+
pub unsafe extern "C" fn enif_free(ptr: *mut u8) {
19+
let real_ptr = ptr.wrapping_sub(HEADER);
20+
let size = *(real_ptr as *const usize);
21+
if let Ok(layout) = Layout::from_size_align(size + HEADER, ALIGNMENT) {
22+
dealloc(real_ptr, layout);
23+
}
24+
}
25+
326
#[no_mangle]
427
pub static enif_alloc_binary: usize = 0;
528
#[no_mangle]
@@ -13,8 +36,6 @@ pub static enif_compare: usize = 0;
1336
#[no_mangle]
1437
pub static enif_consume_timeslice: usize = 0;
1538
#[no_mangle]
16-
pub static enif_free: usize = 0;
17-
#[no_mangle]
1839
pub static enif_free_env: usize = 0;
1940
#[no_mangle]
2041
pub static enif_get_atom: usize = 0;

0 commit comments

Comments
 (0)