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
use core::{
alloc::{GlobalAlloc, Layout},
cell::UnsafeCell,
ptr::{null_mut, NonNull},
};
#[cfg(debug_assertions)]
use core::sync::atomic::{AtomicU64, Ordering};
use allocator_api2::alloc::{AllocError, Allocator};
use crate::{cold, sync::SyncBlinkAlloc, LocalBlinkAlloc};
struct State<A: Allocator> {
blink: SyncBlinkAlloc<A>,
enabled: bool,
}
impl<A: Allocator> State<A> {
#[inline(always)]
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
match self.enabled {
true => self.blink.allocate(layout),
false => {
cold();
self.blink.inner().allocate(layout)
}
}
}
#[inline(always)]
fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
match self.enabled {
true => self.blink.allocate_zeroed(layout),
false => {
cold();
self.blink.inner().allocate_zeroed(layout)
}
}
}
#[inline(always)]
unsafe fn resize(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> {
match self.enabled {
true => self.blink.resize(ptr, old_layout, new_layout),
false => {
cold();
if old_layout.size() >= new_layout.size() {
self.blink.inner().grow(ptr, old_layout, new_layout)
} else {
self.blink.inner().shrink(ptr, old_layout, new_layout)
}
}
}
}
#[inline(always)]
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
match self.enabled {
true => self.blink.deallocate(ptr, layout.size()),
false => {
cold();
self.blink.inner().deallocate(ptr, layout)
}
}
}
}
switch_std_default! {
/// [`GlobalAlloc`] implementation based on [`SyncBlinkAlloc`].
///
/// # Example
///
/// ```
/// use blink_alloc::GlobalBlinkAlloc;
///
/// #[global_allocator]
/// static GLOBAL_ALLOC: GlobalBlinkAlloc = GlobalBlinkAlloc::new();
///
/// fn main() {
/// let _ = Box::new(42);
/// let _ = vec![1, 2, 3];
/// }
/// ```
pub struct GlobalBlinkAlloc<A: Allocator = +std::alloc::System> {
state: UnsafeCell<State<A>>,
#[cfg(debug_assertions)]
allocations: AtomicU64,
}
}
unsafe impl<A: Allocator + Send> Send for GlobalBlinkAlloc<A> {}
unsafe impl<A: Allocator + Sync> Sync for GlobalBlinkAlloc<A> {}
#[cfg(feature = "std")]
impl GlobalBlinkAlloc<std::alloc::System> {
/// Create a new [`GlobalBlinkAlloc`].
///
/// Const function can be used to initialize a static variable.
///
/// # Example
///
/// ```
/// use blink_alloc::GlobalBlinkAlloc;
///
/// #[global_allocator]
/// static GLOBAL_ALLOC: GlobalBlinkAlloc = GlobalBlinkAlloc::new();
///
/// fn main() {
/// let _ = Box::new(42);
/// let _ = vec![1, 2, 3];
/// }
/// ```
pub const fn new() -> Self {
GlobalBlinkAlloc::new_in(std::alloc::System)
}
/// Create a new [`GlobalBlinkAlloc`].
///
/// This method allows to specify initial chunk size.
///
/// Const function can be used to initialize a static variable.
///
/// # Example
///
/// ```
/// use blink_alloc::GlobalBlinkAlloc;
///
/// #[global_allocator]
/// static GLOBAL_ALLOC: GlobalBlinkAlloc = GlobalBlinkAlloc::new();
///
/// fn main() {
/// let _ = Box::new(42);
/// let _ = vec![1, 2, 3];
/// }
/// ```
pub const fn with_chunk_size(chunk_size: usize) -> Self {
GlobalBlinkAlloc::with_chunk_size_in(chunk_size, std::alloc::System)
}
}
impl<A> GlobalBlinkAlloc<A>
where
A: Allocator,
{
/// Create a new [`GlobalBlinkAlloc`]
/// with specified underlying allocator.
///
/// Const function can be used to initialize a static variable.
///
/// # Example
///
/// ```
/// use blink_alloc::GlobalBlinkAlloc;
///
/// #[global_allocator]
/// static GLOBAL_ALLOC: GlobalBlinkAlloc<std::alloc::System> = GlobalBlinkAlloc::new_in(std::alloc::System);
///
/// fn main() {
/// let _ = Box::new(42);
/// let _ = vec![1, 2, 3];
/// }
/// ```
pub const fn new_in(allocator: A) -> Self {
GlobalBlinkAlloc {
state: UnsafeCell::new(State {
blink: SyncBlinkAlloc::new_in(allocator),
enabled: false,
}),
#[cfg(debug_assertions)]
allocations: AtomicU64::new(0),
}
}
/// Create a new [`GlobalBlinkAlloc`]
/// with specified underlying allocator.
///
/// This method allows to specify initial chunk size.
///
/// Const function can be used to initialize a static variable.
///
/// # Example
///
/// ```
/// use blink_alloc::GlobalBlinkAlloc;
///
/// #[global_allocator]
/// static GLOBAL_ALLOC: GlobalBlinkAlloc<std::alloc::System> = GlobalBlinkAlloc::new_in(std::alloc::System);
///
/// fn main() {
/// let _ = Box::new(42);
/// let _ = vec![1, 2, 3];
/// }
/// ```
pub const fn with_chunk_size_in(chunk_size: usize, allocator: A) -> Self {
GlobalBlinkAlloc {
state: UnsafeCell::new(State {
blink: SyncBlinkAlloc::with_chunk_size_in(chunk_size, allocator),
enabled: false,
}),
#[cfg(debug_assertions)]
allocations: AtomicU64::new(0),
}
}
/// Resets this allocator, deallocating all chunks except the last one.
/// Last chunk will be reused.
/// With steady memory usage after few iterations
/// one chunk should be sufficient for all allocations between resets.
///
/// # Safety
///
/// Memory allocated from this allocator in blink mode becomes invalidated.
/// The user is responsible to ensure that previously allocated memory
/// won't be used after reset.
///
/// # Example
///
/// ```
/// # #[cfg(feature = "std")] fn main() {
/// use blink_alloc::UnsafeGlobalBlinkAlloc;
///
/// #[global_allocator]
/// static GLOBAL_ALLOC: UnsafeGlobalBlinkAlloc = unsafe { UnsafeGlobalBlinkAlloc::new() };
///
/// unsafe { GLOBAL_ALLOC.blink_mode() };
///
/// let b = Box::new(42);
/// let v = vec![1, 2, 3];
/// drop(b);
/// drop(v);
///
/// // Safety: memory allocated in blink mode won't be used after reset.
/// unsafe {
/// GLOBAL_ALLOC.reset();
/// GLOBAL_ALLOC.direct_mode();
/// };
/// # }
/// # #[cfg(not(feature = "std"))] fn main() {}
/// ```
#[inline(always)]
pub unsafe fn reset(&self) {
#[cfg(debug_assertions)]
{
assert_eq!(
self.allocations.load(Ordering::SeqCst),
0,
"Not everything was deallocated"
);
}
(*self.state.get()).blink.reset_unchecked();
}
/// Switches allocator to blink mode.
/// All allocations will be served by blink-allocator.
///
/// The type is created in direct mode.
/// When used as global allocator, user may manually switch into blink mode
/// in `main` or at any point later.
///
/// However user must switch back to direct mode before returning from `main`.
///
/// # Safety
///
/// Must be externally synchronized with other threads accessing this allocator.
/// Memory allocated in direct mode must not be deallocated while in blink mode.
#[inline(always)]
pub unsafe fn blink_mode(&self) {
(*self.state.get()).enabled = true;
}
/// Switches allocator to blink mode.
/// All allocations will be served by underlying allocator.
///
/// The type is created in direct mode.
/// When used as global allocator, user may manually switch into blink mode
/// in `main` or at any point later.
///
/// However user must switch back to direct mode before returning from `main`.
///
/// # Safety
///
/// Must be externally synchronized with other threads accessing this allocator.
/// Memory allocated in blink mode must not be deallocated while in direct mode.
#[inline(always)]
pub unsafe fn direct_mode(&self) {
self.reset();
(*self.state.get()).enabled = false;
}
/// Creates a new thread-local blink allocator proxy
/// that borrows from this multi-threaded allocator.
///
/// The local proxy allocator works faster and
/// allows more consistent memory reuse.
/// It can be recreated without resetting the multi-threaded allocator,
/// allowing [`SyncBlinkAlloc`] to be warm-up and serve all allocations
/// from a single chunk without ever blocking.
///
/// Best works for fork-join style of parallelism.
/// Create a local allocator for each thread/task.
/// Reset after all threads/tasks are finished.
///
/// # Examples
///
/// ```
/// # #![cfg_attr(feature = "nightly", feature(allocator_api))]
/// # use blink_alloc::GlobalBlinkAlloc;
/// # use allocator_api2::vec::Vec;
/// # fn main() {
/// static BLINK: GlobalBlinkAlloc = GlobalBlinkAlloc::new();
///
/// for _ in 0..3 {
/// for i in 0..16 {
/// let mut blink = BLINK.local(); // Sendable and 'static.
/// std::thread::scope(move |_| {
/// let mut vec = Vec::new_in(&blink);
/// vec.push(i);
/// for j in i*2..i*30 {
/// vec.push(j); // Proxy will allocate enough memory to grow vec without reallocating on 2nd iteration and later.
/// }
/// drop(vec); // Without this line it will fail to borrow mutable on next line.
/// blink.reset();
/// });
///
/// // Safety: Proxies and allocations are dropped.
/// unsafe { BLINK.reset() };
/// }
/// }
/// # }
/// ```
pub fn local(&self) -> LocalBlinkAlloc<'_, A> {
unsafe { (*self.state.get()).blink.local() }
}
}
unsafe impl<A> GlobalAlloc for GlobalBlinkAlloc<A>
where
A: Allocator,
{
#[inline]
unsafe fn alloc(&self, layout: core::alloc::Layout) -> *mut u8 {
match (*self.state.get()).allocate(layout) {
Ok(ptr) => {
#[cfg(debug_assertions)]
if (*self.state.get()).enabled {
self.allocations.fetch_add(1, Ordering::SeqCst);
}
ptr.as_ptr().cast()
}
Err(_) => null_mut(),
}
}
#[inline]
unsafe fn dealloc(&self, ptr: *mut u8, layout: core::alloc::Layout) {
let ptr = NonNull::new_unchecked(ptr);
(*self.state.get()).deallocate(ptr, layout);
#[cfg(debug_assertions)]
{
if (*self.state.get()).enabled {
let _ = self.allocations.fetch_sub(1, Ordering::SeqCst);
}
}
}
#[inline]
unsafe fn alloc_zeroed(&self, layout: core::alloc::Layout) -> *mut u8 {
match (*self.state.get()).allocate_zeroed(layout) {
Ok(ptr) => {
#[cfg(debug_assertions)]
if (*self.state.get()).enabled {
self.allocations.fetch_add(1, Ordering::SeqCst);
}
ptr.as_ptr().cast()
}
Err(_) => null_mut(),
}
}
#[inline]
unsafe fn realloc(
&self,
ptr: *mut u8,
layout: core::alloc::Layout,
new_size: usize,
) -> *mut u8 {
let Ok(new_layout) = Layout::from_size_align(new_size, layout.align()) else {
return null_mut();
};
let result = match NonNull::new(ptr) {
None => (*self.state.get()).allocate(new_layout),
Some(ptr) => (*self.state.get()).resize(ptr, layout, new_layout),
};
match result {
Ok(ptr) => ptr.as_ptr().cast(),
Err(_) => null_mut(),
}
}
}