flex_alloc/storage/
alloc.rs

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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
use core::alloc::{Layout, LayoutError};
use core::fmt;
use core::marker::PhantomData;
use core::mem::{align_of, ManuallyDrop};
use core::ptr;
use core::ptr::NonNull;

use const_default::ConstDefault;

use super::RawBuffer;
use crate::alloc::{AllocateIn, Allocator, AllocatorDefault};
use crate::error::StorageError;

#[cfg(feature = "zeroize")]
use crate::alloc::AllocatorZeroizes;

/// A header type used by a buffer to determine its size.
pub trait BufferHeader<T: ?Sized>: Copy + fmt::Debug + Sized {
    /// The header value for a zero-sized buffer.
    const EMPTY: Self;

    /// Determine if this header represents a zero-sized buffer.
    fn is_empty(&self) -> bool;

    /// Calculate the layout for the associated value.
    fn layout(&self) -> Result<Layout, LayoutError>;

    /// Update the header from the result of a new allocation.
    fn update_for_alloc(&mut self, ptr: NonNull<[u8]>, exact: bool) -> NonNull<T>;
}

/// A slice allocation handle which stores the header metadata in the handle.
#[derive(Debug)]
pub struct FatBuffer<T: ?Sized, H: BufferHeader<T>, A: Allocator> {
    pub(crate) header: H,
    pub(crate) data: NonNull<T>,
    pub(crate) alloc: A,
}

impl<T, H: BufferHeader<T>, A: Allocator> FatBuffer<T, H, A> {
    #[inline]
    pub(crate) fn allocate_in<I>(header: H, alloc_in: I, exact: bool) -> Result<Self, StorageError>
    where
        I: AllocateIn<Alloc = A>,
    {
        let layout = header.layout()?;
        let (ptr, alloc) = alloc_in
            .allocate_in(layout)
            .map_err(|_| StorageError::AllocError(layout))?;
        let mut header = H::EMPTY;
        let data = header.update_for_alloc(ptr, exact);
        Ok(Self {
            header,
            data,
            alloc,
        })
    }

    #[inline]
    pub(crate) fn grow(&mut self, mut new_header: H, exact: bool) -> Result<(), StorageError> {
        let new_layout = new_header.layout()?;
        let ptr = if self.is_dangling() {
            self.alloc
                .allocate(new_layout)
                .map_err(|_| StorageError::AllocError(new_layout))?
        } else {
            let old_layout: Layout = self.header.layout()?;
            // SAFETY: the allocated pointer is guaranteed to be from this
            // allocator with the provided layout, or a larger layout with
            // the same alignment.
            unsafe { self.alloc.grow(self.data.cast(), old_layout, new_layout) }
                .map_err(|_| StorageError::AllocError(new_layout))?
        };
        self.data = new_header.update_for_alloc(ptr, exact);
        self.header = new_header;
        Ok(())
    }

    #[inline]
    pub(crate) fn shrink(&mut self, mut new_header: H) -> Result<(), StorageError> {
        if new_header.is_empty() {
            if !self.is_dangling() {
                let layout = self.header.layout()?;
                // SAFETY: the allocated pointer is guaranteed to be from this
                // allocator with the provided layout, or a larger layout with
                // the same alignment.
                unsafe { self.alloc.deallocate(self.data.cast(), layout) };
                self.data = NonNull::dangling();
            }
        } else {
            let new_layout = new_header.layout()?;
            let ptr = if self.is_dangling() {
                self.alloc
                    .allocate(new_layout)
                    .map_err(|_| StorageError::AllocError(new_layout))?
            } else {
                let old_layout: Layout = self.header.layout()?;
                // SAFETY: the allocated pointer is guaranteed to be from this
                // allocator with the provided layout, or a larger layout with
                // the same alignment.
                unsafe { self.alloc.shrink(self.data.cast(), old_layout, new_layout) }
                    .map_err(|_| StorageError::AllocError(new_layout))?
            };
            self.data = new_header.update_for_alloc(ptr, true);
        }
        self.header = new_header;
        Ok(())
    }
}

impl<T, H: BufferHeader<T>, A: Allocator> FatBuffer<T, H, A> {
    #[inline]
    pub(crate) const fn dangling(alloc: A) -> Self {
        Self {
            header: H::EMPTY,
            data: NonNull::dangling(),
            alloc,
        }
    }

    #[inline]
    pub(crate) fn is_dangling(&self) -> bool {
        self.data == NonNull::dangling()
    }
}

impl<T: ?Sized, H: BufferHeader<T>, A: Allocator> FatBuffer<T, H, A> {
    #[inline]
    pub(crate) fn from_parts(header: H, data: NonNull<T>, alloc: A) -> Self {
        Self {
            header,
            data,
            alloc,
        }
    }

    #[inline]
    pub(crate) fn into_parts(self) -> (H, NonNull<T>, A) {
        let slf = ManuallyDrop::new(self);
        (slf.header, slf.data, unsafe { ptr::read(&slf.alloc) })
    }
}

