microsoft / microsoft/mimalloc
[Question] Clarification on heap thread-safety and arena usage for console port
- Dominant language
- C
- Stars
- 13.4k
- Forks
- 1.2k
- Avg merge
- 4d 45m
- Merged PRs (30d)
- 13
Description
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?
```cpp
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?
Contributor guide
No contributing guide indexed for this repository
Research direction
Review the documented behavior and relevant source for mi_reserve_os_memory_ex, mi_heap_new_in_arena, mi_heap_destroy, and mi_free. Provide definitive answers to the three numbered questions, including thread-exit cleanup and freeing allocations after heap destruction.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- c
- Domain
- operating-systems
- Issue type
- Documentation
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100