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
use crate::raw::{
capacity::{capacity, Round},
Storage, StorageWithCapacity,
};
use core::{
alloc::Layout,
mem::{align_of, size_of},
ptr::NonNull,
};
use std::{alloc::handle_alloc_error, mem::MaybeUninit};
use std::alloc::{Allocator, Global};
doc_heap! {
#[repr(C)]
#[cfg_attr(doc, doc(cfg(feature = "alloc")))]
pub struct Heap<T, A: Allocator = Global>(Box<[MaybeUninit<T>], A>);
}
unsafe impl<T, A: Allocator + Send> Send for Heap<T, A> {}
unsafe impl<T, A: Allocator + Sync> Sync for Heap<T, A> {}
enum OnFailure {
Abort,
Error,
}
impl<T> Heap<T> {
pub fn new() -> Self { Self(Box::new_uninit_slice(0)) }
pub unsafe fn from_raw_parts(ptr: NonNull<T>, capacity: usize) -> Self {
unsafe { Self::from_raw_parts_in(ptr, capacity, Global) }
}
pub fn into_raw_parts(self) -> (NonNull<T>, usize) {
let ptr = Box::into_raw(self.0);
unsafe {
let (ptr, capacity) = ptr.to_raw_parts();
(NonNull::new_unchecked(ptr.cast()), capacity)
}
}
}
#[cfg_attr(doc, doc(cfg(feature = "nightly")))]
impl<T, A: Allocator> Heap<T, A> {
pub fn with_alloc(allocator: A) -> Self { Self(Box::new_uninit_slice_in(0, allocator)) }
pub unsafe fn from_raw_parts_in(ptr: NonNull<T>, capacity: usize, allocator: A) -> Self {
unsafe {
let ptr = std::ptr::slice_from_raw_parts_mut(ptr.as_ptr().cast(), capacity);
Self(Box::from_raw_in(ptr, allocator))
}
}
pub fn into_raw_parts_with_alloc(self) -> (NonNull<T>, usize, A) {
let (ptr, alloc) = Box::into_raw_with_allocator(self.0);
unsafe {
let (ptr, capacity) = ptr.to_raw_parts();
(NonNull::new_unchecked(ptr.cast()), capacity, alloc)
}
}
}
impl<T, A: Allocator + Default> Default for Heap<T, A> {
fn default() -> Self { Self::with_alloc(Default::default()) }
}
unsafe impl<T, U, A: Allocator> Storage<U> for Heap<T, A> {
const IS_ALIGNED: bool = align_of::<T>() >= align_of::<U>();
fn capacity(&self) -> usize { capacity(self.0.len(), size_of::<T>(), size_of::<U>(), Round::Down) }
fn as_ptr(&self) -> *const U { (self.0.as_ptr() as *const T).cast() }
fn as_mut_ptr(&mut self) -> *mut U { (self.0.as_mut_ptr() as *mut T).cast() }
fn reserve(&mut self, new_capacity: usize) {
let new_capacity = capacity(new_capacity, size_of::<U>(), size_of::<T>(), Round::Up);
if self.0.len() < new_capacity {
let _ = self.reserve_slow(new_capacity, OnFailure::Abort);
}
}
fn try_reserve(&mut self, new_capacity: usize) -> bool {
let new_capacity = capacity(new_capacity, size_of::<U>(), size_of::<T>(), Round::Up);
if self.0.len() < new_capacity {
self.reserve_slow(new_capacity, OnFailure::Error)
} else {
true
}
}
}
impl<T, A: Default + Allocator> Heap<T, A> {
fn with_capacity(capacity: usize) -> Self { Self(Box::new_uninit_slice_in(capacity, A::default())) }
}
unsafe impl<T, U, A: Default + Allocator> StorageWithCapacity<U> for Heap<T, A> {
fn with_capacity(cap: usize) -> Self {
Self::with_capacity(capacity(cap, size_of::<U>(), size_of::<T>(), Round::Up))
}
}
impl<T, A: Allocator> Heap<T, A> {
#[cold]
#[inline(never)]
fn reserve_slow(&mut self, new_capacity: usize, on_failure: OnFailure) -> bool {
assert!(new_capacity > self.0.len());
let (ptr, cap, alloc) = unsafe { Self::into_raw_parts_with_alloc(std::ptr::read(self)) };
let new_capacity = new_capacity
.max(cap.checked_mul(2).expect("Could not grow further"))
.max(super::INIT_ALLOC_CAPACITY);
let layout = Layout::new::<T>().repeat(new_capacity).expect("Invalid layout").0;
let ptr = if cap == 0 {
unsafe { alloc.allocate(layout) }
} else {
let new_layout = layout;
let old_layout = Layout::new::<T>().repeat(cap).expect("Invalid layout").0;
unsafe { alloc.grow(ptr.cast(), old_layout, new_layout) }
};
let ptr = match (ptr, on_failure) {
(Ok(ptr), _) => ptr,
(Err(_), OnFailure::Abort) => handle_alloc_error(layout),
(Err(_), OnFailure::Error) => return false,
};
unsafe {
let new = Self::from_raw_parts_in(ptr.cast(), new_capacity, alloc);
let old = std::mem::replace(self, new);
std::mem::forget(old);
}
true
}
}