microsoft / mimalloc

mimalloc is a compact general purpose allocator with excellent performance.
MIT License
10.57k stars 862 forks source link

[Ask for help] Use mimalloc Heap for GC? #298

Open playXE opened 4 years ago

playXE commented 4 years ago

I want to do mostly precise GC and for scanning roots I need a way to find heap pointer, do I understand correctly that to identify allocated heap pointer I should use mi_heap_check_owned and mi_heap_contains_block like this?


void cons_root(gc_heap* gc ,void**start,void**end) {
    for (;start < end;start++) {
          if (mi_heap_check_owned(gc->mi_heap,*start) && mi_heap_contains_block(gc->mi_heap,*start) { push_gray(gc,*start); } 
    }
}
daanx commented 4 years ago

Hi @playXE ; fun project :-) Scanning roots should be fast so mimalloc may be a good fit: on 64-bit systems, mimalloc allocates above the 4TiB address space -- that gives you a quick initial test right there. It will also allocate under 30TiB (unless using extreme workloads) so that is another quick check. The mi_heap_check_owned is slowish and more meant for analysis tools; the mi_heap_contains_block is faster, but as you noticed in #299, it does not work for large allocations (since those are not owned).

The call you need is mi_is_in_heap_region which should work for most allocations except very large ones (I believe over 128MiB) The implementation in the dev-slice branch of mimalloc does work for any allocation, and you may base your work of that (the intention is for dev-slice to be a future version of mimalloc).

Hope this helps!

playXE commented 4 years ago

Thanks for answer @daanx ! This helps a lot :)