flex_alloc/storage/
spill.rs1use core::alloc::Layout;
2use core::marker::PhantomData;
3use core::ptr::{self, NonNull};
4
5use crate::alloc::{AllocError, AllocateIn, Allocator, AllocatorDefault, Fixed, Spill};
6
7#[derive(Debug, Default, Clone)]
10pub struct SpillStorage<'a, I: 'a, A> {
11 pub(crate) buffer: I,
12 pub(crate) alloc: A,
13 _pd: PhantomData<&'a mut ()>,
14}
15
16impl<I, A: Allocator> SpillStorage<'_, I, A> {
17 #[inline]
18 pub(crate) fn new_in(buffer: I, alloc: A) -> Self {
19 Self {
20 buffer,
21 alloc,
22 _pd: PhantomData,
23 }
24 }
25}
26
27impl<'a, I, A> AllocateIn for SpillStorage<'a, I, A>
28where
29 I: AllocateIn<Alloc = Fixed<'a>>,
30 A: Allocator,
31{
32 type Alloc = Spill<'a, A>;
33
34 #[inline]
35 fn allocate_in(self, layout: Layout) -> Result<(NonNull<[u8]>, Self::Alloc), AllocError> {
36 match self.buffer.allocate_in(layout) {
37 Ok((ptr, fixed)) => {
38 let alloc = Spill::new(self.alloc, ptr.as_ptr().cast(), fixed);
39 Ok((ptr, alloc))
40 }
41 Err(_) => {
42 let ptr = self.alloc.allocate(layout)?;
43 let alloc = Spill::new(self.alloc, ptr::null(), Fixed::DEFAULT);
44 Ok((ptr, alloc))
45 }
46 }
47 }
48}