xref: /third_party/node/deps/v8/src/common/ptr-compr.h (revision 1cb0ef41)
1// Copyright 2022 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_COMMON_PTR_COMPR_H_
6#define V8_COMMON_PTR_COMPR_H_
7
8#include "src/base/memory.h"
9#include "src/common/globals.h"
10
11namespace v8 {
12namespace internal {
13
14// Accessors for fields that may be unaligned due to pointer compression.
15
16template <typename V>
17static inline V ReadMaybeUnalignedValue(Address p) {
18  // Pointer compression causes types larger than kTaggedSize to be unaligned.
19#ifdef V8_COMPRESS_POINTERS
20  constexpr bool v8_pointer_compression_unaligned = sizeof(V) > kTaggedSize;
21#else
22  constexpr bool v8_pointer_compression_unaligned = false;
23#endif
24  // Bug(v8:8875) Double fields may be unaligned.
25  constexpr bool unaligned_double_field =
26      std::is_same<V, double>::value && kDoubleSize > kTaggedSize;
27  if (unaligned_double_field || v8_pointer_compression_unaligned) {
28    return base::ReadUnalignedValue<V>(p);
29  } else {
30    return base::Memory<V>(p);
31  }
32}
33
34template <typename V>
35static inline void WriteMaybeUnalignedValue(Address p, V value) {
36  // Pointer compression causes types larger than kTaggedSize to be unaligned.
37#ifdef V8_COMPRESS_POINTERS
38  constexpr bool v8_pointer_compression_unaligned = sizeof(V) > kTaggedSize;
39#else
40  constexpr bool v8_pointer_compression_unaligned = false;
41#endif
42  // Bug(v8:8875) Double fields may be unaligned.
43  constexpr bool unaligned_double_field =
44      std::is_same<V, double>::value && kDoubleSize > kTaggedSize;
45  if (unaligned_double_field || v8_pointer_compression_unaligned) {
46    base::WriteUnalignedValue<V>(p, value);
47  } else {
48    base::Memory<V>(p) = value;
49  }
50}
51
52}  // namespace internal
53}  // namespace v8
54
55#endif  // V8_COMMON_PTR_COMPR_H_
56