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
use crate::{helper::AllocInit, Owns, ReallocateInPlace};
use core::{
    alloc::{AllocError, AllocRef, Layout},
    ptr::NonNull,
};

/// Allocate memory with a multiple size of the provided chunk size.
///
/// # Examples
///
/// ```rust
/// #![feature(allocator_api, slice_ptr_len)]
///
/// use alloc_compose::Chunk;
/// use std::alloc::{AllocRef, Layout, System};
///
/// let mut alloc = Chunk::<_, 64>(System);
/// let ptr = alloc.alloc(Layout::new::<[u8; 16]>())?;
/// assert_eq!(ptr.len() % 64, 0);
/// assert!(ptr.len() >= 64);
/// # Ok::<(), core::alloc::AllocError>(())
/// ```
///
/// When growing or shrinking the memory, `Chunk` will try to alter
/// the memory in place before delegating to the underlying allocator.
///
/// ```rust
/// #![feature(slice_ptr_get)]
/// # #![feature(allocator_api, slice_ptr_len)]
/// # use alloc_compose::Chunk;
/// # use std::{alloc::{AllocRef, Layout, System}};
/// # let mut alloc = Chunk::<_, 64>(System);
/// # let ptr = alloc.alloc(Layout::new::<[u8; 16]>())?;
///
/// let new_ptr = unsafe {
///     alloc.grow(
///         ptr.as_non_null_ptr(),
///         Layout::new::<[u8; 16]>(),
///         Layout::new::<[u8; 24]>(),
///     )?
/// };
///
/// assert_eq!(ptr, new_ptr);
/// # Ok::<(), core::alloc::AllocError>(())
/// ```
///
/// This can be enforced by using [`ReallocateInPlace::grow_in_place`].
///
/// ```rust
/// # #![feature(allocator_api, slice_ptr_len, slice_ptr_get)]
/// # use alloc_compose::Chunk;
/// # use std::{alloc::{AllocRef, Layout, System}};
/// # let mut alloc = Chunk::<_, 64>(System);
/// # let ptr = alloc.alloc(Layout::new::<[u8; 24]>())?;
/// use alloc_compose::ReallocateInPlace;
///
/// let len = unsafe {
///     alloc.grow_in_place(
///         ptr.as_non_null_ptr(),
///         Layout::new::<[u8; 24]>(),
///         Layout::new::<[u8; 32]>(),
///     )?
/// };
///
/// assert_eq!(len % 64, 0);
/// assert!(len >= 64);
/// # Ok::<(), core::alloc::AllocError>(())
/// ```
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq)]
pub struct Chunk<A, const SIZE: usize>(pub A);

mod sealed {
    pub trait SizeIsPowerOfTwo {}
}
use sealed::SizeIsPowerOfTwo;

macro_rules! is_power_of_two {
    ($($N:literal)+) => {
        $(
            impl<A> SizeIsPowerOfTwo for Chunk<A, { usize::pow(2, $N) }> {}
        )+
    };
}

is_power_of_two!(1 2 3 4 5 6 7);
#[cfg(any(
    target_pointer_width = "16",
    target_pointer_width = "32",
    target_pointer_width = "64"
))]
is_power_of_two!(8 9 10 11 12 13 14 15);
#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))]
is_power_of_two!(16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31);
#[cfg(target_pointer_width = "64")]
is_power_of_two!(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);

