Skip to content

Commit 6983aa1

Browse files
committed
fixup! Reapply "[scudo] Make local cache be agnostic to the type of node in f… (llvm#68626)
1 parent 23290d8 commit 6983aa1

File tree

4 files changed

+93
-89
lines changed

4 files changed

+93
-89
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ template <class SizeClassAllocator> struct SizeClassAllocatorLocalCache {
181181
Allocator->popBlocks(this, ClassId, C->Chunks);
182182
DCHECK_LE(NumBlocksRefilled,
183183
getMaxCached(SizeClassAllocator::getSizeByClassId(ClassId)));
184-
C->Count += NumBlocksRefilled;
184+
C->Count = static_cast<u16>(C->Count + NumBlocksRefilled);
185185
return NumBlocksRefilled != 0;
186186
}
187187

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

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@ template <typename Config> class SizeClassAllocator32 {
5454
"");
5555
typedef SizeClassAllocator32<Config> ThisT;
5656
typedef SizeClassAllocatorLocalCache<ThisT> CacheT;
57-
typedef TransferBatch<ThisT> TransferBatch;
58-
typedef BatchGroup<ThisT> BatchGroup;
57+
typedef TransferBatch<ThisT> TransferBatchT;
58+
typedef BatchGroup<ThisT> BatchGroupT;
5959

60-
static_assert(sizeof(BatchGroup) <= sizeof(TransferBatch),
61-
"BatchGroup uses the same class size as TransferBatch");
60+
static_assert(sizeof(BatchGroupT) <= sizeof(TransferBatchT),
61+
"BatchGroupT uses the same class size as TransferBatchT");
6262

6363
static uptr getSizeByClassId(uptr ClassId) {
6464
return (ClassId == SizeClassMap::BatchClassId)
65-
? sizeof(TransferBatch)
65+
? sizeof(TransferBatchT)
6666
: SizeClassMap::getSizeByClassId(ClassId);
6767
}
6868