impl<T, H, A> ConstDefault for FatBuffer<T, H, A>
where
    H: BufferHeader<T>,
    A: AllocatorDefault,
{
    const DEFAULT: Self = Self::dangling(A::DEFAULT);
}

impl<T: ?Sized, H: BufferHeader<T>, A: Allocator> RawBuffer for FatBuffer<T, H, A> {
    type RawData = T;

    #[inline]
    fn data_ptr(&self) -> *const T {
        self.data.as_ptr()
    }

    #[inline]
    fn data_ptr_mut(&mut self) -> *mut T {
        self.data.as_ptr()
    }
}

impl<T: ?Sized, H: BufferHeader<T>, A: Allocator> Drop for FatBuffer<T, H, A> {
    fn drop(&mut self) {
        let layout = self.header.layout().expect("Layout error");
        if layout.size() > 0 {
            unsafe {
                self.alloc.deallocate(self.data.cast(), layout);
            }
        }
    }
}

#[cfg(feature = "zeroize")]
impl<T: ?Sized, H: BufferHeader<T>, A: AllocatorZeroizes> zeroize::ZeroizeOnDrop
    for FatBuffer<T, H, A>
{
}

pub(crate) struct ThinPtr<T, H: BufferHeader<T>>(NonNull<T>, PhantomData<H>);

impl<T, H: BufferHeader<T>> ThinPtr<T, H> {
    const DATA_OFFSET: usize = {
        // Calculate the byte offset of Data when following Header. This should
        // be equivalent to offset_of!((Meta::Header, Meta::Data), 1),
        // although repr(C) would need to be used to guarantee consistency.
        // See `Layout::padding_needed_for` (currently unstable) for reference.
        let header = Layout::new::<H>();
        let data_align = align_of::<T>();
        header.size().wrapping_add(data_align).wrapping_sub(1) & !data_align.wrapping_sub(1)
    };

    #[inline]
    pub const fn dangling() -> Self {
        Self(NonNull::dangling(), PhantomData)
    }

    #[inline]
    pub fn is_dangling(&self) -> bool {
        ptr::eq(self.0.as_ptr(), NonNull::dangling().as_ptr())
    }

    #[inline]
    pub fn from_alloc(mut header: H, ptr: NonNull<[u8]>, exact: bool) -> Self {
        #[allow(clippy::len_zero)]
        if ptr.len() == 0 {
            Self::dangling()
        } else {
            assert!(
                ptr.len() >= Self::DATA_OFFSET,
                "allocation too small for thin ptr"
            );
            // SAFETY: the allocation is at least `DATA_OFFSET` in size.
            let head = unsafe { ptr.cast::<u8>().add(Self::DATA_OFFSET) };
            let data_alloc = NonNull::slice_from_raw_parts(head, ptr.len() - Self::DATA_OFFSET);
            let data = header.update_for_alloc(data_alloc, exact);
            unsafe { ptr.cast::<H>().as_ptr().write(header) };
            Self(data, PhantomData)
        }
    }

    #[inline]
    fn layout(header: &H) -> Result<Layout, LayoutError> {
        if header.is_empty() {
            Ok(unsafe { Layout::from_size_align_unchecked(0, align_of::<H>()) })
        } else {
            let data_layout = header.layout()?;
            match Layout::new::<H>().extend(data_layout) {
                Ok((layout, _)) => Ok(layout),
                Err(err) => Err(err),
            }
        }
    }

    /// # Safety
    /// The pointer must be established to be non-dangling.
    #[inline]
    pub const unsafe fn to_alloc(&self) -> NonNull<u8> {
        NonNull::new_unchecked(self.header_ptr()).cast()
    }

    #[inline]
    pub const fn as_ptr(&self) -> *mut T {
        self.0.as_ptr()
    }

    #[inline]
    pub const fn header_ptr(&self) -> *mut H {
        unsafe { (self.0.as_ptr() as *mut u8).sub(Self::DATA_OFFSET) as *mut _ }
    }
}

impl<T, H: BufferHeader<T>> fmt::Debug for ThinPtr<T, H> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:?}", &self.0)
    }
}

/// A slice allocation handle which stores the header metadata in the handle.
#[derive(Debug)]
pub struct ThinBuffer<T, H: BufferHeader<T>, A: Allocator> {
    pub(crate) data: ThinPtr<T, H>,
    pub(crate) alloc: A,
}

impl<T, H: BufferHeader<T>, A: Allocator> ThinBuffer<T, H, A> {
    #[inline]
    pub(crate) fn allocate_in<I>(header: H, alloc_in: I, exact: bool) -> Result<Self, StorageError>
    where
        I: AllocateIn<Alloc = A>,
    {
        let layout = ThinPtr::layout(&header)?;
        let (ptr, alloc) = alloc_in
            .allocate_in(layout)
            .map_err(|_| StorageError::AllocError(layout))?;
        let data = ThinPtr::from_alloc(header, ptr, exact);
        Ok(Self { data, alloc })
    }

