Skip to content

Commit

Permalink
Fixed memory.h: mem_copy, mem_move, mem_compare for pointers to const (
Browse files Browse the repository at this point in the history
  • Loading branch information
rolandreichweinbmw authored Jan 4, 2025
1 parent 99d7537 commit 599931f
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 37 deletions.
74 changes: 37 additions & 37 deletions include/etl/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -2264,13 +2264,13 @@ namespace etl
/// \param destination begin
/// \return A pointer to the destination.
//***************************************************************************
template <typename TPointer>
typename etl::enable_if<etl::is_trivially_copyable<typename etl::iterator_traits<TPointer>::value_type>::value, TPointer>::type
mem_copy(const TPointer sb, const TPointer se, TPointer db) ETL_NOEXCEPT
template <typename T>
typename etl::enable_if<etl::is_trivially_copyable<typename etl::iterator_traits<T*>::value_type>::value, T*>::type
mem_copy(const T* sb, const T* se, T* db) ETL_NOEXCEPT
{
return reinterpret_cast<TPointer>(memcpy(reinterpret_cast<void*>(db),
reinterpret_cast<void*>(sb),
sizeof(typename etl::iterator_traits<TPointer>::value_type) * static_cast<size_t>(se - sb)));
return reinterpret_cast<T*>(memcpy(reinterpret_cast<void*>(db),
reinterpret_cast<const void*>(sb),
sizeof(typename etl::iterator_traits<T*>::value_type) * static_cast<size_t>(se - sb)));
}

//***************************************************************************
Expand All @@ -2280,13 +2280,13 @@ namespace etl
/// \param source length
/// \param destination begin
//***************************************************************************
template <typename TPointer>
typename etl::enable_if<etl::is_trivially_copyable<typename etl::iterator_traits<TPointer>::value_type>::value, TPointer>::type
mem_copy(const TPointer sb, size_t n, TPointer db) ETL_NOEXCEPT
template <typename T>
typename etl::enable_if<etl::is_trivially_copyable<typename etl::iterator_traits<T*>::value_type>::value, T*>::type
mem_copy(const T* sb, size_t n, T* db) ETL_NOEXCEPT
{
return reinterpret_cast<TPointer>(memcpy(reinterpret_cast<void*>(db),
reinterpret_cast<void*>(sb),
sizeof(typename etl::iterator_traits<TPointer>::value_type) * n));
return reinterpret_cast<T*>(memcpy(reinterpret_cast<void*>(db),
reinterpret_cast<const void*>(sb),
sizeof(typename etl::iterator_traits<T*>::value_type) * n));
}

//***************************************************************************
Expand All @@ -2296,13 +2296,13 @@ namespace etl
/// \param source end
/// \param destination begin
//***************************************************************************
template <typename TPointer>
typename etl::enable_if<etl::is_trivially_copyable<typename etl::iterator_traits<TPointer>::value_type>::value, TPointer>::type
mem_move(const TPointer sb, const TPointer se, TPointer db) ETL_NOEXCEPT
template <typename T>
typename etl::enable_if<etl::is_trivially_copyable<typename etl::iterator_traits<T*>::value_type>::value, T*>::type
mem_move(const T* sb, const T* se, T* db) ETL_NOEXCEPT
{
return reinterpret_cast<TPointer>(memmove(reinterpret_cast<void*>(db),
reinterpret_cast<void*>(sb),
sizeof(typename etl::iterator_traits<TPointer>::value_type) * static_cast<size_t>(se - sb)));
return reinterpret_cast<T*>(memmove(reinterpret_cast<void*>(db),
reinterpret_cast<const void*>(sb),
sizeof(typename etl::iterator_traits<T*>::value_type) * static_cast<size_t>(se - sb)));
}

//***************************************************************************
Expand All @@ -2312,13 +2312,13 @@ namespace etl
/// \param source length
/// \param destination begin
//***************************************************************************
template <typename TPointer>
typename etl::enable_if<etl::is_trivially_copyable<typename etl::iterator_traits<TPointer>::value_type>::value, TPointer>::type
mem_move(const TPointer sb, size_t n, TPointer db) ETL_NOEXCEPT
template <typename T>
typename etl::enable_if<etl::is_trivially_copyable<typename etl::iterator_traits<T*>::value_type>::value, T*>::type
mem_move(const T* sb, size_t n, T* db) ETL_NOEXCEPT
{
return reinterpret_cast<TPointer>(memmove(reinterpret_cast<void*>(db),
reinterpret_cast<void*>(sb),
sizeof(typename etl::iterator_traits<TPointer>::value_type) * n));
return reinterpret_cast<T*>(memmove(reinterpret_cast<void*>(db),
reinterpret_cast<const void*>(sb),
sizeof(typename etl::iterator_traits<T*>::value_type) * n));
}

//***************************************************************************
Expand All @@ -2330,14 +2330,14 @@ namespace etl
/// 0 The contents of both memory blocks are equal
/// > 0 The first byte that does not match in both memory blocks has a greater value in 'sb' than in 'db' when evaluated as unsigned char values.
//***************************************************************************
template <typename TPointer>
template <typename T>
ETL_NODISCARD
typename etl::enable_if<etl::is_trivially_copyable<typename etl::iterator_traits<TPointer>::value_type>::value, int>::type
mem_compare(const TPointer sb, const TPointer se, TPointer db) ETL_NOEXCEPT
typename etl::enable_if<etl::is_trivially_copyable<typename etl::iterator_traits<T*>::value_type>::value, int>::type
mem_compare(const T* sb, const T* se, const T* db) ETL_NOEXCEPT
{
return memcmp(reinterpret_cast<void*>(db),
reinterpret_cast<void*>(sb),
sizeof(typename etl::iterator_traits<TPointer>::value_type) * static_cast<size_t>(se - sb));
return memcmp(reinterpret_cast<const void*>(db),
reinterpret_cast<const void*>(sb),
sizeof(typename etl::iterator_traits<T*>::value_type) * static_cast<size_t>(se - sb));
}

//***************************************************************************
Expand All @@ -2349,14 +2349,14 @@ namespace etl
/// 0 The contents of both memory blocks are equal
/// > 0 The first byte that does not match in both memory blocks has a greater value in 'sb' than in 'db' when evaluated as unsigned char values.
//***************************************************************************
template <typename TPointer>
template <typename T>
ETL_NODISCARD
typename etl::enable_if<etl::is_trivially_copyable<typename etl::iterator_traits<TPointer>::value_type>::value, int>::type
mem_compare(const TPointer sb, size_t n, TPointer db) ETL_NOEXCEPT
typename etl::enable_if<etl::is_trivially_copyable<typename etl::iterator_traits<T*>::value_type>::value, int>::type
mem_compare(const T* sb, size_t n, const T* db) ETL_NOEXCEPT
{
return memcmp(reinterpret_cast<void*>(db),
reinterpret_cast<void*>(sb),
sizeof(typename etl::iterator_traits<TPointer>::value_type) * n);
return memcmp(reinterpret_cast<const void*>(db),
reinterpret_cast<const void*>(sb),
sizeof(typename etl::iterator_traits<T*>::value_type) * n);
}

//***************************************************************************
Expand Down Expand Up @@ -2384,7 +2384,7 @@ namespace etl
//***************************************************************************
template <typename TPointer, typename T>
typename etl::enable_if<etl::is_trivially_copyable<typename etl::iterator_traits<TPointer>::value_type>::value, TPointer>::type
mem_set(const TPointer db, size_t n, T value) ETL_NOEXCEPT
mem_set(TPointer db, size_t n, T value) ETL_NOEXCEPT
{
return reinterpret_cast<TPointer>(memset(reinterpret_cast<void*>(db),
static_cast<char>(value),
Expand Down
96 changes: 96 additions & 0 deletions test/test_memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1180,14 +1180,34 @@ namespace
CHECK(std::equal(src, src + 8, dst));
}

//*************************************************************************
TEST(test_mem_copy_const_pointer_const_pointer_pointer)
{
const uint32_t src[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67234501, 0x45016723, 0x01324576, 0x76453201 };
uint32_t dst[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };

etl::mem_copy(src, src + 8, dst);

CHECK(std::equal(src, src + 8, dst));
}

//*************************************************************************
TEST(test_mem_copy_pointer_length_pointer)
{
uint32_t src[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67234501, 0x45016723, 0x01324576, 0x76453201 };
uint32_t dst[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };

etl::mem_copy(src, 8, dst);
CHECK(std::equal(src, src + 8, dst));
}

//*************************************************************************
TEST(test_mem_copy_const_pointer_length_pointer)
{
const uint32_t src[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67234501, 0x45016723, 0x01324576, 0x76453201 };
uint32_t dst[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };

etl::mem_copy(src, 8, dst);
CHECK(std::equal(src, src + 8, dst));
}

Expand All @@ -1202,6 +1222,19 @@ namespace
CHECK(std::equal(expected, expected + 8, data + 4));
}

//*************************************************************************
TEST(test_mem_move_const_pointer_const_pointer_pointer)
{
uint32_t expected[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67234501, 0x45016723, 0x01324576, 0x76453201 };
uint32_t data[12] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67234501, 0x45016723, 0x01324576, 0x76453201, 0, 0, 0, 0 };
const uint32_t* data_begin = &data[0];
const uint32_t* data_end = &data[8];

etl::mem_move(data_begin, data_end, data + 4);

CHECK(std::equal(expected, expected + 8, data + 4));
}

//*************************************************************************
TEST(test_mem_move_pointer_length_pointer)
{
Expand All @@ -1213,6 +1246,17 @@ namespace
CHECK(std::equal(expected, expected + 8, data + 4));
}

//*************************************************************************
TEST(test_mem_move_const_pointer_length_pointer)
{
uint32_t expected[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67234501, 0x45016723, 0x01324576, 0x76453201 };
uint32_t data[12] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67234501, 0x45016723, 0x01324576, 0x76453201, 0, 0, 0, 0 };
const uint32_t* data_begin = &data[0];
etl::mem_move(data_begin, 8, data + 4);

CHECK(std::equal(expected, expected + 8, data + 4));
}