@@ -130,7 +130,7 @@ template <typename Config> class SizeClassAllocator32 {
130130
SizeClassInfo *Sci = getSizeClassInfo(I);
131131
ScopedLock L1(Sci->Mutex);
132132
uptr TotalBlocks = 0;
133-
for (BatchGroup &BG : Sci->FreeListInfo.BlockList) {
133+
for (BatchGroupT &BG : Sci->FreeListInfo.BlockList) {
134134
// `BG::Batches` are `TransferBatches`. +1 for `BatchGroup`.
135135
BatchClassUsedInFreeLists += BG.Batches.size() + 1;
136136
for (const auto &It : BG.Batches)
@@ -145,7 +145,7 @@ template <typename Config> class SizeClassAllocator32 {
145145
SizeClassInfo *Sci = getSizeClassInfo(SizeClassMap::BatchClassId);
146146
ScopedLock L1(Sci->Mutex);
147147
uptr TotalBlocks = 0;
148-
for (BatchGroup &BG : Sci->FreeListInfo.BlockList) {
148+
for (BatchGroupT &BG : Sci->FreeListInfo.BlockList) {
149149
if (LIKELY(!BG.Batches.empty())) {
150150
for (const auto &It : BG.Batches)
151151
TotalBlocks += It.getCount();
@@ -192,7 +192,7 @@ template <typename Config> class SizeClassAllocator32 {
192192
}
193193

194194
u16 popBlocks(CacheT *C, uptr ClassId, CompactPtrT *ToArray) {
195-
TransferBatch *B = popBatch(C, ClassId);
195+
TransferBatchT *B = popBatch(C, ClassId);
196196
if (!B)
197197
return 0;
198198

@@ -206,11 +206,11 @@ template <typename Config> class SizeClassAllocator32 {
206206
return Count;
207207
}
208208

209-
TransferBatch *popBatch(CacheT *C, uptr ClassId) {
209+
TransferBatchT *popBatch(CacheT *C, uptr ClassId) {
210210
DCHECK_LT(ClassId, NumClasses);
211211
SizeClassInfo *Sci = getSizeClassInfo(ClassId);
212212
ScopedLock L(Sci->Mutex);
213-
TransferBatch *B = popBatchImpl(C, ClassId, Sci);
213+
TransferBatchT *B = popBatchImpl(C, ClassId, Sci);
214214
if (UNLIKELY(!B)) {
215215
if (UNLIKELY(!populateFreeList(C, ClassId, Sci)))
216216
return nullptr;
@@ -400,7 +400,7 @@ template <typename Config> class SizeClassAllocator32 {
400400
};
401401

402402
struct BlocksInfo {
403-
SinglyLinkedList<BatchGroup> BlockList = {};
403+
SinglyLinkedList<BatchGroupT> BlockList = {};
404404
uptr PoppedBlocks = 0;
405405
uptr PushedBlocks = 0;
406406
};
@@ -524,11 +524,11 @@ template <typename Config> class SizeClassAllocator32 {
524524
// reusable and don't need additional space for them.
525525

526526
Sci->FreeListInfo.PushedBlocks += Size;
527-
BatchGroup *BG = Sci->FreeListInfo.BlockList.front();
527+
BatchGroupT *BG = Sci->FreeListInfo.BlockList.front();
528528

529529
if (BG == nullptr) {
530530
// Construct `BatchGroup` on the last element.
531-
BG = reinterpret_cast<BatchGroup *>(
531+
BG = reinterpret_cast<BatchGroupT *>(
532532
decompactPtr(SizeClassMap::BatchClassId, Array[Size - 1]));
533533
--Size;
534534
BG->Batches.clear();
@@ -553,7 +553,7 @@ template <typename Config> class SizeClassAllocator32 {
553553
// 2. Only 1 block is pushed when the freelist is empty.
554554
if (BG->Batches.empty()) {
555555
// Construct the `TransferBatch` on the last element.
556-
TransferBatch *TB = reinterpret_cast<TransferBatch *>(
556+
TransferBatchT *TB = reinterpret_cast<TransferBatchT *>(
557557
decompactPtr(SizeClassMap::BatchClassId, Array[Size - 1]));
558558
TB->clear();
559559
// As mentioned above, addresses of `TransferBatch` and `BatchGroup` are
@@ -568,14 +568,14 @@ template <typename Config> class SizeClassAllocator32 {
568568
BG->Batches.push_front(TB);
569569
}
570570

571-
TransferBatch *CurBatch = BG->Batches.front();
571+
TransferBatchT *CurBatch = BG->Batches.front();
572572
DCHECK_NE(CurBatch, nullptr);
573573

574574
for (u32 I = 0; I < Size;) {
575575
u16 UnusedSlots =
576576
static_cast<u16>(BG->MaxCachedPerBatch - CurBatch->getCount());
577577
if (UnusedSlots == 0) {
578-
CurBatch = reinterpret_cast<TransferBatch *>(
578+
CurBatch = reinterpret_cast<TransferBatchT *>(
579579
decompactPtr(SizeClassMap::BatchClassId, Array[I]));
580580
CurBatch->clear();
581581
// Self-contained
@@ -619,10 +619,11 @@ template <typename Config> class SizeClassAllocator32 {
619619
DCHECK_GT(Size, 0U);
620620

621621
auto CreateGroup = [&](uptr CompactPtrGroupBase) {
622-
BatchGroup *BG = reinterpret_cast<BatchGroup *>(C->getBatchClassBlock());
622+
BatchGroupT *BG =
623+
reinterpret_cast<BatchGroupT *>(C->getBatchClassBlock());
623624
BG->Batches.clear();
624-
TransferBatch *TB =
625-
reinterpret_cast<TransferBatch *>(C->getBatchClassBlock());
625+
TransferBatchT *TB =
626+
reinterpret_cast<TransferBatchT *>(C->getBatchClassBlock());
626627
TB->clear();
627628

628629
BG->CompactPtrGroupBase = CompactPtrGroupBase;
@@ -634,17 +635,18 @@ template <typename Config> class SizeClassAllocator32 {
634635
return BG;
635636
};
636637

637-
auto InsertBlocks = [&](BatchGroup *BG, CompactPtrT *Array, u32 Size) {
638-
SinglyLinkedList<TransferBatch> &Batches = BG->Batches;
639-
TransferBatch *CurBatch = Batches.front();
638+
auto InsertBlocks = [&](BatchGroupT *BG, CompactPtrT *Array, u32 Size) {
639+
SinglyLinkedList<TransferBatchT> &Batches = BG->Batches;
640+
TransferBatchT *CurBatch = Batches.front();
640641
DCHECK_NE(CurBatch, nullptr);
641642

642643
for (u32 I = 0; I < Size;) {
643644
DCHECK_GE(BG->MaxCachedPerBatch, CurBatch->getCount());
644645
u16 UnusedSlots =
645646
static_cast<u16>(BG->MaxCachedPerBatch - CurBatch->getCount());
646647
if (UnusedSlots == 0) {
647-
CurBatch = reinterpret_cast<TransferBatch *>(C->getBatchClassBlock());
648+
CurBatch =
649+
reinterpret_cast<TransferBatchT *>(C->getBatchClassBlock());
648650
CurBatch->clear();
649651
Batches.push_front(CurBatch);
650652
UnusedSlots = BG->MaxCachedPerBatch;
@@ -659,11 +661,11 @@ template <typename Config> class SizeClassAllocator32 {
659661
};
660662

661663
Sci->FreeListInfo.PushedBlocks += Size;
662-
BatchGroup *Cur = Sci->FreeListInfo.BlockList.front();
664+
BatchGroupT *Cur = Sci->FreeListInfo.BlockList.front();
663665

664666
// In the following, `Cur` always points to the BatchGroup for blocks that
665667
// will be pushed next. `Prev` is the element right before `Cur`.
666-
BatchGroup *Prev = nullptr;
668+
BatchGroupT *Prev = nullptr;
667669

668670
while (Cur != nullptr &&
669671
compactPtrGroupBase(Array[0]) > Cur->CompactPtrGroupBase) {
@@ -724,36 +726,36 @@ template <typename Config> class SizeClassAllocator32 {
724726
// group id will be considered first.
725727
//
726728
// The region mutex needs to be held while calling this method.
727-
TransferBatch *popBatchImpl(CacheT *C, uptr ClassId, SizeClassInfo *Sci)
729+
TransferBatchT *popBatchImpl(CacheT *C, uptr ClassId, SizeClassInfo *Sci)
728730
REQUIRES(Sci->Mutex) {
729731
if (Sci->FreeListInfo.BlockList.empty())
730732
return nullptr;
731733

732-
SinglyLinkedList<TransferBatch> &Batches =
734+
SinglyLinkedList<TransferBatchT> &Batches =
733735
Sci->FreeListInfo.BlockList.front()->Batches;
734736

735737
if (Batches.empty()) {
736738
DCHECK_EQ(ClassId, SizeClassMap::BatchClassId);
737-
BatchGroup *BG = Sci->FreeListInfo.BlockList.front();
739+
BatchGroupT *BG = Sci->FreeListInfo.BlockList.front();
738740
Sci->FreeListInfo.BlockList.pop_front();
739741

740742
// Block used by `BatchGroup` is from BatchClassId. Turn the block into
741743
// `TransferBatch` with single block.
742-
TransferBatch *TB = reinterpret_cast<TransferBatch *>(BG);
744+
TransferBatchT *TB = reinterpret_cast<TransferBatchT *>(BG);
743745
TB->clear();
744746
TB->add(
745747
compactPtr(SizeClassMap::BatchClassId, reinterpret_cast<uptr>(TB)));
746748
Sci->FreeListInfo.PoppedBlocks += 1;
747749
return TB;
748750
}
749751

750-
TransferBatch *B = Batches.front();
752+
TransferBatchT *B = Batches.front();
751753
Batches.pop_front();
752754
DCHECK_NE(B, nullptr);
753755
DCHECK_GT(B->getCount(), 0U);
754756

755757
if (Batches.empty()) {
756-
BatchGroup *BG = Sci->FreeListInfo.BlockList.front();
758+
BatchGroupT *BG = Sci->FreeListInfo.BlockList.front();
757759
Sci->FreeListInfo.BlockList.pop_front();
758760

759761
// We don't keep BatchGroup with zero blocks to avoid empty-checking while
@@ -805,7 +807,7 @@ template <typename Config> class SizeClassAllocator32 {
805807
DCHECK_GT(NumberOfBlocks, 0U);
806808

807809
constexpr u32 ShuffleArraySize =
808-
MaxNumBatches * TransferBatch::MaxNumCached;
810+
MaxNumBatches * TransferBatchT::MaxNumCached;
809811
// Fill the transfer batches and put them in the size-class freelist. We
810812
// need to randomize the blocks for security purposes, so we first fill a
811813
// local array that we then shuffle before populating the batches.
@@ -1070,7 +1072,7 @@ template <typename Config> class SizeClassAllocator32 {
10701072
auto DecompactPtr = [](CompactPtrT CompactPtr) {
10711073
return reinterpret_cast<uptr>(CompactPtr);
10721074
};
1073-
for (BatchGroup &BG : Sci->FreeListInfo.BlockList) {
1075+
for (BatchGroupT &BG : Sci->FreeListInfo.BlockList) {
10741076
const uptr GroupBase = decompactGroupBase(BG.CompactPtrGroupBase);
10751077
// The `GroupSize` may not be divided by `BlockSize`, which means there is
10761078
// an unused space at the end of Region. Exclude that space to avoid

0 commit comments

Comments
 (0)