From 60a5ce1bed306f7ad64cf5208c7224f4f14be34e Mon Sep 17 00:00:00 2001 From: 0z5a Date: Fri, 13 Nov 2026 08:59:50 +0100 Subject: [PATCH] Check the in-use allocator count after the loop in debug_in_use debug_in_use(count) checked "count != 0" inside the loop over the allocator pool, right after the first allocator. Any call with count > 0 therefore failed with "two few allocators in use" unless the first allocator in the pool was the only one in use: debug_in_use(2) always failed, and with count > 0 an empty pool was not reported at all. Move the check after the loop so the count is compared once every allocator has been visited. The in-tree callers only pass 0, which behaves as before. func/teardown now calls debug_in_use with one and two scoped allocators held. --- src/snmalloc/global/globalalloc.h | 8 ++++---- src/test/func/teardown/teardown.cc | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/snmalloc/global/globalalloc.h b/src/snmalloc/global/globalalloc.h index 7607e582a..ca9aaf509 100644 --- a/src/snmalloc/global/globalalloc.h +++ b/src/snmalloc/global/globalalloc.h @@ -120,11 +120,11 @@ namespace snmalloc count--; } alloc = AllocPool::iterate(alloc); + } - if (count != 0) - { - error("Error: two few allocators in use."); - } + if (count != 0) + { + error("Error: two few allocators in use."); } } diff --git a/src/test/func/teardown/teardown.cc b/src/test/func/teardown/teardown.cc index 02129374d..3ad56925d 100644 --- a/src/test/func/teardown/teardown.cc +++ b/src/test/func/teardown/teardown.cc @@ -112,6 +112,20 @@ void f(size_t size) fflush(stdout); } +void check_debug_in_use() +{ + { + auto a = snmalloc::get_scoped_allocator(); + snmalloc::debug_in_use(1); + { + auto b = snmalloc::get_scoped_allocator(); + snmalloc::debug_in_use(2); + } + snmalloc::debug_in_use(1); + } + snmalloc::debug_in_use(0); +} + int main(int, char**) { setup(); @@ -142,4 +156,6 @@ int main(int, char**) f(shifted(7) - 1); printf("\n"); } + + check_debug_in_use(); }