mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-11-02 07:41:35 +08:00
First stab at global refactoring of memory management - the library builds, but unit tests are failing horribly
This commit is contained in:
@@ -65,7 +65,7 @@ private:
|
||||
static const unsigned DefaultAnonMsgMask = 0xFF;
|
||||
static const unsigned DefaultAnonMsgID = 0x0;
|
||||
|
||||
typedef uavcan::Multiset<CanFilterConfig, 1> MultisetConfigContainer;
|
||||
typedef uavcan::Multiset<CanFilterConfig> MultisetConfigContainer;
|
||||
|
||||
static CanFilterConfig mergeFilters(CanFilterConfig &a_, CanFilterConfig &b_);
|
||||
static uint8_t countBits(uint32_t n_);
|
||||
|
||||
@@ -60,6 +60,8 @@ public:
|
||||
* Outgoing transfer registry keeps track of Transfer ID values for all currently existing local transfer senders.
|
||||
* If a local transfer sender was inactive for a sufficiently long time, the outgoing transfer registry will
|
||||
* remove the respective Transfer ID tracking object.
|
||||
*
|
||||
* TODO: Deinterface this.
|
||||
*/
|
||||
class UAVCAN_EXPORT IOutgoingTransferRegistry
|
||||
{
|
||||
@@ -74,7 +76,6 @@ public:
|
||||
};
|
||||
|
||||
|
||||
template <int NumStaticEntries>
|
||||
class UAVCAN_EXPORT OutgoingTransferRegistry : public IOutgoingTransferRegistry, Noncopyable
|
||||
{
|
||||
struct Value
|
||||
@@ -123,7 +124,7 @@ class UAVCAN_EXPORT OutgoingTransferRegistry : public IOutgoingTransferRegistry,
|
||||
}
|
||||
};
|
||||
|
||||
Map<OutgoingTransferRegistryKey, Value, NumStaticEntries> map_;
|
||||
Map<OutgoingTransferRegistryKey, Value> map_;
|
||||
|
||||
public:
|
||||
explicit OutgoingTransferRegistry(IPoolAllocator& allocator)
|
||||
@@ -140,11 +141,12 @@ public:
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
/*
|
||||
* OutgoingTransferRegistry<>
|
||||
* OutgoingTransferRegistry
|
||||
* TODO: deinterface and move to .cpp
|
||||
*/
|
||||
template <int NumStaticEntries>
|
||||
TransferID* OutgoingTransferRegistry<NumStaticEntries>::accessOrCreate(const OutgoingTransferRegistryKey& key,
|
||||
MonotonicTime new_deadline)
|
||||
inline
|
||||
TransferID* OutgoingTransferRegistry::accessOrCreate(const OutgoingTransferRegistryKey& key,
|
||||
MonotonicTime new_deadline)
|
||||
{
|
||||
UAVCAN_ASSERT(!new_deadline.isZero());
|
||||
Value* p = map_.access(key);
|
||||
@@ -161,14 +163,14 @@ TransferID* OutgoingTransferRegistry<NumStaticEntries>::accessOrCreate(const Out
|
||||
return &p->tid;
|
||||
}
|
||||
|
||||
template <int NumStaticEntries>
|
||||
bool OutgoingTransferRegistry<NumStaticEntries>::exists(DataTypeID dtid, TransferType tt) const
|
||||
inline
|
||||
bool OutgoingTransferRegistry::exists(DataTypeID dtid, TransferType tt) const
|
||||
{
|
||||
return NULL != map_.find(ExistenceCheckingPredicate(dtid, tt));
|
||||
}
|
||||
|
||||
template <int NumStaticEntries>
|
||||
void OutgoingTransferRegistry<NumStaticEntries>::cleanup(MonotonicTime ts)
|
||||
inline
|
||||
void OutgoingTransferRegistry::cleanup(MonotonicTime ts)
|
||||
{
|
||||
map_.removeAllWhere(DeadlineExpiredPredicate(ts));
|
||||
}
|
||||
|
||||
@@ -194,8 +194,6 @@ public:
|
||||
|
||||
virtual int read(unsigned offset, uint8_t* data, unsigned len) const;
|
||||
virtual int write(unsigned offset, const uint8_t* data, unsigned len);
|
||||
|
||||
bool migrateFrom(const TransferBufferManagerEntry* tbme);
|
||||
};
|
||||
|
||||
template <uint16_t Size>
|
||||
@@ -250,11 +248,7 @@ class TransferBufferManagerImpl : public ITransferBufferManager, Noncopyable
|
||||
IPoolAllocator& allocator_;
|
||||
const uint16_t max_buf_size_;
|
||||
|
||||
virtual StaticTransferBufferManagerEntryImpl* getStaticByIndex(uint16_t index) const = 0;
|
||||
|
||||
StaticTransferBufferManagerEntryImpl* findFirstStatic(const TransferBufferManagerKey& key);
|
||||
DynamicTransferBufferManagerEntry* findFirstDynamic(const TransferBufferManagerKey& key);
|
||||
void optimizeStorage();
|
||||
|
||||
public:
|
||||
TransferBufferManagerImpl(uint16_t max_buf_size, IPoolAllocator& allocator)
|
||||
@@ -269,49 +263,20 @@ public:
|
||||
virtual void remove(const TransferBufferManagerKey& key);
|
||||
virtual bool isEmpty() const;
|
||||
|
||||
unsigned getNumDynamicBuffers() const;
|
||||
unsigned getNumStaticBuffers() const;
|
||||
};
|
||||
|
||||
template <uint16_t MaxBufSize, uint8_t NumStaticBufs>
|
||||
class UAVCAN_EXPORT TransferBufferManager : public TransferBufferManagerImpl
|
||||
{
|
||||
mutable StaticTransferBufferManagerEntry<MaxBufSize> static_buffers_[NumStaticBufs];
|
||||
|
||||
virtual StaticTransferBufferManagerEntry<MaxBufSize>* getStaticByIndex(uint16_t index) const
|
||||
{
|
||||
return (index < NumStaticBufs) ? &static_buffers_[index] : NULL;
|
||||
}
|
||||
|
||||
public:
|
||||
explicit TransferBufferManager(IPoolAllocator& allocator)
|
||||
: TransferBufferManagerImpl(MaxBufSize, allocator)
|
||||
{
|
||||
#if UAVCAN_TINY
|
||||
StaticAssert<(NumStaticBufs == 0)>::check(); // Static buffers in UAVCAN_TINY mode are not allowed
|
||||
#endif
|
||||
StaticAssert<(MaxBufSize > 0)>::check();
|
||||
}
|
||||
unsigned getNumBuffers() const;
|
||||
};
|
||||
|
||||
template <uint16_t MaxBufSize>
|
||||
class UAVCAN_EXPORT TransferBufferManager<MaxBufSize, 0> : public TransferBufferManagerImpl
|
||||
class UAVCAN_EXPORT TransferBufferManager : public TransferBufferManagerImpl
|
||||
{
|
||||
virtual StaticTransferBufferManagerEntry<MaxBufSize>* getStaticByIndex(uint16_t) const
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
public:
|
||||
explicit TransferBufferManager(IPoolAllocator& allocator)
|
||||
: TransferBufferManagerImpl(MaxBufSize, allocator)
|
||||
{
|
||||
StaticAssert<(MaxBufSize > 0)>::check();
|
||||
}
|
||||
explicit TransferBufferManager(IPoolAllocator& allocator) :
|
||||
TransferBufferManagerImpl(MaxBufSize, allocator)
|
||||
{ }
|
||||
};
|
||||
|
||||
template <>
|
||||
class UAVCAN_EXPORT TransferBufferManager<0, 0> : public ITransferBufferManager
|
||||
class UAVCAN_EXPORT TransferBufferManager<0> : public ITransferBufferManager
|
||||
{
|
||||
public:
|
||||
TransferBufferManager() { }
|
||||
|
||||
@@ -99,7 +99,7 @@ public:
|
||||
class UAVCAN_EXPORT TransferListenerBase : public LinkedListNode<TransferListenerBase>, Noncopyable
|
||||
{
|
||||
const DataTypeDescriptor& data_type_;
|
||||
MapBase<TransferBufferManagerKey, TransferReceiver>& receivers_;
|
||||
Map<TransferBufferManagerKey, TransferReceiver>& receivers_;
|
||||
ITransferBufferManager& bufmgr_;
|
||||
TransferPerfCounter& perf_;
|
||||
const TransferCRC crc_base_; ///< Pre-initialized with data type hash, thus constant
|
||||
@@ -123,7 +123,7 @@ class UAVCAN_EXPORT TransferListenerBase : public LinkedListNode<TransferListene
|
||||
|
||||
protected:
|
||||
TransferListenerBase(TransferPerfCounter& perf, const DataTypeDescriptor& data_type,
|
||||
MapBase<TransferBufferManagerKey, TransferReceiver>& receivers,
|
||||
Map<TransferBufferManagerKey, TransferReceiver>& receivers,
|
||||
ITransferBufferManager& bufmgr)
|
||||
: data_type_(data_type)
|
||||
, receivers_(receivers)
|
||||
@@ -157,24 +157,18 @@ public:
|
||||
/**
|
||||
* This class should be derived by transfer receivers (subscribers, servers).
|
||||
*/
|
||||
template <unsigned MaxBufSize, unsigned NumStaticBufs, unsigned NumStaticReceivers>
|
||||
template <unsigned MaxBufSize>
|
||||
class UAVCAN_EXPORT TransferListener : public TransferListenerBase
|
||||
{
|
||||
TransferBufferManager<MaxBufSize, NumStaticBufs> bufmgr_;
|
||||
Map<TransferBufferManagerKey, TransferReceiver, NumStaticReceivers> receivers_;
|
||||
TransferBufferManager<MaxBufSize> bufmgr_;
|
||||
Map<TransferBufferManagerKey, TransferReceiver> receivers_;
|
||||
|
||||
public:
|
||||
TransferListener(TransferPerfCounter& perf, const DataTypeDescriptor& data_type, IPoolAllocator& allocator)
|
||||
: TransferListenerBase(perf, data_type, receivers_, bufmgr_)
|
||||
, bufmgr_(allocator)
|
||||
, receivers_(allocator)
|
||||
{
|
||||
#if UAVCAN_TINY
|
||||
StaticAssert<NumStaticBufs == 0>::check();
|
||||
StaticAssert<NumStaticReceivers == 0>::check();
|
||||
#endif
|
||||
StaticAssert<(NumStaticReceivers >= NumStaticBufs)>::check(); // Otherwise it would be meaningless
|
||||
}
|
||||
{ }
|
||||
|
||||
virtual ~TransferListener()
|
||||
{
|
||||
@@ -200,15 +194,15 @@ public:
|
||||
/**
|
||||
* This class should be derived by callers.
|
||||
*/
|
||||
template <unsigned MaxBufSize, unsigned NumStaticBufs, unsigned NumStaticReceivers>
|
||||
class UAVCAN_EXPORT TransferListenerWithFilter : public TransferListener<MaxBufSize, NumStaticBufs, NumStaticReceivers>
|
||||
template <unsigned MaxBufSize>
|
||||
class UAVCAN_EXPORT TransferListenerWithFilter : public TransferListener<MaxBufSize>
|
||||
{
|
||||
const ITransferAcceptanceFilter* filter_;
|
||||
|
||||
virtual void handleFrame(const RxFrame& frame);
|
||||
|
||||
public:
|
||||
typedef TransferListener<MaxBufSize, NumStaticBufs, NumStaticReceivers> BaseType;
|
||||
typedef TransferListener<MaxBufSize> BaseType;
|
||||
|
||||
TransferListenerWithFilter(TransferPerfCounter& perf, const DataTypeDescriptor& data_type,
|
||||
IPoolAllocator& allocator)
|
||||
@@ -227,8 +221,8 @@ public:
|
||||
/*
|
||||
* TransferListenerWithFilter<>
|
||||
*/
|
||||
template <unsigned MaxBufSize, unsigned NumStaticBufs, unsigned NumStaticReceivers>
|
||||
void TransferListenerWithFilter<MaxBufSize, NumStaticBufs, NumStaticReceivers>::handleFrame(const RxFrame& frame)
|
||||
template <unsigned MaxBufSize>
|
||||
void TransferListenerWithFilter<MaxBufSize>::handleFrame(const RxFrame& frame)
|
||||
{
|
||||
if (filter_ != NULL)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user