-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathalloc.rs
212 lines (184 loc) · 5.01 KB
/
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
use std::{
alloc::Layout,
cell::Cell,
mem::transmute,
ops::{Deref, DerefMut},
ptr::NonNull,
};
use allocator_api2::alloc::Global;
use bumpalo::Bump;
use crate::FastAlloc;
thread_local! {
static ALLOC: Cell<Option<&'static Allocator>> = const { Cell::new(None) };
}
/// The actual storage for [FastAlloc].
#[derive(Default)]
pub struct Allocator {
alloc: Bump,
}
impl Allocator {
/// Invokes `f` in a scope where the allocations are done in this allocator.
///
/// # Safety
///
/// [Allocator] must be dropped after dropping all [crate::boxed::Box] and
/// [crate::vec::Vec] created in the scope.
#[inline(always)]
pub fn scope<'a, F, R>(&'a self, f: F) -> R
where
F: FnOnce() -> R,
{
let s = unsafe {
// Safery: We are using a scoped API
transmute::<&'a Allocator, &'static Allocator>(self)
};
ALLOC.set(Some(s));
let ret = f();
ALLOC.set(None);
ret
}
}
impl Default for FastAlloc {
fn default() -> Self {
Self {
#[cfg(feature = "scoped")]
alloc: if let Some(v) = ALLOC.get() {
Some(v)
} else {
None
},
}
}
}
impl FastAlloc {
/// `true` is passed to `f` if the box is allocated with a custom allocator.
#[cfg(feature = "scoped")]
fn with_allocator<T>(
&self,
f: impl FnOnce(&dyn allocator_api2::alloc::Allocator, bool) -> T,
) -> T {
if let Some(arena) = &self.alloc {
return f(
(&&arena.alloc) as &dyn allocator_api2::alloc::Allocator,
true,
);
}
f(&allocator_api2::alloc::Global, false)
}
/// `true` is passed to `f` if the box is allocated with a custom allocator.
#[cfg(not(feature = "scoped"))]
#[inline(always)]
fn with_allocator<T>(&self, f: impl FnOnce(allocator_api2::alloc::Global, bool) -> T) -> T {
f(allocator_api2::alloc::Global, false)
}
}
fn mark_ptr_as_arena_mode(ptr: NonNull<[u8]>) -> NonNull<[u8]> {
ptr
}
unsafe impl allocator_api2::alloc::Allocator for FastAlloc {
#[inline]
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, allocator_api2::alloc::AllocError> {
self.with_allocator(|a, is_arena_mode| {
let ptr = a.allocate(layout)?;
if is_arena_mode {
Ok(mark_ptr_as_arena_mode(ptr))
} else {
Ok(ptr)
}
})
}
#[inline]
fn allocate_zeroed(
&self,
layout: Layout,
) -> Result<NonNull<[u8]>, allocator_api2::alloc::AllocError> {
self.with_allocator(|a, is_arena_mode| {
let ptr = a.allocate_zeroed(layout)?;
if is_arena_mode {
Ok(mark_ptr_as_arena_mode(ptr))
} else {
Ok(ptr)
}
})
}
#[inline]
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
#[cfg(feature = "scoped")]
if self.alloc.is_some() {
self.with_allocator(|alloc, _| alloc.deallocate(ptr, layout));
return;
}
Global.deallocate(ptr, layout)
}
#[inline]
unsafe fn grow(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, allocator_api2::alloc::AllocError> {
self.with_allocator(|alloc, is_arena_mode| {
let ptr = alloc.grow(ptr, old_layout, new_layout)?;
if is_arena_mode {
Ok(mark_ptr_as_arena_mode(ptr))
} else {
Ok(ptr)
}
})
}
#[inline]
unsafe fn grow_zeroed(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, allocator_api2::alloc::AllocError> {
self.with_allocator(|alloc, is_arena_mode| {
let ptr = alloc.grow_zeroed(ptr, old_layout, new_layout)?;
if is_arena_mode {
Ok(mark_ptr_as_arena_mode(ptr))
} else {
Ok(ptr)
}
})
}
#[inline]
unsafe fn shrink(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, allocator_api2::alloc::AllocError> {
self.with_allocator(|alloc, is_arena_mode| {
let ptr = alloc.shrink(ptr, old_layout, new_layout)?;
if is_arena_mode {
Ok(mark_ptr_as_arena_mode(ptr))
} else {
Ok(ptr)
}
})
}
#[inline(always)]
fn by_ref(&self) -> &Self
where
Self: Sized,
{
self
}
}
impl From<Bump> for Allocator {
fn from(alloc: Bump) -> Self {
Self { alloc }
}
}
impl Deref for Allocator {
type Target = Bump;
fn deref(&self) -> &Bump {
&self.alloc
}
}
impl DerefMut for Allocator {
fn deref_mut(&mut self) -> &mut Bump {
&mut self.alloc
}
}