From 286a7cc918882e3778984f3eaf835b2dda18a075 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 17 Aug 2026 11:39:52 +0100 Subject: [PATCH] Moved the check for large allocations to the start of IntrusiveAllocator::deallocate(..) to avoid searching the memoryBlocks containers. Added size == 0 and ptr == nullptr checks to allocation and deallocation to avoid doing any more in this null cases. --- src/vsg/core/IntrusiveAllocator.cpp | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/vsg/core/IntrusiveAllocator.cpp b/src/vsg/core/IntrusiveAllocator.cpp index aac14a35b..51fb074a6 100644 --- a/src/vsg/core/IntrusiveAllocator.cpp +++ b/src/vsg/core/IntrusiveAllocator.cpp @@ -905,6 +905,8 @@ void IntrusiveAllocator::report(std::ostream& out) const void* IntrusiveAllocator::allocate(std::size_t size, AllocatorAffinity allocatorAffinity) { + if (size == 0) return nullptr; + std::scoped_lock lock(mutex); // create a MemoryBlocks entry if one doesn't already exist @@ -941,9 +943,19 @@ void* IntrusiveAllocator::allocate(std::size_t size, AllocatorAffinity allocator bool IntrusiveAllocator::deallocate(void* ptr, std::size_t size) { + if (ptr == nullptr) return false; + std::scoped_lock lock(mutex); - if (memoryBlocks.empty()) return false; + auto la_itr = largeAllocations.find(ptr); + if (la_itr != largeAllocations.end()) + { + // large allocation; + // std::cout<<"IntrusiveAllocator::deallocate("<second.first}); + largeAllocations.erase(la_itr); + return true; + } auto itr = memoryBlocks.upper_bound(ptr); if (itr != memoryBlocks.end()) @@ -975,16 +987,6 @@ bool IntrusiveAllocator::deallocate(void* ptr, std::size_t size) } } - auto la_itr = largeAllocations.find(ptr); - if (la_itr != largeAllocations.end()) - { - // large allocation; - // std::cout<<"IntrusiveAllocator::deallocate("<second.first}); - largeAllocations.erase(la_itr); - return true; - } - if (nestedAllocator && nestedAllocator->deallocate(ptr, size)) { return true;