impl<A, const SIZE: usize> Chunk<A, SIZE>
where
    Self: SizeIsPowerOfTwo,
{
    fn round_up(size: usize) -> Result<usize, AllocError> {
        Ok((size.checked_add(SIZE).ok_or(AllocError)? - 1) & !(SIZE - 1))
    }

    unsafe fn round_up_unchecked(size: usize) -> usize {
        let new_size = (size.wrapping_add(SIZE) - 1) & !(SIZE - 1);
        debug_assert_eq!(new_size, Self::round_up(size).unwrap());
        new_size
    }

    const fn round_down(size: usize) -> usize {
        size & !(SIZE - 1)
    }

    const fn round_down_ptr_len(ptr: NonNull<[u8]>) -> NonNull<[u8]> {
        NonNull::slice_from_raw_parts(ptr.as_non_null_ptr(), Self::round_down(ptr.len()))
    }

    #[inline]
    fn alloc_impl(
        layout: Layout,
        alloc: impl FnOnce(Layout) -> Result<NonNull<[u8]>, AllocError>,
    ) -> Result<NonNull<[u8]>, AllocError> {
        let new_size = Self::round_up(layout.size())?;
        let new_layout = unsafe { Layout::from_size_align_unchecked(new_size, layout.align()) };

        alloc(new_layout).map(Self::round_down_ptr_len)
    }

    #[inline]
    unsafe fn grow_impl(
        old_ptr: NonNull<u8>,
        old_layout: Layout,
        new_layout: Layout,
        init: AllocInit,
        grow: impl FnOnce(NonNull<u8>, Layout, Layout) -> Result<NonNull<[u8]>, AllocError>,
    ) -> Result<NonNull<[u8]>, AllocError> {
        let old_size = old_layout.size();
        let current_size = Self::round_up_unchecked(old_size);
        let new_size = new_layout.size();
        if new_layout.align() <= old_layout.align() && new_size <= current_size {
            let ptr = NonNull::slice_from_raw_parts(old_ptr, current_size);
            init.init_offset(ptr, old_size);
            return Ok(ptr);
        }

        grow(
            old_ptr,
            Layout::from_size_align_unchecked(current_size, old_layout.align()),
            Layout::from_size_align_unchecked(Self::round_up(new_size)?, new_layout.align()),
        )
        .map(Self::round_down_ptr_len)
    }

    #[inline]
    unsafe fn shrink_impl(
        old_ptr: NonNull<u8>,
        old_layout: Layout,
        new_layout: Layout,
        shrink: impl FnOnce(NonNull<u8>, Layout, Layout) -> Result<NonNull<[u8]>, AllocError>,
    ) -> Result<NonNull<[u8]>, AllocError> {
        let current_size = Self::round_up_unchecked(old_layout.size());
        let new_size = new_layout.size();
        if new_layout.align() <= old_layout.align() && new_layout.size() > current_size - SIZE {
            return Ok(NonNull::slice_from_raw_parts(old_ptr, current_size));
        }

        shrink(
            old_ptr,
            old_layout,
            Layout::from_size_align_unchecked(
                Self::round_up_unchecked(new_size),
                new_layout.align(),
            ),
        )
        .map(Self::round_down_ptr_len)
    }
}

unsafe impl<A: AllocRef, const SIZE: usize> AllocRef for Chunk<A, SIZE>
where
    Self: SizeIsPowerOfTwo,
{
    impl_alloc_ref!(0);

    unsafe fn dealloc(&self, ptr: NonNull<u8>, layout: Layout) {
        crate::check_dealloc_precondition(ptr, layout);

        self.0.dealloc(
            ptr,
            Layout::from_size_align_unchecked(
                Self::round_up_unchecked(layout.size()),
                layout.align(),
            ),
        )
    }
}

// unsafe impl<A: AllocateAll, const SIZE: usize> AllocateAll for Chunk<A, SIZE>
// where
//     Self: SizeIsPowerOfTwo,
// {
//     impl_alloc_all!(0);
// }

unsafe impl<A, const SIZE: usize> ReallocateInPlace for Chunk<A, SIZE>
where
    Self: SizeIsPowerOfTwo,
{
    impl_realloc_in_place_spec!(0);
}

unsafe impl<A: ReallocateInPlace, const SIZE: usize> ReallocateInPlace for Chunk<A, SIZE>
where
    Self: SizeIsPowerOfTwo,
{
    impl_realloc_in_place!(0);
}

impl<A: Owns, const SIZE: usize> Owns for Chunk<A, SIZE>
where
    Self: SizeIsPowerOfTwo,
{
    fn owns(&self, memory: NonNull<[u8]>) -> bool {
        self.0.owns(memory)
    }
}

#[cfg(test)]
mod tests {
    use super::Chunk;
    use crate::{helper::tracker, ReallocateInPlace};
    use alloc::alloc::Global;
    use core::alloc::{AllocRef, Layout};

    #[test]
    fn alloc() {
        let alloc = Chunk::<_, 64>(tracker(Global));
        let memory = alloc
            .alloc(Layout::new::<[u8; 2]>())
            .expect("Could not allocate 64 bytes");
        assert_eq!(memory.len() % 64, 0);
        assert!(memory.len() >= 64);

        unsafe {
            alloc.dealloc(memory.as_non_null_ptr(), Layout::new::<u8>());
        }
    }

