[][src]Macro context_allocator::global_thread_and_coroutine_switchable_allocator

macro_rules! global_thread_and_coroutine_switchable_allocator {
    ($mod_name: ident, $CoroutineLocalAllocator: ty, $ThreadLocalAllocator: ty, $GlobalAllocator: ty, $global_allocator_instance: expr) => { ... };
}

Creates a new global, switchable allocator inside a module $mod_name.

Parameters:-

  • $mod_name: A pub(crate) module.
  • $CoroutineLocalAllocator: the type of the coroutine local allocator. Must implement LocalAllocator.
  • $ThreadLocalAllocator: the type of the thread local allocator. Must implement LocalAllocator.
  • $GlobalAllocator: the type of the thread local allocator. Must implement Allocator; a common usage is GlobalAllocToAllocatorAdaptor<System>.
  • global_allocator_instance: a constant expression for instantiating the global allocator. A common usage is GlobalAllocToAllocatorAdaptor(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();

...