Finalize the allocator
| 2771 | |
| 2772 | //! Finalize the allocator |
| 2773 | void |
| 2774 | rpmalloc_finalize(void) { |
| 2775 | rpmalloc_thread_finalize(); |
| 2776 | //rpmalloc_dump_statistics(stdout); |
| 2777 | |
| 2778 | if (_memory_global_reserve) { |
| 2779 | atomic_add32(&_memory_global_reserve_master->remaining_spans, -(int32_t)_memory_global_reserve_count); |
| 2780 | _memory_global_reserve_master = 0; |
| 2781 | _memory_global_reserve_count = 0; |
| 2782 | _memory_global_reserve = 0; |
| 2783 | } |
| 2784 | atomic_store32_release(&_memory_global_lock, 0); |
| 2785 | |
| 2786 | //Free all thread caches and fully free spans |
| 2787 | for (size_t list_idx = 0; list_idx < HEAP_ARRAY_SIZE; ++list_idx) { |
| 2788 | heap_t* heap = _memory_heaps[list_idx]; |
| 2789 | while (heap) { |
| 2790 | heap_t* next_heap = heap->next_heap; |
| 2791 | heap->finalize = 1; |
| 2792 | _rpmalloc_heap_global_finalize(heap); |
| 2793 | heap = next_heap; |
| 2794 | } |
| 2795 | } |
| 2796 | |
| 2797 | #if ENABLE_GLOBAL_CACHE |
| 2798 | //Free global caches |
| 2799 | for (size_t iclass = 0; iclass < LARGE_CLASS_COUNT; ++iclass) |
| 2800 | _rpmalloc_global_cache_finalize(&_memory_span_cache[iclass]); |
| 2801 | #endif |
| 2802 | |
| 2803 | #if (defined(__APPLE__) || defined(__HAIKU__)) && ENABLE_PRELOAD |
| 2804 | pthread_key_delete(_memory_thread_heap); |
| 2805 | #endif |
| 2806 | #if defined(_WIN32) && (!defined(BUILD_DYNAMIC_LINK) || !BUILD_DYNAMIC_LINK) |
| 2807 | FlsFree(fls_key); |
| 2808 | fls_key = 0; |
| 2809 | #endif |
| 2810 | #if ENABLE_STATISTICS |
| 2811 | //If you hit these asserts you probably have memory leaks (perhaps global scope data doing dynamic allocations) or double frees in your code |
| 2812 | assert(atomic_load32(&_mapped_pages) == 0); |
| 2813 | assert(atomic_load32(&_reserved_spans) == 0); |
| 2814 | assert(atomic_load32(&_mapped_pages_os) == 0); |
| 2815 | #endif |
| 2816 | |
| 2817 | _rpmalloc_initialized = 0; |
| 2818 | } |
| 2819 | |
| 2820 | //! Initialize thread, assign heap |
| 2821 | extern inline void |