From 6c70abfe1621a83074f21476d680bc808a52576f Mon Sep 17 00:00:00 2001 From: "Robin.Mueller" Date: Tue, 12 May 2020 16:32:01 +0200 Subject: [PATCH] moved pool accessor fuctions to local pool --- storagemanager/LocalPool.h | 10 ++++++- storagemanager/LocalPool.tpp | 43 +++++++++++++++++++++++++++++-- storagemanager/PoolManager.h | 19 +++++++------- storagemanager/PoolManager.tpp | 5 ++++ storagemanager/StorageAccessor.h | 4 +-- storagemanager/StorageManagerIF.h | 17 +++++++++++- 6 files changed, 82 insertions(+), 16 deletions(-) diff --git a/storagemanager/LocalPool.h b/storagemanager/LocalPool.h index a6334458..6892b881 100644 --- a/storagemanager/LocalPool.h +++ b/storagemanager/LocalPool.h @@ -6,7 +6,8 @@ #include #include #include -#include +#include +#include /** * @brief The LocalPool class provides an intermediate data storage with @@ -67,10 +68,17 @@ public: size_t size, bool ignoreFault = false) override; ReturnValue_t getFreeElement(store_address_t* storageId,const size_t size, uint8_t** p_data, bool ignoreFault = false) override; + + ConstAccessorPair getData(store_address_t packet_id) override; + ReturnValue_t getData(store_address_t packet_id, ConstStorageAccessor&) override; ReturnValue_t getData(store_address_t packet_id, const uint8_t** packet_ptr, size_t * size) override; + + AccessorPair modifyData(store_address_t packet_id) override; + ReturnValue_t modifyData(store_address_t packet_id, StorageAccessor&) override; ReturnValue_t modifyData(store_address_t packet_id, uint8_t** packet_ptr, size_t * size) override; + virtual ReturnValue_t deleteData(store_address_t) override; virtual ReturnValue_t deleteData(uint8_t* ptr, size_t size, store_address_t* storeId = NULL) override; diff --git a/storagemanager/LocalPool.tpp b/storagemanager/LocalPool.tpp index c46e1a8d..31034974 100644 --- a/storagemanager/LocalPool.tpp +++ b/storagemanager/LocalPool.tpp @@ -121,8 +121,8 @@ inline LocalPool::~LocalPool(void) { } } -template -inline ReturnValue_t LocalPool::addData(store_address_t* storageId, +template inline +ReturnValue_t LocalPool::addData(store_address_t* storageId, const uint8_t* data, size_t size, bool ignoreFault) { ReturnValue_t status = reserveSpace(size, storageId, ignoreFault); if (status == RETURN_OK) { @@ -144,6 +144,25 @@ inline ReturnValue_t LocalPool::getFreeElement( return status; } +template +inline ConstAccessorPair LocalPool::getData( + store_address_t storeId) { + uint8_t* tempData = nullptr; + ConstStorageAccessor constAccessor(storeId); + ReturnValue_t status = modifyData(storeId, &tempData, &constAccessor.size_); + constAccessor.constDataPointer = tempData; + return ConstAccessorPair(status, std::move(constAccessor)); +} + +template +inline ReturnValue_t LocalPool::getData(store_address_t storeId, + ConstStorageAccessor& storeAccessor) { + uint8_t* tempData = nullptr; + ReturnValue_t status = modifyData(storeId, &tempData, &storeAccessor.size_); + storeAccessor.constDataPointer = tempData; + return status; +} + template inline ReturnValue_t LocalPool::getData( store_address_t packet_id, const uint8_t** packet_ptr, size_t* size) { @@ -153,6 +172,26 @@ inline ReturnValue_t LocalPool::getData( return status; } +template +inline AccessorPair LocalPool::modifyData( + store_address_t storeId) { + uint8_t* tempData = nullptr; + StorageAccessor accessor(storeId); + ReturnValue_t status = modifyData(storeId, &tempData, &accessor.size_); + accessor.constDataPointer = tempData; + accessor.assignConstPointer(); + return AccessorPair(status, std::move(accessor)); +} + +template +inline ReturnValue_t LocalPool::modifyData( + store_address_t storeId, StorageAccessor& storeAccessor) { + ReturnValue_t status = modifyData(storeId, &storeAccessor.dataPointer, + &storeAccessor.size_); + storeAccessor.assignConstPointer(); + return status; +} + template inline ReturnValue_t LocalPool::modifyData( store_address_t packet_id, uint8_t** packet_ptr, size_t* size) { diff --git a/storagemanager/PoolManager.h b/storagemanager/PoolManager.h index 41325273..87670a82 100644 --- a/storagemanager/PoolManager.h +++ b/storagemanager/PoolManager.h @@ -1,9 +1,9 @@ #ifndef POOLMANAGER_H_ #define POOLMANAGER_H_ - #include #include +#include /** * @brief The PoolManager class provides an intermediate data storage with @@ -11,20 +11,21 @@ * @details Uses local pool calls but is thread safe by protecting the call * with a lock. */ - -template +template class PoolManager : public LocalPool { public: - PoolManager( object_id_t setObjectId, const uint16_t element_sizes[NUMBER_OF_POOLS], - const uint16_t n_elements[NUMBER_OF_POOLS] ); - /** - * @brief In the PoolManager's destructor all allocated memory is freed. - */ + PoolManager(object_id_t setObjectId, + const uint16_t element_sizes[NUMBER_OF_POOLS], + const uint16_t n_elements[NUMBER_OF_POOLS]); + + //! @brief In the PoolManager's destructor all allocated memory is freed. virtual ~PoolManager(); + //! @brief LocalPool overrides for thread-safety. Decorator function which + //! wraps LocalPool calls with a mutex protection. ReturnValue_t deleteData(store_address_t) override; ReturnValue_t deleteData(uint8_t* buffer, size_t size, - store_address_t* storeId = NULL) override; + store_address_t* storeId = nullptr) override; protected: ReturnValue_t reserveSpace(const uint32_t size, store_address_t* address, bool ignoreFault) override; diff --git a/storagemanager/PoolManager.tpp b/storagemanager/PoolManager.tpp index 0408d55b..7025d795 100644 --- a/storagemanager/PoolManager.tpp +++ b/storagemanager/PoolManager.tpp @@ -1,3 +1,6 @@ +#ifndef POOLMANAGER_TPP_ +#define POOLMANAGER_TPP_ + template inline PoolManager::PoolManager(object_id_t setObjectId, const uint16_t element_sizes[NUMBER_OF_POOLS], @@ -40,3 +43,5 @@ inline ReturnValue_t PoolManager::deleteData(uint8_t* buffer, return status; } +#endif + diff --git a/storagemanager/StorageAccessor.h b/storagemanager/StorageAccessor.h index 2e1d44bd..da319a97 100644 --- a/storagemanager/StorageAccessor.h +++ b/storagemanager/StorageAccessor.h @@ -4,8 +4,7 @@ * @details These helper can be used together with the * StorageManager classes to manage access to a storage. * It can take care of thread-safety while also providing - * mechanisms to automatically clear storage data and unlocking the - * pool. + * mechanisms to automatically clear storage data. */ #ifndef TEST_PROTOTYPES_STORAGEACCESSOR_H_ #define TEST_PROTOTYPES_STORAGEACCESSOR_H_ @@ -14,7 +13,6 @@ #include #include - /** * @brief Accessor class which can be returned by pool managers * or passed and set by pool managers to have safe access to the pool diff --git a/storagemanager/StorageManagerIF.h b/storagemanager/StorageManagerIF.h index d1f9cde5..27448ef7 100644 --- a/storagemanager/StorageManagerIF.h +++ b/storagemanager/StorageManagerIF.h @@ -3,7 +3,14 @@ #include #include -#include +#include +#include + +class StorageAccessor; +class ConstStorageAccessor; + +using AccessorPair = std::pair; +using ConstAccessorPair = std::pair; /** * This union defines the type that identifies where a data packet is @@ -150,6 +157,14 @@ public: */ virtual ReturnValue_t getFreeElement(store_address_t* storageId, const size_t size, uint8_t** p_data, bool ignoreFault = false ) = 0; + + virtual AccessorPair modifyData(store_address_t storeId) = 0; + virtual ConstAccessorPair getData(store_address_t storeId) = 0; + virtual ReturnValue_t modifyData(store_address_t storeId, + StorageAccessor&) = 0; + virtual ReturnValue_t getData(store_address_t storeId, + ConstStorageAccessor&) = 0; + /** * Clears the whole store. * Use with care!