Basic test to make sure that we can make multiple suballocations of the same size while using the expected number of buffers.
| 131 | /// Basic test to make sure that we can make multiple suballocations of the same size |
| 132 | /// while using the expected number of buffers. |
| 133 | TEST_F(SuballocatorTest, SameSizeAllocations) { |
| 134 | const int64_t TOTAL_MEM = TEST_BUFFER_LEN * 100; |
| 135 | InitPool(TEST_BUFFER_LEN, TOTAL_MEM); |
| 136 | BufferPool::ClientHandle* client; |
| 137 | RegisterClient(&global_reservation_, &client); |
| 138 | Suballocator allocator(buffer_pool(), client, TEST_BUFFER_LEN); |
| 139 | vector<unique_ptr<Suballocation>> allocs; |
| 140 | |
| 141 | // Make suballocations smaller than the buffer size. |
| 142 | const int64_t ALLOC_SIZE = TEST_BUFFER_LEN / 4; |
| 143 | int64_t allocated_mem = 0; |
| 144 | while (allocated_mem < TOTAL_MEM) { |
| 145 | allocs.emplace_back(); |
| 146 | ASSERT_OK(allocator.Allocate(ALLOC_SIZE, &allocs.back())); |
| 147 | ASSERT_TRUE(allocs.back() != nullptr) << ALLOC_SIZE << " " << allocated_mem << " " |
| 148 | << global_reservation_.DebugString(); |
| 149 | allocated_mem += ALLOC_SIZE; |
| 150 | } |
| 151 | |
| 152 | // Attempts to allocate more memory should fail gracefully. |
| 153 | const int64_t MAX_ALLOC_SIZE = 1L << 24; |
| 154 | for (int alloc_size = 1; alloc_size <= MAX_ALLOC_SIZE; alloc_size *= 2) { |
| 155 | unique_ptr<Suballocation> failed_alloc; |
| 156 | ASSERT_OK(allocator.Allocate(alloc_size, &failed_alloc)); |
| 157 | ASSERT_TRUE(failed_alloc == nullptr) << alloc_size << " " << allocated_mem << " " |
| 158 | << global_reservation_.DebugString(); |
| 159 | } |
| 160 | AssertMemoryValid(allocs); |
| 161 | |
| 162 | // Check that reservation usage matches the amount allocated. |
| 163 | EXPECT_EQ(client->GetUsedReservation(), allocated_mem) |
| 164 | << global_reservation_.DebugString(); |
| 165 | FreeAllocations(&allocator, &allocs); |
| 166 | ExpectReservationUnused(client); |
| 167 | } |
| 168 | |
| 169 | /// Check behaviour of zero-length allocation. |
| 170 | TEST_F(SuballocatorTest, ZeroLengthAllocation) { |
nothing calls this directly
no test coverage detected