summaryrefslogtreecommitdiffstats
path: root/chromium/third_party/WebKit/Source/platform/heap/PersistentNode.cpp
blob: 10a035f847256f49e8f6df2b27abe431f27abad2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Copyright 2015 The Chromium 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 "platform/heap/PersistentNode.h"

#include "base/debug/alias.h"
#include "platform/heap/Handle.h"

namespace blink {

namespace {

class DummyGCBase final : public GarbageCollected<DummyGCBase> {
 public:
  void Trace(blink::Visitor* visitor) {}
};
}

PersistentRegion::~PersistentRegion() {
  PersistentNodeSlots* slots = slots_;
  while (slots) {
    PersistentNodeSlots* dead_slots = slots;
    slots = slots->next_;
    delete dead_slots;
  }
}

int PersistentRegion::NumberOfPersistents() {
  int persistent_count = 0;
  for (PersistentNodeSlots* slots = slots_; slots; slots = slots->next_) {
    for (int i = 0; i < PersistentNodeSlots::kSlotCount; ++i) {
      if (!slots->slot_[i].IsUnused())
        ++persistent_count;
    }
  }
#if DCHECK_IS_ON()
  DCHECK_EQ(persistent_count, persistent_count_);
#endif
  return persistent_count;
}

void PersistentRegion::EnsurePersistentNodeSlots(void* self,
                                                 TraceCallback trace) {
  DCHECK(!free_list_head_);
  PersistentNodeSlots* slots = new PersistentNodeSlots;
  for (int i = 0; i < PersistentNodeSlots::kSlotCount; ++i) {
    PersistentNode* node = &slots->slot_[i];
    node->SetFreeListNext(free_list_head_);
    free_list_head_ = node;
    DCHECK(node->IsUnused());
  }
  slots->next_ = slots_;
  slots_ = slots;
}

void PersistentRegion::ReleasePersistentNode(
    PersistentNode* persistent_node,
    ThreadState::PersistentClearCallback callback) {
  DCHECK(!persistent_node->IsUnused());
  // 'self' is in use, containing the persistent wrapper object.
  void* self = persistent_node->Self();
  if (callback) {
    (*callback)(self);
    DCHECK(persistent_node->IsUnused());
    return;
  }
  Persistent<DummyGCBase>* persistent =
      reinterpret_cast<Persistent<DummyGCBase>*>(self);
  persistent->Clear();
  DCHECK(persistent_node->IsUnused());
}

// This function traces all PersistentNodes. If we encounter
// a PersistentNodeSlot that contains only freed PersistentNodes,
// we delete the PersistentNodeSlot. This function rebuilds the free
// list of PersistentNodes.
void PersistentRegion::TracePersistentNodes(Visitor* visitor,
                                            ShouldTraceCallback should_trace) {
  size_t debug_marked_object_size = ProcessHeap::TotalMarkedObjectSize();
  base::debug::Alias(&debug_marked_object_size);

  free_list_head_ = nullptr;
  int persistent_count = 0;
  PersistentNodeSlots** prev_next = &slots_;
  PersistentNodeSlots* slots = slots_;
  while (slots) {
    PersistentNode* free_list_next = nullptr;
    PersistentNode* free_list_last = nullptr;
    int free_count = 0;
    for (int i = 0; i < PersistentNodeSlots::kSlotCount; ++i) {
      PersistentNode* node = &slots->slot_[i];
      if (node->IsUnused()) {
        if (!free_list_next)
          free_list_last = node;
        node->SetFreeListNext(free_list_next);
        free_list_next = node;
        ++free_count;
      } else {
        ++persistent_count;
        if (!should_trace(visitor, node))
          continue;
        node->TracePersistentNode(visitor);
        debug_marked_object_size = ProcessHeap::TotalMarkedObjectSize();
      }
    }
    if (free_count == PersistentNodeSlots::kSlotCount) {
      PersistentNodeSlots* dead_slots = slots;
      *prev_next = slots->next_;
      slots = slots->next_;
      delete dead_slots;
    } else {
      if (free_list_last) {
        DCHECK(free_list_next);
        DCHECK(!free_list_last->FreeListNext());
        free_list_last->SetFreeListNext(free_list_head_);
        free_list_head_ = free_list_next;
      }
      prev_next = &slots->next_;
      slots = slots->next_;
    }
  }
#if DCHECK_IS_ON()
  DCHECK_EQ(persistent_count, persistent_count_);
#endif
}

void PersistentRegion::PrepareForThreadStateTermination() {
  DCHECK(!IsMainThread());
  PersistentNodeSlots* slots = slots_;
  while (slots) {
    for (int i = 0; i < PersistentNodeSlots::kSlotCount; ++i) {
      PersistentNode* node = &slots->slot_[i];
      if (node->IsUnused())
        continue;
      // It is safe to cast to Persistent<DummyGCBase> because persistent heap
      // collections are banned in non-main threads.

      Persistent<DummyGCBase>* persistent =
          reinterpret_cast<Persistent<DummyGCBase>*>(node->Self());
      DCHECK(persistent);
      persistent->Clear();
      DCHECK(node->IsUnused());
    }
    slots = slots->next_;
  }
#if DCHECK_IS_ON()
  DCHECK_EQ(persistent_count_, 0);
#endif
}

bool CrossThreadPersistentRegion::ShouldTracePersistentNode(
    Visitor* visitor,
    PersistentNode* node) {
  CrossThreadPersistent<DummyGCBase>* persistent =
      reinterpret_cast<CrossThreadPersistent<DummyGCBase>*>(node->Self());
  DCHECK(persistent);
  DCHECK(!persistent->IsHashTableDeletedValue());
  Address raw_object = reinterpret_cast<Address>(persistent->Get());
  if (!raw_object)
    return false;
  return &visitor->Heap() == &ThreadState::FromObject(raw_object)->Heap();
}

void CrossThreadPersistentRegion::PrepareForThreadStateTermination(
    ThreadState* thread_state) {
  // For heaps belonging to a thread that's detaching, any cross-thread
  // persistents pointing into them needs to be disabled. Do that by clearing
  // out the underlying heap reference.
  MutexLocker lock(mutex_);

  PersistentNodeSlots* slots = persistent_region_->slots_;
  while (slots) {
    for (int i = 0; i < PersistentNodeSlots::kSlotCount; ++i) {
      if (slots->slot_[i].IsUnused())
        continue;

      // 'self' is in use, containing the cross-thread persistent wrapper
      // object.
      CrossThreadPersistent<DummyGCBase>* persistent =
          reinterpret_cast<CrossThreadPersistent<DummyGCBase>*>(
              slots->slot_[i].Self());
      DCHECK(persistent);
      void* raw_object = persistent->AtomicGet();
      if (!raw_object)
        continue;
      BasePage* page = PageFromObject(raw_object);
      DCHECK(page);
      if (page->Arena()->GetThreadState() == thread_state) {
        persistent->Clear();
        DCHECK(slots->slot_[i].IsUnused());
      }
    }
    slots = slots->next_;
  }
}

#if defined(ADDRESS_SANITIZER)
void CrossThreadPersistentRegion::UnpoisonCrossThreadPersistents() {
  MutexLocker lock(mutex_);
  int persistent_count = 0;
  for (PersistentNodeSlots* slots = persistent_region_->slots_; slots;
       slots = slots->next_) {
    for (int i = 0; i < PersistentNodeSlots::kSlotCount; ++i) {
      const PersistentNode& node = slots->slot_[i];
      if (!node.IsUnused()) {
        ASAN_UNPOISON_MEMORY_REGION(node.Self(),
                                    sizeof(CrossThreadPersistent<void*>));
        ++persistent_count;
      }
    }
  }
#if DCHECK_IS_ON()
  DCHECK_EQ(persistent_count, persistent_region_->persistent_count_);
#endif
}
#endif

}  // namespace blink