Skip to content

Commit 5c168fe

Browse files
committed
fix CustomHeap Page::CanAllocate calculation
1 parent 2451fe4 commit 5c168fe

File tree

2 files changed

+5
-12
lines changed

2 files changed

+5
-12
lines changed

lib/Common/Memory/CustomHeap.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,17 +1102,9 @@ inline BucketId GetBucketForSize(size_t bytes)
11021102
return BucketId::LargeObjectList;
11031103
}
11041104

1105-
BucketId bucket = (BucketId) (log2(bytes) - 7);
1106-
1107-
// < 8 => 0
1108-
// 8 => 1
1109-
// 9 => 2 ...
1105+
BucketId bucket = (BucketId) (log2(bytes / Page::sizePerBit));
11101106
Assert(bucket < BucketId::LargeObjectList);
1111-
1112-
if (bucket < BucketId::SmallObjectList)
1113-
{
1114-
bucket = BucketId::SmallObjectList;
1115-
}
1107+
Assert(bucket >= BucketId::SmallObjectList);
11161108

11171109
return bucket;
11181110
}

lib/Common/Memory/CustomHeap.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ struct Page
5757

5858
bool CanAllocate(BucketId targetBucket)
5959
{
60-
return freeBitVector.FirstStringOfOnes(targetBucket + 1) != BVInvalidIndex;
60+
return freeBitVector.FirstStringOfOnes(1 << targetBucket) != BVInvalidIndex;
6161
}
6262

6363
Page(__in char* address, void* segment, BucketId bucket):
@@ -72,8 +72,9 @@ struct Page
7272

7373
// Each bit in the bit vector corresponds to 128 bytes of memory
7474
// This implies that 128 bytes is the smallest allocation possible
75-
static const uint Alignment = 128;
7675
static const uint MaxAllocationSize = 4096;
76+
static const uint sizePerBit = MaxAllocationSize / 32; // pagesize / freeBitVector bit count
77+
static const uint Alignment = sizePerBit; // 128
7778
};
7879

7980
struct Allocation

0 commit comments

Comments
 (0)