1// Copyright 2020 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_HEAP_MARKING_BARRIER_H_
6#define V8_HEAP_MARKING_BARRIER_H_
7
8#include "include/v8-internal.h"
9#include "src/common/globals.h"
10#include "src/heap/mark-compact.h"
11
12namespace v8 {
13namespace internal {
14
15class Heap;
16class IncrementalMarking;
17class LocalHeap;
18class PagedSpace;
19class NewSpace;
20
21class MarkingBarrier {
22 public:
23  explicit MarkingBarrier(Heap*);
24  explicit MarkingBarrier(LocalHeap*);
25  ~MarkingBarrier();
26
27  void Activate(bool is_compacting);
28  void Deactivate();
29  void Publish();
30
31  static void ActivateAll(Heap* heap, bool is_compacting);
32  static void DeactivateAll(Heap* heap);
33  V8_EXPORT_PRIVATE static void PublishAll(Heap* heap);
34
35  void Write(HeapObject host, HeapObjectSlot, HeapObject value);
36  void Write(Code host, RelocInfo*, HeapObject value);
37  void Write(JSArrayBuffer host, ArrayBufferExtension*);
38  void Write(DescriptorArray, int number_of_own_descriptors);
39  // Only usable when there's no valid JS host object for this write, e.g., when
40  // value is held alive from a global handle.
41  void WriteWithoutHost(HeapObject value);
42
43  // Returns true if the slot needs to be recorded.
44  inline bool MarkValue(HeapObject host, HeapObject value);
45
46 private:
47  using MarkingState = MarkCompactCollector::MarkingState;
48
49  inline bool WhiteToGreyAndPush(HeapObject value);
50
51  void RecordRelocSlot(Code host, RelocInfo* rinfo, HeapObject target);
52
53  void ActivateSpace(PagedSpace*);
54  void ActivateSpace(NewSpace*);
55
56  void DeactivateSpace(PagedSpace*);
57  void DeactivateSpace(NewSpace*);
58
59  bool IsCurrentMarkingBarrier();
60
61  template <typename TSlot>
62  inline void MarkRange(HeapObject value, TSlot start, TSlot end);
63
64  Heap* heap_;
65  MarkCompactCollector* collector_;
66  IncrementalMarking* incremental_marking_;
67  MarkingWorklist::Local worklist_;
68  MarkingState marking_state_;
69  std::unordered_map<MemoryChunk*, std::unique_ptr<TypedSlots>,
70                     MemoryChunk::Hasher>
71      typed_slots_map_;
72  bool is_compacting_ = false;
73  bool is_activated_ = false;
74  bool is_main_thread_barrier_;
75  bool is_shared_heap_;
76};
77
78}  // namespace internal
79}  // namespace v8
80
81#endif  // V8_HEAP_MARKING_BARRIER_H_
82