|
17 | 17 |
|
18 | 18 | #include <algorithm> |
19 | 19 | #include <cstdint> |
| 20 | +#include <memory> |
20 | 21 |
|
21 | 22 | #include <gtest/gtest.h> |
22 | 23 |
|
@@ -290,4 +291,93 @@ TEST(Jemalloc, GetAllocationStats) { |
290 | 291 | #endif |
291 | 292 | } |
292 | 293 |
|
| 294 | +class TestCappedMemoryPool : public ::arrow::TestMemoryPoolBase { |
| 295 | + public: |
| 296 | + MemoryPool* memory_pool() override { return InitPool(/*limit=*/1'000'000'000LL); } |
| 297 | + |
| 298 | + MemoryPool* InitPool(int64_t limit) { |
| 299 | + proxy_memory_pool_ = std::make_shared<ProxyMemoryPool>(default_memory_pool()); |
| 300 | + capped_memory_pool_ = |
| 301 | + std::make_shared<CappedMemoryPool>(proxy_memory_pool_.get(), limit); |
| 302 | + return capped_memory_pool_.get(); |
| 303 | + } |
| 304 | + |
| 305 | + protected: |
| 306 | + std::shared_ptr<MemoryPool> proxy_memory_pool_; |
| 307 | + std::shared_ptr<CappedMemoryPool> capped_memory_pool_; |
| 308 | +}; |
| 309 | + |
| 310 | +TEST_F(TestCappedMemoryPool, MemoryTracking) { this->TestMemoryTracking(); } |
| 311 | + |
| 312 | +TEST_F(TestCappedMemoryPool, OOM) { |
| 313 | + // CappedMemoryPool rejects the huge allocation without hitting the underlying |
| 314 | + // allocator, so this should work even under Address Sanitizer. |
| 315 | + this->TestOOM(); |
| 316 | +} |
| 317 | + |
| 318 | +TEST_F(TestCappedMemoryPool, Reallocate) { this->TestReallocate(); } |
| 319 | + |
| 320 | +TEST_F(TestCappedMemoryPool, Alignment) { this->TestAlignment(); } |
| 321 | + |
| 322 | +TEST_F(TestCappedMemoryPool, AllocateLimit) { |
| 323 | + auto pool = InitPool(/*limit=*/1000); |
| 324 | + |
| 325 | + uint8_t* data1; |
| 326 | + uint8_t* data2; |
| 327 | + ASSERT_OK(pool->Allocate(600, &data1)); |
| 328 | + ASSERT_EQ(600, pool->bytes_allocated()); |
| 329 | + ASSERT_EQ(600, pool->total_bytes_allocated()); |
| 330 | + ASSERT_EQ(600, pool->max_memory()); |
| 331 | + |
| 332 | + ASSERT_OK(pool->Allocate(400, &data2)); |
| 333 | + ASSERT_EQ(1000, pool->bytes_allocated()); |
| 334 | + ASSERT_EQ(1000, pool->total_bytes_allocated()); |
| 335 | + ASSERT_EQ(1000, pool->max_memory()); |
| 336 | + pool->Free(data2, 400); |
| 337 | + ASSERT_EQ(600, pool->bytes_allocated()); |
| 338 | + ASSERT_EQ(1000, pool->total_bytes_allocated()); |
| 339 | + ASSERT_EQ(1000, pool->max_memory()); |
| 340 | + |
| 341 | + ASSERT_OK(pool->Allocate(300, &data2)); |
| 342 | + ASSERT_EQ(900, pool->bytes_allocated()); |
| 343 | + ASSERT_EQ(1300, pool->total_bytes_allocated()); |
| 344 | + ASSERT_EQ(1000, pool->max_memory()); |
| 345 | + pool->Free(data2, 300); |
| 346 | + ASSERT_EQ(600, pool->bytes_allocated()); |
| 347 | + ASSERT_EQ(1300, pool->total_bytes_allocated()); |
| 348 | + ASSERT_EQ(1000, pool->max_memory()); |
| 349 | + |
| 350 | + ASSERT_RAISES(OutOfMemory, pool->Allocate(401, &data2)); |
| 351 | + ASSERT_EQ(600, pool->bytes_allocated()); |
| 352 | + ASSERT_EQ(1300, pool->total_bytes_allocated()); |
| 353 | + ASSERT_EQ(1000, pool->max_memory()); |
| 354 | + |
| 355 | + pool->Free(data1, 600); |
| 356 | +} |
| 357 | + |
| 358 | +TEST_F(TestCappedMemoryPool, ReallocateLimit) { |
| 359 | + auto pool = InitPool(/*limit=*/1000); |
| 360 | + |
| 361 | + uint8_t* data1; |
| 362 | + uint8_t* data2; |
| 363 | + ASSERT_OK(pool->Allocate(600, &data1)); |
| 364 | + ASSERT_OK(pool->Allocate(400, &data2)); |
| 365 | + ASSERT_EQ(1000, pool->bytes_allocated()); |
| 366 | + ASSERT_EQ(1000, pool->total_bytes_allocated()); |
| 367 | + ASSERT_EQ(1000, pool->max_memory()); |
| 368 | + |
| 369 | + ASSERT_OK(pool->Reallocate(400, 300, &data2)); |
| 370 | + ASSERT_EQ(900, pool->bytes_allocated()); |
| 371 | + ASSERT_EQ(1000, pool->total_bytes_allocated()); |
| 372 | + ASSERT_EQ(1000, pool->max_memory()); |
| 373 | + |
| 374 | + ASSERT_RAISES(OutOfMemory, pool->Reallocate(300, 401, &data2)); |
| 375 | + ASSERT_EQ(900, pool->bytes_allocated()); |
| 376 | + ASSERT_EQ(1000, pool->total_bytes_allocated()); |
| 377 | + ASSERT_EQ(1000, pool->max_memory()); |
| 378 | + |
| 379 | + pool->Free(data1, 600); |
| 380 | + pool->Free(data2, 300); |
| 381 | +} |
| 382 | + |
293 | 383 | } // namespace arrow |
0 commit comments