diff --git a/src/internal_modules/roc_core/limited_pool.h b/src/internal_modules/roc_core/limited_pool.h index 8d96d95f9..f437cf1aa 100644 --- a/src/internal_modules/roc_core/limited_pool.h +++ b/src/internal_modules/roc_core/limited_pool.h @@ -28,8 +28,7 @@ class LimitedPool : public NonCopyable, public IPool { //! Get size of object allocated by pool. size_t object_size() const; - //! Reserve memory for given number of objects, after checking with the memory - //! limiter. + //! Reserve memory for given number of objects. //! @returns //! false if allocation failed. ROC_ATTR_NODISCARD bool reserve(size_t n_objects); diff --git a/src/tests/roc_core/test_limited_arena.cpp b/src/tests/roc_core/test_limited_arena.cpp index 1805473b2..bbadf1af7 100644 --- a/src/tests/roc_core/test_limited_arena.cpp +++ b/src/tests/roc_core/test_limited_arena.cpp @@ -51,5 +51,22 @@ TEST(limited_arena, enforce_limit) { } } +TEST(limited_arena, size_of_and_overhead) { + HeapArena heapArena; + MemoryLimiter memoryLimiter("test", 256); + + { + LimitedArena arena(heapArena, memoryLimiter); + + void* pointer0 = arena.allocate(128); + CHECK(pointer0); + + CHECK(arena.size_of(pointer0) == 128); + + arena.deallocate(pointer0); + + CHECK(arena.overhead() > 0); + } +} } // namespace core } // namespace roc diff --git a/src/tests/roc_core/test_limited_pool.cpp b/src/tests/roc_core/test_limited_pool.cpp index 9a390434f..772b5e10d 100644 --- a/src/tests/roc_core/test_limited_pool.cpp +++ b/src/tests/roc_core/test_limited_pool.cpp @@ -38,7 +38,33 @@ TEST(limited_pool, enforce_limit) { void* memory0 = pool.allocate(); CHECK(memory0); + CHECK(memoryLimiter.acquired() > 128); + + void* memory1 = pool.allocate(); + CHECK(memory1 == NULL); + + pool.deallocate(memory0); + + memory1 = pool.allocate(); + CHECK(memory1); + + pool.deallocate(memory1); + CHECK(memoryLimiter.acquired() == 0); + } +} + +TEST(limited_pool, enforce_limit_despite_reserve) { + HeapArena arena; + SlabPool slabPool("test", arena); + MemoryLimiter memoryLimiter("test", 256); + + { + LimitedPool pool(slabPool, memoryLimiter); + CHECK(pool.reserve(5)); + + void* memory0 = pool.allocate(); + CHECK(memory0); CHECK(memoryLimiter.acquired() > 128); void* memory1 = pool.allocate(); @@ -55,5 +81,40 @@ TEST(limited_pool, enforce_limit) { } } +TEST(limited_pool, track_but_no_enforce_limit) { + HeapArena arena; + SlabPool slabPool("test", arena); + MemoryLimiter memoryLimiter("test", 0); + + { + LimitedPool pool(slabPool, memoryLimiter); + + void* memory0 = pool.allocate(); + CHECK(memory0); + CHECK(memoryLimiter.acquired() > 128); + + void* memory1 = pool.allocate(); + CHECK(memory1); + CHECK(memoryLimiter.acquired() > 256); + + pool.deallocate(memory0); + pool.deallocate(memory1); + CHECK(memoryLimiter.acquired() == 0); + } +} + +TEST(limited_pool, object_size_and_overhead) { + HeapArena arena; + SlabPool slabPool("test", arena); + MemoryLimiter memoryLimiter("test", 256); + + { + LimitedPool pool(slabPool, memoryLimiter); + + CHECK(pool.object_size() == 128); + CHECK(pool.overhead() > 0); + } +} + } // namespace core } // namespace roc