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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use {
    crate::{AllocStorage, InlineStorage, Memory, Storage},
    core::{
        alloc::{AllocError, Allocator, Layout},
        hint::unreachable_unchecked,
        ptr::copy_nonoverlapping,
    },
};

/// A single storage which stores memory inline if it fits, otherwise falling
/// back to using an [`Allocator`].
///
/// The `DataStore` type parameter determines the layout of the inline storage.
/// (It would be nice to use `const LAYOUT: Layout` instead, but the needed
/// features are currently a little *too* incomplete to be usable here.)
pub struct SmallStorage<DataStore, A: Allocator> {
    inline: InlineStorage<DataStore>,
    outline: AllocStorage<A>,
}

impl<DataStore, A: Allocator> SmallStorage<DataStore, A> {
    pub fn new(alloc: A) -> Self {
        Self {
            inline: InlineStorage::new(),
            outline: AllocStorage::new(alloc),
        }
    }

    const OUTLINE_HANDLE_LAYOUT: Layout = Layout::new::<<AllocStorage<A> as Storage>::Handle>();
}

unsafe impl<DataStore, A: Allocator> Storage for SmallStorage<DataStore, A> {
    type Handle = ();

    fn allocate(&mut self, layout: Layout) -> Result<Self::Handle, AllocError> {
        if self.inline.fits(layout) {
            self.inline.allocate(layout)
        } else {
            let addr = self.outline.allocate(layout)?;
            let addr_handle = self.inline.allocate(Self::OUTLINE_HANDLE_LAYOUT)?;
            unsafe {
                *self
                    .inline
                    .resolve_mut(addr_handle, Self::OUTLINE_HANDLE_LAYOUT)
                    .as_mut_ptr()
                    .cast() = addr;
            }
            Ok(addr_handle)
        }
    }

    unsafe fn deallocate(&mut self, handle: Self::Handle, layout: Layout) {
        if self.inline.fits(layout) {
            self.inline.deallocate(handle, layout)
        } else {
            let addr = *self
                .inline
                .resolve_mut(handle, Self::OUTLINE_HANDLE_LAYOUT)
                .as_ptr()
                .cast();
            self.inline.deallocate(handle, Self::OUTLINE_HANDLE_LAYOUT);
            self.outline.deallocate(addr, layout);
        }
    }

    unsafe fn resolve(&self, handle: Self::Handle, layout: Layout) -> &Memory {
        if self.inline.fits(layout) {
            self.inline.resolve(handle, layout)
        } else {
            let addr = *self
                .inline
                .resolve(handle, Self::OUTLINE_HANDLE_LAYOUT)
                .as_ptr()
                .cast();
            self.outline.resolve(addr, layout)
        }
    }

    unsafe fn resolve_mut(&mut self, handle: Self::Handle, layout: Layout) -> &mut Memory {
        if self.inline.fits(layout) {
            self.inline.resolve_mut(handle, layout)
        } else {
            let addr = *self
                .inline
                .resolve_mut(handle, Self::OUTLINE_HANDLE_LAYOUT)
                .as_mut_ptr()
                .cast();
            self.outline.resolve_mut(addr, layout)
        }
    }

    unsafe fn grow(
        &mut self,
        handle: Self::Handle,
        old_layout: Layout,
        new_layout: Layout,
    ) -> Result<Self::Handle, AllocError> {
        match (self.inline.fits(old_layout), self.inline.fits(new_layout)) {
            (true, true) => self.inline.grow(handle, old_layout, new_layout),
            (false, true) => unreachable_unchecked(),
            (false, false) => {
                let addr = self
                    .inline
                    .resolve_mut(handle, Self::OUTLINE_HANDLE_LAYOUT)
                    .as_mut_ptr()
                    .cast();
                *addr = self.outline.grow(*addr, old_layout, new_layout)?;
                Ok(handle)
            },
            (true, false) => {
                if !self.inline.fits(Self::OUTLINE_HANDLE_LAYOUT) {
                    return Err(AllocError);
                }

                let addr = self.outline.allocate(new_layout)?;
                let new_ptr = self.outline.resolve_mut(addr, new_layout);
                let old_ptr = self.inline.resolve_mut(handle, old_layout);

                copy_nonoverlapping(
                    old_ptr.as_mut_ptr(),
                    new_ptr.as_mut_ptr(),
                    old_layout.size(),
                );

                self.inline.deallocate(handle, old_layout);
                let addr_handle = self
                    .inline
                    .allocate(Self::OUTLINE_HANDLE_LAYOUT)
                    .unwrap_unchecked();
                *self
                    .inline
                    .resolve_mut(addr_handle, Self::OUTLINE_HANDLE_LAYOUT)
                    .as_mut_ptr()
                    .cast() = addr;
                Ok(addr_handle)
            },
        }
    }

    unsafe fn shrink(
        &mut self,
        handle: Self::Handle,
        old_layout: Layout,
        new_layout: Layout,
    ) -> Result<Self::Handle, AllocError> {
        match (self.inline.fits(old_layout), self.inline.fits(new_layout)) {
            (true, true) => self.inline.shrink(handle, old_layout, new_layout),
            (true, false) => unreachable_unchecked(),
            (false, false) => {
                let addr = self
                    .inline
                    .resolve_mut(handle, Self::OUTLINE_HANDLE_LAYOUT)
                    .as_mut_ptr()
                    .cast();
                *addr = self.outline.shrink(*addr, old_layout, new_layout)?;
                Ok(handle)
            },
            (false, true) => {
                todo!();
            },
        }
    }
}