    #[test]
    fn dealloc() {
        let alloc = Chunk::<_, 64>(tracker(Global));

        unsafe {
            let memory = alloc
                .alloc(Layout::new::<[u8; 4]>())
                .expect("Could not allocate 4 bytes");
            assert_eq!(memory.len() % 64, 0);
            alloc.dealloc(memory.as_non_null_ptr(), Layout::new::<[u8; 4]>());

            let memory = alloc
                .alloc(Layout::new::<[u8; 8]>())
                .expect("Could not allocate 8 bytes");
            assert_eq!(memory.len() % 64, 0);
            alloc.dealloc(memory.as_non_null_ptr(), Layout::new::<[u8; 8]>());

            let memory = alloc
                .alloc(Layout::new::<[u8; 32]>())
                .expect("Could not allocate 32 bytes");
            assert_eq!(memory.len() % 64, 0);
            alloc.dealloc(memory.as_non_null_ptr(), Layout::new::<[u8; 32]>());

            let memory = alloc
                .alloc(Layout::new::<[u8; 64]>())
                .expect("Could not allocate 64 bytes");
            assert_eq!(memory.len() % 64, 0);
            alloc.dealloc(memory.as_non_null_ptr(), Layout::new::<[u8; 64]>());
        }
    }

    #[test]
    fn grow() {
        let alloc = Chunk::<_, 64>(tracker(Global));

        let memory = alloc
            .alloc(Layout::new::<[u8; 4]>())
            .expect("Could not allocate 4 bytes");
        assert_eq!(memory.len() % 64, 0);

        unsafe {
            let len = alloc
                .grow_in_place(
                    memory.as_non_null_ptr(),
                    Layout::new::<[u8; 4]>(),
                    Layout::new::<[u8; 64]>(),
                )
                .expect("Could not grow to 8 bytes");
            assert_eq!(len % 64, 0);
            assert!(len >= 64);

            let len = alloc
                .grow_in_place(
                    memory.as_non_null_ptr(),
                    Layout::new::<[u8; 8]>(),
                    Layout::new::<[u8; 64]>(),
                )
                .expect("Could not grow to 64 bytes");
            assert_eq!(len % 64, 0);
            assert!(len >= 64);

            alloc
                .grow_in_place(
                    memory.as_non_null_ptr(),
                    Layout::new::<[u8; 64]>(),
                    Layout::new::<[u8; 65]>(),
                )
                .expect_err("Could grow to 65 bytes in place");

            let memory = alloc
                .grow(
                    memory.as_non_null_ptr(),
                    Layout::new::<[u8; 64]>(),
                    Layout::new::<[u8; 65]>(),
                )
                .expect("Could not grow to 65 bytes");

            alloc.dealloc(memory.as_non_null_ptr(), Layout::new::<[u8; 65]>());
        }
    }

    #[test]
    fn shrink() {
        let alloc = Chunk::<_, 64>(tracker(Global));

        let memory = alloc
            .alloc(Layout::new::<[u8; 128]>())
            .expect("Could not allocate 128 bytes");
        assert_eq!(memory.len() % 64, 0);

        unsafe {
            let len = alloc
                .shrink_in_place(
                    memory.as_non_null_ptr(),
                    Layout::new::<[u8; 128]>(),
                    Layout::new::<[u8; 100]>(),
                )
                .expect("Could not shrink to 100 bytes");
            assert_eq!(len % 64, 0);
            assert!(len >= 128);

            let len = alloc
                .shrink_in_place(
                    memory.as_non_null_ptr(),
                    Layout::new::<[u8; 100]>(),
                    Layout::new::<[u8; 65]>(),
                )
                .expect("Could not shrink to 65 bytes");
            assert_eq!(len % 64, 0);
            assert!(len >= 128);

            alloc
                .shrink_in_place(
                    memory.as_non_null_ptr(),
                    Layout::new::<[u8; 65]>(),
                    Layout::new::<[u8; 64]>(),
                )
                .expect_err("Could shrink to 64 bytes in place");

            let memory = alloc
                .shrink(
                    memory.as_non_null_ptr(),
                    Layout::new::<[u8; 128]>(),
                    Layout::new::<[u8; 64]>(),
                )
                .expect("Could not shrink to 64 bytes");

            alloc.dealloc(memory.as_non_null_ptr(), Layout::new::<[u8; 64]>());
        }
    }
}