From cbb1063e8257923ba4138d53ae7385056d48279e Mon Sep 17 00:00:00 2001 From: Matthew Parkinson Date: Mon, 12 Aug 2019 18:16:11 +0100 Subject: [PATCH] Fix up checking for empty to account for stub messages. --- src/mem/globalalloc.h | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/src/mem/globalalloc.h b/src/mem/globalalloc.h index bda5c35..cf90567 100644 --- a/src/mem/globalalloc.h +++ b/src/mem/globalalloc.h @@ -107,41 +107,43 @@ namespace snmalloc #endif } - void debug_check_empty() + /** + If you pass a pointer to a bool, then it returns whether all the allocators are empty. + If you don't pass a pointer to a bool, then will raise an error all the allocators are + not empty. + */ + void debug_check_empty(bool* result = nullptr) { #ifndef USE_MALLOC // This is a debugging function. It checks that all memory from all // allocators has been freed. - size_t alloc_count = 0; - auto* alloc = Parent::iterate(); - // Count the linked allocators. - while (alloc != nullptr) - { - alloc = Parent::iterate(alloc); - alloc_count++; - } - bool done = false; + size_t non_empty_count = 0; while (!done) { done = true; alloc = Parent::iterate(); + non_empty_count = 0; while (alloc != nullptr) { // Destroy the message queue so that it has no stub message. Remote* p = alloc->message_queue().destroy(); - while (p != nullptr) + while (p != nullptr) { Remote* next = p->non_atomic_next; alloc->handle_dealloc_remote(p); p = next; } + // Check that the allocator has freed all memory. + if (!alloc->stats().is_empty()) + non_empty_count++; + // Place the static stub message on the queue. alloc->init_message_queue(); @@ -158,20 +160,16 @@ namespace snmalloc } } - alloc = Parent::iterate(); - size_t empty_count = 0; - - while (alloc != nullptr) + if (result != nullptr) { - // Check that the allocator has freed all memory. - if (alloc->stats().is_empty()) - empty_count++; - - alloc = Parent::iterate(alloc); + *result = non_empty_count == 0; + return; } - if (alloc_count != empty_count) - error("Incorrect number of allocators"); + if (non_empty_count != 0) + { + error("debug_check_empty: found non-empty allocators"); + } #endif } };