| 1 | // Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file |
| 2 | // for details. All rights reserved. Use of this source code is governed by a |
| 3 | // BSD-style license that can be found in the LICENSE file. |
| 4 | |
| 5 | #ifndef RUNTIME_PLATFORM_UNALIGNED_H_ |
| 6 | #define RUNTIME_PLATFORM_UNALIGNED_H_ |
| 7 | |
| 8 | #include "platform/globals.h" |
| 9 | #include "platform/undefined_behavior_sanitizer.h" |
| 10 | |
| 11 | namespace dart { |
| 12 | |
| 13 | #if defined(HOST_ARCH_ARM) || defined(HOST_ARCH_ARM64) |
| 14 | template <typename T> |
| 15 | static inline T LoadUnaligned(const T* ptr) { |
| 16 | T value; |
| 17 | memcpy(reinterpret_cast<void*>(&value), reinterpret_cast<const void*>(ptr), |
| 18 | sizeof(value)); |
| 19 | return value; |
| 20 | } |
| 21 | |
| 22 | template <typename T> |
| 23 | static inline void StoreUnaligned(T* ptr, T value) { |
| 24 | memcpy(reinterpret_cast<void*>(ptr), reinterpret_cast<const void*>(&value), |
| 25 | sizeof(value)); |
| 26 | } |
| 27 | #else // !(HOST_ARCH_ARM || HOST_ARCH_ARM64) |
| 28 | template <typename T> |
| 29 | NO_SANITIZE_UNDEFINED("alignment" ) |
| 30 | static inline T LoadUnaligned(const T* ptr) { |
| 31 | return *ptr; |
| 32 | } |
| 33 | |
| 34 | template <typename T> |
| 35 | NO_SANITIZE_UNDEFINED("alignment" ) |
| 36 | static inline void StoreUnaligned(T* ptr, T value) { |
| 37 | *ptr = value; |
| 38 | } |
| 39 | #endif // !(HOST_ARCH_ARM || HOST_ARCH_ARM64) |
| 40 | |
| 41 | } // namespace dart |
| 42 | |
| 43 | #endif // RUNTIME_PLATFORM_UNALIGNED_H_ |
| 44 | |