flipperzero_alloc/
lib.rs

1//! Alloc support for the Flipper Zero.
2//! *Note:* This currently requires using nightly.
3
4#![no_std]
5#![deny(rustdoc::broken_intra_doc_links)]
6
7use core::alloc::{GlobalAlloc, Layout};
8
9use flipperzero_sys as sys;
10
11pub struct FuriAlloc;
12
13unsafe impl GlobalAlloc for FuriAlloc {
14    #[inline]
15    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
16        unsafe { sys::aligned_malloc(layout.size(), layout.align()).cast() }
17    }
18
19    #[inline]
20    unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
21        unsafe { sys::aligned_free(ptr.cast()) }
22    }
23
24    #[inline]
25    unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
26        // https://github.com/flipperdevices/flipperzero-firmware/issues/1747#issuecomment-1253636552
27        unsafe { self.alloc(layout) }
28    }
29}
30
31#[global_allocator]
32static ALLOCATOR: FuriAlloc = FuriAlloc;