write-barrier.cc 2.44 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "include/cppgc/internal/write-barrier.h"

#include "include/cppgc/internal/pointer-policies.h"
8
#include "src/heap/cppgc/globals.h"
9
#include "src/heap/cppgc/heap-object-header.h"
10
#include "src/heap/cppgc/heap-page.h"
11 12 13 14
#include "src/heap/cppgc/heap.h"
#include "src/heap/cppgc/marker.h"
#include "src/heap/cppgc/marking-visitor.h"

15 16 17 18
#if defined(CPPGC_CAGED_HEAP)
#include "include/cppgc/internal/caged-heap-local-data.h"
#endif

19 20 21 22 23
namespace cppgc {
namespace internal {

namespace {

24
void MarkValue(const BasePage* page, MarkerBase* marker, const void* value) {
25 26 27 28 29 30
#if defined(CPPGC_CAGED_HEAP)
  DCHECK(reinterpret_cast<CagedHeapLocalData*>(
             reinterpret_cast<uintptr_t>(value) &
             ~(kCagedHeapReservationAlignment - 1))
             ->is_marking_in_progress);
#endif
31 32 33 34 35 36
  auto& header =
      const_cast<HeapObjectHeader&>(page->ObjectHeaderFromInnerAddress(value));
  if (!header.TryMarkAtomic()) return;

  DCHECK(marker);

37 38 39
  if (V8_UNLIKELY(
          header
              .IsInConstruction<HeapObjectHeader::AccessMode::kNonAtomic>())) {
40 41 42
    // It is assumed that objects on not_fully_constructed_worklist_ are not
    // marked.
    header.Unmark();
43
    marker->WriteBarrierForInConstructionObject(header.Payload());
44 45 46
    return;
  }

47
  marker->WriteBarrierForObject(header);
48 49 50 51
}

}  // namespace

52
void WriteBarrier::MarkingBarrierSlowWithSentinelCheck(const void* value) {
53 54
  if (!value || value == kSentinelPointer) return;

55 56 57 58
  MarkingBarrierSlow(value);
}

void WriteBarrier::MarkingBarrierSlow(const void* value) {
59
  const BasePage* page = BasePage::FromPayload(value);
60
  const auto* heap = page->heap();
61 62 63

  // Marker being not set up means that no incremental/concurrent marking is in
  // progress.
64
  if (!heap->marker()) return;
65

66
  MarkValue(page, heap->marker(), value);
67 68
}

69 70 71 72 73 74 75 76 77 78 79
#if defined(CPPGC_YOUNG_GENERATION)
void WriteBarrier::GenerationalBarrierSlow(CagedHeapLocalData* local_data,
                                           const AgeTable& age_table,
                                           const void* slot,
                                           uintptr_t value_offset) {
  if (age_table[value_offset] == AgeTable::Age::kOld) return;
  // Record slot.
  local_data->heap_base->remembered_slots().insert(const_cast<void*>(slot));
}
#endif

80 81
}  // namespace internal
}  // namespace cppgc