mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-05-01 10:34:06 +08:00
PoolManager<> removed (was useless) (#33)
This commit is contained in:
parent
95091ab26c
commit
00319909c1
@ -26,51 +26,9 @@ public:
|
||||
virtual void* allocate(std::size_t size) = 0;
|
||||
virtual void deallocate(const void* ptr) = 0;
|
||||
|
||||
virtual bool isInPool(const void* ptr) const = 0;
|
||||
|
||||
virtual std::size_t getBlockSize() const = 0;
|
||||
virtual std::size_t getNumBlocks() const = 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* Pool manager contains multiple pool allocators of different block sizes and
|
||||
* finds the most suitable allocator for every allocation request.
|
||||
*/
|
||||
template <unsigned MaxPools>
|
||||
class UAVCAN_EXPORT PoolManager : public IPoolAllocator, Noncopyable
|
||||
{
|
||||
IPoolAllocator* pools_[MaxPools];
|
||||
|
||||
static int qsortComparePoolAllocators(const void* raw_a, const void* raw_b)
|
||||
{
|
||||
const IPoolAllocator* const a = *static_cast<const IPoolAllocator* const*>(raw_a);
|
||||
const IPoolAllocator* const b = *static_cast<const IPoolAllocator* const*>(raw_b);
|
||||
const std::size_t a_size = a ? a->getBlockSize() : NumericTraits<std::size_t>::max();
|
||||
const std::size_t b_size = b ? b->getBlockSize() : NumericTraits<std::size_t>::max();
|
||||
if (a_size != b_size)
|
||||
{
|
||||
return (a_size > b_size) ? 1 : -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public:
|
||||
PoolManager()
|
||||
{
|
||||
(void)std::memset(pools_, 0, sizeof(pools_));
|
||||
}
|
||||
|
||||
bool addPool(IPoolAllocator* pool);
|
||||
|
||||
virtual void* allocate(std::size_t size);
|
||||
virtual void deallocate(const void* ptr);
|
||||
|
||||
virtual bool isInPool(const void* ptr) const;
|
||||
|
||||
virtual std::size_t getBlockSize() const;
|
||||
virtual std::size_t getNumBlocks() const;
|
||||
};
|
||||
|
||||
/**
|
||||
* Classic implementation of a pool allocator (Meyers).
|
||||
*/
|
||||
@ -100,9 +58,6 @@ public:
|
||||
virtual void* allocate(std::size_t size);
|
||||
virtual void deallocate(const void* ptr);
|
||||
|
||||
virtual bool isInPool(const void* ptr) const;
|
||||
|
||||
virtual std::size_t getBlockSize() const { return BlockSize; }
|
||||
virtual std::size_t getNumBlocks() const { return NumBlocks; }
|
||||
|
||||
unsigned getNumFreeBlocks() const;
|
||||
@ -130,111 +85,11 @@ public:
|
||||
virtual void* allocate(std::size_t size);
|
||||
virtual void deallocate(const void* ptr);
|
||||
|
||||
virtual bool isInPool(const void* ptr) const;
|
||||
|
||||
virtual std::size_t getBlockSize() const;
|
||||
virtual std::size_t getNumBlocks() const;
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
/*
|
||||
* PoolManager<>
|
||||
*/
|
||||
template <unsigned MaxPools>
|
||||
bool PoolManager<MaxPools>::addPool(IPoolAllocator* pool)
|
||||
{
|
||||
UAVCAN_ASSERT(pool);
|
||||
bool retval = false;
|
||||
for (unsigned i = 0; i < MaxPools; i++)
|
||||
{
|
||||
UAVCAN_ASSERT(pools_[i] != pool);
|
||||
if (pools_[i] == NULL || pools_[i] == pool)
|
||||
{
|
||||
pools_[i] = pool;
|
||||
retval = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// We need to keep the pools in order, so that smallest blocks go first
|
||||
qsort(pools_, MaxPools, sizeof(IPoolAllocator*), &PoolManager::qsortComparePoolAllocators);
|
||||
return retval;
|
||||
}
|
||||
|
||||
template <unsigned MaxPools>
|
||||
void* PoolManager<MaxPools>::allocate(std::size_t size)
|
||||
{
|
||||
for (unsigned i = 0; i < MaxPools; i++)
|
||||
{
|
||||
if (pools_[i] == NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
void* const pmem = pools_[i]->allocate(size);
|
||||
if (pmem != NULL)
|
||||
{
|
||||
return pmem;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
template <unsigned MaxPools>
|
||||
void PoolManager<MaxPools>::deallocate(const void* ptr)
|
||||
{
|
||||
for (unsigned i = 0; i < MaxPools; i++)
|
||||
{
|
||||
if (pools_[i] == NULL)
|
||||
{
|
||||
UAVCAN_ASSERT(0);
|
||||
break;
|
||||
}
|
||||
if (pools_[i]->isInPool(ptr))
|
||||
{
|
||||
pools_[i]->deallocate(ptr);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <unsigned MaxPools>
|
||||
bool PoolManager<MaxPools>::isInPool(const void* ptr) const
|
||||
{
|
||||
for (unsigned i = 0; i < MaxPools; i++)
|
||||
{
|
||||
if (pools_[i] == NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
if (pools_[i]->isInPool(ptr))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
template <unsigned MaxPools>
|
||||
std::size_t PoolManager<MaxPools>::getBlockSize() const
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
template <unsigned MaxPools>
|
||||
std::size_t PoolManager<MaxPools>::getNumBlocks() const
|
||||
{
|
||||
std::size_t ret = 0;
|
||||
for (unsigned i = 0; i < MaxPools; i++)
|
||||
{
|
||||
if (pools_[i] == NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
ret += pools_[i]->getNumBlocks();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* PoolAllocator<>
|
||||
*/
|
||||
@ -275,13 +130,6 @@ void PoolAllocator<PoolSize, BlockSize>::deallocate(const void* ptr)
|
||||
free_list_ = p;
|
||||
}
|
||||
|
||||
template <std::size_t PoolSize, std::size_t BlockSize>
|
||||
bool PoolAllocator<PoolSize, BlockSize>::isInPool(const void* ptr) const
|
||||
{
|
||||
return ptr >= pool_.bytes &&
|
||||
ptr < (pool_.bytes + PoolSize);
|
||||
}
|
||||
|
||||
template <std::size_t PoolSize, std::size_t BlockSize>
|
||||
unsigned PoolAllocator<PoolSize, BlockSize>::getNumFreeBlocks() const
|
||||
{
|
||||
|
||||
@ -33,16 +33,6 @@ void LimitedPoolAllocator::deallocate(const void* ptr)
|
||||
}
|
||||
}
|
||||
|
||||
bool LimitedPoolAllocator::isInPool(const void* ptr) const
|
||||
{
|
||||
return allocator_.isInPool(ptr);
|
||||
}
|
||||
|
||||
std::size_t LimitedPoolAllocator::getBlockSize() const
|
||||
{
|
||||
return allocator_.getBlockSize();
|
||||
}
|
||||
|
||||
std::size_t LimitedPoolAllocator::getNumBlocks() const
|
||||
{
|
||||
return min(max_blocks_, allocator_.getNumBlocks());
|
||||
|
||||
@ -8,91 +8,34 @@
|
||||
TEST(DynamicMemory, Basic)
|
||||
{
|
||||
uavcan::PoolAllocator<128, 32> pool32;
|
||||
uavcan::PoolAllocator<256, 64> pool64;
|
||||
uavcan::PoolAllocator<512, 128> pool128;
|
||||
|
||||
EXPECT_EQ(4, pool32.getNumFreeBlocks());
|
||||
EXPECT_EQ(4, pool64.getNumFreeBlocks());
|
||||
EXPECT_EQ(4, pool128.getNumFreeBlocks());
|
||||
|
||||
uavcan::PoolManager<2> poolmgr;
|
||||
EXPECT_TRUE(poolmgr.addPool(&pool64)); // Order of insertion shall not matter
|
||||
EXPECT_TRUE(poolmgr.addPool(&pool32));
|
||||
EXPECT_FALSE(poolmgr.addPool(&pool128));
|
||||
|
||||
EXPECT_EQ(4 * 2, poolmgr.getNumBlocks());
|
||||
|
||||
const void* ptr1 = poolmgr.allocate(16);
|
||||
EXPECT_TRUE(ptr1);
|
||||
|
||||
const void* ptr2 = poolmgr.allocate(32);
|
||||
EXPECT_TRUE(ptr2);
|
||||
|
||||
const void* ptr3 = poolmgr.allocate(64);
|
||||
EXPECT_TRUE(ptr3);
|
||||
|
||||
EXPECT_FALSE(poolmgr.allocate(120));
|
||||
|
||||
EXPECT_EQ(2, pool32.getNumUsedBlocks());
|
||||
EXPECT_EQ(1, pool64.getNumUsedBlocks());
|
||||
EXPECT_EQ(0, pool128.getNumUsedBlocks());
|
||||
|
||||
poolmgr.deallocate(ptr1);
|
||||
const void* ptr1 = pool32.allocate(16);
|
||||
ASSERT_TRUE(ptr1);
|
||||
EXPECT_EQ(1, pool32.getNumUsedBlocks());
|
||||
poolmgr.deallocate(ptr2);
|
||||
EXPECT_FALSE(pool32.allocate(120));
|
||||
EXPECT_EQ(1, pool32.getNumUsedBlocks());
|
||||
pool32.deallocate(ptr1);
|
||||
EXPECT_EQ(0, pool32.getNumUsedBlocks());
|
||||
EXPECT_EQ(1, pool64.getNumUsedBlocks());
|
||||
poolmgr.deallocate(ptr3);
|
||||
EXPECT_EQ(0, pool64.getNumUsedBlocks());
|
||||
EXPECT_EQ(0, pool128.getNumUsedBlocks());
|
||||
}
|
||||
|
||||
TEST(DynamicMemory, OutOfMemory)
|
||||
{
|
||||
uavcan::PoolAllocator<64, 32> pool32;
|
||||
uavcan::PoolAllocator<128, 64> pool64;
|
||||
|
||||
EXPECT_EQ(2, pool32.getNumFreeBlocks());
|
||||
EXPECT_EQ(2, pool64.getNumFreeBlocks());
|
||||
EXPECT_EQ(0, pool32.getNumUsedBlocks());
|
||||
|
||||
uavcan::PoolManager<4> poolmgr;
|
||||
EXPECT_TRUE(poolmgr.addPool(&pool64));
|
||||
EXPECT_TRUE(poolmgr.addPool(&pool32));
|
||||
|
||||
const void* ptr1 = poolmgr.allocate(32);
|
||||
EXPECT_TRUE(ptr1);
|
||||
EXPECT_TRUE(pool32.isInPool(ptr1));
|
||||
EXPECT_FALSE(pool64.isInPool(ptr1));
|
||||
|
||||
const void* ptr2 = poolmgr.allocate(32);
|
||||
EXPECT_TRUE(ptr2);
|
||||
EXPECT_TRUE(pool32.isInPool(ptr2));
|
||||
EXPECT_FALSE(pool64.isInPool(ptr2));
|
||||
|
||||
const void* ptr3 = poolmgr.allocate(32);
|
||||
EXPECT_TRUE(ptr3);
|
||||
EXPECT_FALSE(pool32.isInPool(ptr3));
|
||||
EXPECT_TRUE(pool64.isInPool(ptr3)); // One block went to the next pool
|
||||
const void* ptr1 = pool32.allocate(32);
|
||||
ASSERT_TRUE(ptr1);
|
||||
EXPECT_EQ(1, pool32.getNumUsedBlocks());
|
||||
|
||||
const void* ptr2 = pool32.allocate(32);
|
||||
ASSERT_TRUE(ptr2);
|
||||
EXPECT_EQ(2, pool32.getNumUsedBlocks());
|
||||
EXPECT_EQ(1, pool64.getNumUsedBlocks());
|
||||
|
||||
poolmgr.deallocate(ptr2);
|
||||
EXPECT_EQ(1, pool32.getNumUsedBlocks());
|
||||
EXPECT_EQ(1, pool64.getNumUsedBlocks());
|
||||
|
||||
const void* ptr4 = poolmgr.allocate(64);
|
||||
EXPECT_TRUE(ptr4);
|
||||
EXPECT_EQ(1, pool32.getNumUsedBlocks());
|
||||
EXPECT_EQ(2, pool64.getNumUsedBlocks()); // Top pool is 100% used
|
||||
|
||||
EXPECT_FALSE(poolmgr.allocate(64)); // No free blocks left --> NULL
|
||||
EXPECT_EQ(1, pool32.getNumUsedBlocks());
|
||||
EXPECT_EQ(2, pool64.getNumUsedBlocks());
|
||||
|
||||
poolmgr.deallocate(ptr3); // This was small chunk allocated in big pool
|
||||
EXPECT_EQ(1, pool32.getNumUsedBlocks());
|
||||
EXPECT_EQ(1, pool64.getNumUsedBlocks()); // Make sure it was properly deallocated
|
||||
ASSERT_FALSE(pool32.allocate(32)); // No free blocks left --> NULL
|
||||
EXPECT_EQ(2, pool32.getNumUsedBlocks());
|
||||
EXPECT_EQ(0, pool32.getNumFreeBlocks());
|
||||
}
|
||||
|
||||
TEST(DynamicMemory, LimitedPoolAllocator)
|
||||
|
||||
@ -19,17 +19,15 @@
|
||||
struct TestNode : public uavcan::INode
|
||||
{
|
||||
uavcan::PoolAllocator<uavcan::MemPoolBlockSize * 100, uavcan::MemPoolBlockSize> pool;
|
||||
uavcan::PoolManager<1> poolmgr;
|
||||
uavcan::OutgoingTransferRegistry<8> otr;
|
||||
uavcan::Scheduler scheduler;
|
||||
uint64_t internal_failure_count;
|
||||
|
||||
TestNode(uavcan::ICanDriver& can_driver, uavcan::ISystemClock& clock_driver, uavcan::NodeID self_node_id)
|
||||
: otr(poolmgr)
|
||||
, scheduler(can_driver, poolmgr, clock_driver, otr)
|
||||
: otr(pool)
|
||||
, scheduler(can_driver, pool, clock_driver, otr)
|
||||
, internal_failure_count(0)
|
||||
{
|
||||
poolmgr.addPool(&pool);
|
||||
setNodeID(self_node_id);
|
||||
}
|
||||
|
||||
@ -39,7 +37,7 @@ struct TestNode : public uavcan::INode
|
||||
internal_failure_count++;
|
||||
}
|
||||
|
||||
virtual uavcan::PoolManager<1>& getAllocator() { return poolmgr; }
|
||||
virtual uavcan::IPoolAllocator& getAllocator() { return pool; }
|
||||
virtual uavcan::Scheduler& getScheduler() { return scheduler; }
|
||||
virtual const uavcan::Scheduler& getScheduler() const { return scheduler; }
|
||||
};
|
||||
|
||||
@ -23,15 +23,13 @@ TEST(CanIOManager, Reception)
|
||||
{
|
||||
// Memory
|
||||
uavcan::PoolAllocator<sizeof(uavcan::CanTxQueue::Entry) * 4, sizeof(uavcan::CanTxQueue::Entry)> pool;
|
||||
uavcan::PoolManager<2> poolmgr;
|
||||
poolmgr.addPool(&pool);
|
||||
|
||||
// Platform interface
|
||||
SystemClockMock clockmock;
|
||||
CanDriverMock driver(2, clockmock);
|
||||
|
||||
// IO Manager
|
||||
uavcan::CanIOManager iomgr(driver, poolmgr, clockmock);
|
||||
uavcan::CanIOManager iomgr(driver, pool, clockmock);
|
||||
ASSERT_EQ(2, iomgr.getNumIfaces());
|
||||
|
||||
/*
|
||||
@ -120,15 +118,13 @@ TEST(CanIOManager, Transmission)
|
||||
|
||||
// Memory
|
||||
uavcan::PoolAllocator<sizeof(CanTxQueue::Entry) * 4, sizeof(CanTxQueue::Entry)> pool;
|
||||
uavcan::PoolManager<2> poolmgr;
|
||||
poolmgr.addPool(&pool);
|
||||
|
||||
// Platform interface
|
||||
SystemClockMock clockmock;
|
||||
CanDriverMock driver(2, clockmock);
|
||||
|
||||
// IO Manager
|
||||
CanIOManager iomgr(driver, poolmgr, clockmock, 9999);
|
||||
CanIOManager iomgr(driver, pool, clockmock, 9999);
|
||||
ASSERT_EQ(2, iomgr.getNumIfaces());
|
||||
|
||||
const int ALL_IFACES_MASK = 3;
|
||||
@ -312,15 +308,13 @@ TEST(CanIOManager, Loopback)
|
||||
|
||||
// Memory
|
||||
uavcan::PoolAllocator<sizeof(CanTxQueue::Entry) * 4, sizeof(CanTxQueue::Entry)> pool;
|
||||
uavcan::PoolManager<2> poolmgr;
|
||||
poolmgr.addPool(&pool);
|
||||
|
||||
// Platform interface
|
||||
SystemClockMock clockmock;
|
||||
CanDriverMock driver(2, clockmock);
|
||||
|
||||
// IO Manager
|
||||
CanIOManager iomgr(driver, poolmgr, clockmock);
|
||||
CanIOManager iomgr(driver, pool, clockmock);
|
||||
ASSERT_EQ(2, iomgr.getNumIfaces());
|
||||
|
||||
CanFrame fr1;
|
||||
|
||||
@ -68,12 +68,10 @@ TEST(CanTxQueue, TxQueue)
|
||||
ASSERT_GE(40, sizeof(CanTxQueue::Entry)); // should be true for any platforms, though not required
|
||||
|
||||
uavcan::PoolAllocator<40 * 4, 40> pool;
|
||||
uavcan::PoolManager<2> poolmgr;
|
||||
poolmgr.addPool(&pool);
|
||||
|
||||
SystemClockMock clockmock;
|
||||
|
||||
CanTxQueue queue(poolmgr, clockmock, 99999);
|
||||
CanTxQueue queue(pool, clockmock, 99999);
|
||||
EXPECT_TRUE(queue.isEmpty());
|
||||
|
||||
const uavcan::CanIOFlags flags = 0;
|
||||
|
||||
@ -53,15 +53,13 @@ static const uavcan::NodeID SELF_NODE_ID(64);
|
||||
TEST(Dispatcher, Reception)
|
||||
{
|
||||
uavcan::PoolAllocator<uavcan::MemPoolBlockSize * 8, uavcan::MemPoolBlockSize> pool;
|
||||
uavcan::PoolManager<1> poolmgr;
|
||||
poolmgr.addPool(&pool);
|
||||
|
||||
SystemClockMock clockmock(100);
|
||||
CanDriverMock driver(2, clockmock);
|
||||
|
||||
uavcan::OutgoingTransferRegistry<8> out_trans_reg(poolmgr);
|
||||
uavcan::OutgoingTransferRegistry<8> out_trans_reg(pool);
|
||||
|
||||
uavcan::Dispatcher dispatcher(driver, poolmgr, clockmock, out_trans_reg);
|
||||
uavcan::Dispatcher dispatcher(driver, pool, clockmock, out_trans_reg);
|
||||
ASSERT_TRUE(dispatcher.setNodeID(SELF_NODE_ID)); // Can be set only once
|
||||
ASSERT_FALSE(dispatcher.setNodeID(SELF_NODE_ID));
|
||||
ASSERT_EQ(SELF_NODE_ID, dispatcher.getNodeID());
|
||||
@ -93,12 +91,12 @@ TEST(Dispatcher, Reception)
|
||||
static const int NUM_SUBSCRIBERS = 6;
|
||||
SubscriberPtr subscribers[NUM_SUBSCRIBERS] =
|
||||
{
|
||||
SubscriberPtr(new Subscriber(dispatcher.getTransferPerfCounter(), TYPES[0], poolmgr)), // msg
|
||||
SubscriberPtr(new Subscriber(dispatcher.getTransferPerfCounter(), TYPES[0], poolmgr)), // msg // Two similar
|
||||
SubscriberPtr(new Subscriber(dispatcher.getTransferPerfCounter(), TYPES[1], poolmgr)), // msg
|
||||
SubscriberPtr(new Subscriber(dispatcher.getTransferPerfCounter(), TYPES[2], poolmgr)), // srv
|
||||
SubscriberPtr(new Subscriber(dispatcher.getTransferPerfCounter(), TYPES[3], poolmgr)), // srv
|
||||
SubscriberPtr(new Subscriber(dispatcher.getTransferPerfCounter(), TYPES[3], poolmgr)) // srv // Repeat again
|
||||
SubscriberPtr(new Subscriber(dispatcher.getTransferPerfCounter(), TYPES[0], pool)), // msg
|
||||
SubscriberPtr(new Subscriber(dispatcher.getTransferPerfCounter(), TYPES[0], pool)), // msg // Two similar
|
||||
SubscriberPtr(new Subscriber(dispatcher.getTransferPerfCounter(), TYPES[1], pool)), // msg
|
||||
SubscriberPtr(new Subscriber(dispatcher.getTransferPerfCounter(), TYPES[2], pool)), // srv
|
||||
SubscriberPtr(new Subscriber(dispatcher.getTransferPerfCounter(), TYPES[3], pool)), // srv
|
||||
SubscriberPtr(new Subscriber(dispatcher.getTransferPerfCounter(), TYPES[3], pool)) // srv // Repeat again
|
||||
};
|
||||
|
||||
static const std::string DATA[6] =
|
||||
@ -259,15 +257,13 @@ TEST(Dispatcher, Reception)
|
||||
TEST(Dispatcher, Transmission)
|
||||
{
|
||||
uavcan::PoolAllocator<uavcan::MemPoolBlockSize * 8, uavcan::MemPoolBlockSize> pool;
|
||||
uavcan::PoolManager<1> poolmgr;
|
||||
poolmgr.addPool(&pool);
|
||||
|
||||
SystemClockMock clockmock(100);
|
||||
CanDriverMock driver(2, clockmock);
|
||||
|
||||
uavcan::OutgoingTransferRegistry<8> out_trans_reg(poolmgr);
|
||||
uavcan::OutgoingTransferRegistry<8> out_trans_reg(pool);
|
||||
|
||||
uavcan::Dispatcher dispatcher(driver, poolmgr, clockmock, out_trans_reg);
|
||||
uavcan::Dispatcher dispatcher(driver, pool, clockmock, out_trans_reg);
|
||||
ASSERT_TRUE(dispatcher.setNodeID(SELF_NODE_ID)); // Can be set only once
|
||||
ASSERT_FALSE(dispatcher.setNodeID(SELF_NODE_ID));
|
||||
|
||||
@ -324,7 +320,7 @@ TEST(Dispatcher, Transmission)
|
||||
|
||||
TEST(Dispatcher, Spin)
|
||||
{
|
||||
uavcan::PoolManager<1> poolmgr;
|
||||
NullAllocator poolmgr;
|
||||
|
||||
SystemClockMock clockmock(100);
|
||||
CanDriverMock driver(2, clockmock);
|
||||
@ -370,7 +366,7 @@ struct DispatcherTestLoopbackFrameListener : public uavcan::LoopbackFrameListene
|
||||
|
||||
TEST(Dispatcher, Loopback)
|
||||
{
|
||||
uavcan::PoolManager<1> poolmgr;
|
||||
NullAllocator poolmgr;
|
||||
|
||||
SystemClockMock clockmock(100);
|
||||
CanDriverMock driver(2, clockmock);
|
||||
|
||||
@ -6,6 +6,7 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <uavcan/transport/transfer_listener.hpp>
|
||||
#include "../clock.hpp"
|
||||
#include "transfer_test_helpers.hpp"
|
||||
|
||||
|
||||
static uavcan::RxFrame makeFrame()
|
||||
@ -71,7 +72,7 @@ TEST(MultiFrameIncomingTransfer, Basic)
|
||||
using uavcan::RxFrame;
|
||||
using uavcan::MultiFrameIncomingTransfer;
|
||||
|
||||
uavcan::PoolManager<1> poolmgr; // We don't need dynamic memory
|
||||
NullAllocator poolmgr; // We don't need dynamic memory
|
||||
uavcan::TransferBufferManager<256, 1> bufmgr(poolmgr);
|
||||
|
||||
const RxFrame frame = makeFrame();
|
||||
|
||||
@ -6,12 +6,13 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <uavcan/transport/outgoing_transfer_registry.hpp>
|
||||
#include "../clock.hpp"
|
||||
#include "transfer_test_helpers.hpp"
|
||||
|
||||
|
||||
TEST(OutgoingTransferRegistry, Basic)
|
||||
{
|
||||
using uavcan::OutgoingTransferRegistryKey;
|
||||
uavcan::PoolManager<1> poolmgr; // Empty
|
||||
NullAllocator poolmgr; // Empty
|
||||
uavcan::OutgoingTransferRegistry<4> otr(poolmgr);
|
||||
|
||||
otr.cleanup(tsMono(1000));
|
||||
|
||||
@ -133,10 +133,8 @@ TEST(DynamicTransferBufferManagerEntry, Basic)
|
||||
static const int MAX_SIZE = TEST_BUFFER_SIZE;
|
||||
static const int POOL_BLOCKS = 8;
|
||||
uavcan::PoolAllocator<uavcan::MemPoolBlockSize * POOL_BLOCKS, uavcan::MemPoolBlockSize> pool;
|
||||
uavcan::PoolManager<2> poolmgr;
|
||||
poolmgr.addPool(&pool);
|
||||
|
||||
DynamicTransferBufferManagerEntry buf(poolmgr, MAX_SIZE);
|
||||
DynamicTransferBufferManagerEntry buf(pool, MAX_SIZE);
|
||||
|
||||
uint8_t local_buffer[TEST_BUFFER_SIZE * 2];
|
||||
const uint8_t* const test_data_ptr = reinterpret_cast<const uint8_t*>(TEST_DATA.c_str());
|
||||
@ -233,11 +231,9 @@ TEST(TransferBufferManager, Basic)
|
||||
|
||||
static const int POOL_BLOCKS = 6;
|
||||
uavcan::PoolAllocator<uavcan::MemPoolBlockSize * POOL_BLOCKS, uavcan::MemPoolBlockSize> pool;
|
||||
uavcan::PoolManager<1> poolmgr;
|
||||
poolmgr.addPool(&pool);
|
||||
|
||||
typedef TransferBufferManager<MGR_MAX_BUFFER_SIZE, 2> TransferBufferManagerType;
|
||||
std::auto_ptr<TransferBufferManagerType> mgr(new TransferBufferManagerType(poolmgr));
|
||||
std::auto_ptr<TransferBufferManagerType> mgr(new TransferBufferManagerType(pool));
|
||||
|
||||
// Empty
|
||||
ASSERT_FALSE(mgr->access(TransferBufferManagerKey(0, uavcan::TransferTypeMessageUnicast)));
|
||||
|
||||
@ -36,11 +36,9 @@ TEST(TransferListener, BasicMFT)
|
||||
|
||||
static const int NUM_POOL_BLOCKS = 12; // This number is just enough to pass the test
|
||||
uavcan::PoolAllocator<uavcan::MemPoolBlockSize * NUM_POOL_BLOCKS, uavcan::MemPoolBlockSize> pool;
|
||||
uavcan::PoolManager<1> poolmgr;
|
||||
poolmgr.addPool(&pool);
|
||||
|
||||
uavcan::TransferPerfCounter perf;
|
||||
TestListener<256, 1, 1> subscriber(perf, type, poolmgr);
|
||||
TestListener<256, 1, 1> subscriber(perf, type, pool);
|
||||
|
||||
/*
|
||||
* Test data
|
||||
@ -97,7 +95,7 @@ TEST(TransferListener, CrcFailure)
|
||||
{
|
||||
const uavcan::DataTypeDescriptor type(uavcan::DataTypeKindMessage, 123, uavcan::DataTypeSignature(123456789), "A");
|
||||
|
||||
uavcan::PoolManager<1> poolmgr; // No dynamic memory
|
||||
NullAllocator poolmgr; // No dynamic memory
|
||||
uavcan::TransferPerfCounter perf;
|
||||
TestListener<256, 2, 2> subscriber(perf, type, poolmgr); // Static buffer only, 2 entries
|
||||
|
||||
@ -140,7 +138,7 @@ TEST(TransferListener, BasicSFT)
|
||||
{
|
||||
const uavcan::DataTypeDescriptor type(uavcan::DataTypeKindMessage, 123, uavcan::DataTypeSignature(123456789), "A");
|
||||
|
||||
uavcan::PoolManager<1> poolmgr; // No dynamic memory. At all.
|
||||
NullAllocator poolmgr; // No dynamic memory. At all.
|
||||
uavcan::TransferPerfCounter perf;
|
||||
TestListener<0, 0, 5> subscriber(perf, type, poolmgr); // Max buf size is 0, i.e. SFT-only
|
||||
|
||||
@ -176,7 +174,7 @@ TEST(TransferListener, Cleanup)
|
||||
{
|
||||
const uavcan::DataTypeDescriptor type(uavcan::DataTypeKindMessage, 123, uavcan::DataTypeSignature(123456789), "A");
|
||||
|
||||
uavcan::PoolManager<1> poolmgr; // No dynamic memory
|
||||
NullAllocator poolmgr; // No dynamic memory
|
||||
uavcan::TransferPerfCounter perf;
|
||||
TestListener<256, 1, 2> subscriber(perf, type, poolmgr); // Static buffer only, 1 entry
|
||||
|
||||
@ -231,7 +229,7 @@ TEST(TransferListener, MaximumTransferLength)
|
||||
{
|
||||
const uavcan::DataTypeDescriptor type(uavcan::DataTypeKindMessage, 123, uavcan::DataTypeSignature(123456789), "A");
|
||||
|
||||
uavcan::PoolManager<1> poolmgr;
|
||||
NullAllocator poolmgr;
|
||||
uavcan::TransferPerfCounter perf;
|
||||
TestListener<uavcan::MaxPossibleTransferPayloadLen * 2, 3, 3> subscriber(perf, type, poolmgr);
|
||||
|
||||
@ -260,7 +258,7 @@ TEST(TransferListener, AnonymousTransfers)
|
||||
{
|
||||
const uavcan::DataTypeDescriptor type(uavcan::DataTypeKindMessage, 123, uavcan::DataTypeSignature(123456789), "A");
|
||||
|
||||
uavcan::PoolManager<1> poolmgr;
|
||||
NullAllocator poolmgr;
|
||||
uavcan::TransferPerfCounter perf;
|
||||
TestListener<0, 0, 0> subscriber(perf, type, poolmgr);
|
||||
|
||||
|
||||
@ -6,6 +6,7 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <uavcan/transport/transfer_receiver.hpp>
|
||||
#include "../clock.hpp"
|
||||
#include "transfer_test_helpers.hpp"
|
||||
|
||||
/*
|
||||
* Beware!
|
||||
@ -46,17 +47,16 @@ struct RxFrameGenerator
|
||||
const uavcan::TransferBufferManagerKey RxFrameGenerator::DEFAULT_KEY(42, uavcan::TransferTypeMessageBroadcast);
|
||||
|
||||
|
||||
template <unsigned BUFSIZE>
|
||||
template <unsigned BufSize>
|
||||
struct Context
|
||||
{
|
||||
uavcan::PoolManager<1> poolmgr; // We don't need dynamic memory for this test
|
||||
uavcan::TransferReceiver receiver; // Must be default constructible and copyable
|
||||
uavcan::TransferBufferManager<BUFSIZE, 1> bufmgr;
|
||||
NullAllocator pool; // We don't need dynamic memory for this test
|
||||
uavcan::TransferReceiver receiver; // Must be default constructible and copyable
|
||||
uavcan::TransferBufferManager<BufSize, 1> bufmgr;
|
||||
|
||||
Context()
|
||||
: bufmgr(poolmgr)
|
||||
Context() : bufmgr(pool)
|
||||
{
|
||||
assert(poolmgr.allocate(1) == NULL);
|
||||
assert(pool.allocate(1) == NULL);
|
||||
}
|
||||
|
||||
~Context()
|
||||
|
||||
@ -29,7 +29,7 @@ static int sendOne(uavcan::TransferSender& sender, const std::string& data,
|
||||
|
||||
TEST(TransferSender, Basic)
|
||||
{
|
||||
uavcan::PoolManager<1> poolmgr;
|
||||
NullAllocator poolmgr;
|
||||
|
||||
SystemClockMock clockmock(100);
|
||||
CanDriverMock driver(2, clockmock);
|
||||
@ -185,7 +185,7 @@ struct TransferSenderTestLoopbackFrameListener : public uavcan::LoopbackFrameLis
|
||||
|
||||
TEST(TransferSender, Loopback)
|
||||
{
|
||||
uavcan::PoolManager<1> poolmgr;
|
||||
NullAllocator poolmgr;
|
||||
|
||||
SystemClockMock clockmock(100);
|
||||
CanDriverMock driver(2, clockmock);
|
||||
@ -226,7 +226,7 @@ TEST(TransferSender, Loopback)
|
||||
|
||||
TEST(TransferSender, PassiveMode)
|
||||
{
|
||||
uavcan::PoolManager<1> poolmgr;
|
||||
NullAllocator poolmgr;
|
||||
|
||||
SystemClockMock clockmock(100);
|
||||
CanDriverMock driver(2, clockmock);
|
||||
|
||||
@ -10,10 +10,8 @@
|
||||
TEST(TransferTestHelpers, Transfer)
|
||||
{
|
||||
uavcan::PoolAllocator<uavcan::MemPoolBlockSize * 8, uavcan::MemPoolBlockSize> pool;
|
||||
uavcan::PoolManager<1> poolmgr;
|
||||
poolmgr.addPool(&pool);
|
||||
|
||||
uavcan::TransferBufferManager<128, 1> mgr(poolmgr);
|
||||
uavcan::TransferBufferManager<128, 1> mgr(pool);
|
||||
uavcan::TransferBufferAccessor tba(mgr, uavcan::TransferBufferManagerKey(0, uavcan::TransferTypeMessageUnicast));
|
||||
|
||||
uavcan::RxFrame frame(uavcan::Frame(123, uavcan::TransferTypeMessageBroadcast, 1, 0, 0, 0, true),
|
||||
|
||||
@ -323,3 +323,14 @@ public:
|
||||
|
||||
template <int SIZE> void send(const Transfer (&transfers)[SIZE]) { send(transfers, SIZE); }
|
||||
};
|
||||
|
||||
/**
|
||||
* Zero allocator - always fails
|
||||
*/
|
||||
class NullAllocator : public uavcan::IPoolAllocator
|
||||
{
|
||||
public:
|
||||
virtual void* allocate(std::size_t) { return NULL; }
|
||||
virtual void deallocate(const void*) { }
|
||||
virtual std::size_t getNumBlocks() const { return 0; }
|
||||
};
|
||||
|
||||
@ -45,11 +45,9 @@ TEST(Map, Basic)
|
||||
|
||||
static const int POOL_BLOCKS = 3;
|
||||
uavcan::PoolAllocator<uavcan::MemPoolBlockSize * POOL_BLOCKS, uavcan::MemPoolBlockSize> pool;
|
||||
uavcan::PoolManager<2> poolmgr;
|
||||
poolmgr.addPool(&pool);
|
||||
|
||||
typedef Map<std::string, std::string, 2> MapType;
|
||||
std::auto_ptr<MapType> map(new MapType(poolmgr));
|
||||
std::auto_ptr<MapType> map(new MapType(pool));
|
||||
|
||||
// Empty
|
||||
ASSERT_FALSE(map->access("hi"));
|
||||
@ -208,11 +206,9 @@ TEST(Map, NoStatic)
|
||||
|
||||
static const int POOL_BLOCKS = 3;
|
||||
uavcan::PoolAllocator<uavcan::MemPoolBlockSize * POOL_BLOCKS, uavcan::MemPoolBlockSize> pool;
|
||||
uavcan::PoolManager<2> poolmgr;
|
||||
poolmgr.addPool(&pool);
|
||||
|
||||
typedef Map<std::string, std::string> MapType;
|
||||
std::auto_ptr<MapType> map(new MapType(poolmgr));
|
||||
std::auto_ptr<MapType> map(new MapType(pool));
|
||||
|
||||
// Empty
|
||||
ASSERT_FALSE(map->access("hi"));
|
||||
@ -241,11 +237,9 @@ TEST(Map, PrimitiveKey)
|
||||
|
||||
static const int POOL_BLOCKS = 3;
|
||||
uavcan::PoolAllocator<uavcan::MemPoolBlockSize * POOL_BLOCKS, uavcan::MemPoolBlockSize> pool;
|
||||
uavcan::PoolManager<2> poolmgr;
|
||||
poolmgr.addPool(&pool);
|
||||
|
||||
typedef Map<short, short, 2> MapType;
|
||||
std::auto_ptr<MapType> map(new MapType(poolmgr));
|
||||
std::auto_ptr<MapType> map(new MapType(pool));
|
||||
|
||||
// Empty
|
||||
ASSERT_FALSE(map->access(1));
|
||||
|
||||
@ -70,11 +70,9 @@ TEST(Multiset, Basic)
|
||||
|
||||
static const int POOL_BLOCKS = 3;
|
||||
uavcan::PoolAllocator<uavcan::MemPoolBlockSize * POOL_BLOCKS, uavcan::MemPoolBlockSize> pool;
|
||||
uavcan::PoolManager<2> poolmgr;
|
||||
poolmgr.addPool(&pool);
|
||||
|
||||
typedef Multiset<std::string, 2> MultisetType;
|
||||
std::auto_ptr<MultisetType> mset(new MultisetType(poolmgr));
|
||||
std::auto_ptr<MultisetType> mset(new MultisetType(pool));
|
||||
|
||||
typedef SummationOperator<std::string> StringConcatenationOperator;
|
||||
|
||||
@ -220,11 +218,9 @@ TEST(Multiset, PrimitiveKey)
|
||||
|
||||
static const int POOL_BLOCKS = 3;
|
||||
uavcan::PoolAllocator<uavcan::MemPoolBlockSize * POOL_BLOCKS, uavcan::MemPoolBlockSize> pool;
|
||||
uavcan::PoolManager<2> poolmgr;
|
||||
poolmgr.addPool(&pool);
|
||||
|
||||
typedef Multiset<int, 2> MultisetType;
|
||||
std::auto_ptr<MultisetType> mset(new MultisetType(poolmgr));
|
||||
std::auto_ptr<MultisetType> mset(new MultisetType(pool));
|
||||
|
||||
// Empty
|
||||
mset->removeFirst(8);
|
||||
@ -272,11 +268,9 @@ TEST(Multiset, NoncopyableWithCounter)
|
||||
|
||||
static const int POOL_BLOCKS = 3;
|
||||
uavcan::PoolAllocator<uavcan::MemPoolBlockSize * POOL_BLOCKS, uavcan::MemPoolBlockSize> pool;
|
||||
uavcan::PoolManager<2> poolmgr;
|
||||
poolmgr.addPool(&pool);
|
||||
|
||||
typedef Multiset<NoncopyableWithCounter, 2> MultisetType;
|
||||
std::auto_ptr<MultisetType> mset(new MultisetType(poolmgr));
|
||||
std::auto_ptr<MultisetType> mset(new MultisetType(pool));
|
||||
|
||||
ASSERT_EQ(0, NoncopyableWithCounter::num_objects);
|
||||
ASSERT_EQ(0, mset->emplace()->value);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user