mimalloc3_rs/
lib.rs

1
2use core::alloc::{GlobalAlloc, Layout};
3use core::ffi::c_void;
4use libmimalloc3_sys::*;
5
6pub struct MiMalloc;
7
8unsafe impl GlobalAlloc for MiMalloc {
9    #[inline]
10    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
11        unsafe {
12            mi_malloc_aligned(layout.size(), layout.align()) as *mut u8
13        }
14    }
15
16    #[inline]
17    unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
18        unsafe {
19            mi_free(ptr as *mut c_void);
20        }
21    }
22
23    #[inline]
24    unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
25        unsafe {
26            mi_zalloc_aligned(layout.size(), layout.align()) as *mut u8
27        }
28    }
29
30    #[inline]
31    unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
32        unsafe {
33            mi_realloc_aligned(ptr as *mut c_void, new_size, layout.align()) as *mut u8
34        }
35    }
36}