• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_BASE_MEMORY_H_
6 #define V8_BASE_MEMORY_H_
7 
8 #include "src/base/macros.h"
9 #include "src/base/platform/wrappers.h"
10 
11 namespace v8 {
12 namespace base {
13 
14 using Address = uintptr_t;
15 using byte = uint8_t;
16 
17 // Memory provides an interface to 'raw' memory. It encapsulates the casts
18 // that typically are needed when incompatible pointer types are used.
19 // Note that this class currently relies on undefined behaviour. There is a
20 // proposal (http://wg21.link/p0593r2) to make it defined behaviour though.
21 template <class T>
Memory(Address addr)22 inline T& Memory(Address addr) {
23   DCHECK(IsAligned(addr, alignof(T)));
24   return *reinterpret_cast<T*>(addr);
25 }
26 template <class T>
Memory(byte * addr)27 inline T& Memory(byte* addr) {
28   return Memory<T>(reinterpret_cast<Address>(addr));
29 }
30 
31 template <typename V>
ReadUnalignedValue(Address p)32 static inline V ReadUnalignedValue(Address p) {
33   ASSERT_TRIVIALLY_COPYABLE(V);
34   V r;
35   memcpy(&r, reinterpret_cast<void*>(p), sizeof(V));
36   return r;
37 }
38 
39 template <typename V>
WriteUnalignedValue(Address p,V value)40 static inline void WriteUnalignedValue(Address p, V value) {
41   ASSERT_TRIVIALLY_COPYABLE(V);
42   memcpy(reinterpret_cast<void*>(p), &value, sizeof(V));
43 }
44 
45 template <typename V>
ReadLittleEndianValue(Address p)46 static inline V ReadLittleEndianValue(Address p) {
47 #if defined(V8_TARGET_LITTLE_ENDIAN)
48   return ReadUnalignedValue<V>(p);
49 #elif defined(V8_TARGET_BIG_ENDIAN)
50   V ret{};
51   const byte* src = reinterpret_cast<const byte*>(p);
52   byte* dst = reinterpret_cast<byte*>(&ret);
53   for (size_t i = 0; i < sizeof(V); i++) {
54     dst[i] = src[sizeof(V) - i - 1];
55   }
56   return ret;
57 #endif  // V8_TARGET_LITTLE_ENDIAN
58 }
59 
60 template <typename V>
WriteLittleEndianValue(Address p,V value)61 static inline void WriteLittleEndianValue(Address p, V value) {
62 #if defined(V8_TARGET_LITTLE_ENDIAN)
63   WriteUnalignedValue<V>(p, value);
64 #elif defined(V8_TARGET_BIG_ENDIAN)
65   byte* src = reinterpret_cast<byte*>(&value);
66   byte* dst = reinterpret_cast<byte*>(p);
67   for (size_t i = 0; i < sizeof(V); i++) {
68     dst[i] = src[sizeof(V) - i - 1];
69   }
70 #endif  // V8_TARGET_LITTLE_ENDIAN
71 }
72 
73 template <typename V>
ReadLittleEndianValue(V * p)74 static inline V ReadLittleEndianValue(V* p) {
75   return ReadLittleEndianValue<V>(reinterpret_cast<Address>(p));
76 }
77 
78 template <typename V>
WriteLittleEndianValue(V * p,V value)79 static inline void WriteLittleEndianValue(V* p, V value) {
80   static_assert(
81       !std::is_array<V>::value,
82       "Passing an array decays to pointer, causing unexpected results.");
83   WriteLittleEndianValue<V>(reinterpret_cast<Address>(p), value);
84 }
85 
86 }  // namespace base
87 }  // namespace v8
88 
89 #endif  // V8_BASE_MEMORY_H_
90