palloc/global/mod.rs
1use core::alloc::GlobalAlloc;
2use core::ptr::NonNull;
3
4#[cfg(feature = "allocator_api")]
5use core::alloc::Allocator;
6
7#[cfg(feature = "allocator_api")]
8pub trait GlobalPallocConstraint = GlobalAlloc + Allocator;
9#[cfg(not(feature = "allocator_api"))]
10pub trait GlobalPallocConstraint = GlobalAlloc;
11
12/// Defines what an allocator implementing GlobalAlloc
13/// and Allocator for Palloc should look like.
14/// Struct implementing this are guaranteed to implement GlobalAlloc
15/// and Allocator (not if allocato_api is disabled).
16///
17/// Allocator implementations may implement an empty const method
18/// for static initialization.
19///
20/// All safety concerns that apply to [`Palloc`](crate::Palloc)
21/// apply to here too.
22pub trait GlobalPalloc: GlobalPallocConstraint + Sized + Send {
23 /// Creates an empty uninitialized instance of the allocator
24 fn new() -> Self;
25
26 /// Initialies the allocator from the base pointer and size
27 /// components
28 ///
29 /// ### Safety
30 /// Check out [`Palloc.init`](crate::Palloc::init)
31 /// for more informations.
32 unsafe fn init(&mut self, bottom: NonNull<u8>, size: usize);
33
34 /// Initializes the allocator from a slice. Panics if pointing to
35 /// a null pointer.
36 ///
37 /// ### Safety
38 /// Check out [`Palloc.init_from_slice`](crate::Palloc::init_from_slice)
39 /// for more informations.
40 unsafe fn init_from_slice(&mut self, heap: &mut [u8]);
41
42 /// Creates a [`new`](#tymethod.new) allocator and calls [`init`]
43 ///
44 /// ### Safety
45 /// Refer to [`init`]
46 ///
47 /// [`init`]: #tymethod.init
48 unsafe fn new_initialized(bottom: NonNull<u8>, size: usize) -> Self {
49 let mut allocator = Self::new();
50 allocator.init(bottom, size);
51
52 allocator
53 }
54
55 /// Creates a [`new`](#tymethod.new) allocator and calls [`init_from_slice`]
56 ///
57 /// ### Safety
58 /// Refer to [`init_from_slice`]
59 ///
60 /// [`init_from_slice`]: #tymethod.init_from_slice
61 unsafe fn new_from_slice(heap: &mut [u8]) -> Self {
62 let mut allocator = Self::new();
63 allocator.init_from_slice(heap);
64
65 allocator
66 }
67}
68
69/// unsafecell based global allocator for
70/// generic mmu-less single core systems
71pub mod unsafecell;
72pub use self::unsafecell::UnsafeCellPalloc;
73
74/// spinlock based global allocator
75#[cfg(feature = "spin")]
76pub mod spin;
77#[cfg(feature = "spin")]
78pub use self::spin::SpinPalloc;