diff options
-rw-r--r-- | searchlib/src/tests/alignment/alignment.cpp | 5 | ||||
-rw-r--r-- | vespalib/src/tests/memory/memory_test.cpp | 4 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/util/memory.h | 16 |
3 files changed, 18 insertions, 7 deletions
diff --git a/searchlib/src/tests/alignment/alignment.cpp b/searchlib/src/tests/alignment/alignment.cpp index 06acf96e16c..3c6906f45bf 100644 --- a/searchlib/src/tests/alignment/alignment.cpp +++ b/searchlib/src/tests/alignment/alignment.cpp @@ -6,6 +6,9 @@ LOG_SETUP("alignment_test"); #include <sys/time.h> #include <vespa/vespalib/testkit/testapp.h> #include <vespa/vespalib/util/size_literals.h> +#include <vespa/vespalib/util/memory.h> + +using vespalib::Unaligned; struct Timer { rusage usage; @@ -28,7 +31,7 @@ TEST_SETUP(Test); double timeAccess(void *bufp, uint32_t len, double &sum) { - double *buf = (double *)bufp; + auto buf = Unaligned<double>::ptr(bufp); Timer timer; timer.start(); for(uint32_t i = 0; i < 512_Ki; ++i) { diff --git a/vespalib/src/tests/memory/memory_test.cpp b/vespalib/src/tests/memory/memory_test.cpp index 1c58da3f663..5345d98cbc7 100644 --- a/vespalib/src/tests/memory/memory_test.cpp +++ b/vespalib/src/tests/memory/memory_test.cpp @@ -168,8 +168,8 @@ TEST("require that Unaligned wrapper works as expected") { Data data; EXPECT_EQUAL(sizeof(Unaligned<uint32_t>), sizeof(uint32_t)); EXPECT_EQUAL(alignof(Unaligned<uint32_t>), 1u); - Unaligned<uint32_t> *arr = &Unaligned<uint32_t>::at(data.get(0)); - const Unaligned<uint32_t> *carr = &Unaligned<uint32_t>::at(data.cget(0)); + Unaligned<uint32_t> *arr = Unaligned<uint32_t>::ptr(data.get(0)); + const Unaligned<uint32_t> *carr = Unaligned<uint32_t>::ptr(data.cget(0)); Unaligned<uint32_t>::at(data.get(0)).write(123); Unaligned<uint32_t>::at(data.get(1)) = 456; arr[2] = 789; diff --git a/vespalib/src/vespa/vespalib/util/memory.h b/vespalib/src/vespa/vespalib/util/memory.h index f2ed6ccae3e..64428756e41 100644 --- a/vespalib/src/vespa/vespalib/util/memory.h +++ b/vespalib/src/vespa/vespalib/util/memory.h @@ -51,12 +51,20 @@ public: static_assert(std::is_trivial_v<T>); static_assert(alignof(T) > 1, "value is always aligned"); - constexpr static Unaligned &at(void *ptr) noexcept { - return *reinterpret_cast<Unaligned*>(ptr); + constexpr static Unaligned &at(void *p) noexcept { + return *reinterpret_cast<Unaligned*>(p); } - constexpr static const Unaligned &at(const void *ptr) noexcept { - return *reinterpret_cast<const Unaligned*>(ptr); + constexpr static const Unaligned &at(const void *p) noexcept { + return *reinterpret_cast<const Unaligned*>(p); } + + constexpr static Unaligned *ptr(void *p) noexcept { + return reinterpret_cast<Unaligned*>(p); + } + constexpr static const Unaligned *ptr(const void *p) noexcept { + return reinterpret_cast<const Unaligned*>(p); + } + T read() const noexcept { T value; static_assert(sizeof(_data) == sizeof(value)); |