Finalize the allocator
| 1339 | |
| 1340 | //! Finalize the allocator |
| 1341 | void |
| 1342 | rpmalloc_finalize(void) { |
| 1343 | atomic_thread_fence_acquire(); |
| 1344 | |
| 1345 | //Free all thread caches |
| 1346 | for (size_t list_idx = 0; list_idx < HEAP_ARRAY_SIZE; ++list_idx) { |
| 1347 | heap_t* heap = atomic_load_ptr(&_memory_heaps[list_idx]); |
| 1348 | while (heap) { |
| 1349 | _memory_deallocate_deferred(heap, 0); |
| 1350 | |
| 1351 | for (size_t iclass = 0; iclass < SPAN_CLASS_COUNT; ++iclass) { |
| 1352 | const size_t page_count = (iclass + 1) * SPAN_CLASS_GRANULARITY; |
| 1353 | span_t* span = heap->span_cache[iclass]; |
| 1354 | unsigned int span_count = span ? span->data.list_size : 0; |
| 1355 | for (unsigned int ispan = 0; ispan < span_count; ++ispan) { |
| 1356 | span_t* next_span = span->next_span; |
| 1357 | _memory_unmap(span, page_count); |
| 1358 | span = next_span; |
| 1359 | } |
| 1360 | } |
| 1361 | |
| 1362 | //Free large spans |
| 1363 | for (size_t iclass = 0; iclass < LARGE_CLASS_COUNT; ++iclass) { |
| 1364 | const size_t span_count = iclass + 1; |
| 1365 | span_t* span = heap->large_cache[iclass]; |
| 1366 | while (span) { |
| 1367 | span_t* next_span = span->next_span; |
| 1368 | _memory_unmap(span, span_count * SPAN_MAX_PAGE_COUNT); |
| 1369 | span = next_span; |
| 1370 | } |
| 1371 | } |
| 1372 | |
| 1373 | heap_t* next_heap = heap->next_heap; |
| 1374 | _memory_unmap(heap, 2); |
| 1375 | heap = next_heap; |
| 1376 | } |
| 1377 | |
| 1378 | atomic_store_ptr(&_memory_heaps[list_idx], 0); |
| 1379 | } |
| 1380 | atomic_store_ptr(&_memory_orphan_heaps, 0); |
| 1381 | |
| 1382 | //Free global caches |
| 1383 | for (size_t iclass = 0; iclass < SPAN_CLASS_COUNT; ++iclass) { |
| 1384 | void* span_ptr = atomic_load_ptr(&_memory_span_cache[iclass]); |
| 1385 | size_t cache_count = (uintptr_t)span_ptr & ~SPAN_MASK; |
| 1386 | span_t* span = (span_t*)((void*)((uintptr_t)span_ptr & SPAN_MASK)); |
| 1387 | while (cache_count) { |
| 1388 | span_t* skip_span = span->prev_span; |
| 1389 | unsigned int span_count = span->data.list_size; |
| 1390 | for (unsigned int ispan = 0; ispan < span_count; ++ispan) { |
| 1391 | span_t* next_span = span->next_span; |
| 1392 | _memory_unmap(span, (iclass + 1) * SPAN_CLASS_GRANULARITY); |
| 1393 | span = next_span; |
| 1394 | } |
| 1395 | span = skip_span; |
| 1396 | cache_count -= span_count; |
| 1397 | } |
| 1398 | atomic_store_ptr(&_memory_span_cache[iclass], 0); |
no test coverage detected