mirror of
https://github.com/ETLCPP/etl.git
synced 2026-11-09 14:49:53 +08:00
Merge remote-tracking branch 'origin/development'
# Conflicts: # include/etl/version.h # support/Release notes.txt
This commit is contained in:
parent
09bc257d9f
commit
665c4591f2
@ -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 <b>must</b> 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 <typename T>
|
||||
struct wipe_on_destruct
|
||||
void memory_clear(T &object)
|
||||
{
|
||||
~wipe_on_destruct()
|
||||
{
|
||||
char* pobject = reinterpret_cast<char*>(static_cast<T*>(this));
|
||||
memset(pobject, 0, sizeof(T));
|
||||
}
|
||||
};
|
||||
std::fill_n(reinterpret_cast<char*>(&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 <typename T>
|
||||
void memory_set(T &object, int value)
|
||||
{
|
||||
std::fill_n(reinterpret_cast<char*>(&object),
|
||||
sizeof(T),
|
||||
static_cast<char>(value));
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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.
|
||||
|
||||
@ -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<Data>
|
||||
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<char, sizeof(Data)> 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<Data*>(buffer.data());
|
||||
CHECK(other == Data(1, 'b', 3.4));
|
||||
CHECK_EQUAL(0x00000000, data.d1);
|
||||
CHECK_EQUAL(0x00, data.d2);
|
||||
}
|
||||
|
||||
// Cleared compare buffer.
|
||||
std::array<char, sizeof(Data)> 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);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user