diff --git a/include/mimalloc-internal.h b/include/mimalloc-internal.h index 97abb4a..14c111c 100644 --- a/include/mimalloc-internal.h +++ b/include/mimalloc-internal.h @@ -59,7 +59,7 @@ void _mi_page_abandon(mi_page_t* page, mi_page_queue_t* pq); // void _mi_heap_delayed_free(mi_heap_t* heap); void _mi_page_use_delayed_free(mi_page_t* page, bool enable); -void _mi_page_queue_append(mi_heap_t* heap, mi_page_queue_t* pq, mi_page_queue_t* append); +size_t _mi_page_queue_append(mi_heap_t* heap, mi_page_queue_t* pq, mi_page_queue_t* append); void _mi_deferred_free(mi_heap_t* heap, bool force); void _mi_page_free_collect(mi_page_t* page); @@ -136,6 +136,7 @@ static inline bool mi_mul_overflow(size_t size, size_t count, size_t* total) { // Align a byte size to a size in _machine words_, // i.e. byte size == `wsize*sizeof(void*)`. static inline size_t _mi_wsize_from_size(size_t size) { + mi_assert_internal(size <= SIZE_MAX - sizeof(uintptr_t)); return (size + sizeof(uintptr_t) - 1) / sizeof(uintptr_t); } @@ -190,7 +191,7 @@ static inline mi_segment_t* _mi_ptr_segment(const void* p) { // Segment belonging to a page static inline mi_segment_t* _mi_page_segment(const mi_page_t* page) { mi_segment_t* segment = _mi_ptr_segment(page); - mi_assert_internal(page == &segment->pages[page->segment_idx]); + mi_assert_internal(segment == NULL || page == &segment->pages[page->segment_idx]); return segment; } diff --git a/src/heap.c b/src/heap.c index f474b1a..57cb4f7 100644 --- a/src/heap.c +++ b/src/heap.c @@ -302,9 +302,12 @@ static void mi_heap_absorb(mi_heap_t* heap, mi_heap_t* from) { for (size_t i = 0; i < MI_BIN_FULL; i++) { mi_page_queue_t* pq = &heap->pages[i]; mi_page_queue_t* append = &from->pages[i]; - _mi_page_queue_append(heap, pq, append); + size_t pcount = _mi_page_queue_append(heap, pq, append); + heap->page_count += pcount; + from->page_count -= pcount; } mi_assert_internal(from->thread_delayed_free == NULL); + mi_assert_internal(from->page_count == 0); // and reset the `from` heap mi_heap_reset_pages(from); diff --git a/src/page-queue.c b/src/page-queue.c index 4d3547a..ba5e929 100644 --- a/src/page-queue.c +++ b/src/page-queue.c @@ -323,15 +323,17 @@ static void mi_page_queue_enqueue_from(mi_page_queue_t* to, mi_page_queue_t* fro page->flags.in_full = mi_page_queue_is_full(to); } -void _mi_page_queue_append(mi_heap_t* heap, mi_page_queue_t* pq, mi_page_queue_t* append) { +size_t _mi_page_queue_append(mi_heap_t* heap, mi_page_queue_t* pq, mi_page_queue_t* append) { mi_assert_internal(mi_heap_contains_queue(heap,pq)); mi_assert_internal(pq->block_size == append->block_size); - if (append->first==NULL) return; + if (append->first==NULL) return 0; - // set append pages to new heap + // set append pages to new heap and count + size_t count = 0; for (mi_page_t* page = append->first; page != NULL; page = page->next) { page->heap = heap; + count++; } if (pq->last==NULL) { @@ -349,4 +351,5 @@ void _mi_page_queue_append(mi_heap_t* heap, mi_page_queue_t* pq, mi_page_queue_t append->first->prev = pq->last; pq->last = append->last; } + return count; }