@@ -36,13 +36,14 @@ class PtrCompressor;
36
36
// is used for pointer compression by the memory allocator.
37
37
38
38
// 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.
46
47
class CACHELIB_PACKED_ATTR CompressedPtr {
47
48
public:
48
49
using PtrType = uint32_t ;
@@ -93,7 +94,10 @@ class CACHELIB_PACKED_ATTR CompressedPtr {
93
94
PtrType ptr_{kNull };
94
95
95
96
// 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 )
97
101
: ptr_(compress(slabIdx, allocIdx, isMultiTiered, tid)) {}
98
102
99
103
constexpr explicit CompressedPtr (PtrType ptr) noexcept : ptr_{ptr} {}
@@ -115,18 +119,22 @@ class CACHELIB_PACKED_ATTR CompressedPtr {
115
119
// Number of bits for the slab index. This will be the top 16 bits of the
116
120
// compressed ptr.
117
121
static constexpr unsigned int kNumSlabIdxBits =
118
- kNumTierIdxOffset - kNumAllocIdxBits ;
122
+ kNumTierIdxOffset - kNumAllocIdxBits ;
119
123
120
124
// Compress the given slabIdx and allocIdx into a 64-bit compressed
121
125
// 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 {
123
130
XDCHECK_LE (allocIdx, kAllocIdxMask );
124
131
if (!isMultiTiered) {
125
- XDCHECK_LT (slabIdx, (1u << (kNumSlabIdxBits + 1 )) - 1 );
132
+ XDCHECK_LT (slabIdx, (1u << (kNumSlabIdxBits + 1 )) - 1 );
126
133
return (slabIdx << kNumAllocIdxBits ) + allocIdx;
127
134
}
128
135
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;
130
138
}
131
139
132
140
// Get the slab index of the compressed ptr
@@ -149,7 +157,7 @@ class CACHELIB_PACKED_ATTR CompressedPtr {
149
157
}
150
158
151
159
void setTierId (TierId tid) noexcept {
152
- ptr_ += static_cast <uint64_t >(tid) << kNumTierIdxOffset ;
160
+ ptr_ += static_cast <uint32_t >(tid) << kNumTierIdxOffset ;
153
161
}
154
162
155
163
friend SlabAllocator;
@@ -196,7 +204,8 @@ class PtrCompressor {
196
204
197
205
TierId tid;
198
206
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)))
200
209
break ;
201
210
}
202
211
@@ -213,8 +222,9 @@ class PtrCompressor {
213
222
return nullptr ;
214
223
}
215
224
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));
218
228
}
219
229
220
230
bool operator ==(const PtrCompressor& rhs) const noexcept {
0 commit comments