[−][src]Macro context_allocator::global_thread_and_coroutine_switchable_allocator
Creates a new global, switchable allocator inside a module $mod_name
.
Parameters:-
$mod_name
: Apub(crate)
module.$CoroutineLocalAllocator
: the type of the coroutine local allocator. Must implementLocalAllocator
.$ThreadLocalAllocator
: the type of the thread local allocator. Must implementLocalAllocator
.$GlobalAllocator
: the type of the thread local allocator. Must implementAllocator
; a common usage isGlobalAllocToAllocatorAdaptor<System>
.global_allocator_instance
: a constant expression for instantiating the global allocator. A common usage isGlobalAllocToAllocatorAdaptor(System)
.
To access the switchable allocator, call $mod_name::global_thread_and_coroutine_switchable_allocator()
; this returns an object reference that implements the trait GlobalThreadAndCoroutineSwitchableAllocator
.
Done using a macro due to a limitation when combining thread-local statics with generics (which could be solved using pthread keys, but these aren't always the most efficient of approaches); in essence, a thread-local struct field is needed.
Example
global_thread_and_coroutine_switchable_allocator!(MyGlobalAllocator, BumpAllocator<ArenaMemorySource<MemoryMapSource>>, MultipleBinarySearchTreeAllocator<MemoryMapSource>, GlobalAllocToAllocatorAdaptor<System>, GlobalAllocToAllocatorAdaptor(System));
...
let global = MyGlobalAllocator::global_thread_and_coroutine_switchable_allocator();
...