Skip to content

Commit 0347c11

Browse files
authored
[scudo] Remove unused field in BatchGroup (#109322)
Also fix the logic while determining the size of BatchClass and update some legacy comments.
1 parent 2102607 commit 0347c11

File tree

3 files changed

+14
-43
lines changed

3 files changed

+14
-43
lines changed

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,13 @@ template <class SizeClassAllocator> struct BatchGroup {
7575
BatchGroup *Next;
7676
// The compact base address of each group
7777
uptr CompactPtrGroupBase;
78-
// Cache value of SizeClassAllocatorLocalCache::getMaxCached()
79-
u16 MaxCachedPerBatch;
80-
// Number of blocks pushed into this group. This is an increment-only
81-
// counter.
82-
uptr PushedBlocks;
8378
// This is used to track how many bytes are not in-use since last time we
8479
// tried to release pages.
8580
uptr BytesInBGAtLastCheckpoint;
8681
// Blocks are managed by TransferBatch in a list.
8782
SinglyLinkedList<TransferBatch<SizeClassAllocator>> Batches;
83+
// Cache value of SizeClassAllocatorLocalCache::getMaxCached()
84+
u16 MaxCachedPerBatch;
8885
};
8986

9087
} // namespace scudo

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

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,9 @@ template <typename Config> class SizeClassAllocator32 {
5656
typedef TransferBatch<ThisT> TransferBatchT;
5757
typedef BatchGroup<ThisT> BatchGroupT;
5858

59-
static_assert(sizeof(BatchGroupT) <= sizeof(TransferBatchT),
60-
"BatchGroupT uses the same class size as TransferBatchT");
61-
6259
static uptr getSizeByClassId(uptr ClassId) {
6360
return (ClassId == SizeClassMap::BatchClassId)
64-
? sizeof(TransferBatchT)
61+
? Max(sizeof(BatchGroupT), sizeof(TransferBatchT))
6562
: SizeClassMap::getSizeByClassId(ClassId);
6663
}
6764

@@ -531,9 +528,6 @@ template <typename Config> class SizeClassAllocator32 {
531528
// BatchClass hasn't enabled memory group. Use `0` to indicate there's no
532529
// memory group here.
533530
BG->CompactPtrGroupBase = 0;
534-
// `BG` is also the block of BatchClassId. Note that this is different
535-
// from `CreateGroup` in `pushBlocksImpl`
536-
BG->PushedBlocks = 1;
537531
BG->BytesInBGAtLastCheckpoint = 0;
538532
BG->MaxCachedPerBatch =
539533
CacheT::getMaxCached(getSizeByClassId(SizeClassMap::BatchClassId));
@@ -558,9 +552,6 @@ template <typename Config> class SizeClassAllocator32 {
558552
TB->add(
559553
compactPtr(SizeClassMap::BatchClassId, reinterpret_cast<uptr>(BG)));
560554
--Size;
561-
DCHECK_EQ(BG->PushedBlocks, 1U);
562-
// `TB` is also the block of BatchClassId.
563-
BG->PushedBlocks += 1;
564555
BG->Batches.push_front(TB);
565556
}
566557

@@ -587,8 +578,6 @@ template <typename Config> class SizeClassAllocator32 {
587578
CurBatch->appendFromArray(&Array[I], AppendSize);
588579
I += AppendSize;
589580
}
590-
591-
BG->PushedBlocks += Size;
592581
}
593582
// Push the blocks to their batch group. The layout will be like,
594583
//
@@ -624,7 +613,6 @@ template <typename Config> class SizeClassAllocator32 {
624613

625614
BG->CompactPtrGroupBase = CompactPtrGroupBase;
626615
BG->Batches.push_front(TB);
627-
BG->PushedBlocks = 0;
628616
BG->BytesInBGAtLastCheckpoint = 0;
629617
BG->MaxCachedPerBatch = TransferBatchT::MaxNumCached;
630618

@@ -652,8 +640,6 @@ template <typename Config> class SizeClassAllocator32 {
652640
CurBatch->appendFromArray(&Array[I], AppendSize);
653641
I += AppendSize;
654642
}
655-
656-
BG->PushedBlocks += Size;
657643
};
658644

659645
Sci->FreeListInfo.PushedBlocks += Size;

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

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ template <typename Config> class SizeClassAllocator64 {
6161
typedef TransferBatch<ThisT> TransferBatchT;
6262
typedef BatchGroup<ThisT> BatchGroupT;
6363

64-
static_assert(sizeof(BatchGroupT) <= sizeof(TransferBatchT),
65-
"BatchGroupT uses the same class size as TransferBatchT");
66-
64+
// BachClass is used to store internal metadata so it needs to be at least as
65+
// large as the largest data structure.
6766
static uptr getSizeByClassId(uptr ClassId) {
6867
return (ClassId == SizeClassMap::BatchClassId)
69-
? roundUp(sizeof(TransferBatchT), 1U << CompactPtrScale)
68+
? roundUp(Max(sizeof(TransferBatchT), sizeof(BatchGroupT)),
69+
1U << CompactPtrScale)
7070
: SizeClassMap::getSizeByClassId(ClassId);
7171
}
7272

@@ -236,7 +236,7 @@ template <typename Config> class SizeClassAllocator64 {
236236
} else {
237237
while (true) {
238238
// When two threads compete for `Region->MMLock`, we only want one of
239-
// them to call populateFreeListAndPopBatch(). To avoid both of them
239+
// them to call populateFreeListAndPopBlocks(). To avoid both of them
240240
// doing that, always check the freelist before mapping new pages.
241241
ScopedLock ML(Region->MMLock);
242242
{
@@ -690,9 +690,6 @@ template <typename Config> class SizeClassAllocator64 {
690690
// BatchClass hasn't enabled memory group. Use `0` to indicate there's no
691691
// memory group here.
692692
BG->CompactPtrGroupBase = 0;
693-
// `BG` is also the block of BatchClassId. Note that this is different
694-
// from `CreateGroup` in `pushBlocksImpl`
695-
BG->PushedBlocks = 1;
696693
BG->BytesInBGAtLastCheckpoint = 0;
697694
BG->MaxCachedPerBatch =
698695
CacheT::getMaxCached(getSizeByClassId(SizeClassMap::BatchClassId));
@@ -717,9 +714,6 @@ template <typename Config> class SizeClassAllocator64 {
717714
TB->add(
718715
compactPtr(SizeClassMap::BatchClassId, reinterpret_cast<uptr>(BG)));
719716
--Size;
720-
DCHECK_EQ(BG->PushedBlocks, 1U);
721-
// `TB` is also the block of BatchClassId.
722-
BG->PushedBlocks += 1;
723717
BG->Batches.push_front(TB);
724718
}
725719

@@ -746,8 +740,6 @@ template <typename Config> class SizeClassAllocator64 {
746740
CurBatch->appendFromArray(&Array[I], AppendSize);
747741
I += AppendSize;
748742
}
749-
750-
BG->PushedBlocks += Size;
751743
}
752744

753745
// Push the blocks to their batch group. The layout will be like,
@@ -782,7 +774,6 @@ template <typename Config> class SizeClassAllocator64 {
782774

783775
BG->CompactPtrGroupBase = CompactPtrGroupBase;
784776
BG->Batches.push_front(TB);
785-
BG->PushedBlocks = 0;
786777
BG->BytesInBGAtLastCheckpoint = 0;
787778
BG->MaxCachedPerBatch = TransferBatchT::MaxNumCached;
788779

@@ -810,8 +801,6 @@ template <typename Config> class SizeClassAllocator64 {
810801
CurBatch->appendFromArray(&Array[I], AppendSize);
811802
I += AppendSize;
812803
}
813-
814-
BG->PushedBlocks += Size;
815804
};
816805

817806
Region->FreeListInfo.PushedBlocks += Size;
@@ -884,7 +873,7 @@ template <typename Config> class SizeClassAllocator64 {
884873
while (true) {
885874
// We only expect one thread doing the freelist refillment and other
886875
// threads will be waiting for either the completion of the
887-
// `populateFreeListAndPopBatch()` or `pushBlocks()` called by other
876+
// `populateFreeListAndPopBlocks()` or `pushBlocks()` called by other
888877
// threads.
889878
bool PopulateFreeList = false;
890879
{
@@ -922,7 +911,7 @@ template <typename Config> class SizeClassAllocator64 {
922911
// At here, there are two preconditions to be met before waiting,
923912
// 1. The freelist is empty.
924913
// 2. Region->isPopulatingFreeList == true, i.e, someone is still doing
925-
// `populateFreeListAndPopBatch()`.
914+
// `populateFreeListAndPopBlocks()`.
926915
//
927916
// Note that it has the chance that freelist is empty but
928917
// Region->isPopulatingFreeList == false because all the new populated
@@ -938,8 +927,8 @@ template <typename Config> class SizeClassAllocator64 {
938927

939928
// Now the freelist is empty and someone's doing the refillment. We will
940929
// wait until anyone refills the freelist or someone finishes doing
941-
// `populateFreeListAndPopBatch()`. The refillment can be done by
942-
// `populateFreeListAndPopBatch()`, `pushBlocks()`,
930+
// `populateFreeListAndPopBlocks()`. The refillment can be done by
931+
// `populateFreeListAndPopBlocks()`, `pushBlocks()`,
943932
// `pushBatchClassBlocks()` and `mergeGroupsToReleaseBack()`.
944933
Region->FLLockCV.wait(Region->FLLock);
945934

@@ -1119,8 +1108,8 @@ template <typename Config> class SizeClassAllocator64 {
11191108

11201109
// Note that `PushedBlocks` and `PoppedBlocks` are supposed to only record
11211110
// the requests from `PushBlocks` and `PopBatch` which are external
1122-
// interfaces. `populateFreeListAndPopBatch` is the internal interface so we
1123-
// should set the values back to avoid incorrectly setting the stats.
1111+
// interfaces. `populateFreeListAndPopBlocks` is the internal interface so
1112+
// we should set the values back to avoid incorrectly setting the stats.
11241113
Region->FreeListInfo.PushedBlocks -= NumberOfBlocks;
11251114

11261115
const uptr AllocatedUser = Size * NumberOfBlocks;
@@ -1689,7 +1678,6 @@ template <typename Config> class SizeClassAllocator64 {
16891678
GroupsToRelease.pop_front();
16901679

16911680
if (BG->CompactPtrGroupBase == Cur->CompactPtrGroupBase) {
1692-
BG->PushedBlocks += Cur->PushedBlocks;
16931681
// We have updated `BatchGroup::BytesInBGAtLastCheckpoint` while
16941682
// collecting the `GroupsToRelease`.
16951683
BG->BytesInBGAtLastCheckpoint = Cur->BytesInBGAtLastCheckpoint;

0 commit comments

Comments
 (0)