From ef93f1b1e8bc4294e01ffe402417e93bfbc05a7c Mon Sep 17 00:00:00 2001 From: Pavel Kirienko Date: Thu, 15 Oct 2015 10:50:42 +0300 Subject: [PATCH] Heap based pool allocator - configurable getNumBlocks() --- .../uavcan/helpers/heap_based_pool_allocator.hpp | 11 ++++++++--- libuavcan/test/helpers/heap_based_pool_allocator.cpp | 8 ++++++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/libuavcan/include/uavcan/helpers/heap_based_pool_allocator.hpp b/libuavcan/include/uavcan/helpers/heap_based_pool_allocator.hpp index a012ee2c9a..566e21f2d3 100644 --- a/libuavcan/include/uavcan/helpers/heap_based_pool_allocator.hpp +++ b/libuavcan/include/uavcan/helpers/heap_based_pool_allocator.hpp @@ -49,6 +49,7 @@ class UAVCAN_EXPORT HeapBasedPoolAllocator : public IPoolAllocator, Noncopyable }; Node* volatile cache_; + uint16_t reported_num_blocks_; Node* popCache() { @@ -73,7 +74,10 @@ public: /** * The allocator initializes with empty cache, so first allocations will be served from heap. */ - HeapBasedPoolAllocator() : cache_(NULL) { } + HeapBasedPoolAllocator(uint16_t reported_num_blocks) : + cache_(NULL), + reported_num_blocks_(reported_num_blocks) + { } /** * The destructor de-allocates all blocks that are currently in the cache. @@ -121,9 +125,10 @@ public: } /** - * Heap-based pool is virutally infinite in size, so this method just returns maximum possible number of blocks. + * Heap-based pool is virutally infinite in size, so this method just returns some pre-defined value. */ - virtual uint16_t getNumBlocks() const { return NumericTraits::max(); } + virtual uint16_t getNumBlocks() const { return reported_num_blocks_; } + void setReportedNumBlocks(uint16_t x) { reported_num_blocks_ = x; } /** * Frees all blocks that are not in use at the moment. diff --git a/libuavcan/test/helpers/heap_based_pool_allocator.cpp b/libuavcan/test/helpers/heap_based_pool_allocator.cpp index 61daf378d8..ae250be287 100644 --- a/libuavcan/test/helpers/heap_based_pool_allocator.cpp +++ b/libuavcan/test/helpers/heap_based_pool_allocator.cpp @@ -16,10 +16,14 @@ TEST(HeapBasedPoolAllocator, Basic) std::cout << ">>> HEAP BEFORE:" << std::endl; malloc_stats(); - uavcan::HeapBasedPoolAllocator al; + uavcan::HeapBasedPoolAllocator al(64); ASSERT_EQ(0, al.getNumCachedBlocks()); + ASSERT_EQ(64, al.getNumBlocks()); + al.setReportedNumBlocks(123); + ASSERT_EQ(123, al.getNumBlocks()); + void* a = al.allocate(10); void* b = al.allocate(10); void* c = al.allocate(10); @@ -77,7 +81,7 @@ TEST(HeapBasedPoolAllocator, Concurrency) std::cout << ">>> HEAP BEFORE:" << std::endl; malloc_stats(); - uavcan::HeapBasedPoolAllocator al; + uavcan::HeapBasedPoolAllocator al(1); volatile bool terminate = false;