diff --git a/compiler-rt/lib/scudo/standalone/primary64.h b/compiler-rt/lib/scudo/standalone/primary64.h --- a/compiler-rt/lib/scudo/standalone/primary64.h +++ b/compiler-rt/lib/scudo/standalone/primary64.h @@ -192,31 +192,17 @@ RegionInfo *Region = getRegionInfo(ClassId); if (ClassId == SizeClassMap::BatchClassId) { - bool PrintStats = false; - { - ScopedLock L(Region->Mutex); - // Constructing a batch group in the free list will use two blocks in - // BatchClassId. If we are pushing BatchClassId blocks, we will use the - // blocks in the array directly (can't delegate local cache which will - // cause a recursive allocation). However, The number of free blocks may - // be less than two. Therefore, populate the free list before inserting - // the blocks. - if (Size >= 2U) { - pushBlocksImpl(C, SizeClassMap::BatchClassId, Region, Array, Size); - Region->FreeListInfo.PushedBlocks += Size; - } else { - const bool RegionIsExhausted = Region->Exhausted; - if (UNLIKELY( - RegionIsExhausted || - !populateFreeList(C, SizeClassMap::BatchClassId, Region))) { - PrintStats = !RegionIsExhausted && Region->Exhausted; - } - } - } - - // Note that `getStats()` requires the lock of each region so we can't - // call it while locking the Region->Mutex in the above. - if (UNLIKELY(PrintStats)) { + ScopedLock L(Region->Mutex); + // Constructing a batch group in the free list will use two blocks in + // BatchClassId. If we are pushing BatchClassId blocks, we will use the + // blocks in the array directly (can't delegate local cache which will + // cause a recursive allocation). However, The number of free blocks may + // be less than two. Therefore, populate the free list before inserting + // the blocks. + const bool NeedToRefill = + Size == 1U && Region->FreeListInfo.BlockList.empty(); + if (UNLIKELY(NeedToRefill && + !populateFreeList(C, SizeClassMap::BatchClassId, Region))) { ScopedString Str; getStats(&Str); Str.append( @@ -227,6 +213,10 @@ // when it happens. reportOutOfBatchClass(); } + + pushBlocksImpl(C, SizeClassMap::BatchClassId, Region, Array, Size); + Region->FreeListInfo.PushedBlocks += Size; + return; }