From 3705e39db987fdc7037aaaff9af6d4a16fddb36b Mon Sep 17 00:00:00 2001 From: Jakob Hasse Date: Wed, 23 Nov 2022 14:40:38 +0100 Subject: [PATCH] refactor(nvs): custom allocator for all objects allocated in NVS --- components/nvs_flash/src/intrusive_list.h | 1 + components/nvs_flash/src/nvs_api.cpp | 4 +- .../nvs_flash/src/nvs_handle_simple.hpp | 23 +++---- .../nvs_flash/src/nvs_item_hash_list.hpp | 21 +++---- .../nvs_flash/src/nvs_memory_management.hpp | 62 +++++++++++++++++++ components/nvs_flash/src/nvs_ops.cpp | 4 +- components/nvs_flash/src/nvs_page.hpp | 9 +-- .../nvs_flash/src/nvs_partition_manager.hpp | 21 +++---- components/nvs_flash/src/nvs_storage.hpp | 28 ++++----- 9 files changed, 108 insertions(+), 65 deletions(-) create mode 100644 components/nvs_flash/src/nvs_memory_management.hpp diff --git a/components/nvs_flash/src/intrusive_list.h b/components/nvs_flash/src/intrusive_list.h index bb580502ec..d03457dbe2 100644 --- a/components/nvs_flash/src/intrusive_list.h +++ b/components/nvs_flash/src/intrusive_list.h @@ -15,6 +15,7 @@ #define intrusive_list_h #include +#include #include template diff --git a/components/nvs_flash/src/nvs_api.cpp b/components/nvs_flash/src/nvs_api.cpp index 8d643ea41e..d06e88cf23 100644 --- a/components/nvs_flash/src/nvs_api.cpp +++ b/components/nvs_flash/src/nvs_api.cpp @@ -24,6 +24,8 @@ #ifdef CONFIG_NVS_ENCRYPTION #include "nvs_encr.hpp" #endif +#include "nvs_memory_management.hpp" +#include "esp_err.h" #ifdef ESP_PLATFORM #include @@ -37,7 +39,7 @@ static const char* TAG = "nvs"; #define ESP_LOGD(...) #endif -class NVSHandleEntry : public intrusive_list_node { +class NVSHandleEntry : public intrusive_list_node, public ExceptionlessAllocatable { public: NVSHandleEntry(nvs::NVSHandleSimple *handle, const char* part_name) : nvs_handle(handle), diff --git a/components/nvs_flash/src/nvs_handle_simple.hpp b/components/nvs_flash/src/nvs_handle_simple.hpp index 0f3a407fdc..8fb7cbc721 100644 --- a/components/nvs_flash/src/nvs_handle_simple.hpp +++ b/components/nvs_flash/src/nvs_handle_simple.hpp @@ -1,16 +1,8 @@ -// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at - -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/* + * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #ifndef NVS_HANDLE_SIMPLE_HPP_ #define NVS_HANDLE_SIMPLE_HPP_ @@ -18,6 +10,7 @@ #include "nvs_storage.hpp" #include "nvs_platform.hpp" +#include "nvs_memory_management.hpp" #include "nvs_handle.hpp" namespace nvs { @@ -30,7 +23,9 @@ namespace nvs { * * For more details about the general member functions, see nvs_handle.hpp. */ -class NVSHandleSimple : public intrusive_list_node, public NVSHandle { +class NVSHandleSimple : public intrusive_list_node, + public NVSHandle, + public ExceptionlessAllocatable { friend class NVSPartitionManager; public: NVSHandleSimple(bool readOnly, uint8_t nsIndex, Storage *StoragePtr) : diff --git a/components/nvs_flash/src/nvs_item_hash_list.hpp b/components/nvs_flash/src/nvs_item_hash_list.hpp index ca21c92c18..60e24e55b0 100644 --- a/components/nvs_flash/src/nvs_item_hash_list.hpp +++ b/components/nvs_flash/src/nvs_item_hash_list.hpp @@ -1,22 +1,15 @@ -// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at - -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/* + * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #ifndef nvs_item_hash_list_h #define nvs_item_hash_list_h #include "nvs.h" #include "nvs_types.hpp" +#include "nvs_memory_management.hpp" #include "intrusive_list.h" namespace nvs @@ -54,7 +47,7 @@ protected: uint32_t mHash : 24; }; - struct HashListBlock : public intrusive_list_node { + struct HashListBlock : public intrusive_list_node, public ExceptionlessAllocatable { HashListBlock(); static const size_t BYTE_SIZE = 128; diff --git a/components/nvs_flash/src/nvs_memory_management.hpp b/components/nvs_flash/src/nvs_memory_management.hpp new file mode 100644 index 0000000000..933f08256e --- /dev/null +++ b/components/nvs_flash/src/nvs_memory_management.hpp @@ -0,0 +1,62 @@ +/* + * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +#pragma once + +/** + * @brief Type that is only usable with new (std::nothrow) to avoid exceptions. + * + * This struct shall be inherited by all types in NVS that may be allocated dynamically (with new). + * + * NVS allocates memory at runtime. Because we use C++, we need to avoid the global ``operator new`` from libstdc++, + * since it throws exceptions and we compile NVS with ``-fno-exceptions``. We also need to avoid the global + * non-throwing version of that operator from libstdc++, since it is merely a wrapper around the original operator + * catching all exceptions. + * + * This struct removes the normal operator new from this and all types inheriting from it. It furthermore provides + * a custom version of operator new (..., const std::nothrow_t&) noexcept that will not use C++ exceptions. + * + * E.g., if you have a type MyType inheriting from ExceptionlessAllocatable, you want to use it as follows: + * @code{c++} + * MyType : public ExceptionlessAllocatable { + * ExceptionlessAllocatable(); + * ExceptionlessAllocatable(int param); + * }; + * // ... + * MyType *m0 = new (std::nothrow) MyType; + * MyType *m1 = new (std::nothrow) MyType(47); + * // ... + * delete m1; + * delete m0; + * @endcode + */ +struct ExceptionlessAllocatable { + /** + * Disallow use of the default new operator, all of NVS is currently tailored to not throw exceptions + */ + static void *operator new( std::size_t ) = delete; + + /** + * Simple implementation with malloc(). No exceptions are thrown if the allocation fails. + * To use this operator, your type must inherit from this class and then allocate with: + * @code{c} + * new (std::nothrow) ; // default constructor + * new (std::nothrow) (); // non-default constructor + * @endcode + */ + void *operator new (size_t size, const std::nothrow_t&) noexcept { + return std::malloc(size); + } + + /** + * Use \c delete as normal. This operator will be called automatically instead of the global one from libstdc++. + */ + void operator delete (void *obj) noexcept { + free(obj); + } +}; diff --git a/components/nvs_flash/src/nvs_ops.cpp b/components/nvs_flash/src/nvs_ops.cpp index 4dfcc9c111..427b8d5ae1 100644 --- a/components/nvs_flash/src/nvs_ops.cpp +++ b/components/nvs_flash/src/nvs_ops.cpp @@ -14,9 +14,11 @@ #include "esp_spi_flash.h" #include "nvs_ops.hpp" +#include #ifdef CONFIG_NVS_ENCRYPTION #include "nvs_encr.hpp" #include +#include #endif namespace nvs @@ -39,7 +41,7 @@ esp_err_t nvs_flash_write(size_t destAddr, const void *srcAddr, size_t size) { return err; } err = spi_flash_write(destAddr, buf, size); - delete buf; + free(buf); return err; } } diff --git a/components/nvs_flash/src/nvs_page.hpp b/components/nvs_flash/src/nvs_page.hpp index a491752406..bcc5512f8e 100644 --- a/components/nvs_flash/src/nvs_page.hpp +++ b/components/nvs_flash/src/nvs_page.hpp @@ -24,12 +24,13 @@ #include "compressed_enum_table.hpp" #include "intrusive_list.h" #include "nvs_item_hash_list.hpp" +#include "nvs_memory_management.hpp" namespace nvs { -class Page : public intrusive_list_node +class Page : public intrusive_list_node, public ExceptionlessAllocatable { public: static const uint32_t PSB_INIT = 0x1; @@ -87,7 +88,7 @@ public: esp_err_t getSeqNumber(uint32_t& seqNumber) const; esp_err_t setSeqNumber(uint32_t seqNumber); - + esp_err_t setVersion(uint8_t version); esp_err_t writeItem(uint8_t nsIndex, ItemType datatype, const char* key, const void* data, size_t dataSize, uint8_t chunkIdx = CHUNK_ANY); @@ -188,7 +189,7 @@ protected: esp_err_t readEntry(size_t index, Item& dst) const; esp_err_t writeEntry(const Item& item); - + esp_err_t writeEntryData(const uint8_t* data, size_t size); esp_err_t eraseEntryAndSpan(size_t index); @@ -205,7 +206,7 @@ protected: assert(entry < ENTRY_COUNT); return mBaseAddress + ENTRY_DATA_OFFSET + static_cast(entry) * ENTRY_SIZE; } - + static const char* pageStateToName(PageState ps); diff --git a/components/nvs_flash/src/nvs_partition_manager.hpp b/components/nvs_flash/src/nvs_partition_manager.hpp index baa0c09d10..f332042b2d 100644 --- a/components/nvs_flash/src/nvs_partition_manager.hpp +++ b/components/nvs_flash/src/nvs_partition_manager.hpp @@ -1,21 +1,14 @@ -// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at - -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/* + * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #ifndef NVS_PARTITION_MANAGER_HPP_ #define NVS_PARTITION_MANAGER_HPP_ #include "nvs_handle_simple.hpp" #include "nvs_storage.hpp" +#include "nvs_memory_management.hpp" #ifdef CONFIG_NVS_ENCRYPTION #include "nvs_encr.hpp" @@ -23,7 +16,7 @@ namespace nvs { -class NVSPartitionManager { +class NVSPartitionManager : public ExceptionlessAllocatable { public: virtual ~NVSPartitionManager() { } diff --git a/components/nvs_flash/src/nvs_storage.hpp b/components/nvs_flash/src/nvs_storage.hpp index 9e17c7179c..9be3d01ecb 100644 --- a/components/nvs_flash/src/nvs_storage.hpp +++ b/components/nvs_flash/src/nvs_storage.hpp @@ -1,39 +1,33 @@ -// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at - -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/* + * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #ifndef nvs_storage_hpp #define nvs_storage_hpp #include +#include #include #include "nvs.hpp" #include "nvs_types.hpp" #include "nvs_page.hpp" #include "nvs_pagemanager.hpp" +#include "nvs_memory_management.hpp" //extern void dumpBytes(const uint8_t* data, size_t count); namespace nvs { -class Storage : public intrusive_list_node +class Storage : public intrusive_list_node, public ExceptionlessAllocatable { enum class StorageState : uint32_t { INVALID, ACTIVE, }; - struct NamespaceEntry : public intrusive_list_node { + struct NamespaceEntry : public intrusive_list_node, public ExceptionlessAllocatable { public: char mName[Item::MAX_KEY_LENGTH + 1]; uint8_t mIndex; @@ -41,13 +35,13 @@ class Storage : public intrusive_list_node typedef intrusive_list TNamespaces; - struct UsedPageNode: public intrusive_list_node { + struct UsedPageNode: public intrusive_list_node, public ExceptionlessAllocatable { public: Page* mPage; }; typedef intrusive_list TUsedPageList; - struct BlobIndexNode: public intrusive_list_node { + struct BlobIndexNode: public intrusive_list_node, public ExceptionlessAllocatable { public: char key[Item::MAX_KEY_LENGTH + 1]; uint8_t nsIndex;