Skip to content

Commit

Permalink
Implement enif_alloc/free in case these are used by the lib's allocator
Browse files Browse the repository at this point in the history
  • Loading branch information
filmor committed Jun 6, 2024
1 parent 2366e44 commit df18a0f
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions rustler_tool/src/fake_symbols.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
#[no_mangle]
pub static enif_alloc: usize = 0;
use std::alloc::{Layout, alloc, dealloc};

const HEADER: usize = 8;
const ALIGNMENT: usize = 8;

#[no_mangle]
pub unsafe extern "C" fn enif_alloc(size: usize) -> *mut u8 {
if let Ok(layout) = Layout::from_size_align(size + HEADER, ALIGNMENT) {
let ptr = alloc(layout);
*(ptr as *mut usize) = size;
return ptr.wrapping_add(HEADER);
}

std::ptr::null_mut()
}

#[no_mangle]
pub unsafe extern "C" fn enif_free(ptr: *mut u8) {
let real_ptr = ptr.wrapping_sub(HEADER);
let size = *(real_ptr as *const usize);
if let Ok(layout) = Layout::from_size_align(size + HEADER, ALIGNMENT) {
dealloc(real_ptr, layout);
}
}

#[no_mangle]
pub static enif_alloc_binary: usize = 0;
#[no_mangle]
Expand All @@ -13,8 +36,6 @@ pub static enif_compare: usize = 0;
#[no_mangle]
pub static enif_consume_timeslice: usize = 0;
#[no_mangle]
pub static enif_free: usize = 0;
#[no_mangle]
pub static enif_free_env: usize = 0;
#[no_mangle]
pub static enif_get_atom: usize = 0;
Expand Down

0 comments on commit df18a0f

Please sign in to comment.