File tree Expand file tree Collapse file tree 1 file changed +25
-4
lines changed
Expand file tree Collapse file tree 1 file changed +25
-4
lines changed Original file line number Diff line number Diff line change 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]
427pub static enif_alloc_binary: usize = 0 ;
528#[ no_mangle]
@@ -13,8 +36,6 @@ pub static enif_compare: usize = 0;
1336#[ no_mangle]
1437pub static enif_consume_timeslice: usize = 0 ;
1538#[ no_mangle]
16- pub static enif_free: usize = 0 ;
17- #[ no_mangle]
1839pub static enif_free_env: usize = 0 ;
1940#[ no_mangle]
2041pub static enif_get_atom: usize = 0 ;
You can’t perform that action at this time.
0 commit comments