    #[inline]
    pub(crate) fn grow(&mut self, new_header: H, exact: bool) -> Result<(), StorageError> {
        let old_header = self.header();
        let new_layout = ThinPtr::layout(&new_header)?;
        assert!(new_layout.size() != 0, "Cannot grow to empty buffer");
        let ptr = if old_header.is_empty() {
            self.alloc
                .allocate(new_layout)
                .map_err(|_| StorageError::AllocError(new_layout))?
        } else {
            let old_layout: Layout = ThinPtr::<T, H>::layout(&old_header)?;
            unsafe {
                self.alloc
                    .grow(self.data.to_alloc(), old_layout, new_layout)
            }
            .map_err(|_| StorageError::AllocError(new_layout))?
        };
        self.data = ThinPtr::from_alloc(new_header, ptr, exact);
        Ok(())
    }

    #[inline]
    pub(crate) fn shrink(&mut self, new_header: H) -> Result<(), StorageError> {
        let old_header = self.header();
        let old_layout = if old_header.is_empty() {
            None
        } else {
            Some(ThinPtr::<T, H>::layout(&old_header)?)
        };
        if new_header.is_empty() {
            if let Some(old_layout) = old_layout {
                unsafe { self.alloc.deallocate(self.data.to_alloc(), old_layout) };
                self.data = ThinPtr::dangling();
            }
        } else {
            let new_layout = ThinPtr::<T, H>::layout(&new_header)?;
            let ptr = if let Some(old_layout) = old_layout {
                unsafe {
                    self.alloc
                        .shrink(self.data.to_alloc(), old_layout, new_layout)
                }
                .map_err(|_| StorageError::AllocError(new_layout))?
            } else {
                self.alloc
                    .allocate(new_layout)
                    .map_err(|_| StorageError::AllocError(new_layout))?
            };
            self.data = ThinPtr::from_alloc(new_header, ptr, true);
        }
        Ok(())
    }
}

impl<T, H: BufferHeader<T>, A: Allocator> ThinBuffer<T, H, A> {
    #[inline]
    pub(crate) const fn dangling(alloc: A) -> Self {
        Self {
            data: ThinPtr::dangling(),
            alloc,
        }
    }

    #[inline]
    pub(crate) fn is_dangling(&self) -> bool {
        self.data.is_dangling()
    }

    #[inline]
    pub(crate) fn header(&self) -> H {
        if self.is_dangling() {
            H::EMPTY
        } else {
            unsafe { ptr::read(self.data.header_ptr()) }
        }
    }

    #[inline]
    pub(crate) unsafe fn set_header(&mut self, header: H) {
        self.data.header_ptr().write(header)
    }
}

impl<T, H: BufferHeader<T>, A: Allocator> ThinBuffer<T, H, A> {
    #[inline]
    pub(crate) fn from_parts(_header: H, data: NonNull<T>, alloc: A) -> Self {
        // FIXME assert headers match?
        Self {
            data: ThinPtr(data, PhantomData),
            alloc,
        }
    }

    #[inline]
    pub(crate) fn into_parts(self) -> (H, NonNull<T>, A) {
        let slf = ManuallyDrop::new(self);
        let header = if slf.is_dangling() {
            H::EMPTY
        } else {
            unsafe { ptr::read(slf.data.header_ptr()) }
        };
        (header, slf.data.0, unsafe { ptr::read(&slf.alloc) })
    }
}

impl<T, H, A> ConstDefault for ThinBuffer<T, H, A>
where
    H: BufferHeader<T>,
    A: AllocatorDefault,
{
    const DEFAULT: Self = Self::dangling(A::DEFAULT);
}

impl<T, H: BufferHeader<T>, A: Allocator> RawBuffer for ThinBuffer<T, H, A> {
    type RawData = T;

    #[inline]
    fn data_ptr(&self) -> *const T {
        self.data.as_ptr()
    }

    #[inline]
    fn data_ptr_mut(&mut self) -> *mut T {
        self.data.as_ptr()
    }
}

#[cfg(feature = "zeroize")]
impl<T, H: BufferHeader<T>, A: AllocatorZeroizes> zeroize::ZeroizeOnDrop for ThinBuffer<T, H, A> {}

impl<T, H: BufferHeader<T>, A: Allocator> Drop for ThinBuffer<T, H, A> {
    fn drop(&mut self) {
        let header = self.header();
        if !header.is_empty() {
            let layout = ThinPtr::<T, H>::layout(&header).expect("Layout error");
            // SAFETY: the allocated pointer is guaranteed to be from this
            // allocator with the provided layout, or a larger layout with
            // the same alignment.
            unsafe { self.alloc.deallocate(self.data.to_alloc(), layout) };
        }
    }
}