From 599931f0bf30503691b4719a6eebca0d50623392 Mon Sep 17 00:00:00 2001 From: rolandreichweinbmw Date: Sat, 4 Jan 2025 15:31:35 +0100 Subject: [PATCH] Fixed memory.h: mem_copy, mem_move, mem_compare for pointers to const (#1005) --- include/etl/memory.h | 74 +++++++++++++++++----------------- test/test_memory.cpp | 96 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+), 37 deletions(-) diff --git a/include/etl/memory.h b/include/etl/memory.h index b285068bf..14964b859 100644 --- a/include/etl/memory.h +++ b/include/etl/memory.h @@ -2264,13 +2264,13 @@ namespace etl /// \param destination begin /// \return A pointer to the destination. //*************************************************************************** - template - typename etl::enable_if::value_type>::value, TPointer>::type - mem_copy(const TPointer sb, const TPointer se, TPointer db) ETL_NOEXCEPT + template + typename etl::enable_if::value_type>::value, T*>::type + mem_copy(const T* sb, const T* se, T* db) ETL_NOEXCEPT { - return reinterpret_cast(memcpy(reinterpret_cast(db), - reinterpret_cast(sb), - sizeof(typename etl::iterator_traits::value_type) * static_cast(se - sb))); + return reinterpret_cast(memcpy(reinterpret_cast(db), + reinterpret_cast(sb), + sizeof(typename etl::iterator_traits::value_type) * static_cast(se - sb))); } //*************************************************************************** @@ -2280,13 +2280,13 @@ namespace etl /// \param source length /// \param destination begin //*************************************************************************** - template - typename etl::enable_if::value_type>::value, TPointer>::type - mem_copy(const TPointer sb, size_t n, TPointer db) ETL_NOEXCEPT + template + typename etl::enable_if::value_type>::value, T*>::type + mem_copy(const T* sb, size_t n, T* db) ETL_NOEXCEPT { - return reinterpret_cast(memcpy(reinterpret_cast(db), - reinterpret_cast(sb), - sizeof(typename etl::iterator_traits::value_type) * n)); + return reinterpret_cast(memcpy(reinterpret_cast(db), + reinterpret_cast(sb), + sizeof(typename etl::iterator_traits::value_type) * n)); } //*************************************************************************** @@ -2296,13 +2296,13 @@ namespace etl /// \param source end /// \param destination begin //*************************************************************************** - template - typename etl::enable_if::value_type>::value, TPointer>::type - mem_move(const TPointer sb, const TPointer se, TPointer db) ETL_NOEXCEPT + template + typename etl::enable_if::value_type>::value, T*>::type + mem_move(const T* sb, const T* se, T* db) ETL_NOEXCEPT { - return reinterpret_cast(memmove(reinterpret_cast(db), - reinterpret_cast(sb), - sizeof(typename etl::iterator_traits::value_type) * static_cast(se - sb))); + return reinterpret_cast(memmove(reinterpret_cast(db), + reinterpret_cast(sb), + sizeof(typename etl::iterator_traits::value_type) * static_cast(se - sb))); } //*************************************************************************** @@ -2312,13 +2312,13 @@ namespace etl /// \param source length /// \param destination begin //*************************************************************************** - template - typename etl::enable_if::value_type>::value, TPointer>::type - mem_move(const TPointer sb, size_t n, TPointer db) ETL_NOEXCEPT + template + typename etl::enable_if::value_type>::value, T*>::type + mem_move(const T* sb, size_t n, T* db) ETL_NOEXCEPT { - return reinterpret_cast(memmove(reinterpret_cast(db), - reinterpret_cast(sb), - sizeof(typename etl::iterator_traits::value_type) * n)); + return reinterpret_cast(memmove(reinterpret_cast(db), + reinterpret_cast(sb), + sizeof(typename etl::iterator_traits::value_type) * n)); } //*************************************************************************** @@ -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 + template ETL_NODISCARD - typename etl::enable_if::value_type>::value, int>::type - mem_compare(const TPointer sb, const TPointer se, TPointer db) ETL_NOEXCEPT + typename etl::enable_if::value_type>::value, int>::type + mem_compare(const T* sb, const T* se, const T* db) ETL_NOEXCEPT { - return memcmp(reinterpret_cast(db), - reinterpret_cast(sb), - sizeof(typename etl::iterator_traits::value_type) * static_cast(se - sb)); + return memcmp(reinterpret_cast(db), + reinterpret_cast(sb), + sizeof(typename etl::iterator_traits::value_type) * static_cast(se - sb)); } //*************************************************************************** @@ -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 + template ETL_NODISCARD - typename etl::enable_if::value_type>::value, int>::type - mem_compare(const TPointer sb, size_t n, TPointer db) ETL_NOEXCEPT + typename etl::enable_if::value_type>::value, int>::type + mem_compare(const T* sb, size_t n, const T* db) ETL_NOEXCEPT { - return memcmp(reinterpret_cast(db), - reinterpret_cast(sb), - sizeof(typename etl::iterator_traits::value_type) * n); + return memcmp(reinterpret_cast(db), + reinterpret_cast(sb), + sizeof(typename etl::iterator_traits::value_type) * n); } //*************************************************************************** @@ -2384,7 +2384,7 @@ namespace etl //*************************************************************************** template typename etl::enable_if::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(memset(reinterpret_cast(db), static_cast(value), diff --git a/test/test_memory.cpp b/test/test_memory.cpp index 9838094da..13cd14e67 100644 --- a/test/test_memory.cpp +++ b/test/test_memory.cpp @@ -1180,6 +1180,17 @@ 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) { @@ -1187,7 +1198,16 @@ namespace 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)); } @@ -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) { @@ -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) { @@ -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) { @@ -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) {