aligned_vec/
raw.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
use crate::{Alignment, TryReserveError};
use alloc::alloc::{alloc, dealloc, handle_alloc_error, realloc, Layout};
use core::{
    marker::PhantomData,
    mem::{align_of, size_of},
    ptr::{null_mut, NonNull},
};

pub struct ARawVec<T, A: Alignment> {
    pub ptr: NonNull<T>,
    pub capacity: usize,
    pub align: A,
    _marker: PhantomData<T>,
}

impl<T, A: Alignment> Drop for ARawVec<T, A> {
    #[inline]
    fn drop(&mut self) {
        // this can't overflow since we already have this much stored in a slice
        let size_bytes = self.capacity * size_of::<T>();
        if size_bytes > 0 {
            // SAFETY: memory was allocated with alloc::alloc::alloc
            unsafe {
                dealloc(
                    self.ptr.as_ptr() as *mut u8,
                    Layout::from_size_align_unchecked(
                        size_bytes,
                        self.align.alignment(align_of::<T>()),
                    ),
                )
            }
        }
    }
}

pub fn capacity_overflow() -> ! {
    panic!("capacity overflow")
}

impl<T, A: Alignment> ARawVec<T, A> {
    /// # Safety
    ///
    /// `align` must be a power of two.  
    /// `align` must be greater than or equal to `core::mem::align_of::<T>()`.
    #[inline]
    pub unsafe fn new_unchecked(align: usize) -> Self {
        let cap = if size_of::<T>() == 0 { usize::MAX } else { 0 };
        Self::from_raw_parts(null_mut::<u8>().wrapping_add(align) as *mut T, cap, align)
    }

    /// # Safety
    ///
    /// `align` must be a power of two.  
    /// `align` must be greater than or equal to `core::mem::align_of::<T>()`.
    #[inline]
    pub unsafe fn with_capacity_unchecked(capacity: usize, align: usize) -> Self {
        if capacity == 0 || size_of::<T>() == 0 {
            Self::new_unchecked(align)
        } else {
            Self {
                ptr: NonNull::<T>::new_unchecked(with_capacity_unchecked(
                    capacity,
                    align,
                    size_of::<T>(),
                ) as *mut T),
                capacity,
                align: A::new(align, align_of::<T>()),
                _marker: PhantomData,
            }
        }
    }

    /// # Safety
    ///
    /// `align` must be a power of two.  
    /// `align` must be greater than or equal to `core::mem::align_of::<T>()`.
    #[inline]
    pub unsafe fn try_with_capacity_unchecked(
        capacity: usize,
        align: usize,
    ) -> Result<Self, TryReserveError> {
        if capacity == 0 || size_of::<T>() == 0 {
            Ok(Self::new_unchecked(align))
        } else {
            Ok(Self {
                ptr: NonNull::<T>::new_unchecked(try_with_capacity_unchecked(
                    capacity,
                    align,
                    size_of::<T>(),
                )? as *mut T),
                capacity,
                align: A::new(align, align_of::<T>()),
                _marker: PhantomData,
            })
        }
    }

    const MIN_NON_ZERO_CAP: usize = if size_of::<T>() == 1 {
        8
    } else if size_of::<T>() <= 1024 {
        4
    } else {
        1
    };

    pub unsafe fn grow_amortized(&mut self, len: usize, additional: usize) {
        debug_assert!(additional > 0);
        if self.capacity == 0 {
            *self = Self::with_capacity_unchecked(
                additional.max(Self::MIN_NON_ZERO_CAP),
                self.align.alignment(align_of::<T>()),
            );
            return;
        }

        if size_of::<T>() == 0 {
            debug_assert_eq!(self.capacity, usize::MAX);
            capacity_overflow();
        }

        let new_cap = match len.checked_add(additional) {
            Some(cap) => cap,
            None => capacity_overflow(),
        };

        // self.cap * 2 can't overflow because it's less than isize::MAX
        let new_cap = new_cap.max(self.capacity * 2);
        let new_cap = new_cap.max(Self::MIN_NON_ZERO_CAP);

        let ptr = {
            grow_unchecked(
                self.as_mut_ptr() as *mut u8,
                self.capacity,
                new_cap,
                self.align.alignment(align_of::<T>()),
                size_of::<T>(),
            ) as *mut T
        };

        self.capacity = new_cap;
        self.ptr = NonNull::<T>::new_unchecked(ptr);
    }

