C++: implement an allocator that use libc allocator when no_std

It is important to use the same heap, when there isn't a lot of
available memory
This commit is contained in:
Olivier Goffart 2023-07-06 15:35:33 +02:00 committed by Olivier Goffart
parent 06cb0e8703
commit 008e60b530

View file

@ -127,3 +127,27 @@ pub unsafe extern "C" fn slint_register_font_from_data(
pub unsafe extern "C" fn slint_testing_init_backend() {
i_slint_backend_testing::init();
}
#[cfg(not(feature = "std"))]
mod allocator {
use core::alloc::Layout;
use core::ffi::c_void;
extern "C" {
pub fn free(p: *mut c_void);
// This function is part of C11 & C++17
pub fn aligned_alloc(alignment: usize, size: usize) -> *mut c_void;
}
struct CAlloc;
unsafe impl core::alloc::GlobalAlloc for CAlloc {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
aligned_alloc(layout.align(), layout.size()) as *mut u8
}
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
free(ptr as *mut c_void);
}
}
#[global_allocator]
static ALLOCATOR: CAlloc = CAlloc;
}