microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
allocator/mimalloc-sys/mimalloc/src/heap.c
626lines · modecode
| 1 | /*---------------------------------------------------------------------------- |
| 2 | Copyright (c) 2018-2021, Microsoft Research, Daan Leijen |
| 3 | This is free software; you can redistribute it and/or modify it under the |
| 4 | terms of the MIT license. A copy of the license can be found in the file |
| 5 | "LICENSE" at the root of this distribution. |
| 6 | -----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #include "mimalloc.h" |
| 9 | #include "mimalloc/internal.h" |
| 10 | #include "mimalloc/atomic.h" |
| 11 | #include "mimalloc/prim.h" // mi_prim_get_default_heap |
| 12 | |
| 13 | #include <string.h> // memset, memcpy |
| 14 | |
| 15 | #if defined(_MSC_VER) && (_MSC_VER < 1920) |
| 16 | #pragma warning(disable:4204) // non-constant aggregate initializer |
| 17 | #endif |
| 18 | |
| 19 | /* ----------------------------------------------------------- |
| 20 | Helpers |
| 21 | ----------------------------------------------------------- */ |
| 22 | |
| 23 | // return `true` if ok, `false` to break |
| 24 | typedef bool (heap_page_visitor_fun)(mi_heap_t* heap, mi_page_queue_t* pq, mi_page_t* page, void* arg1, void* arg2); |
| 25 | |
| 26 | // Visit all pages in a heap; returns `false` if break was called. |
| 27 | static bool mi_heap_visit_pages(mi_heap_t* heap, heap_page_visitor_fun* fn, void* arg1, void* arg2) |
| 28 | { |
| 29 | if (heap==NULL || heap->page_count==0) return 0; |
| 30 | |
| 31 | // visit all pages |
| 32 | #if MI_DEBUG>1 |
| 33 | size_t total = heap->page_count; |
| 34 | size_t count = 0; |
| 35 | #endif |
| 36 | |
| 37 | for (size_t i = 0; i <= MI_BIN_FULL; i++) { |
| 38 | mi_page_queue_t* pq = &heap->pages[i]; |
| 39 | mi_page_t* page = pq->first; |
| 40 | while(page != NULL) { |
| 41 | mi_page_t* next = page->next; // save next in case the page gets removed from the queue |
| 42 | mi_assert_internal(mi_page_heap(page) == heap); |
| 43 | #if MI_DEBUG>1 |
| 44 | count++; |
| 45 | #endif |
| 46 | if (!fn(heap, pq, page, arg1, arg2)) return false; |
| 47 | page = next; // and continue |
| 48 | } |
| 49 | } |
| 50 | mi_assert_internal(count == total); |
| 51 | return true; |
| 52 | } |
| 53 | |
| 54 | |
| 55 | #if MI_DEBUG>=2 |
| 56 | static bool mi_heap_page_is_valid(mi_heap_t* heap, mi_page_queue_t* pq, mi_page_t* page, void* arg1, void* arg2) { |
| 57 | MI_UNUSED(arg1); |
| 58 | MI_UNUSED(arg2); |
| 59 | MI_UNUSED(pq); |
| 60 | mi_assert_internal(mi_page_heap(page) == heap); |
| 61 | mi_segment_t* segment = _mi_page_segment(page); |
| 62 | mi_assert_internal(segment->thread_id == heap->thread_id); |
| 63 | mi_assert_expensive(_mi_page_is_valid(page)); |
| 64 | return true; |
| 65 | } |
| 66 | #endif |
| 67 | #if MI_DEBUG>=3 |
| 68 | static bool mi_heap_is_valid(mi_heap_t* heap) { |
| 69 | mi_assert_internal(heap!=NULL); |
| 70 | mi_heap_visit_pages(heap, &mi_heap_page_is_valid, NULL, NULL); |
| 71 | return true; |
| 72 | } |
| 73 | #endif |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | /* ----------------------------------------------------------- |
| 79 | "Collect" pages by migrating `local_free` and `thread_free` |
| 80 | lists and freeing empty pages. This is done when a thread |
| 81 | stops (and in that case abandons pages if there are still |
| 82 | blocks alive) |
| 83 | ----------------------------------------------------------- */ |
| 84 | |
| 85 | typedef enum mi_collect_e { |
| 86 | MI_NORMAL, |
| 87 | MI_FORCE, |
| 88 | MI_ABANDON |
| 89 | } mi_collect_t; |
| 90 | |
| 91 | |
| 92 | static bool mi_heap_page_collect(mi_heap_t* heap, mi_page_queue_t* pq, mi_page_t* page, void* arg_collect, void* arg2 ) { |
| 93 | MI_UNUSED(arg2); |
| 94 | MI_UNUSED(heap); |
| 95 | mi_assert_internal(mi_heap_page_is_valid(heap, pq, page, NULL, NULL)); |
| 96 | mi_collect_t collect = *((mi_collect_t*)arg_collect); |
| 97 | _mi_page_free_collect(page, collect >= MI_FORCE); |
| 98 | if (mi_page_all_free(page)) { |
| 99 | // no more used blocks, free the page. |
| 100 | // note: this will free retired pages as well. |
| 101 | _mi_page_free(page, pq, collect >= MI_FORCE); |
| 102 | } |
| 103 | else if (collect == MI_ABANDON) { |
| 104 | // still used blocks but the thread is done; abandon the page |
| 105 | _mi_page_abandon(page, pq); |
| 106 | } |
| 107 | return true; // don't break |
| 108 | } |
| 109 | |
| 110 | static bool mi_heap_page_never_delayed_free(mi_heap_t* heap, mi_page_queue_t* pq, mi_page_t* page, void* arg1, void* arg2) { |
| 111 | MI_UNUSED(arg1); |
| 112 | MI_UNUSED(arg2); |
| 113 | MI_UNUSED(heap); |
| 114 | MI_UNUSED(pq); |
| 115 | _mi_page_use_delayed_free(page, MI_NEVER_DELAYED_FREE, false); |
| 116 | return true; // don't break |
| 117 | } |
| 118 | |
| 119 | static void mi_heap_collect_ex(mi_heap_t* heap, mi_collect_t collect) |
| 120 | { |
| 121 | if (heap==NULL || !mi_heap_is_initialized(heap)) return; |
| 122 | |
| 123 | const bool force = collect >= MI_FORCE; |
| 124 | _mi_deferred_free(heap, force); |
| 125 | |
| 126 | // note: never reclaim on collect but leave it to threads that need storage to reclaim |
| 127 | const bool force_main = |
| 128 | #ifdef NDEBUG |
| 129 | collect == MI_FORCE |
| 130 | #else |
| 131 | collect >= MI_FORCE |
| 132 | #endif |
| 133 | && _mi_is_main_thread() && mi_heap_is_backing(heap) && !heap->no_reclaim; |
| 134 | |
| 135 | if (force_main) { |
| 136 | // the main thread is abandoned (end-of-program), try to reclaim all abandoned segments. |
| 137 | // if all memory is freed by now, all segments should be freed. |
| 138 | _mi_abandoned_reclaim_all(heap, &heap->tld->segments); |
| 139 | } |
| 140 | |
| 141 | // if abandoning, mark all pages to no longer add to delayed_free |
| 142 | if (collect == MI_ABANDON) { |
| 143 | mi_heap_visit_pages(heap, &mi_heap_page_never_delayed_free, NULL, NULL); |
| 144 | } |
| 145 | |
| 146 | // free all current thread delayed blocks. |
| 147 | // (if abandoning, after this there are no more thread-delayed references into the pages.) |
| 148 | _mi_heap_delayed_free_all(heap); |
| 149 | |
| 150 | // collect retired pages |
| 151 | _mi_heap_collect_retired(heap, force); |
| 152 | |
| 153 | // collect all pages owned by this thread |
| 154 | mi_heap_visit_pages(heap, &mi_heap_page_collect, &collect, NULL); |
| 155 | mi_assert_internal( collect != MI_ABANDON || mi_atomic_load_ptr_acquire(mi_block_t,&heap->thread_delayed_free) == NULL ); |
| 156 | |
| 157 | // collect abandoned segments (in particular, purge expired parts of segments in the abandoned segment list) |
| 158 | // note: forced purge can be quite expensive if many threads are created/destroyed so we do not force on abandonment |
| 159 | _mi_abandoned_collect(heap, collect == MI_FORCE /* force? */, &heap->tld->segments); |
| 160 | |
| 161 | // collect segment local caches |
| 162 | if (force) { |
| 163 | _mi_segment_thread_collect(&heap->tld->segments); |
| 164 | } |
| 165 | |
| 166 | // collect regions on program-exit (or shared library unload) |
| 167 | if (force && _mi_is_main_thread() && mi_heap_is_backing(heap)) { |
| 168 | _mi_thread_data_collect(); // collect thread data cache |
| 169 | _mi_arena_collect(true /* force purge */, &heap->tld->stats); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | void _mi_heap_collect_abandon(mi_heap_t* heap) { |
| 174 | mi_heap_collect_ex(heap, MI_ABANDON); |
| 175 | } |
| 176 | |
| 177 | void mi_heap_collect(mi_heap_t* heap, bool force) mi_attr_noexcept { |
| 178 | mi_heap_collect_ex(heap, (force ? MI_FORCE : MI_NORMAL)); |
| 179 | } |
| 180 | |
| 181 | void mi_collect(bool force) mi_attr_noexcept { |
| 182 | mi_heap_collect(mi_prim_get_default_heap(), force); |
| 183 | } |
| 184 | |
| 185 | |
| 186 | /* ----------------------------------------------------------- |
| 187 | Heap new |
| 188 | ----------------------------------------------------------- */ |
| 189 | |
| 190 | mi_heap_t* mi_heap_get_default(void) { |
| 191 | mi_thread_init(); |
| 192 | return mi_prim_get_default_heap(); |
| 193 | } |
| 194 | |
| 195 | static bool mi_heap_is_default(const mi_heap_t* heap) { |
| 196 | return (heap == mi_prim_get_default_heap()); |
| 197 | } |
| 198 | |
| 199 | |
| 200 | mi_heap_t* mi_heap_get_backing(void) { |
| 201 | mi_heap_t* heap = mi_heap_get_default(); |
| 202 | mi_assert_internal(heap!=NULL); |
| 203 | mi_heap_t* bheap = heap->tld->heap_backing; |
| 204 | mi_assert_internal(bheap!=NULL); |
| 205 | mi_assert_internal(bheap->thread_id == _mi_thread_id()); |
| 206 | return bheap; |
| 207 | } |
| 208 | |
| 209 | mi_decl_nodiscard mi_heap_t* mi_heap_new_in_arena(mi_arena_id_t arena_id) { |
| 210 | mi_heap_t* bheap = mi_heap_get_backing(); |
| 211 | mi_heap_t* heap = mi_heap_malloc_tp(bheap, mi_heap_t); // todo: OS allocate in secure mode? |
| 212 | if (heap == NULL) return NULL; |
| 213 | _mi_memcpy_aligned(heap, &_mi_heap_empty, sizeof(mi_heap_t)); |
| 214 | heap->tld = bheap->tld; |
| 215 | heap->thread_id = _mi_thread_id(); |
| 216 | heap->arena_id = arena_id; |
| 217 | _mi_random_split(&bheap->random, &heap->random); |
| 218 | heap->cookie = _mi_heap_random_next(heap) | 1; |
| 219 | heap->keys[0] = _mi_heap_random_next(heap); |
| 220 | heap->keys[1] = _mi_heap_random_next(heap); |
| 221 | heap->no_reclaim = true; // don't reclaim abandoned pages or otherwise destroy is unsafe |
| 222 | // push on the thread local heaps list |
| 223 | heap->next = heap->tld->heaps; |
| 224 | heap->tld->heaps = heap; |
| 225 | return heap; |
| 226 | } |
| 227 | |
| 228 | mi_decl_nodiscard mi_heap_t* mi_heap_new(void) { |
| 229 | return mi_heap_new_in_arena(_mi_arena_id_none()); |
| 230 | } |
| 231 | |
| 232 | bool _mi_heap_memid_is_suitable(mi_heap_t* heap, mi_memid_t memid) { |
| 233 | return _mi_arena_memid_is_suitable(memid, heap->arena_id); |
| 234 | } |
| 235 | |
| 236 | uintptr_t _mi_heap_random_next(mi_heap_t* heap) { |
| 237 | return _mi_random_next(&heap->random); |
| 238 | } |
| 239 | |
| 240 | // zero out the page queues |
| 241 | static void mi_heap_reset_pages(mi_heap_t* heap) { |
| 242 | mi_assert_internal(heap != NULL); |
| 243 | mi_assert_internal(mi_heap_is_initialized(heap)); |
| 244 | // TODO: copy full empty heap instead? |
| 245 | memset(&heap->pages_free_direct, 0, sizeof(heap->pages_free_direct)); |
| 246 | _mi_memcpy_aligned(&heap->pages, &_mi_heap_empty.pages, sizeof(heap->pages)); |
| 247 | heap->thread_delayed_free = NULL; |
| 248 | heap->page_count = 0; |
| 249 | } |
| 250 | |
| 251 | // called from `mi_heap_destroy` and `mi_heap_delete` to free the internal heap resources. |
| 252 | static void mi_heap_free(mi_heap_t* heap) { |
| 253 | mi_assert(heap != NULL); |
| 254 | mi_assert_internal(mi_heap_is_initialized(heap)); |
| 255 | if (heap==NULL || !mi_heap_is_initialized(heap)) return; |
| 256 | if (mi_heap_is_backing(heap)) return; // dont free the backing heap |
| 257 | |
| 258 | // reset default |
| 259 | if (mi_heap_is_default(heap)) { |
| 260 | _mi_heap_set_default_direct(heap->tld->heap_backing); |
| 261 | } |
| 262 | |
| 263 | // remove ourselves from the thread local heaps list |
| 264 | // linear search but we expect the number of heaps to be relatively small |
| 265 | mi_heap_t* prev = NULL; |
| 266 | mi_heap_t* curr = heap->tld->heaps; |
| 267 | while (curr != heap && curr != NULL) { |
| 268 | prev = curr; |
| 269 | curr = curr->next; |
| 270 | } |
| 271 | mi_assert_internal(curr == heap); |
| 272 | if (curr == heap) { |
| 273 | if (prev != NULL) { prev->next = heap->next; } |
| 274 | else { heap->tld->heaps = heap->next; } |
| 275 | } |
| 276 | mi_assert_internal(heap->tld->heaps != NULL); |
| 277 | |
| 278 | // and free the used memory |
| 279 | mi_free(heap); |
| 280 | } |
| 281 | |
| 282 | |
| 283 | /* ----------------------------------------------------------- |
| 284 | Heap destroy |
| 285 | ----------------------------------------------------------- */ |
| 286 | |
| 287 | static bool _mi_heap_page_destroy(mi_heap_t* heap, mi_page_queue_t* pq, mi_page_t* page, void* arg1, void* arg2) { |
| 288 | MI_UNUSED(arg1); |
| 289 | MI_UNUSED(arg2); |
| 290 | MI_UNUSED(heap); |
| 291 | MI_UNUSED(pq); |
| 292 | |
| 293 | // ensure no more thread_delayed_free will be added |
| 294 | _mi_page_use_delayed_free(page, MI_NEVER_DELAYED_FREE, false); |
| 295 | |
| 296 | // stats |
| 297 | const size_t bsize = mi_page_block_size(page); |
| 298 | if (bsize > MI_MEDIUM_OBJ_SIZE_MAX) { |
| 299 | if (bsize <= MI_LARGE_OBJ_SIZE_MAX) { |
| 300 | mi_heap_stat_decrease(heap, large, bsize); |
| 301 | } |
| 302 | else { |
| 303 | mi_heap_stat_decrease(heap, huge, bsize); |
| 304 | } |
| 305 | } |
| 306 | #if (MI_STAT) |
| 307 | _mi_page_free_collect(page, false); // update used count |
| 308 | const size_t inuse = page->used; |
| 309 | if (bsize <= MI_LARGE_OBJ_SIZE_MAX) { |
| 310 | mi_heap_stat_decrease(heap, normal, bsize * inuse); |
| 311 | #if (MI_STAT>1) |
| 312 | mi_heap_stat_decrease(heap, normal_bins[_mi_bin(bsize)], inuse); |
| 313 | #endif |
| 314 | } |
| 315 | mi_heap_stat_decrease(heap, malloc, bsize * inuse); // todo: off for aligned blocks... |
| 316 | #endif |
| 317 | |
| 318 | /// pretend it is all free now |
| 319 | mi_assert_internal(mi_page_thread_free(page) == NULL); |
| 320 | page->used = 0; |
| 321 | |
| 322 | // and free the page |
| 323 | // mi_page_free(page,false); |
| 324 | page->next = NULL; |
| 325 | page->prev = NULL; |
| 326 | _mi_segment_page_free(page,false /* no force? */, &heap->tld->segments); |
| 327 | |
| 328 | return true; // keep going |
| 329 | } |
| 330 | |
| 331 | void _mi_heap_destroy_pages(mi_heap_t* heap) { |
| 332 | mi_heap_visit_pages(heap, &_mi_heap_page_destroy, NULL, NULL); |
| 333 | mi_heap_reset_pages(heap); |
| 334 | } |
| 335 | |
| 336 | #if MI_TRACK_HEAP_DESTROY |
| 337 | static bool mi_cdecl mi_heap_track_block_free(const mi_heap_t* heap, const mi_heap_area_t* area, void* block, size_t block_size, void* arg) { |
| 338 | MI_UNUSED(heap); MI_UNUSED(area); MI_UNUSED(arg); MI_UNUSED(block_size); |
| 339 | mi_track_free_size(block,mi_usable_size(block)); |
| 340 | return true; |
| 341 | } |
| 342 | #endif |
| 343 | |
| 344 | void mi_heap_destroy(mi_heap_t* heap) { |
| 345 | mi_assert(heap != NULL); |
| 346 | mi_assert(mi_heap_is_initialized(heap)); |
| 347 | mi_assert(heap->no_reclaim); |
| 348 | mi_assert_expensive(mi_heap_is_valid(heap)); |
| 349 | if (heap==NULL || !mi_heap_is_initialized(heap)) return; |
| 350 | if (!heap->no_reclaim) { |
| 351 | // don't free in case it may contain reclaimed pages |
| 352 | mi_heap_delete(heap); |
| 353 | } |
| 354 | else { |
| 355 | // track all blocks as freed |
| 356 | #if MI_TRACK_HEAP_DESTROY |
| 357 | mi_heap_visit_blocks(heap, true, mi_heap_track_block_free, NULL); |
| 358 | #endif |
| 359 | // free all pages |
| 360 | _mi_heap_destroy_pages(heap); |
| 361 | mi_heap_free(heap); |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | // forcefully destroy all heaps in the current thread |
| 366 | void _mi_heap_unsafe_destroy_all(void) { |
| 367 | mi_heap_t* bheap = mi_heap_get_backing(); |
| 368 | mi_heap_t* curr = bheap->tld->heaps; |
| 369 | while (curr != NULL) { |
| 370 | mi_heap_t* next = curr->next; |
| 371 | if (curr->no_reclaim) { |
| 372 | mi_heap_destroy(curr); |
| 373 | } |
| 374 | else { |
| 375 | _mi_heap_destroy_pages(curr); |
| 376 | } |
| 377 | curr = next; |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | /* ----------------------------------------------------------- |
| 382 | Safe Heap delete |
| 383 | ----------------------------------------------------------- */ |
| 384 | |
| 385 | // Transfer the pages from one heap to the other |
| 386 | static void mi_heap_absorb(mi_heap_t* heap, mi_heap_t* from) { |
| 387 | mi_assert_internal(heap!=NULL); |
| 388 | if (from==NULL || from->page_count == 0) return; |
| 389 | |
| 390 | // reduce the size of the delayed frees |
| 391 | _mi_heap_delayed_free_partial(from); |
| 392 | |
| 393 | // transfer all pages by appending the queues; this will set a new heap field |
| 394 | // so threads may do delayed frees in either heap for a while. |
| 395 | // note: appending waits for each page to not be in the `MI_DELAYED_FREEING` state |
| 396 | // so after this only the new heap will get delayed frees |
| 397 | for (size_t i = 0; i <= MI_BIN_FULL; i++) { |
| 398 | mi_page_queue_t* pq = &heap->pages[i]; |
| 399 | mi_page_queue_t* append = &from->pages[i]; |
| 400 | size_t pcount = _mi_page_queue_append(heap, pq, append); |
| 401 | heap->page_count += pcount; |
| 402 | from->page_count -= pcount; |
| 403 | } |
| 404 | mi_assert_internal(from->page_count == 0); |
| 405 | |
| 406 | // and do outstanding delayed frees in the `from` heap |
| 407 | // note: be careful here as the `heap` field in all those pages no longer point to `from`, |
| 408 | // turns out to be ok as `_mi_heap_delayed_free` only visits the list and calls a |
| 409 | // the regular `_mi_free_delayed_block` which is safe. |
| 410 | _mi_heap_delayed_free_all(from); |
| 411 | #if !defined(_MSC_VER) || (_MSC_VER > 1900) // somehow the following line gives an error in VS2015, issue #353 |
| 412 | mi_assert_internal(mi_atomic_load_ptr_relaxed(mi_block_t,&from->thread_delayed_free) == NULL); |
| 413 | #endif |
| 414 | |
| 415 | // and reset the `from` heap |
| 416 | mi_heap_reset_pages(from); |
| 417 | } |
| 418 | |
| 419 | // Safe delete a heap without freeing any still allocated blocks in that heap. |
| 420 | void mi_heap_delete(mi_heap_t* heap) |
| 421 | { |
| 422 | mi_assert(heap != NULL); |
| 423 | mi_assert(mi_heap_is_initialized(heap)); |
| 424 | mi_assert_expensive(mi_heap_is_valid(heap)); |
| 425 | if (heap==NULL || !mi_heap_is_initialized(heap)) return; |
| 426 | |
| 427 | if (!mi_heap_is_backing(heap)) { |
| 428 | // tranfer still used pages to the backing heap |
| 429 | mi_heap_absorb(heap->tld->heap_backing, heap); |
| 430 | } |
| 431 | else { |
| 432 | // the backing heap abandons its pages |
| 433 | _mi_heap_collect_abandon(heap); |
| 434 | } |
| 435 | mi_assert_internal(heap->page_count==0); |
| 436 | mi_heap_free(heap); |
| 437 | } |
| 438 | |
| 439 | mi_heap_t* mi_heap_set_default(mi_heap_t* heap) { |
| 440 | mi_assert(heap != NULL); |
| 441 | mi_assert(mi_heap_is_initialized(heap)); |
| 442 | if (heap==NULL || !mi_heap_is_initialized(heap)) return NULL; |
| 443 | mi_assert_expensive(mi_heap_is_valid(heap)); |
| 444 | mi_heap_t* old = mi_prim_get_default_heap(); |
| 445 | _mi_heap_set_default_direct(heap); |
| 446 | return old; |
| 447 | } |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | /* ----------------------------------------------------------- |
| 453 | Analysis |
| 454 | ----------------------------------------------------------- */ |
| 455 | |
| 456 | // static since it is not thread safe to access heaps from other threads. |
| 457 | static mi_heap_t* mi_heap_of_block(const void* p) { |
| 458 | if (p == NULL) return NULL; |
| 459 | mi_segment_t* segment = _mi_ptr_segment(p); |
| 460 | bool valid = (_mi_ptr_cookie(segment) == segment->cookie); |
| 461 | mi_assert_internal(valid); |
| 462 | if mi_unlikely(!valid) return NULL; |
| 463 | return mi_page_heap(_mi_segment_page_of(segment,p)); |
| 464 | } |
| 465 | |
| 466 | bool mi_heap_contains_block(mi_heap_t* heap, const void* p) { |
| 467 | mi_assert(heap != NULL); |
| 468 | if (heap==NULL || !mi_heap_is_initialized(heap)) return false; |
| 469 | return (heap == mi_heap_of_block(p)); |
| 470 | } |
| 471 | |
| 472 | |
| 473 | static bool mi_heap_page_check_owned(mi_heap_t* heap, mi_page_queue_t* pq, mi_page_t* page, void* p, void* vfound) { |
| 474 | MI_UNUSED(heap); |
| 475 | MI_UNUSED(pq); |
| 476 | bool* found = (bool*)vfound; |
| 477 | mi_segment_t* segment = _mi_page_segment(page); |
| 478 | void* start = _mi_page_start(segment, page, NULL); |
| 479 | void* end = (uint8_t*)start + (page->capacity * mi_page_block_size(page)); |
| 480 | *found = (p >= start && p < end); |
| 481 | return (!*found); // continue if not found |
| 482 | } |
| 483 | |
| 484 | bool mi_heap_check_owned(mi_heap_t* heap, const void* p) { |
| 485 | mi_assert(heap != NULL); |
| 486 | if (heap==NULL || !mi_heap_is_initialized(heap)) return false; |
| 487 | if (((uintptr_t)p & (MI_INTPTR_SIZE - 1)) != 0) return false; // only aligned pointers |
| 488 | bool found = false; |
| 489 | mi_heap_visit_pages(heap, &mi_heap_page_check_owned, (void*)p, &found); |
| 490 | return found; |
| 491 | } |
| 492 | |
| 493 | bool mi_check_owned(const void* p) { |
| 494 | return mi_heap_check_owned(mi_prim_get_default_heap(), p); |
| 495 | } |
| 496 | |
| 497 | /* ----------------------------------------------------------- |
| 498 | Visit all heap blocks and areas |
| 499 | Todo: enable visiting abandoned pages, and |
| 500 | enable visiting all blocks of all heaps across threads |
| 501 | ----------------------------------------------------------- */ |
| 502 | |
| 503 | // Separate struct to keep `mi_page_t` out of the public interface |
| 504 | typedef struct mi_heap_area_ex_s { |
| 505 | mi_heap_area_t area; |
| 506 | mi_page_t* page; |
| 507 | } mi_heap_area_ex_t; |
| 508 | |
| 509 | static bool mi_heap_area_visit_blocks(const mi_heap_area_ex_t* xarea, mi_block_visit_fun* visitor, void* arg) { |
| 510 | mi_assert(xarea != NULL); |
| 511 | if (xarea==NULL) return true; |
| 512 | const mi_heap_area_t* area = &xarea->area; |
| 513 | mi_page_t* page = xarea->page; |
| 514 | mi_assert(page != NULL); |
| 515 | if (page == NULL) return true; |
| 516 | |
| 517 | _mi_page_free_collect(page,true); |
| 518 | mi_assert_internal(page->local_free == NULL); |
| 519 | if (page->used == 0) return true; |
| 520 | |
| 521 | const size_t bsize = mi_page_block_size(page); |
| 522 | const size_t ubsize = mi_page_usable_block_size(page); // without padding |
| 523 | size_t psize; |
| 524 | uint8_t* pstart = _mi_page_start(_mi_page_segment(page), page, &psize); |
| 525 | |
| 526 | if (page->capacity == 1) { |
| 527 | // optimize page with one block |
| 528 | mi_assert_internal(page->used == 1 && page->free == NULL); |
| 529 | return visitor(mi_page_heap(page), area, pstart, ubsize, arg); |
| 530 | } |
| 531 | |
| 532 | // create a bitmap of free blocks. |
| 533 | #define MI_MAX_BLOCKS (MI_SMALL_PAGE_SIZE / sizeof(void*)) |
| 534 | uintptr_t free_map[MI_MAX_BLOCKS / sizeof(uintptr_t)]; |
| 535 | memset(free_map, 0, sizeof(free_map)); |
| 536 | |
| 537 | #if MI_DEBUG>1 |
| 538 | size_t free_count = 0; |
| 539 | #endif |
| 540 | for (mi_block_t* block = page->free; block != NULL; block = mi_block_next(page,block)) { |
| 541 | #if MI_DEBUG>1 |
| 542 | free_count++; |
| 543 | #endif |
| 544 | mi_assert_internal((uint8_t*)block >= pstart && (uint8_t*)block < (pstart + psize)); |
| 545 | size_t offset = (uint8_t*)block - pstart; |
| 546 | mi_assert_internal(offset % bsize == 0); |
| 547 | size_t blockidx = offset / bsize; // Todo: avoid division? |
| 548 | mi_assert_internal( blockidx < MI_MAX_BLOCKS); |
| 549 | size_t bitidx = (blockidx / sizeof(uintptr_t)); |
| 550 | size_t bit = blockidx - (bitidx * sizeof(uintptr_t)); |
| 551 | free_map[bitidx] |= ((uintptr_t)1 << bit); |
| 552 | } |
| 553 | mi_assert_internal(page->capacity == (free_count + page->used)); |
| 554 | |
| 555 | // walk through all blocks skipping the free ones |
| 556 | #if MI_DEBUG>1 |
| 557 | size_t used_count = 0; |
| 558 | #endif |
| 559 | for (size_t i = 0; i < page->capacity; i++) { |
| 560 | size_t bitidx = (i / sizeof(uintptr_t)); |
| 561 | size_t bit = i - (bitidx * sizeof(uintptr_t)); |
| 562 | uintptr_t m = free_map[bitidx]; |
| 563 | if (bit == 0 && m == UINTPTR_MAX) { |
| 564 | i += (sizeof(uintptr_t) - 1); // skip a run of free blocks |
| 565 | } |
| 566 | else if ((m & ((uintptr_t)1 << bit)) == 0) { |
| 567 | #if MI_DEBUG>1 |
| 568 | used_count++; |
| 569 | #endif |
| 570 | uint8_t* block = pstart + (i * bsize); |
| 571 | if (!visitor(mi_page_heap(page), area, block, ubsize, arg)) return false; |
| 572 | } |
| 573 | } |
| 574 | mi_assert_internal(page->used == used_count); |
| 575 | return true; |
| 576 | } |
| 577 | |
| 578 | typedef bool (mi_heap_area_visit_fun)(const mi_heap_t* heap, const mi_heap_area_ex_t* area, void* arg); |
| 579 | |
| 580 | |
| 581 | static bool mi_heap_visit_areas_page(mi_heap_t* heap, mi_page_queue_t* pq, mi_page_t* page, void* vfun, void* arg) { |
| 582 | MI_UNUSED(heap); |
| 583 | MI_UNUSED(pq); |
| 584 | mi_heap_area_visit_fun* fun = (mi_heap_area_visit_fun*)vfun; |
| 585 | mi_heap_area_ex_t xarea; |
| 586 | const size_t bsize = mi_page_block_size(page); |
| 587 | const size_t ubsize = mi_page_usable_block_size(page); |
| 588 | xarea.page = page; |
| 589 | xarea.area.reserved = page->reserved * bsize; |
| 590 | xarea.area.committed = page->capacity * bsize; |
| 591 | xarea.area.blocks = _mi_page_start(_mi_page_segment(page), page, NULL); |
| 592 | xarea.area.used = page->used; // number of blocks in use (#553) |
| 593 | xarea.area.block_size = ubsize; |
| 594 | xarea.area.full_block_size = bsize; |
| 595 | return fun(heap, &xarea, arg); |
| 596 | } |
| 597 | |
| 598 | // Visit all heap pages as areas |
| 599 | static bool mi_heap_visit_areas(const mi_heap_t* heap, mi_heap_area_visit_fun* visitor, void* arg) { |
| 600 | if (visitor == NULL) return false; |
| 601 | return mi_heap_visit_pages((mi_heap_t*)heap, &mi_heap_visit_areas_page, (void*)(visitor), arg); // note: function pointer to void* :-{ |
| 602 | } |
| 603 | |
| 604 | // Just to pass arguments |
| 605 | typedef struct mi_visit_blocks_args_s { |
| 606 | bool visit_blocks; |
| 607 | mi_block_visit_fun* visitor; |
| 608 | void* arg; |
| 609 | } mi_visit_blocks_args_t; |
| 610 | |
| 611 | static bool mi_heap_area_visitor(const mi_heap_t* heap, const mi_heap_area_ex_t* xarea, void* arg) { |
| 612 | mi_visit_blocks_args_t* args = (mi_visit_blocks_args_t*)arg; |
| 613 | if (!args->visitor(heap, &xarea->area, NULL, xarea->area.block_size, args->arg)) return false; |
| 614 | if (args->visit_blocks) { |
| 615 | return mi_heap_area_visit_blocks(xarea, args->visitor, args->arg); |
| 616 | } |
| 617 | else { |
| 618 | return true; |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | // Visit all blocks in a heap |
| 623 | bool mi_heap_visit_blocks(const mi_heap_t* heap, bool visit_blocks, mi_block_visit_fun* visitor, void* arg) { |
| 624 | mi_visit_blocks_args_t args = { visit_blocks, visitor, arg }; |
| 625 | return mi_heap_visit_areas(heap, &mi_heap_area_visitor, &args); |
| 626 | } |
| 627 | |