//*************************************************************************
TEST(test_mem_compare_pointer_pointer_pointer)
{
Expand All @@ -1226,6 +1270,32 @@ namespace
CHECK(etl::mem_compare(data, data + 8, less) < 0);
}

//*************************************************************************
TEST(test_mem_compare_const_pointer_const_pointer_pointer)
{
const uint32_t data[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67234501, 0x45016723, 0x01324576, 0x76453201 };
uint32_t same[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67234501, 0x45016723, 0x01324576, 0x76453201 };
uint32_t grtr[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67235501, 0x45016723, 0x01324576, 0x76453201 };
uint32_t less[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67134501, 0x45016723, 0x01324576, 0x76453201 };

CHECK(etl::mem_compare(data, data + 8, same) == 0);
CHECK(etl::mem_compare(data, data + 8, grtr) > 0);
CHECK(etl::mem_compare(data, data + 8, less) < 0);
}

//*************************************************************************
TEST(test_mem_compare_const_pointer_const_pointer_const_pointer)
{
const uint32_t data[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67234501, 0x45016723, 0x01324576, 0x76453201 };
const uint32_t same[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67234501, 0x45016723, 0x01324576, 0x76453201 };
uint32_t grtr[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67235501, 0x45016723, 0x01324576, 0x76453201 };
uint32_t less[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67134501, 0x45016723, 0x01324576, 0x76453201 };

CHECK(etl::mem_compare(data, data + 8, same) == 0);
CHECK(etl::mem_compare(data, data + 8, grtr) > 0);
CHECK(etl::mem_compare(data, data + 8, less) < 0);
}

