Skip to content

Implement GlobalAlloc for ESP32? #4

@stevefan1999-personal

Description

@stevefan1999-personal

Currently I put TLSF in as a global static mut with a static memory provider from EspHeap:

#[global_allocator]
static ALLOCATOR: EspHeap = EspHeap::empty();

unsafe fn init_heap() {
    const HEAP_SIZE: usize = 32 * 1024;

    extern "C" {
        static mut _heap_start: u32;
    }

    unsafe {
        let heap_start = &_heap_start as *const _ as usize;
        ALLOCATOR.init(heap_start as *mut u8, HEAP_SIZE);
    }

    let mut TLSF: FlexTlsf<GlobalAllocAsFlexSource<EspHeap, 1024>, u16, u16, 12, 16> = FlexTlsf::new(GlobalAllocAsFlexSource(ALLOCATOR));

    unsafe {
        let mut ptr1 = TLSF.allocate(Layout::new::<u64>()).unwrap().cast::<u64>();
        let mut ptr2 = TLSF.allocate(Layout::new::<u64>()).unwrap().cast::<u64>();
        *ptr1.as_mut() = 42;
        *ptr2.as_mut() = 56;
        assert_eq!(*ptr1.as_ref(), 42);
        assert_eq!(*ptr2.as_ref(), 56);
        TLSF.deallocate(ptr1.cast(), Layout::new::<u64>().align());
        TLSF.deallocate(ptr2.cast(), Layout::new::<u64>().align());
    }

}

It seems like EspHeap does not offer a default method either so I can't take the heap from system (so EspHeap is layered upon). I may need to directly implement a flex source. What are the pointers for me to implement a FlexSource and eventually a global allocator?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions