MCPcopy Create free account
hub / github.com/facebook/CacheLib / fillUpMemoryPool

Function fillUpMemoryPool

cachelib/allocator/memory/tests/MemoryPoolTest.cpp:189–223  ·  view source on GitHub ↗

fills up the memory pool and returns the allocations in the outparam

Source from the content-addressed store, hash-verified

187
188// fills up the memory pool and returns the allocations in the outparam
189void fillUpMemoryPool(MemoryPool& mp,
190 const std::set<uint32_t>& allocSizes,
191 AllocInfoT& poolAllocs) {
192 for (const auto size : allocSizes) {
193 // ensure it is power of two.
194 ASSERT_EQ(size & (size - 1), 0);
195 }
196
197 const auto maxSize = mp.getPoolSize();
198 using ClassFullT = std::unordered_map<uint32_t, bool>;
199 ClassFullT isClassFull;
200
201 // pick a random class and allocate from that until the memory is full. If
202 // an allocation class fails allocation, it should not succeed any later.
203 while (mp.getCurrentAllocSize() < maxSize) {
204 const unsigned int randomClass =
205 folly::Random::rand32() % allocSizes.size();
206 const auto classSize = *(std::next(allocSizes.begin(), randomClass));
207 void* memory = mp.allocate(classSize);
208 if (memory != nullptr) {
209 poolAllocs[classSize].push_back(memory);
210 const auto it = isClassFull.find(classSize);
211 if (it != isClassFull.end()) {
212 ASSERT_FALSE(isClassFull[classSize]);
213 }
214 } else {
215 const auto it = isClassFull.find(classSize);
216 if (it == isClassFull.end()) {
217 isClassFull[classSize] = true;
218 } else {
219 ASSERT_TRUE(isClassFull[classSize]);
220 }
221 }
222 }
223}
224
225TEST_F(MemoryPoolTest, Free) {
226 auto slabAlloc = createSlabAllocator(35);

Callers 1

TEST_FFunction · 0.85

Calls 7

getPoolSizeMethod · 0.80
getCurrentAllocSizeMethod · 0.80
sizeMethod · 0.45
beginMethod · 0.45
allocateMethod · 0.45
findMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected