diff --git a/include/etl/memory.h b/include/etl/memory.h index 35c8cfb9..4bdd43e4 100644 --- a/include/etl/memory.h +++ b/include/etl/memory.h @@ -1107,21 +1107,33 @@ namespace etl }; //***************************************************************************** - /// Base class for objects that require their memory to be wiped after use. - /// Erases the object's memory to zero. - /// Note: This must be the last destructor called for the derived object. - ///\tparam T The derived type. + /// A low level function that clears an object's memory to zero. + ///\tparam T The type. + ///\param object The object to clear. ///\ingroup memory //***************************************************************************** template - struct wipe_on_destruct + void memory_clear(T &object) { - ~wipe_on_destruct() - { - char* pobject = reinterpret_cast(static_cast(this)); - memset(pobject, 0, sizeof(T)); - } - }; + std::fill_n(reinterpret_cast(&object), + sizeof(T), + 0); + } + + //***************************************************************************** + /// A low level function that sets an object's memory to a value. + ///\tparam T The type. + ///\param object The object to set. + ///\param value The value to set the object with. + ///\ingroup memory + //***************************************************************************** + template + void memory_set(T &object, int value) + { + std::fill_n(reinterpret_cast(&object), + sizeof(T), + static_cast(value)); + } } #endif diff --git a/include/etl/version.h b/include/etl/version.h index 8ce39421..3bc191e9 100644 --- a/include/etl/version.h +++ b/include/etl/version.h @@ -38,7 +38,7 @@ SOFTWARE. ///\ingroup utilities #define ETL_VERSION_MAJOR 14 -#define ETL_VERSION_MINOR 21 +#define ETL_VERSION_MINOR 22 #define ETL_VERSION_PATCH 0 #define ETL_VERSION ETL_STRINGIFY(ETL_VERSION_MAJOR) ETL_STRINGIFY(ETL_VERSION_MINOR) ETL_STRINGIFY(ETL_VERSION_PATCH) diff --git a/support/Release notes.txt b/support/Release notes.txt index cde56ef4..40daab67 100644 --- a/support/Release notes.txt +++ b/support/Release notes.txt @@ -1,3 +1,8 @@ +=============================================================================== +14.22.0 +Added etl::memory_clear and etl::memory_set utility functions. +These functions will overwrite the memory allocated to the supplied object. + =============================================================================== 14.21.0 Added variadic min and max functions. diff --git a/test/test_memory.cpp b/test/test_memory.cpp index a0448891..49b0a3fb 100644 --- a/test/test_memory.cpp +++ b/test/test_memory.cpp @@ -506,7 +506,7 @@ namespace std::fill(std::begin(n), std::end(n), 0xFF); CHECK_EQUAL(0x00000000U, etl::make_default_at(pn)); - std::fill(std::begin(n), std::end(n), 0x00); + std::fill(std::begin(n), std::end(n), 0x00); CHECK_EQUAL(0xFFFFFFFFU, etl::make_value_at(pn, 0xFFFFFFFFU)); std::fill(std::begin(n), std::end(n), 0xFF); @@ -530,45 +530,37 @@ namespace } //************************************************************************* - TEST(test_wipe_on_destruct) + TEST(test_memory_clear) { - struct Data : public etl::wipe_on_destruct + struct Data { - Data(int a_, char b_, double c_) - : a(a_), - b(b_), - c(c_) - { - } - - bool operator ==(const Data& other) const - { - return (a == other.a) && (b == other.b) && (c == other.c); - } - - int a; - char b; - double c; + uint32_t d1; + char d2; }; - std::array buffer; - buffer.fill(0); + Data data = { 0xFFFFFFFF, char(0xFF) }; - // Construct in-place. - ::new (buffer.data()) Data(1, 'b', 3.4); + etl::memory_clear(data); - Data& other = *reinterpret_cast(buffer.data()); - CHECK(other == Data(1, 'b', 3.4)); + CHECK_EQUAL(0x00000000, data.d1); + CHECK_EQUAL(0x00, data.d2); + } - // Cleared compare buffer. - std::array clear; - clear.fill(0); + //************************************************************************* + TEST(test_memory_set) + { + struct Data + { + uint32_t d1; + char d2; + }; - // Destruct; - other.~Data(); + Data data = { 0xFFFFFFFF, char(0xFF) }; - // Storage should be wiped. - CHECK(buffer == clear); + etl::memory_set(data, 0x5A); + + CHECK_EQUAL(0x5A5A5A5A, data.d1); + CHECK_EQUAL(0x5A, data.d2); } }; }