From 3122f62b0a1e81268ad5f7ac8fa249a5458dc906 Mon Sep 17 00:00:00 2001 From: "Robin.Mueller" Date: Tue, 12 May 2020 19:02:59 +0200 Subject: [PATCH] bugfixes for write() call --- storagemanager/LocalPool.tpp | 7 ++- storagemanager/StorageAccessor.cpp | 75 ++++++++++++++++++------------ storagemanager/StorageAccessor.h | 5 +- 3 files changed, 50 insertions(+), 37 deletions(-) diff --git a/storagemanager/LocalPool.tpp b/storagemanager/LocalPool.tpp index 98343ea7..59d06ab8 100644 --- a/storagemanager/LocalPool.tpp +++ b/storagemanager/LocalPool.tpp @@ -176,10 +176,9 @@ inline ReturnValue_t LocalPool::getData( template inline AccessorPair LocalPool::modifyData( store_address_t storeId) { - uint8_t* tempData = nullptr; StorageAccessor accessor(storeId, this); - ReturnValue_t status = modifyData(storeId, &tempData, &accessor.size_); - accessor.constDataPointer = tempData; + ReturnValue_t status = modifyData(storeId, &accessor.dataPointer, + &accessor.size_); accessor.assignConstPointer(); return AccessorPair(status, std::move(accessor)); } @@ -187,9 +186,9 @@ inline AccessorPair LocalPool::modifyData( template inline ReturnValue_t LocalPool::modifyData( store_address_t storeId, StorageAccessor& storeAccessor) { + storeAccessor.assignStore(this); ReturnValue_t status = modifyData(storeId, &storeAccessor.dataPointer, &storeAccessor.size_); - storeAccessor.assignStore(this); storeAccessor.assignConstPointer(); return status; } diff --git a/storagemanager/StorageAccessor.cpp b/storagemanager/StorageAccessor.cpp index 67693054..cc292d6c 100644 --- a/storagemanager/StorageAccessor.cpp +++ b/storagemanager/StorageAccessor.cpp @@ -16,6 +16,14 @@ ConstStorageAccessor::~ConstStorageAccessor() { } } +ConstStorageAccessor::ConstStorageAccessor(ConstStorageAccessor&& other): + constDataPointer(other.constDataPointer), storeId(other.storeId), + size_(other.size_), store(other.store), deleteData(other.deleteData), + internalState(other.internalState) { + // This prevent premature deletion + other.store = nullptr; +} + ConstStorageAccessor& ConstStorageAccessor::operator=( ConstStorageAccessor&& other) { constDataPointer = other.constDataPointer; @@ -29,36 +37,6 @@ ConstStorageAccessor& ConstStorageAccessor::operator=( return *this; } -StorageAccessor::StorageAccessor(store_address_t storeId): - ConstStorageAccessor(storeId) { -} - -StorageAccessor::StorageAccessor(store_address_t storeId, - StorageManagerIF* store): - ConstStorageAccessor(storeId, store) { -} - -StorageAccessor& StorageAccessor::operator =( - StorageAccessor&& other) { - // Call the parent move assignment and also assign own member. - dataPointer = other.dataPointer; - StorageAccessor::operator=(std::move(other)); - return * this; -} - -// Call the parent move ctor and also transfer own member. -StorageAccessor::StorageAccessor(StorageAccessor&& other): - ConstStorageAccessor(std::move(other)), dataPointer(other.dataPointer) { -} - -ConstStorageAccessor::ConstStorageAccessor(ConstStorageAccessor&& other): - constDataPointer(other.constDataPointer), storeId(other.storeId), - size_(other.size_), store(other.store), deleteData(other.deleteData), - internalState(other.internalState) { - // This prevent premature deletion - other.store = nullptr; -} - const uint8_t* ConstStorageAccessor::data() const { return constDataPointer; } @@ -113,6 +91,41 @@ void ConstStorageAccessor::assignStore(StorageManagerIF* store) { } +StorageAccessor::StorageAccessor(store_address_t storeId): + ConstStorageAccessor(storeId) { +} + +StorageAccessor::StorageAccessor(store_address_t storeId, + StorageManagerIF* store): + ConstStorageAccessor(storeId, store) { +} + +StorageAccessor& StorageAccessor::operator =( + StorageAccessor&& other) { + // Call the parent move assignment and also assign own member. + dataPointer = other.dataPointer; + StorageAccessor::operator=(std::move(other)); + return * this; +} + +// Call the parent move ctor and also transfer own member. +StorageAccessor::StorageAccessor(StorageAccessor&& other): + ConstStorageAccessor(std::move(other)), dataPointer(other.dataPointer) { +} + +ReturnValue_t StorageAccessor::getDataCopy(uint8_t *pointer, size_t maxSize) { + if(internalState == AccessState::UNINIT) { + sif::warning << "StorageAccessor: Not initialized!" << std::endl; + return HasReturnvaluesIF::RETURN_FAILED; + } + if(size_ > maxSize) { + sif::error << "StorageAccessor: Supplied buffer not large enough" << std::endl; + return HasReturnvaluesIF::RETURN_FAILED; + } + std::copy(dataPointer, dataPointer + size_, pointer); + return HasReturnvaluesIF::RETURN_OK; +} + uint8_t* StorageAccessor::data() { if(internalState == AccessState::UNINIT) { sif::warning << "StorageAccessor: Not initialized!" << std::endl; @@ -130,7 +143,7 @@ ReturnValue_t StorageAccessor::write(uint8_t *data, size_t size, sif::error << "StorageAccessor: Data too large for pool entry!" << std::endl; return HasReturnvaluesIF::RETURN_FAILED; } - std::copy(data, data + size, dataPointer); + std::copy(data, data + size, dataPointer + offset); return HasReturnvaluesIF::RETURN_OK; } diff --git a/storagemanager/StorageAccessor.h b/storagemanager/StorageAccessor.h index 2be80601..8949642c 100644 --- a/storagemanager/StorageAccessor.h +++ b/storagemanager/StorageAccessor.h @@ -49,7 +49,7 @@ public: * @brief Copies the read-only data to the supplied pointer * @param pointer */ - ReturnValue_t getDataCopy(uint8_t *pointer, size_t maxSize); + virtual ReturnValue_t getDataCopy(uint8_t *pointer, size_t maxSize); /** * @brief Calling this will prevent the Accessor from deleting the data @@ -137,8 +137,9 @@ public: StorageAccessor (StorageAccessor&&); ReturnValue_t write(uint8_t *data, size_t size, - uint16_t offset); + uint16_t offset = 0); uint8_t* data(); + ReturnValue_t getDataCopy(uint8_t *pointer, size_t maxSize) override; private: //! Non-const pointer for modifyable data.