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

#ifndef V8_GLOBAL_HANDLES_H_
#define V8_GLOBAL_HANDLES_H_

8
#include <type_traits>
9
#include <vector>
10

11 12
#include "include/v8.h"
#include "include/v8-profiler.h"
13

14
#include "src/handles.h"
15
#include "src/objects.h"
16
#include "src/utils.h"
17

18 19
namespace v8 {
namespace internal {
20

21
class HeapStats;
22
class RootVisitor;
23

24 25 26 27 28 29
// Structure for tracking global handles.
// A single list keeps all the allocated global handles.
// Destroyed handles stay in the list but is added to the free list.
// At GC the destroyed global handles are removed from the free list
// and deallocated.

30
enum WeaknessType {
31 32
  // Embedder gets a handle to the dying object.
  FINALIZER_WEAK,
33
  // In the following cases, the embedder gets the parameter they passed in
34
  // earlier, and 0 or 2 first embedder fields. Note that the internal
35 36 37
  // fields must contain aligned non-V8 pointers.  Getting pointers to V8
  // objects through this interface would be GC unsafe so in that case the
  // embedder gets a null pointer instead.
38
  PHANTOM_WEAK,
39
  PHANTOM_WEAK_2_EMBEDDER_FIELDS,
40 41 42
  // The handle is automatically reset by the garbage collector when
  // the object is no longer reachable.
  PHANTOM_WEAK_RESET_HANDLE
43 44
};

45
class GlobalHandles {
46
 public:
47 48
  ~GlobalHandles();

49
  // Creates a new global handle that is alive until Destroy is called.
50
  Handle<Object> Create(Object value);
51
  Handle<Object> Create(Address value);
52

53 54
  template <typename T>
  Handle<T> Create(T value) {
55
    static_assert(std::is_base_of<Object, T>::value, "static type violation");
56
    // The compiler should only pick this method if T is not Object.
57 58
    static_assert(!std::is_same<Object, T>::value, "compiler error");
    return Handle<T>::cast(Create(Object(value)));
59
  }
60

61
  // Copy a global handle
62
  static Handle<Object> CopyGlobal(Address* location);
63

64
  // Destroy a global handle.
65
  static void Destroy(Address* location);
66 67 68

  // Make the global handle weak and set the callback parameter for the
  // handle.  When the garbage collector recognizes that only weak global
69 70 71 72 73 74 75
  // handles point to an object the callback function is invoked (for each
  // handle) with the handle and corresponding parameter as arguments.  By
  // default the handle still contains a pointer to the object that is being
  // collected.  For this reason the object is not collected until the next
  // GC.  For a phantom weak handle the handle is cleared (set to a Smi)
  // before the callback is invoked, but the handle can still be identified
  // in the callback by using the location() of the handle.
76 77 78
  static void MakeWeak(Address* location, void* parameter,
                       WeakCallbackInfo<void>::Callback weak_callback,
                       v8::WeakCallbackType type);
79

80
  static void MakeWeak(Address** location_addr);
81

82
  static void AnnotateStrongRetainer(Address* location, const char* label);
83

84
  void RecordStats(HeapStats* stats);
85

86
  // Returns the current number of handles to global objects.
87
  int global_handles_count() const {
88 89 90
    return number_of_global_handles_;
  }

91 92 93 94 95 96 97 98
  size_t NumberOfPhantomHandleResets() {
    return number_of_phantom_handle_resets_;
  }

  void ResetNumberOfPhantomHandleResets() {
    number_of_phantom_handle_resets_ = 0;
  }

99
  size_t NumberOfNewSpaceNodes() { return new_space_nodes_.size(); }
100

101
  // Clear the weakness of a global handle.
102
  static void* ClearWeakness(Address* location);
103 104

  // Tells whether global handle is near death.
105
  static bool IsNearDeath(Address* location);
106 107