    pub unsafe fn grow_exact(&mut self, len: usize, additional: usize) {
        debug_assert!(additional > 0);
        if size_of::<T>() == 0 {
            debug_assert_eq!(self.capacity, usize::MAX);
            capacity_overflow();
        }

        if self.capacity == 0 {
            *self =
                Self::with_capacity_unchecked(additional, self.align.alignment(align_of::<T>()));
            return;
        }

        let new_cap = match len.checked_add(additional) {
            Some(cap) => cap,
            None => capacity_overflow(),
        };

        let ptr = grow_unchecked(
            self.as_mut_ptr() as *mut u8,
            self.capacity,
            new_cap,
            self.align.alignment(align_of::<T>()),
            size_of::<T>(),
        ) as *mut T;

        self.capacity = new_cap;
        self.ptr = NonNull::<T>::new_unchecked(ptr);
    }

    pub unsafe fn try_grow_amortized(
        &mut self,
        len: usize,
        additional: usize,
    ) -> Result<(), TryReserveError> {
        debug_assert!(additional > 0);
        if self.capacity == 0 {
            *self = Self::try_with_capacity_unchecked(
                additional.max(Self::MIN_NON_ZERO_CAP),
                self.align.alignment(align_of::<T>()),
            )?;
            return Ok(());
        }

        if size_of::<T>() == 0 {
            debug_assert_eq!(self.capacity, usize::MAX);
            return Err(TryReserveError::CapacityOverflow);
        }

        let new_cap = match len.checked_add(additional) {
            Some(cap) => cap,
            None => return Err(TryReserveError::CapacityOverflow),
        };

        // self.cap * 2 can't overflow because it's less than isize::MAX
        let new_cap = new_cap.max(self.capacity * 2);
        let new_cap = new_cap.max(Self::MIN_NON_ZERO_CAP);

        let ptr = {
            try_grow_unchecked(
                self.as_mut_ptr() as *mut u8,
                self.capacity,
                new_cap,
                self.align.alignment(align_of::<T>()),
                size_of::<T>(),
            )? as *mut T
        };

        self.capacity = new_cap;
        self.ptr = NonNull::<T>::new_unchecked(ptr);
        Ok(())
    }

    pub unsafe fn try_grow_exact(
        &mut self,
        len: usize,
        additional: usize,
    ) -> Result<(), TryReserveError> {
        debug_assert!(additional > 0);
        if size_of::<T>() == 0 {
            debug_assert_eq!(self.capacity, usize::MAX);
            return Err(TryReserveError::CapacityOverflow);
        }

        if self.capacity == 0 {
            *self = Self::try_with_capacity_unchecked(
                additional,
                self.align.alignment(align_of::<T>()),
            )?;
            return Ok(());
        }

        let new_cap = match len.checked_add(additional) {
            Some(cap) => cap,
            None => return Err(TryReserveError::CapacityOverflow),
        };

        let ptr = try_grow_unchecked(
            self.as_mut_ptr() as *mut u8,
            self.capacity,
            new_cap,
            self.align.alignment(align_of::<T>()),
            size_of::<T>(),
        )? as *mut T;

        self.capacity = new_cap;
        self.ptr = NonNull::<T>::new_unchecked(ptr);
        Ok(())
    }

    pub unsafe fn shrink_to(&mut self, len: usize) {
        if size_of::<T>() == 0 {
            return;
        }

        debug_assert!(len < self.capacity());
        let size_of = size_of::<T>();
        let old_capacity = self.capacity;
        let align = self.align;
        let old_ptr = self.ptr.as_ptr() as *mut u8;

        // this cannot overflow or exceed isize::MAX bytes since len < cap and the same was true
        // for cap
        let new_size_bytes = len * size_of;
        let old_size_bytes = old_capacity * size_of;
        let old_layout =
            Layout::from_size_align_unchecked(old_size_bytes, align.alignment(align_of::<T>()));

        let ptr = realloc(old_ptr, old_layout, new_size_bytes);
        let ptr = ptr as *mut T;
        self.capacity = len;
        self.ptr = NonNull::<T>::new_unchecked(ptr);
    }

