1// Copyright 2011 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_V8MEMORY_H_
6#define V8_V8MEMORY_H_
7
8#include "src/globals.h"
9
10namespace v8 {
11namespace internal {
12
13// Memory provides an interface to 'raw' memory. It encapsulates the casts
14// that typically are needed when incompatible pointer types are used.
15// Note that this class currently relies on undefined behaviour. There is a
16// proposal (http://wg21.link/p0593r2) to make it defined behaviour though.
17template <class T>
18T& Memory(Address addr) {
19 // {addr} must be aligned.
20 DCHECK_EQ(0, addr & (alignof(T) - 1));
21 return *reinterpret_cast<T*>(addr);
22}
23template <class T>
24T& Memory(byte* addr) {
25 return Memory<T>(reinterpret_cast<Address>(addr));
26}
27
28template <typename V>
29static inline V ReadUnalignedValue(Address p) {
30 ASSERT_TRIVIALLY_COPYABLE(V);
31 V r;
32 memcpy(&r, reinterpret_cast<void*>(p), sizeof(V));
33 return r;
34}
35
36template <typename V>
37static inline void WriteUnalignedValue(Address p, V value) {
38 ASSERT_TRIVIALLY_COPYABLE(V);
39 memcpy(reinterpret_cast<void*>(p), &value, sizeof(V));
40}
41
42static inline double ReadFloatValue(Address p) {
43 return ReadUnalignedValue<float>(p);
44}
45
46static inline double ReadDoubleValue(Address p) {
47 return ReadUnalignedValue<double>(p);
48}
49
50static inline void WriteDoubleValue(Address p, double value) {
51 WriteUnalignedValue(p, value);
52}
53
54static inline uint16_t ReadUnalignedUInt16(Address p) {
55 return ReadUnalignedValue<uint16_t>(p);
56}
57
58static inline void WriteUnalignedUInt16(Address p, uint16_t value) {
59 WriteUnalignedValue(p, value);
60}
61
62static inline uint32_t ReadUnalignedUInt32(Address p) {
63 return ReadUnalignedValue<uint32_t>(p);
64}
65
66static inline void WriteUnalignedUInt32(Address p, uint32_t value) {
67 WriteUnalignedValue(p, value);
68}
69
70template <typename V>
71static inline V ReadLittleEndianValue(Address p) {
72#if defined(V8_TARGET_LITTLE_ENDIAN)
73 return ReadUnalignedValue<V>(p);
74#elif defined(V8_TARGET_BIG_ENDIAN)
75 V ret{};
76 const byte* src = reinterpret_cast<const byte*>(p);
77 byte* dst = reinterpret_cast<byte*>(&ret);
78 for (size_t i = 0; i < sizeof(V); i++) {
79 dst[i] = src[sizeof(V) - i - 1];
80 }
81 return ret;
82#endif // V8_TARGET_LITTLE_ENDIAN
83}
84
85template <typename V>
86static inline void WriteLittleEndianValue(Address p, V value) {
87#if defined(V8_TARGET_LITTLE_ENDIAN)
88 WriteUnalignedValue<V>(p, value);
89#elif defined(V8_TARGET_BIG_ENDIAN)
90 byte* src = reinterpret_cast<byte*>(&value);
91 byte* dst = reinterpret_cast<byte*>(p);
92 for (size_t i = 0; i < sizeof(V); i++) {
93 dst[i] = src[sizeof(V) - i - 1];
94 }
95#endif // V8_TARGET_LITTLE_ENDIAN
96}
97
98template <typename V>
99static inline V ReadLittleEndianValue(V* p) {
100 return ReadLittleEndianValue<V>(reinterpret_cast<Address>(p));
101}
102
103template <typename V>
104static inline void WriteLittleEndianValue(V* p, V value) {
105 WriteLittleEndianValue<V>(reinterpret_cast<Address>(p), value);
106}
107
108} // namespace internal
109} // namespace v8
110
111#endif // V8_V8MEMORY_H_
112