Line data Source code
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 :
10 : namespace v8 {
11 : namespace 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.
17 : template <class T>
18 : T& Memory(Address addr) {
19 : // {addr} must be aligned.
20 : DCHECK_EQ(0, addr & (alignof(T) - 1));
21 402165741 : return *reinterpret_cast<T*>(addr);
22 : }
23 : template <class T>
24 : T& Memory(byte* addr) {
25 : return Memory<T>(reinterpret_cast<Address>(addr));
26 : }
27 :
28 : template <typename V>
29 1748725 : static inline V ReadUnalignedValue(Address p) {
30 : ASSERT_TRIVIALLY_COPYABLE(V);
31 : V r;
32 60404303 : memcpy(&r, reinterpret_cast<void*>(p), sizeof(V));
33 10428048 : return r;
34 : }
35 :
36 : template <typename V>
37 : static inline void WriteUnalignedValue(Address p, V value) {
38 : ASSERT_TRIVIALLY_COPYABLE(V);
39 144051248 : memcpy(reinterpret_cast<void*>(p), &value, sizeof(V));
40 : }
41 :
42 : static inline double ReadFloatValue(Address p) {
43 : return ReadUnalignedValue<float>(p);
44 : }
45 :
46 : static inline double ReadDoubleValue(Address p) {
47 : return ReadUnalignedValue<double>(p);
48 : }
49 :
50 : static inline void WriteDoubleValue(Address p, double value) {
51 : WriteUnalignedValue(p, value);
52 : }
53 :
54 : static inline uint16_t ReadUnalignedUInt16(Address p) {
55 : return ReadUnalignedValue<uint16_t>(p);
56 : }
57 :
58 : static inline void WriteUnalignedUInt16(Address p, uint16_t value) {
59 : WriteUnalignedValue(p, value);
60 : }
61 :
62 : static inline uint32_t ReadUnalignedUInt32(Address p) {
63 : return ReadUnalignedValue<uint32_t>(p);
64 : }
65 :
66 : static inline void WriteUnalignedUInt32(Address p, uint32_t value) {
67 : WriteUnalignedValue(p, value);
68 : }
69 :
70 : template <typename V>
71 16048 : static inline V ReadLittleEndianValue(Address p) {
72 : #if defined(V8_TARGET_LITTLE_ENDIAN)
73 16048 : 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 :
85 : template <typename V>
86 : static 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 :
98 : template <typename V>
99 : static inline V ReadLittleEndianValue(V* p) {
100 : return ReadLittleEndianValue<V>(reinterpret_cast<Address>(p));
101 : }
102 :
103 : template <typename V>
104 : static 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_
|