pub struct UnsafeGlobalBlinkAlloc<A: Allocator = System> { /* private fields */ }
Expand description
GlobalAlloc
implementation based on BlinkAlloc
.
Implementations§
Source§impl UnsafeGlobalBlinkAlloc<System>
impl UnsafeGlobalBlinkAlloc<System>
Sourcepub const unsafe fn new() -> Self
pub const unsafe fn new() -> Self
Create a new UnsafeGlobalBlinkAlloc
.
Const function can be used to initialize a static variable.
§Safety
This method is unsafe because this type is not thread-safe
but implements Sync
.
Allocator returned by this method must not be used concurrently.
For safer alternative see GlobalBlinkAlloc
.
§Example
use blink_alloc::UnsafeGlobalBlinkAlloc;
// Safety: This program is single-threaded.
#[global_allocator]
static GLOBAL_ALLOC: UnsafeGlobalBlinkAlloc = unsafe { UnsafeGlobalBlinkAlloc::new() };
let _ = Box::new(42);
let _ = vec![1, 2, 3];
Sourcepub const unsafe fn with_chunk_size(chunk_size: usize) -> Self
pub const unsafe fn with_chunk_size(chunk_size: usize) -> Self
Create a new UnsafeGlobalBlinkAlloc
.
This method allows to specify initial chunk size.
Const function can be used to initialize a static variable.
§Safety
This method is unsafe because this type is not thread-safe
but implements Sync
.
Allocator returned by this method must not be used concurrently.
For safer alternative see GlobalBlinkAlloc
.
§Example
use blink_alloc::UnsafeGlobalBlinkAlloc;
// Safety: This program is single-threaded.
#[global_allocator]
static GLOBAL_ALLOC: UnsafeGlobalBlinkAlloc<std::alloc::System> = unsafe { UnsafeGlobalBlinkAlloc::new_in(std::alloc::System) };
let _ = Box::new(42);
let _ = vec![1, 2, 3];
Source§impl<A> UnsafeGlobalBlinkAlloc<A>where
A: Allocator,
impl<A> UnsafeGlobalBlinkAlloc<A>where
A: Allocator,
Sourcepub const unsafe fn new_in(allocator: A) -> Self
pub const unsafe fn new_in(allocator: A) -> Self
Create a new UnsafeGlobalBlinkAlloc
with specified underlying allocator.
Const function can be used to initialize a static variable.
§Safety
This method is unsafe because this type is not thread-safe
but implements Sync
.
Allocator returned by this method must not be used concurrently.
For safer alternative see GlobalBlinkAlloc
.
§Example
use blink_alloc::UnsafeGlobalBlinkAlloc;
// Safety: This program is single-threaded.
#[global_allocator]
static GLOBAL_ALLOC: UnsafeGlobalBlinkAlloc<std::alloc::System> = unsafe { UnsafeGlobalBlinkAlloc::new_in(std::alloc::System) };
let _ = Box::new(42);
let _ = vec![1, 2, 3];
Sourcepub const unsafe fn with_chunk_size_in(chunk_size: usize, allocator: A) -> Self
pub const unsafe fn with_chunk_size_in(chunk_size: usize, allocator: A) -> Self
Create a new UnsafeGlobalBlinkAlloc
with specified underlying allocator.
This method allows to specify initial chunk size.
Const function can be used to initialize a static variable.
§Safety
This method is unsafe because this type is not thread-safe
but implements Sync
.
Allocator returned by this method must not be used concurrently.
For safer alternative see GlobalBlinkAlloc
.
§Example
use blink_alloc::UnsafeGlobalBlinkAlloc;
// Safety: This program is single-threaded.
#[global_allocator]
static GLOBAL_ALLOC: UnsafeGlobalBlinkAlloc<std::alloc::System> = unsafe { UnsafeGlobalBlinkAlloc::new_in(std::alloc::System) };
let _ = Box::new(42);
let _ = vec![1, 2, 3];
Sourcepub unsafe fn reset(&self)
pub unsafe fn reset(&self)
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
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();
};
Sourcepub unsafe fn blink_mode(&self)
pub unsafe fn blink_mode(&self)
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.
Sourcepub unsafe fn direct_mode(&self)
pub unsafe fn direct_mode(&self)
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.
Trait Implementations§
Source§impl<A> GlobalAlloc for UnsafeGlobalBlinkAlloc<A>where
A: Allocator,
impl<A> GlobalAlloc for UnsafeGlobalBlinkAlloc<A>where
A: Allocator,
Source§unsafe fn alloc(&self, layout: Layout) -> *mut u8
unsafe fn alloc(&self, layout: Layout) -> *mut u8
layout
. Read more