Skip to content

Commit fa24793

Browse files
authored
Merge pull request #52 from guptask/cptr_cosmetic_changes
Cosmetic change to return data type
2 parents 265c312 + 9cb5c29 commit fa24793

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

cachelib/allocator/memory/CompressedPtr.h

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,14 @@ class PtrCompressor;
3636
// is used for pointer compression by the memory allocator.
3737

3838
// We compress pointers by storing the tier index, slab index and alloc index of
39-
// the allocation inside the slab. With slab worth kNumSlabBits (22 bits) of data,
40-
// if we have the min allocation size as 64 bytes, that requires kNumSlabBits - 6
41-
// = 16 bits for storing the alloc index. The tier id occupies the 32nd bit only
42-
// since its value cannot exceed kMaxTiers (2). This leaves the remaining
43-
// (32 - (kNumSlabBits - 6) - 1 bit for tier id) = 15 bits for the slab index.
44-
// Hence we can index 128 GiB of memory in slabs per tier and index anything more
45-
// than 64 byte allocations inside the slab using a 32 bit representation.
39+
// the allocation inside the slab. With slab worth kNumSlabBits (22 bits) of
40+
// data, if we have the min allocation size as 64 bytes, that requires
41+
// kNumSlabBits - 6 = 16 bits for storing the alloc index. The tier id occupies
42+
// the 32nd bit only since its value cannot exceed kMaxTiers (2). This leaves
43+
// the remaining (32 - (kNumSlabBits - 6) - 1 bit for tier id) = 15 bits for
44+
// the slab index. Hence we can index 128 GiB of memory in slabs per tier and
45+
// index anything more than 64 byte allocations inside the slab using a 32 bit
46+
// representation.
4647
class CACHELIB_PACKED_ATTR CompressedPtr {
4748
public:
4849
using PtrType = uint32_t;
@@ -93,7 +94,10 @@ class CACHELIB_PACKED_ATTR CompressedPtr {
9394
PtrType ptr_{kNull};
9495

9596
// create a compressed pointer for a valid memory allocation.
96-
CompressedPtr(uint32_t slabIdx, uint32_t allocIdx, bool isMultiTiered, TierId tid = 0)
97+
CompressedPtr(uint32_t slabIdx,
98+
uint32_t allocIdx,
99+
bool isMultiTiered,
100+
TierId tid = 0)
97101
: ptr_(compress(slabIdx, allocIdx, isMultiTiered, tid)) {}
98102

99103
constexpr explicit CompressedPtr(PtrType ptr) noexcept : ptr_{ptr} {}
@@ -115,18 +119,22 @@ class CACHELIB_PACKED_ATTR CompressedPtr {
115119
// Number of bits for the slab index. This will be the top 16 bits of the
116120
// compressed ptr.
117121
static constexpr unsigned int kNumSlabIdxBits =
118-
kNumTierIdxOffset - kNumAllocIdxBits;
122+
kNumTierIdxOffset - kNumAllocIdxBits;
119123

120124
// Compress the given slabIdx and allocIdx into a 64-bit compressed
121125
// pointer.
122-
static PtrType compress(uint32_t slabIdx, uint32_t allocIdx, bool isMultiTiered, TierId tid) noexcept {
126+
static PtrType compress(uint32_t slabIdx,
127+
uint32_t allocIdx,
128+
bool isMultiTiered,
129+
TierId tid) noexcept {
123130
XDCHECK_LE(allocIdx, kAllocIdxMask);
124131
if (!isMultiTiered) {
125-
XDCHECK_LT(slabIdx, (1u << (kNumSlabIdxBits+1)) - 1);
132+
XDCHECK_LT(slabIdx, (1u << (kNumSlabIdxBits + 1)) - 1);
126133
return (slabIdx << kNumAllocIdxBits) + allocIdx;
127134
}
128135
XDCHECK_LT(slabIdx, (1u << kNumSlabIdxBits) - 1);
129-
return (static_cast<uint64_t>(tid) << kNumTierIdxOffset) + (slabIdx << kNumAllocIdxBits) + allocIdx;
136+
return (static_cast<uint32_t>(tid) << kNumTierIdxOffset) +
137+
(slabIdx << kNumAllocIdxBits) + allocIdx;
130138
}
131139

132140
// Get the slab index of the compressed ptr
@@ -149,7 +157,7 @@ class CACHELIB_PACKED_ATTR CompressedPtr {
149157
}
150158

151159
void setTierId(TierId tid) noexcept {
152-
ptr_ += static_cast<uint64_t>(tid) << kNumTierIdxOffset;
160+
ptr_ += static_cast<uint32_t>(tid) << kNumTierIdxOffset;
153161
}
154162

155163
friend SlabAllocator;
@@ -196,7 +204,8 @@ class PtrCompressor {
196204

197205
TierId tid;
198206
for (tid = 0; tid < allocators_.size(); tid++) {
199-
if (allocators_[tid]->isMemoryInAllocator(static_cast<const void*>(uncompressed)))
207+
if (allocators_[tid]->isMemoryInAllocator(
208+
static_cast<const void*>(uncompressed)))
200209
break;
201210
}
202211

@@ -213,8 +222,9 @@ class PtrCompressor {
213222
return nullptr;
214223
}
215224
bool isMultiTiered = allocators_.size() > 1;
216-
auto &allocator = *allocators_[compressed.getTierId(isMultiTiered)];
217-
return static_cast<PtrType*>(allocator.unCompress(compressed, isMultiTiered));
225+
auto& allocator = *allocators_[compressed.getTierId(isMultiTiered)];
226+
return static_cast<PtrType*>(
227+
allocator.unCompress(compressed, isMultiTiered));
218228
}
219229

220230
bool operator==(const PtrCompressor& rhs) const noexcept {

0 commit comments

Comments
 (0)