What in my code runs alloc?
I'm having problems using some custom allocators, so I decided to write a basic allocator. The goal is to see if anything allocates:
#[global_allocator]
static ALLOCATOR: Allocator = Allocator::new();
fn main() {
}
struct Allocator{
}
impl Allocator{
const fn new()->Allocator{
Allocator{}
}
}
unsafe impl core::alloc::GlobalAlloc for Allocator{
unsafe fn alloc(&self, layout: core::alloc::Layout) -> *mut u8 {
panic!("alloc");
}
unsafe fn dealloc(&self, _ptr: *mut u8, layout: core::alloc::Layout) {
}
}
Running this, I get thread panicked while processing panic. aborting
I get that panic can allocate, but I'm not allocating any new memory here! main is an empty function.
The issue that brought this up is that some allocators need initialization (which is non-const) before use, so I put the initialization in main, which breaks the code. Where should I initialize the allocator?
Example running in the playground.
This is an example of alloc returning a null pointer, crashing the program (memory allocation of 5 bytes failed). Where do these 5 bytes come from?
But with a valid backing pointer, it all works out.
Is this because of argc/argv, and if so, is there a way to run an initializer before argc or argv parsing?