Skip to content

Commit e010537

Browse files
committed
[scudo] Skip visiting all free blocks if grouping is disabled
This is only applied to SizeClassAllocator64 which has single region. In SizeClassAllocator32, the region size has to be equal to the group size. Differential Revision: https://reviews.llvm.org/D156740
1 parent d796f36 commit e010537

File tree

2 files changed

+26
-22
lines changed

2 files changed

+26
-22
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,7 @@ template <typename Config> class SizeClassAllocator32 {
932932
uptr TotalReleasedBytes = 0;
933933
const uptr Base = First * RegionSize;
934934
const uptr NumberOfRegions = Last - First + 1U;
935-
const uptr GroupSize = (1U << GroupSizeLog);
935+
const uptr GroupSize = (1UL << GroupSizeLog);
936936
const uptr CurGroupBase =
937937
compactPtrGroupBase(compactPtr(ClassId, Sci->CurrentRegion));
938938

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

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ template <typename Config> class SizeClassAllocator64 {
4848
typedef typename Config::Primary::CompactPtrT CompactPtrT;
4949
typedef typename Config::Primary::SizeClassMap SizeClassMap;
5050
static const uptr CompactPtrScale = Config::Primary::CompactPtrScale;
51+
static const uptr RegionSizeLog = Config::Primary::RegionSizeLog;
5152
static const uptr GroupSizeLog = Config::Primary::GroupSizeLog;
53+
static_assert(RegionSizeLog >= GroupSizeLog,
54+
"Group size shouldn't be greater than the region size");
5255
static const uptr GroupScale = GroupSizeLog - CompactPtrScale;
5356
typedef SizeClassAllocator64<Config> ThisT;
5457
typedef SizeClassAllocatorLocalCache<ThisT> CacheT;
@@ -67,7 +70,7 @@ template <typename Config> class SizeClassAllocator64 {
6770
DCHECK(isAligned(reinterpret_cast<uptr>(this), alignof(ThisT)));
6871

6972
const uptr PageSize = getPageSizeCached();
70-
const uptr GroupSize = (1U << GroupSizeLog);
73+
const uptr GroupSize = (1UL << GroupSizeLog);
7174
const uptr PagesInGroup = GroupSize / PageSize;
7275
const uptr MinSizeClass = getSizeByClassId(1);
7376
// When trying to release pages back to memory, visiting smaller size
@@ -119,11 +122,10 @@ template <typename Config> class SizeClassAllocator64 {
119122
RegionInfo *Region = getRegionInfo(I);
120123
// The actual start of a region is offset by a random number of pages
121124
// when PrimaryEnableRandomOffset is set.
122-
Region->RegionBeg =
123-
(PrimaryBase + (I << Config::Primary::RegionSizeLog)) +
124-
(Config::Primary::EnableRandomOffset
125-
? ((getRandomModN(&Seed, 16) + 1) * PageSize)
126-
: 0);
125+
Region->RegionBeg = (PrimaryBase + (I << RegionSizeLog)) +
126+
(Config::Primary::EnableRandomOffset
127+
? ((getRandomModN(&Seed, 16) + 1) * PageSize)
128+
: 0);
127129
Region->RandState = getRandomU32(&Seed);
128130
// Releasing small blocks is expensive, set a higher threshold to avoid
129131
// frequent page releases.
@@ -134,7 +136,7 @@ template <typename Config> class SizeClassAllocator64 {
134136
Region->ReleaseInfo.LastReleaseAtNs = Time;
135137

136138
Region->MemMapInfo.MemMap = ReservedMemory.dispatch(
137-
PrimaryBase + (I << Config::Primary::RegionSizeLog), RegionSize);
139+
PrimaryBase + (I << RegionSizeLog), RegionSize);
138140
CHECK(Region->MemMapInfo.MemMap.isAllocated());
139141
}
140142
shuffle(RegionInfoArray, NumClasses, &Seed);
@@ -271,19 +273,21 @@ template <typename Config> class SizeClassAllocator64 {
271273
// TODO(chiahungduan): Consider not doing grouping if the group size is not
272274
// greater than the block size with a certain scale.
273275

274-
// Sort the blocks so that blocks belonging to the same group can be pushed
275-
// together.
276276
bool SameGroup = true;
277-
for (u32 I = 1; I < Size; ++I) {
278-
if (compactPtrGroup(Array[I - 1]) != compactPtrGroup(Array[I]))
279-
SameGroup = false;
280-
CompactPtrT Cur = Array[I];
281-
u32 J = I;
282-
while (J > 0 && compactPtrGroup(Cur) < compactPtrGroup(Array[J - 1])) {
283-
Array[J] = Array[J - 1];
284-
--J;
277+
if (GroupSizeLog < RegionSizeLog) {
278+
// Sort the blocks so that blocks belonging to the same group can be
279+
// pushed together.
280+
for (u32 I = 1; I < Size; ++I) {
281+
if (compactPtrGroup(Array[I - 1]) != compactPtrGroup(Array[I]))
282+
SameGroup = false;
283+
CompactPtrT Cur = Array[I];
284+
u32 J = I;
285+
while (J > 0 && compactPtrGroup(Cur) < compactPtrGroup(Array[J - 1])) {
286+
Array[J] = Array[J - 1];
287+
--J;
288+
}
289+
Array[J] = Cur;
285290
}
286-
Array[J] = Cur;
287291
}
288292

289293
{
@@ -477,7 +481,7 @@ template <typename Config> class SizeClassAllocator64 {
477481
AtomicOptions Options;
478482

479483
private:
480-
static const uptr RegionSize = 1UL << Config::Primary::RegionSizeLog;
484+
static const uptr RegionSize = 1UL << RegionSizeLog;
481485
static const uptr NumClasses = SizeClassMap::NumClasses;
482486
static const uptr PrimarySize = RegionSize * NumClasses;
483487

@@ -1125,7 +1129,7 @@ template <typename Config> class SizeClassAllocator64 {
11251129
collectGroupsToRelease(RegionInfo *Region, const uptr BlockSize,
11261130
const uptr AllocatedUserEnd, const uptr CompactPtrBase)
11271131
REQUIRES(Region->MMLock, Region->FLLock) {
1128-
const uptr GroupSize = (1U << GroupSizeLog);
1132+
const uptr GroupSize = (1UL << GroupSizeLog);
11291133
const uptr PageSize = getPageSizeCached();
11301134
SinglyLinkedList<BatchGroup> GroupsToRelease;
11311135

@@ -1292,7 +1296,7 @@ template <typename Config> class SizeClassAllocator64 {
12921296
const uptr AllocatedUserEnd, const uptr CompactPtrBase,
12931297
SinglyLinkedList<BatchGroup> &GroupsToRelease)
12941298
REQUIRES(Region->MMLock) EXCLUDES(Region->FLLock) {
1295-
const uptr GroupSize = (1U << GroupSizeLog);
1299+
const uptr GroupSize = (1UL << GroupSizeLog);
12961300
auto DecompactPtr = [CompactPtrBase](CompactPtrT CompactPtr) {
12971301
return decompactPtrInternal(CompactPtrBase, CompactPtr);
12981302
};

0 commit comments

Comments
 (0)