  // Tells whether global handle is weak.
108
  static bool IsWeak(Address* location);
109

110 111
  int InvokeFirstPassWeakCallbacks();

112
  // Process pending weak handles.
113
  // Returns the number of freed nodes.
114 115
  int PostGarbageCollectionProcessing(
      GarbageCollector collector, const v8::GCCallbackFlags gc_callback_flags);
116

117
  void IterateStrongRoots(RootVisitor* v);
118

119 120
  void IterateWeakRoots(RootVisitor* v);

121
  void IterateAllRoots(RootVisitor* v);
122

123
  void IterateAllNewSpaceRoots(RootVisitor* v);
124
  void IterateNewSpaceRoots(RootVisitor* v, size_t start, size_t end);
125

126
  // Iterates over all handles that have embedder-assigned class ID.
127
  void IterateAllRootsWithClassIds(v8::PersistentHandleVisitor* v);
128

129 130
  // Iterates over all handles in the new space that have embedder-assigned
  // class ID.
131
  void IterateAllRootsInNewSpaceWithClassIds(v8::PersistentHandleVisitor* v);
132

133 134
  // Iterate over all handles in the new space that are weak, unmodified
  // and have class IDs
135
  void IterateWeakRootsInNewSpaceWithClassIds(v8::PersistentHandleVisitor* v);
136

137 138
  // Iterates over weak roots on the heap.
  void IterateWeakRootsForFinalizers(RootVisitor* v);
139 140
  void IterateWeakRootsForPhantomHandles(
      WeakSlotCallbackWithHeap should_reset_handle);
141

142 143
  // Marks all handles that should be finalized based on the predicate
  // |should_reset_handle| as pending.
144
  void IdentifyWeakHandles(WeakSlotCallbackWithHeap should_reset_handle);
145

146
  // NOTE: Five ...NewSpace... functions below are used during
147 148 149 150
  // scavenge collections and iterate over sets of handles that are
  // guaranteed to contain all handles holding new space objects (but
  // may also include old space objects).

151
  // Iterates over strong and dependent handles. See the note above.
152
  void IterateNewSpaceStrongAndDependentRoots(RootVisitor* v);
153

154 155 156 157 158
  // Iterates over strong and dependent handles. See the note above.
  // Also marks unmodified nodes in the same iteration.
  void IterateNewSpaceStrongAndDependentRootsAndIdentifyUnmodified(
      RootVisitor* v, size_t start, size_t end);

159
  // Marks weak unmodified handles satisfying |is_dead| as pending.
160
  void MarkNewSpaceWeakUnmodifiedObjectsPending(
161
      WeakSlotCallbackWithHeap is_dead);
162

163 164
  // Iterates over weak independent or unmodified handles.
  // See the note above.
165 166 167
  void IterateNewSpaceWeakUnmodifiedRootsForFinalizers(RootVisitor* v);
  void IterateNewSpaceWeakUnmodifiedRootsForPhantomHandles(
      RootVisitor* v, WeakSlotCallbackWithHeap should_reset_handle);
168 169 170 171 172

  // Identify unmodified objects that are in weak state and marks them
  // unmodified
  void IdentifyWeakUnmodifiedObjects(WeakSlotCallback is_unmodified);

173
  // Tear down the global handle structure.
174 175 176
  void TearDown();

  Isolate* isolate() { return isolate_; }
177 178

#ifdef DEBUG
179 180
  void PrintStats();
  void Print();
181
#endif  // DEBUG
182

183 184
  void InvokeSecondPassPhantomCallbacks();

185
 private:
186 187 188 189
  // Internal node structures.
  class Node;
  class NodeBlock;
  class NodeIterator;
190
  class PendingPhantomCallback;
191 192

  explicit GlobalHandles(Isolate* isolate);
193

194
  void InvokeSecondPassPhantomCallbacksFromTask();
195 196
  int PostScavengeProcessing(int initial_post_gc_processing_count);
  int PostMarkSweepProcessing(int initial_post_gc_processing_count);
197
  void InvokeOrScheduleSecondPassPhantomCallbacks(bool synchronous_second_pass);
198
  void UpdateListOfNewSpaceNodes();
199 200
  void ApplyPersistentHandleVisitor(v8::PersistentHandleVisitor* visitor,
                                    Node* node);
201

202 203
  Isolate* isolate_;

204 205 206 207 208
  // List of all allocated node blocks.
  NodeBlock* first_block_;

