v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
memory.h
Go to the documentation of this file.
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
10namespace v8 {
11namespace base {
12
13using Address = uintptr_t;
14
15// Memory provides an interface to 'raw' memory. It encapsulates the casts
16// that typically are needed when incompatible pointer types are used.
17template <class T>
18inline T& Memory(Address addr) {
19 DCHECK(IsAligned(addr, alignof(T)));
20 return *reinterpret_cast<T*>(addr);
21}
22template <class T>
23inline T& Memory(uint8_t* addr) {
24 return Memory<T>(reinterpret_cast<Address>(addr));
25}
26
27template <typename V>
28static inline V ReadUnalignedValue(Address p) {
30 V r;
31 memcpy(&r, reinterpret_cast<void*>(p), sizeof(V));
32 return r;
33}
34
35template <typename V>
36static inline V ReadUnalignedValue(const char p[sizeof(V)]) {
37 return ReadUnalignedValue<V>(reinterpret_cast<Address>(p));
38}
39
40template <typename V>
41static inline void WriteUnalignedValue(Address p, V value) {
43 memcpy(reinterpret_cast<void*>(p), &value, sizeof(V));
44}
45
46template <typename V>
47static inline void WriteUnalignedValue(char p[sizeof(V)], V value) {
48 return WriteUnalignedValue<V>(reinterpret_cast<Address>(p), value);
49}
50
51template <typename V>
53#if defined(V8_TARGET_LITTLE_ENDIAN)
54 return ReadUnalignedValue<V>(p);
55#elif defined(V8_TARGET_BIG_ENDIAN)
56 V ret{};
57 const uint8_t* src = reinterpret_cast<const uint8_t*>(p);
58 uint8_t* dst = reinterpret_cast<uint8_t*>(&ret);
59 for (size_t i = 0; i < sizeof(V); i++) {
60 dst[i] = src[sizeof(V) - i - 1];
61 }
62 return ret;
63#endif // V8_TARGET_LITTLE_ENDIAN
64}
65
66template <typename V>
67static inline void WriteLittleEndianValue(Address p, V value) {
68#if defined(V8_TARGET_LITTLE_ENDIAN)
69 WriteUnalignedValue<V>(p, value);
70#elif defined(V8_TARGET_BIG_ENDIAN)
71 uint8_t* src = reinterpret_cast<uint8_t*>(&value);
72 uint8_t* dst = reinterpret_cast<uint8_t*>(p);
73 for (size_t i = 0; i < sizeof(V); i++) {
74 dst[i] = src[sizeof(V) - i - 1];
75 }
76#endif // V8_TARGET_LITTLE_ENDIAN
77}
78
79template <typename V>
80static inline V ReadLittleEndianValue(V* p) {
81 return ReadLittleEndianValue<V>(reinterpret_cast<Address>(p));
82}
83
84template <typename V>
85static inline void WriteLittleEndianValue(V* p, V value) {
86 static_assert(
87 !std::is_array<V>::value,
88 "Passing an array decays to pointer, causing unexpected results.");
89 WriteLittleEndianValue<V>(reinterpret_cast<Address>(p), value);
90}
91
92} // namespace base
93} // namespace v8
94
95#endif // V8_BASE_MEMORY_H_
#define V(Name)
int r
Definition mul-fft.cc:298
static V ReadUnalignedValue(Address p)
Definition memory.h:28
static void WriteLittleEndianValue(Address p, V value)
Definition memory.h:67
T & Memory(Address addr)
Definition memory.h:18
static V ReadLittleEndianValue(Address p)
Definition memory.h:52
uintptr_t Address
Definition memory.h:13
static void WriteUnalignedValue(Address p, V value)
Definition memory.h:41
#define DCHECK(condition)
Definition logging.h:482
#define ASSERT_TRIVIALLY_COPYABLE(T)
Definition macros.h:267
constexpr bool IsAligned(T value, U alignment)
Definition macros.h:403
std::unique_ptr< ValueMirror > value