    #[inline]
    pub unsafe fn from_raw_parts(ptr: *mut T, capacity: usize, align: usize) -> Self {
        Self {
            ptr: NonNull::<T>::new_unchecked(ptr),
            capacity,
            align: A::new(align, align_of::<T>()),
            _marker: PhantomData,
        }
    }

    /// Returns the capacity of the vector.
    #[inline]
    pub fn capacity(&self) -> usize {
        self.capacity
    }

    #[inline]
    pub fn align(&self) -> usize {
        self.align.alignment(align_of::<T>())
    }

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

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

pub unsafe fn with_capacity_unchecked(capacity: usize, align: usize, size_of: usize) -> *mut u8 {
    let size_bytes = match capacity.checked_mul(size_of) {
        Some(size_bytes) => size_bytes,
        None => capacity_overflow(),
    };
    debug_assert!(size_bytes > 0);
    let will_overflow = size_bytes > usize::MAX - (align - 1);
    if will_overflow || !is_valid_alloc(size_bytes) {
        capacity_overflow();
    }

    let layout = Layout::from_size_align_unchecked(size_bytes, align);
    let ptr = alloc(layout);
    if ptr.is_null() {
        handle_alloc_error(layout);
    }
    ptr
}

unsafe fn grow_unchecked(
    old_ptr: *mut u8,
    old_capacity: usize,
    new_capacity: usize,
    align: usize,
    size_of: usize,
) -> *mut u8 {
    let new_size_bytes = match new_capacity.checked_mul(size_of) {
        Some(size_bytes) => size_bytes,
        None => capacity_overflow(),
    };
    let will_overflow = new_size_bytes > usize::MAX - (align - 1);
    if will_overflow || !is_valid_alloc(new_size_bytes) {
        capacity_overflow();
    }

    // can't overflow because we already allocated this much
    let old_size_bytes = old_capacity * size_of;
    let old_layout = Layout::from_size_align_unchecked(old_size_bytes, align);

    let ptr = realloc(old_ptr, old_layout, new_size_bytes);

    if ptr.is_null() {
        let new_layout = Layout::from_size_align_unchecked(old_size_bytes, align);
        handle_alloc_error(new_layout);
    }

    ptr
}

pub unsafe fn try_with_capacity_unchecked(
    capacity: usize,
    align: usize,
    size_of: usize,
) -> Result<*mut u8, TryReserveError> {
    let size_bytes = match capacity.checked_mul(size_of) {
        Some(size_bytes) => size_bytes,
        None => return Err(TryReserveError::CapacityOverflow),
    };
    debug_assert!(size_bytes > 0);
    let will_overflow = size_bytes > usize::MAX - (align - 1);
    if will_overflow || !is_valid_alloc(size_bytes) {
        return Err(TryReserveError::CapacityOverflow);
    }

    let layout = Layout::from_size_align_unchecked(size_bytes, align);
    let ptr = alloc(layout);
    if ptr.is_null() {
        return Err(TryReserveError::AllocError { layout });
    }
    Ok(ptr)
}

unsafe fn try_grow_unchecked(
    old_ptr: *mut u8,
    old_capacity: usize,
    new_capacity: usize,
    align: usize,
    size_of: usize,
) -> Result<*mut u8, TryReserveError> {
    let new_size_bytes = match new_capacity.checked_mul(size_of) {
        Some(size_bytes) => size_bytes,
        None => return Err(TryReserveError::CapacityOverflow),
    };
    let will_overflow = new_size_bytes > usize::MAX - (align - 1);
    if will_overflow || !is_valid_alloc(new_size_bytes) {
        return Err(TryReserveError::CapacityOverflow);
    }

    // can't overflow because we already allocated this much
    let old_size_bytes = old_capacity * size_of;
    let old_layout = Layout::from_size_align_unchecked(old_size_bytes, align);

    let ptr = realloc(old_ptr, old_layout, new_size_bytes);

    if ptr.is_null() {
        let layout = Layout::from_size_align_unchecked(new_size_bytes, align);
        return Err(TryReserveError::AllocError { layout });
    }

    Ok(ptr)
}

#[inline]
fn is_valid_alloc(alloc_size: usize) -> bool {
    !(usize::BITS < 64 && alloc_size > isize::MAX as usize)
}