LimitedPoolAllocator (for TX queue)

This commit is contained in:
Pavel Kirienko 2014-04-12 13:40:15 +04:00
parent 74b62cc3a9
commit 5808bfc0c9
3 changed files with 80 additions and 0 deletions

View File

@ -97,6 +97,26 @@ public:
int getNumUsedBlocks() const { return NumBlocks - getNumFreeBlocks(); }
};
class LimitedPoolAllocator : public IAllocator
{
IAllocator& allocator_;
const std::size_t max_blocks_;
std::size_t used_blocks_;
public:
LimitedPoolAllocator(IAllocator& allocator, std::size_t max_blocks)
: allocator_(allocator)
, max_blocks_(max_blocks)
, used_blocks_(0)
{
assert(max_blocks_ > 0);
}
void* allocate(std::size_t size);
void deallocate(const void* ptr);
};
// ----------------------------------------------------------------------------
/*

View File

@ -0,0 +1,36 @@
/*
* Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
*/
#include <uavcan/dynamic_memory.hpp>
namespace uavcan
{
/*
* LimitedPoolAllocator
*/
void* LimitedPoolAllocator::allocate(std::size_t size)
{
if (used_blocks_ < max_blocks_)
{
used_blocks_++;
return allocator_.allocate(size);
}
else
{
return NULL;
}
}
void LimitedPoolAllocator::deallocate(const void* ptr)
{
allocator_.deallocate(ptr);
assert(used_blocks_ > 0);
if (used_blocks_ > 0)
{
used_blocks_--;
}
}
}

View File

@ -92,3 +92,27 @@ TEST(DynamicMemory, OutOfMemory)
EXPECT_EQ(1, pool32.getNumUsedBlocks());
EXPECT_EQ(1, pool64.getNumUsedBlocks()); // Make sure it was properly deallocated
}
TEST(DynamicMemory, LimitedPoolAllocator)
{
uavcan::PoolAllocator<128, 32> pool32;
uavcan::LimitedPoolAllocator lim(pool32, 2);
const void* ptr1 = lim.allocate(1);
const void* ptr2 = lim.allocate(1);
const void* ptr3 = lim.allocate(1);
EXPECT_TRUE(ptr1);
EXPECT_TRUE(ptr2);
EXPECT_FALSE(ptr3);
lim.deallocate(ptr2);
const void* ptr4 = lim.allocate(1);
lim.deallocate(ptr1);
const void* ptr5 = lim.allocate(1);
const void* ptr6 = lim.allocate(1);
EXPECT_TRUE(ptr4);
EXPECT_TRUE(ptr5);
EXPECT_FALSE(ptr6);
}