  // List of node blocks with used nodes.
  NodeBlock* first_used_block_;
209

210 211
  // Free list of nodes.
  Node* first_free_;
212

213 214
  // Contains all nodes holding new space objects. Note: when the list
  // is accessed, some of the objects may have been promoted already.
215
  std::vector<Node*> new_space_nodes_;
216 217 218

  // Field always containing the number of handles to global objects.
  int number_of_global_handles_;
219

220
  int post_gc_processing_count_;
221

222 223
  size_t number_of_phantom_handle_resets_;

224
  std::vector<PendingPhantomCallback> pending_phantom_callbacks_;
225 226
  std::vector<PendingPhantomCallback> second_pass_callbacks_;
  bool second_pass_callbacks_task_posted_ = false;
227

228 229 230
  friend class Isolate;

  DISALLOW_COPY_AND_ASSIGN(GlobalHandles);
231 232 233
};


234 235
class GlobalHandles::PendingPhantomCallback {
 public:
236
  typedef v8::WeakCallbackInfo<void> Data;
dcarney's avatar
dcarney committed
237 238
  PendingPhantomCallback(
      Node* node, Data::Callback callback, void* parameter,
239
      void* embedder_fields[v8::kEmbedderFieldsInWeakCallback])
dcarney's avatar
dcarney committed
240
      : node_(node), callback_(callback), parameter_(parameter) {
241 242
    for (int i = 0; i < v8::kEmbedderFieldsInWeakCallback; ++i) {
      embedder_fields_[i] = embedder_fields[i];
dcarney's avatar
dcarney committed
243 244
    }
  }
245

dcarney's avatar
dcarney committed
246
  void Invoke(Isolate* isolate);
247 248

  Node* node() { return node_; }
dcarney's avatar
dcarney committed
249
  Data::Callback callback() { return callback_; }
250 251 252 253

 private:
  Node* node_;
  Data::Callback callback_;
dcarney's avatar
dcarney committed
254
  void* parameter_;
255
  void* embedder_fields_[v8::kEmbedderFieldsInWeakCallback];
256 257 258
};


259 260 261 262 263 264 265
class EternalHandles {
 public:
  EternalHandles();
  ~EternalHandles();

  int NumberOfHandles() { return size_; }

266
  // Create an EternalHandle, overwriting the index.
267
  void Create(Isolate* isolate, Object object, int* index);
268 269 270 271 272 273 274

  // Grab the handle for an existing EternalHandle.
  inline Handle<Object> Get(int index) {
    return Handle<Object>(GetLocation(index));
  }

  // Iterates over all handles.
275
  void IterateAllRoots(RootVisitor* visitor);
276
  // Iterates over all handles which might be in new space.
277
  void IterateNewSpaceRoots(RootVisitor* visitor);
278
  // Rebuilds new space list.
279
  void PostGarbageCollectionProcessing();
280 281 282 283 284 285 286

 private:
  static const int kInvalidIndex = -1;
  static const int kShift = 8;
  static const int kSize = 1 << kShift;
  static const int kMask = 0xff;

287 288 289
  // Gets the slot for an index. This returns an Address* rather than an
  // ObjectSlot in order to avoid #including slots.h in this header file.
  inline Address* GetLocation(int index) {
290
    DCHECK(index >= 0 && index < size_);
291 292 293 294
    return &blocks_[index >> kShift][index & kMask];
  }

  int size_;
295
  std::vector<Address*> blocks_;
296
  std::vector<int> new_space_indices_;
297 298 299 300 301

  DISALLOW_COPY_AND_ASSIGN(EternalHandles);
};


302 303
}  // namespace internal
}  // namespace v8
304 305

#endif  // V8_GLOBAL_HANDLES_H_