//*************************************************************************
TEST(test_mem_compare_pointer_length_pointer)
{
Expand All @@ -1239,6 +1309,32 @@ namespace
CHECK(etl::mem_compare(data, 8, less) < 0);
}

//*************************************************************************
TEST(test_mem_compare_const_pointer_length_pointer)
{
const uint32_t data[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67234501, 0x45016723, 0x01324576, 0x76453201 };
uint32_t same[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67234501, 0x45016723, 0x01324576, 0x76453201 };
uint32_t grtr[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67235501, 0x45016723, 0x01324576, 0x76453201 };
uint32_t less[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67134501, 0x45016723, 0x01324576, 0x76453201 };

CHECK(etl::mem_compare(data, 8, same) == 0);
CHECK(etl::mem_compare(data, 8, grtr) > 0);
CHECK(etl::mem_compare(data, 8, less) < 0);
}

//*************************************************************************
TEST(test_mem_compare_const_pointer_length_const_pointer)
{
const uint32_t data[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67234501, 0x45016723, 0x01324576, 0x76453201 };
const uint32_t same[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67234501, 0x45016723, 0x01324576, 0x76453201 };
const uint32_t grtr[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67235501, 0x45016723, 0x01324576, 0x76453201 };
const uint32_t less[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67134501, 0x45016723, 0x01324576, 0x76453201 };

CHECK(etl::mem_compare(data, 8, same) == 0);
CHECK(etl::mem_compare(data, 8, grtr) > 0);
CHECK(etl::mem_compare(data, 8, less) < 0);
}

//*************************************************************************
TEST(test_mem_set_pointer_pointer)
{
Expand Down

0 comments on commit 599931f

Please sign in to comment.