Skip to content

Commit 4e8d6c4

Browse files
authored
[scudo] Pass the max number of blocks to popBlocks (#70243)
Make the cache have the fully control on how many blocks to be popped (At before, it depended the number of blocks stored in the TransferBatch)
1 parent ec456ba commit 4e8d6c4

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

compiler-rt/lib/scudo/standalone/local_cache.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ template <class SizeClassAllocator> struct SizeClassAllocatorLocalCache {
4040
DCHECK_LT(ClassId, NumClasses);
4141
PerClass *C = &PerClassArray[ClassId];
4242
if (C->Count == 0) {
43-
if (UNLIKELY(!refill(C, ClassId)))
43+
initCacheMaybe(C);
44+
45+
// Refill half of the number of max cached.
46+
DCHECK_GT(C->MaxCount / 2, 0U);
47+
if (UNLIKELY(!refill(C, ClassId, C->MaxCount / 2)))
4448
return nullptr;
4549
DCHECK_GT(C->Count, 0);
4650
}
@@ -173,14 +177,10 @@ template <class SizeClassAllocator> struct SizeClassAllocatorLocalCache {
173177
deallocate(BatchClassId, B);
174178
}
175179

176-
NOINLINE bool refill(PerClass *C, uptr ClassId) {
177-
initCacheMaybe(C);
178-
179-
// TODO(chiahungduan): Pass the max number cached for each size class.
180+
NOINLINE bool refill(PerClass *C, uptr ClassId, u16 MaxRefill) {
180181
const u16 NumBlocksRefilled =
181-
Allocator->popBlocks(this, ClassId, C->Chunks);
182-
DCHECK_LE(NumBlocksRefilled,
183-
getMaxCached(SizeClassAllocator::getSizeByClassId(ClassId)));
182+
Allocator->popBlocks(this, ClassId, C->Chunks, MaxRefill);
183+
DCHECK_LE(NumBlocksRefilled, MaxRefill);
184184
C->Count = static_cast<u16>(C->Count + NumBlocksRefilled);
185185
return NumBlocksRefilled != 0;
186186
}

compiler-rt/lib/scudo/standalone/primary32.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,11 @@ template <typename Config> class SizeClassAllocator32 {
191191
return BlockSize > PageSize;
192192
}
193193

194-
u16 popBlocks(CacheT *C, uptr ClassId, CompactPtrT *ToArray) {
194+
// Note that the `MaxBlockCount` will be used when we support arbitrary blocks
195+
// count. Now it's the same as the number of blocks stored in the
196+
// `TransferBatch`.
197+
u16 popBlocks(CacheT *C, uptr ClassId, CompactPtrT *ToArray,
198+
UNUSED const u16 MaxBlockCount) {
195199
TransferBatchT *B = popBatch(C, ClassId);
196200
if (!B)
197201
return 0;

compiler-rt/lib/scudo/standalone/primary64.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,11 @@ template <typename Config> class SizeClassAllocator64 {
221221
DCHECK_EQ(BlocksInUse, BatchClassUsedInFreeLists);
222222
}
223223

224-
u16 popBlocks(CacheT *C, uptr ClassId, CompactPtrT *ToArray) {
224+
// Note that the `MaxBlockCount` will be used when we support arbitrary blocks
225+
// count. Now it's the same as the number of blocks stored in the
226+
// `TransferBatch`.
227+
u16 popBlocks(CacheT *C, uptr ClassId, CompactPtrT *ToArray,
228+
UNUSED const u16 MaxBlockCount) {
225229
TransferBatchT *B = popBatch(C, ClassId);
226230
if (!B)
227231
return 0;

0 commit comments

Comments
 (0)