Skip to content

Commit 599931f

Browse files
Fixed memory.h: mem_copy, mem_move, mem_compare for pointers to const (#1005)
1 parent 99d7537 commit 599931f

File tree

2 files changed

+133
-37
lines changed

2 files changed

+133
-37
lines changed

include/etl/memory.h

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2264,13 +2264,13 @@ namespace etl
22642264
/// \param destination begin
22652265
/// \return A pointer to the destination.
22662266
//***************************************************************************
2267-
template <typename TPointer>
2268-
typename etl::enable_if<etl::is_trivially_copyable<typename etl::iterator_traits<TPointer>::value_type>::value, TPointer>::type
2269-
mem_copy(const TPointer sb, const TPointer se, TPointer db) ETL_NOEXCEPT
2267+
template <typename T>
2268+
typename etl::enable_if<etl::is_trivially_copyable<typename etl::iterator_traits<T*>::value_type>::value, T*>::type
2269+
mem_copy(const T* sb, const T* se, T* db) ETL_NOEXCEPT
22702270
{
2271-
return reinterpret_cast<TPointer>(memcpy(reinterpret_cast<void*>(db),
2272-
reinterpret_cast<void*>(sb),
2273-
sizeof(typename etl::iterator_traits<TPointer>::value_type) * static_cast<size_t>(se - sb)));
2271+
return reinterpret_cast<T*>(memcpy(reinterpret_cast<void*>(db),
2272+
reinterpret_cast<const void*>(sb),
2273+
sizeof(typename etl::iterator_traits<T*>::value_type) * static_cast<size_t>(se - sb)));
22742274
}
22752275

22762276
//***************************************************************************
@@ -2280,13 +2280,13 @@ namespace etl
22802280
/// \param source length
22812281
/// \param destination begin
22822282
//***************************************************************************
2283-
template <typename TPointer>
2284-
typename etl::enable_if<etl::is_trivially_copyable<typename etl::iterator_traits<TPointer>::value_type>::value, TPointer>::type
2285-
mem_copy(const TPointer sb, size_t n, TPointer db) ETL_NOEXCEPT
2283+
template <typename T>
2284+
typename etl::enable_if<etl::is_trivially_copyable<typename etl::iterator_traits<T*>::value_type>::value, T*>::type
2285+
mem_copy(const T* sb, size_t n, T* db) ETL_NOEXCEPT
22862286
{
2287-
return reinterpret_cast<TPointer>(memcpy(reinterpret_cast<void*>(db),
2288-
reinterpret_cast<void*>(sb),
2289-
sizeof(typename etl::iterator_traits<TPointer>::value_type) * n));
2287+
return reinterpret_cast<T*>(memcpy(reinterpret_cast<void*>(db),
2288+
reinterpret_cast<const void*>(sb),
2289+
sizeof(typename etl::iterator_traits<T*>::value_type) * n));
22902290
}
22912291

22922292
//***************************************************************************
@@ -2296,13 +2296,13 @@ namespace etl
22962296
/// \param source end
22972297
/// \param destination begin
22982298
//***************************************************************************
2299-
template <typename TPointer>
2300-
typename etl::enable_if<etl::is_trivially_copyable<typename etl::iterator_traits<TPointer>::value_type>::value, TPointer>::type
2301-
mem_move(const TPointer sb, const TPointer se, TPointer db) ETL_NOEXCEPT
2299+
template <typename T>
2300+
typename etl::enable_if<etl::is_trivially_copyable<typename etl::iterator_traits<T*>::value_type>::value, T*>::type
2301+
mem_move(const T* sb, const T* se, T* db) ETL_NOEXCEPT
23022302
{
2303-
return reinterpret_cast<TPointer>(memmove(reinterpret_cast<void*>(db),
2304-
reinterpret_cast<void*>(sb),
2305-
sizeof(typename etl::iterator_traits<TPointer>::value_type) * static_cast<size_t>(se - sb)));
2303+
return reinterpret_cast<T*>(memmove(reinterpret_cast<void*>(db),
2304+
reinterpret_cast<const void*>(sb),
2305+
sizeof(typename etl::iterator_traits<T*>::value_type) * static_cast<size_t>(se - sb)));
23062306
}
23072307

23082308
//***************************************************************************
@@ -2312,13 +2312,13 @@ namespace etl
23122312
/// \param source length
23132313
/// \param destination begin
23142314
//***************************************************************************
2315-
template <typename TPointer>
2316-
typename etl::enable_if<etl::is_trivially_copyable<typename etl::iterator_traits<TPointer>::value_type>::value, TPointer>::type
2317-
mem_move(const TPointer sb, size_t n, TPointer db) ETL_NOEXCEPT
2315+
template <typename T>
2316+
typename etl::enable_if<etl::is_trivially_copyable<typename etl::iterator_traits<T*>::value_type>::value, T*>::type
2317+
mem_move(const T* sb, size_t n, T* db) ETL_NOEXCEPT
23182318
{
2319-
return reinterpret_cast<TPointer>(memmove(reinterpret_cast<void*>(db),
2320-
reinterpret_cast<void*>(sb),
2321-
sizeof(typename etl::iterator_traits<TPointer>::value_type) * n));
2319+
return reinterpret_cast<T*>(memmove(reinterpret_cast<void*>(db),
2320+
reinterpret_cast<const void*>(sb),
2321+
sizeof(typename etl::iterator_traits<T*>::value_type) * n));
23222322
}
23232323

23242324
//***************************************************************************
@@ -2330,14 +2330,14 @@ namespace etl
23302330
/// 0 The contents of both memory blocks are equal
23312331
/// > 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.
23322332
//***************************************************************************
2333-
template <typename TPointer>
2333+
template <typename T>
23342334
ETL_NODISCARD
2335-
typename etl::enable_if<etl::is_trivially_copyable<typename etl::iterator_traits<TPointer>::value_type>::value, int>::type
2336-
mem_compare(const TPointer sb, const TPointer se, TPointer db) ETL_NOEXCEPT
2335+
typename etl::enable_if<etl::is_trivially_copyable<typename etl::iterator_traits<T*>::value_type>::value, int>::type
2336+
mem_compare(const T* sb, const T* se, const T* db) ETL_NOEXCEPT
23372337
{
2338-
return memcmp(reinterpret_cast<void*>(db),
2339-
reinterpret_cast<void*>(sb),
2340-
sizeof(typename etl::iterator_traits<TPointer>::value_type) * static_cast<size_t>(se - sb));
2338+
return memcmp(reinterpret_cast<const void*>(db),
2339+
reinterpret_cast<const void*>(sb),
2340+
sizeof(typename etl::iterator_traits<T*>::value_type) * static_cast<size_t>(se - sb));
23412341
}
23422342

23432343
//***************************************************************************
@@ -2349,14 +2349,14 @@ namespace etl
23492349
/// 0 The contents of both memory blocks are equal
23502350
/// > 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.
23512351
//***************************************************************************
2352-
template <typename TPointer>
2352+
template <typename T>
23532353
ETL_NODISCARD
2354-
typename etl::enable_if<etl::is_trivially_copyable<typename etl::iterator_traits<TPointer>::value_type>::value, int>::type
2355-
mem_compare(const TPointer sb, size_t n, TPointer db) ETL_NOEXCEPT
2354+
typename etl::enable_if<etl::is_trivially_copyable<typename etl::iterator_traits<T*>::value_type>::value, int>::type
2355+
mem_compare(const T* sb, size_t n, const T* db) ETL_NOEXCEPT
23562356
{
2357-
return memcmp(reinterpret_cast<void*>(db),
2358-
reinterpret_cast<void*>(sb),
2359-
sizeof(typename etl::iterator_traits<TPointer>::value_type) * n);
2357+
return memcmp(reinterpret_cast<const void*>(db),
2358+
reinterpret_cast<const void*>(sb),
2359+
sizeof(typename etl::iterator_traits<T*>::value_type) * n);
23602360
}
23612361

23622362
//***************************************************************************
@@ -2384,7 +2384,7 @@ namespace etl
23842384
//***************************************************************************
23852385
template <typename TPointer, typename T>
23862386
typename etl::enable_if<etl::is_trivially_copyable<typename etl::iterator_traits<TPointer>::value_type>::value, TPointer>::type
2387-
mem_set(const TPointer db, size_t n, T value) ETL_NOEXCEPT
2387+
mem_set(TPointer db, size_t n, T value) ETL_NOEXCEPT
23882388
{
23892389
return reinterpret_cast<TPointer>(memset(reinterpret_cast<void*>(db),
23902390
static_cast<char>(value),

test/test_memory.cpp

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,14 +1180,34 @@ namespace
11801180
CHECK(std::equal(src, src + 8, dst));
11811181
}
11821182

1183+
//*************************************************************************
1184+
TEST(test_mem_copy_const_pointer_const_pointer_pointer)
1185+
{
1186+
const uint32_t src[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67234501, 0x45016723, 0x01324576, 0x76453201 };
1187+
uint32_t dst[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
1188+
1189+
etl::mem_copy(src, src + 8, dst);
1190+
1191+
CHECK(std::equal(src, src + 8, dst));
1192+
}
1193+
11831194
//*************************************************************************
11841195
TEST(test_mem_copy_pointer_length_pointer)
11851196
{
11861197
uint32_t src[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67234501, 0x45016723, 0x01324576, 0x76453201 };
11871198
uint32_t dst[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
11881199

11891200
etl::mem_copy(src, 8, dst);
1201+
CHECK(std::equal(src, src + 8, dst));
1202+
}
1203+
1204+
//*************************************************************************
1205+
TEST(test_mem_copy_const_pointer_length_pointer)
1206+
{
1207+
const uint32_t src[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67234501, 0x45016723, 0x01324576, 0x76453201 };
1208+
uint32_t dst[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
11901209

1210+
etl::mem_copy(src, 8, dst);
11911211
CHECK(std::equal(src, src + 8, dst));
11921212
}
11931213

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

1225+
//*************************************************************************
1226+
TEST(test_mem_move_const_pointer_const_pointer_pointer)
1227+
{
1228+
uint32_t expected[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67234501, 0x45016723, 0x01324576, 0x76453201 };
1229+
uint32_t data[12] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67234501, 0x45016723, 0x01324576, 0x76453201, 0, 0, 0, 0 };
1230+
const uint32_t* data_begin = &data[0];
1231+
const uint32_t* data_end = &data[8];
1232+
1233+
etl::mem_move(data_begin, data_end, data + 4);
1234+
1235+
CHECK(std::equal(expected, expected + 8, data + 4));
1236+
}
1237+
12051238
//*************************************************************************
12061239
TEST(test_mem_move_pointer_length_pointer)
12071240
{
@@ -1213,6 +1246,17 @@ namespace
12131246
CHECK(std::equal(expected, expected + 8, data + 4));
12141247
}
12151248

1249+
//*************************************************************************
1250+
TEST(test_mem_move_const_pointer_length_pointer)
1251+
{
1252+
uint32_t expected[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67234501, 0x45016723, 0x01324576, 0x76453201 };
1253+
uint32_t data[12] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67234501, 0x45016723, 0x01324576, 0x76453201, 0, 0, 0, 0 };
1254+
const uint32_t* data_begin = &data[0];
1255+
etl::mem_move(data_begin, 8, data + 4);
1256+
1257+
CHECK(std::equal(expected, expected + 8, data + 4));
1258+
}
1259+
12161260
//*************************************************************************
12171261
TEST(test_mem_compare_pointer_pointer_pointer)
12181262
{
@@ -1226,6 +1270,32 @@ namespace
12261270
CHECK(etl::mem_compare(data, data + 8, less) < 0);
12271271
}
12281272

1273+
//*************************************************************************
1274+
TEST(test_mem_compare_const_pointer_const_pointer_pointer)
1275+
{
1276+
const uint32_t data[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67234501, 0x45016723, 0x01324576, 0x76453201 };
1277+
uint32_t same[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67234501, 0x45016723, 0x01324576, 0x76453201 };
1278+
uint32_t grtr[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67235501, 0x45016723, 0x01324576, 0x76453201 };
1279+
uint32_t less[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67134501, 0x45016723, 0x01324576, 0x76453201 };
1280+
1281+
CHECK(etl::mem_compare(data, data + 8, same) == 0);
1282+
CHECK(etl::mem_compare(data, data + 8, grtr) > 0);
1283+
CHECK(etl::mem_compare(data, data + 8, less) < 0);
1284+
}
1285+
1286+
//*************************************************************************
1287+
TEST(test_mem_compare_const_pointer_const_pointer_const_pointer)
1288+
{
1289+
const uint32_t data[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67234501, 0x45016723, 0x01324576, 0x76453201 };
1290+
const uint32_t same[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67234501, 0x45016723, 0x01324576, 0x76453201 };
1291+
uint32_t grtr[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67235501, 0x45016723, 0x01324576, 0x76453201 };
1292+
uint32_t less[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67134501, 0x45016723, 0x01324576, 0x76453201 };
1293+
1294+
CHECK(etl::mem_compare(data, data + 8, same) == 0);
1295+
CHECK(etl::mem_compare(data, data + 8, grtr) > 0);
1296+
CHECK(etl::mem_compare(data, data + 8, less) < 0);
1297+
}
1298+
12291299
//*************************************************************************
12301300
TEST(test_mem_compare_pointer_length_pointer)
12311301
{
@@ -1239,6 +1309,32 @@ namespace
12391309
CHECK(etl::mem_compare(data, 8, less) < 0);
12401310
}
12411311

1312+
//*************************************************************************
1313+
TEST(test_mem_compare_const_pointer_length_pointer)
1314+
{
1315+
const uint32_t data[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67234501, 0x45016723, 0x01324576, 0x76453201 };
1316+
uint32_t same[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67234501, 0x45016723, 0x01324576, 0x76453201 };
1317+
uint32_t grtr[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67235501, 0x45016723, 0x01324576, 0x76453201 };
1318+
uint32_t less[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67134501, 0x45016723, 0x01324576, 0x76453201 };
1319+
1320+
CHECK(etl::mem_compare(data, 8, same) == 0);
1321+
CHECK(etl::mem_compare(data, 8, grtr) > 0);
1322+
CHECK(etl::mem_compare(data, 8, less) < 0);
1323+
}
1324+
1325+
//*************************************************************************
1326+
TEST(test_mem_compare_const_pointer_length_const_pointer)
1327+
{
1328+
const uint32_t data[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67234501, 0x45016723, 0x01324576, 0x76453201 };
1329+
const uint32_t same[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67234501, 0x45016723, 0x01324576, 0x76453201 };
1330+
const uint32_t grtr[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67235501, 0x45016723, 0x01324576, 0x76453201 };
1331+
const uint32_t less[8] = { 0x12345678, 0x76543210, 0x01452367, 0x23670145, 0x67134501, 0x45016723, 0x01324576, 0x76453201 };
1332+
1333+
CHECK(etl::mem_compare(data, 8, same) == 0);
1334+
CHECK(etl::mem_compare(data, 8, grtr) > 0);
1335+
CHECK(etl::mem_compare(data, 8, less) < 0);
1336+
}
1337+
12421338
//*************************************************************************
12431339
TEST(test_mem_set_pointer_pointer)
12441340
{

0 commit comments

Comments
 (0)