| 44 | const int MemPoolTest::MAX_CHUNK_SIZE; |
| 45 | |
| 46 | TEST(MemPoolTest, Basic) { |
| 47 | MemTracker tracker; |
| 48 | MemPool p(&tracker); |
| 49 | MemPool p2(&tracker); |
| 50 | MemPool p3(&tracker); |
| 51 | |
| 52 | uint8_t* ptr = NULL; |
| 53 | |
| 54 | for (int iter = 0; iter < 2; ++iter) { |
| 55 | // Allocate 768 25 + 7 byte allocations. |
| 56 | for (int i = 0; i < 768; ++i) { |
| 57 | ptr = p.Allocate(25); |
| 58 | EXPECT_TRUE(ptr != NULL); |
| 59 | EXPECT_EQ(0, reinterpret_cast<uintptr_t>(ptr) % MemPool::DEFAULT_ALIGNMENT) |
| 60 | << "Allocation should be aligned"; |
| 61 | |
| 62 | // Allocate the padding to get 32 bytes |
| 63 | ptr = p.TryAllocateAligned(7, 1); |
| 64 | EXPECT_TRUE(ptr != NULL); |
| 65 | } |
| 66 | // We allocated 24K from 28K of chunks (4, 8, 16) |
| 67 | EXPECT_EQ(24 * 1024, p.total_allocated_bytes()); |
| 68 | EXPECT_EQ(28 * 1024, p.GetTotalChunkSizes()); |
| 69 | |
| 70 | // we're passing on the first two chunks, containing 12K of data; we're left with |
| 71 | // one chunk of 16K containing 12K of data |
| 72 | p2.AcquireData(&p, true); |
| 73 | EXPECT_EQ(12 * 1024, p.total_allocated_bytes()); |
| 74 | EXPECT_EQ(16 * 1024, p.GetTotalChunkSizes()); |
| 75 | |
| 76 | // we allocate 8K, for which there isn't enough room in the current chunk, |
| 77 | // so another one is allocated (32K) |
| 78 | p.Allocate(8 * 1024); |
| 79 | EXPECT_EQ((16 + 32) * 1024, p.GetTotalChunkSizes()); |
| 80 | |
| 81 | // we allocate 65K, which doesn't fit into the current chunk or the default |
| 82 | // size of the next allocated chunk (64K) |
| 83 | p.Allocate(65 * 1024); |
| 84 | EXPECT_EQ((12 + 8 + 65) * 1024, p.total_allocated_bytes()); |
| 85 | EXPECT_EQ((16 + 32 + 65) * 1024, p.GetTotalChunkSizes()); |
| 86 | |
| 87 | // Clear() resets allocated data, but doesn't remove any chunks |
| 88 | p.Clear(); |
| 89 | EXPECT_EQ(0, p.total_allocated_bytes()); |
| 90 | EXPECT_EQ((16 + 32 + 65) * 1024, p.GetTotalChunkSizes()); |
| 91 | |
| 92 | // next allocation reuses existing chunks |
| 93 | p.Allocate(1024); |
| 94 | EXPECT_EQ(1024, p.total_allocated_bytes()); |
| 95 | EXPECT_EQ((16 + 32 + 65) * 1024, p.GetTotalChunkSizes()); |
| 96 | |
| 97 | // ... unless it doesn't fit into any available chunk |
| 98 | p.Allocate(120 * 1024); |
| 99 | EXPECT_EQ((1 + 120) * 1024, p.total_allocated_bytes()); |
| 100 | EXPECT_EQ((130 + 16 + 32 + 65) * 1024, p.GetTotalChunkSizes()); |
| 101 | |
| 102 | // ... Try another chunk that fits into an existing chunk |
| 103 | p.Allocate(33 * 1024); |
nothing calls this directly
no test coverage detected