1// Copyright 2021 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_CPPGC_JS_CPP_MARKING_STATE_H_
6#define V8_HEAP_CPPGC_JS_CPP_MARKING_STATE_H_
7
8#include <memory>
9
10#include "src/heap/cppgc-js/cpp-heap.h"
11#include "src/heap/cppgc/marking-state.h"
12#include "src/heap/cppgc/marking-worklists.h"
13#include "src/objects/embedder-data-slot.h"
14
15namespace v8 {
16namespace internal {
17
18class JSObject;
19class EmbedderDataSlot;
20
21class CppMarkingState {
22 public:
23  using EmbedderDataSnapshot =
24      std::pair<EmbedderDataSlot::EmbedderDataSlotSnapshot,
25                EmbedderDataSlot::EmbedderDataSlotSnapshot>;
26
27  CppMarkingState(Isolate* isolate, const WrapperDescriptor& wrapper_descriptor,
28                  cppgc::internal::MarkingStateBase& main_thread_marking_state)
29      : isolate_(isolate),
30        wrapper_descriptor_(wrapper_descriptor),
31        owned_marking_state_(nullptr),
32        marking_state_(main_thread_marking_state) {}
33
34  CppMarkingState(Isolate* isolate, const WrapperDescriptor& wrapper_descriptor,
35                  std::unique_ptr<cppgc::internal::MarkingStateBase>
36                      concurrent_marking_state)
37      : isolate_(isolate),
38        wrapper_descriptor_(wrapper_descriptor),
39        owned_marking_state_(std::move(concurrent_marking_state)),
40        marking_state_(*owned_marking_state_) {}
41  CppMarkingState(const CppMarkingState&) = delete;
42  CppMarkingState& operator=(const CppMarkingState&) = delete;
43
44  void Publish() { marking_state_.Publish(); }
45
46  inline bool ExtractEmbedderDataSnapshot(Map, JSObject, EmbedderDataSnapshot&);
47
48  inline void MarkAndPush(const EmbedderDataSnapshot&);
49  inline void MarkAndPush(const EmbedderDataSlot type_slot,
50                          const EmbedderDataSlot instance_slot);
51
52  bool IsLocalEmpty() {
53    return marking_state_.marking_worklist().IsLocalEmpty();
54  }
55
56 private:
57  Isolate* const isolate_;
58  const WrapperDescriptor& wrapper_descriptor_;
59
60  std::unique_ptr<cppgc::internal::MarkingStateBase> owned_marking_state_;
61  cppgc::internal::MarkingStateBase& marking_state_;
62};
63
64}  // namespace internal
65}  // namespace v8
66
67#endif  // V8_HEAP_CPPGC_JS_CPP_MARKING_STATE_H_
68