mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-05-20 12:09:06 +08:00
LimitedPoolAllocator (for TX queue)
This commit is contained in:
parent
74b62cc3a9
commit
5808bfc0c9
@ -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);
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
/*
|
||||
|
||||
36
libuavcan/src/uc_dynamic_memory.cpp
Normal file
36
libuavcan/src/uc_dynamic_memory.cpp
Normal 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_--;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user