Skip to content

[Question] Clarification on heap thread-safety and arena usage for console port #1213

@emrahsungu

Description

@emrahsungu

First of all thank you for such a great piece of library!

I'm porting our game engine's memory allocator to use mimalloc on console platforms (PS5/Xbox/Switch). We pre-allocate a large virtual address region (e.g., 2GB) and want multiple threads to allocate from it and disallow extra allocation from OS
My understanding is:

  1. mi_heap_t instances are thread-local - a heap created on thread A should only be used by thread A for allocations
  2. Arenas are thread-safe - multiple threads can each create their own mi_heap_t within the same arena (created via mi_reserve_os_memory_ex) and allocate/free concurrently without external locking
  3. mi_free() is thread-safe - any thread can free memory allocated by any other thread, regardless of which heap allocated it

Questions:

  1. Is my understanding correct?
  2. When a thread exits, do I need to explicitly call mi_heap_destroy() on its thread-local heap, or does mimalloc handle cleanup automatically?
  3. If manual cleanup is needed, what happens to allocations from that heap if they're freed after the heap is destroyed?
int arena_id = mi_reserve_os_memory_ex(2GB, true, true, false);

// Thread A
mi_heap_t* heap_a = mi_heap_new_in_arena(arena_id);
void* ptr_a = mi_heap_malloc(heap_a, 100);

// Thread B  
mi_heap_t* heap_b = mi_heap_new_in_arena(arena_id);
void* ptr_b = mi_heap_malloc(heap_b, 100);

// Any thread
mi_free(ptr_a);  // Safe?
mi_free(ptr_b);  // Safe?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions