1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use crate::base::*;
use core::alloc::{self, AllocError, Allocator};
use core::ptr::{self, NonNull};

pub struct Or<Primary: Allocator, Fallback: Allocator>(pub Primary, pub Fallback);

unsafe impl<Primary: Composable, Fallback: Composable> Composable for Or<Primary, Fallback> {
    unsafe fn has_allocated(&self, ptr: NonNull<u8>, layout: alloc::Layout) -> bool {
        self.0.has_allocated(ptr, layout) || self.1.has_allocated(ptr, layout)
    }

    fn manages_on_its_own(&self, layout: alloc::Layout) -> bool {
        self.0.manages_on_its_own(layout) || self.1.manages_on_its_own(layout)
    }
}

unsafe impl<Primary: Composable, Fallback: Allocator> Allocator for Or<Primary, Fallback> {
    fn allocate(&self, layout: alloc::Layout) -> Result<NonNull<[u8]>, AllocError> {
        if let Ok(block) = self.0.allocate(layout) {
            Ok(block)
        } else if self.0.manages_on_its_own(layout) {
            Err(AllocError)
        } else {
            self.1.allocate(layout)
        }
    }

    unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: alloc::Layout) {
        if self.0.has_allocated(ptr, layout) {
            self.0.deallocate(ptr, layout);
        } else {
            self.1.deallocate(ptr, layout);
        }
    }

    unsafe fn grow(
        &self, 
        ptr: NonNull<u8>, 
        old_layout: alloc::Layout, 
        new_layout: alloc::Layout
    ) -> Result<NonNull<[u8]>, AllocError> {
        if self.0.has_allocated(ptr, old_layout) {
            if let Ok(block) = self.0.grow(ptr, old_layout, new_layout) {
                Ok(block)
            } else {
                if let Ok(block) = self.1.allocate(new_layout) {
                    ptr::copy_nonoverlapping(ptr.as_ptr(), block.as_mut_ptr(), old_layout.size());
                    self.0.deallocate(ptr, old_layout);
                    Ok(block)
                } else {
                    Err(AllocError)
                }
            }
        } else {
            self.1.grow(ptr, old_layout, new_layout)
        }
    }

    unsafe fn shrink(
        &self, 
        ptr: NonNull<u8>, 
        old_layout: alloc::Layout, 
        new_layout: alloc::Layout
    ) -> Result<NonNull<[u8]>, AllocError> {
        if self.0.has_allocated(ptr, old_layout) {
            if let Ok(block) = self.0.shrink(ptr, old_layout, new_layout) {
                Ok(block)
            } else {
                if let Ok(block) = self.1.allocate(new_layout) {
                    ptr::copy_nonoverlapping(ptr.as_ptr(), block.as_mut_ptr(), new_layout.size());
                    self.0.deallocate(ptr, old_layout);
                    Ok(block)
                } else {
                    Err(AllocError)
                }
            }
        } else {
            self.1.shrink(ptr, old_layout, new_layout)
        }
    }
}