1 // Copyright 2011 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.
5 #ifndef V8_GLOBAL_HANDLES_H_
6 #define V8_GLOBAL_HANDLES_H_
8 #include "include/v8.h"
9 #include "include/v8-profiler.h"
11 #include "src/handles.h"
13 #include "src/utils.h"
21 // Structure for tracking global handles.
22 // A single list keeps all the allocated global handles.
23 // Destroyed handles stay in the list but is added to the free list.
24 // At GC the destroyed global handles are removed from the free list
27 // Data structures for tracking object groups and implicit references.
29 // An object group is treated like a single JS object: if one of object in
30 // the group is alive, all objects in the same group are considered alive.
31 // An object group is used to simulate object relationship in a DOM tree.
33 // An implicit references group consists of two parts: a parent object and a
34 // list of children objects. If the parent is alive, all the children are alive
38 explicit ObjectGroup(size_t length)
39 : info(NULL), length(length) {
41 objects = new Object**[length];
45 v8::RetainedObjectInfo* info;
51 struct ImplicitRefGroup {
52 ImplicitRefGroup(HeapObject** parent, size_t length)
53 : parent(parent), length(length) {
55 children = new Object**[length];
65 // For internal bookkeeping.
66 struct ObjectGroupConnection {
67 ObjectGroupConnection(UniqueId id, Object** object)
68 : id(id), object(object) {}
70 bool operator==(const ObjectGroupConnection& other) const {
71 return id == other.id;
74 bool operator<(const ObjectGroupConnection& other) const {
83 struct ObjectGroupRetainerInfo {
84 ObjectGroupRetainerInfo(UniqueId id, RetainedObjectInfo* info)
85 : id(id), info(info) {}
87 bool operator==(const ObjectGroupRetainerInfo& other) const {
88 return id == other.id;
91 bool operator<(const ObjectGroupRetainerInfo& other) const {
96 RetainedObjectInfo* info;
101 NORMAL_WEAK, // Embedder gets a handle to the dying object.
102 // In the following cases, the embedder gets the parameter they passed in
103 // earlier, and 0 or 2 first internal fields. Note that the internal
104 // fields must contain aligned non-V8 pointers. Getting pointers to V8
105 // objects through this interface would be GC unsafe so in that case the
106 // embedder gets a null pointer instead.
108 PHANTOM_WEAK_2_INTERNAL_FIELDS
112 class GlobalHandles {
116 // Creates a new global handle that is alive until Destroy is called.
117 Handle<Object> Create(Object* value);
119 // Copy a global handle
120 static Handle<Object> CopyGlobal(Object** location);
122 // Destroy a global handle.
123 static void Destroy(Object** location);
125 typedef WeakCallbackData<v8::Value, void>::Callback WeakCallback;
127 // For a phantom weak reference, the callback does not have access to the
128 // dying object. Phantom weak references are preferred because they allow
129 // memory to be reclaimed in one GC cycle rather than two. However, for
130 // historical reasons the default is non-phantom.
131 enum PhantomState { Nonphantom, Phantom };
133 // Make the global handle weak and set the callback parameter for the
134 // handle. When the garbage collector recognizes that only weak global
135 // handles point to an object the callback function is invoked (for each
136 // handle) with the handle and corresponding parameter as arguments. By
137 // default the handle still contains a pointer to the object that is being
138 // collected. For this reason the object is not collected until the next
139 // GC. For a phantom weak handle the handle is cleared (set to a Smi)
140 // before the callback is invoked, but the handle can still be identified
141 // in the callback by using the location() of the handle.
142 static void MakeWeak(Object** location, void* parameter,
143 WeakCallback weak_callback);
145 // It would be nice to template this one, but it's really hard to get
146 // the template instantiator to work right if you do.
147 static void MakeWeak(Object** location, void* parameter,
148 WeakCallbackInfo<void>::Callback weak_callback,
149 v8::WeakCallbackType type);
151 void RecordStats(HeapStats* stats);
153 // Returns the current number of weak handles.
154 int NumberOfWeakHandles();
156 // Returns the current number of weak handles to global objects.
157 // These handles are also included in NumberOfWeakHandles().
158 int NumberOfGlobalObjectWeakHandles();
160 // Returns the current number of handles to global objects.
161 int global_handles_count() const {
162 return number_of_global_handles_;
165 // Clear the weakness of a global handle.
166 static void* ClearWeakness(Object** location);
168 // Clear the weakness of a global handle.
169 static void MarkIndependent(Object** location);
171 // Mark the reference to this object externaly unreachable.
172 static void MarkPartiallyDependent(Object** location);
174 static bool IsIndependent(Object** location);
176 // Tells whether global handle is near death.
177 static bool IsNearDeath(Object** location);
179 // Tells whether global handle is weak.
180 static bool IsWeak(Object** location);
182 // Process pending weak handles.
183 // Returns the number of freed nodes.
184 int PostGarbageCollectionProcessing(
185 GarbageCollector collector, const v8::GCCallbackFlags gc_callback_flags);
187 // Iterates over all strong handles.
188 void IterateStrongRoots(ObjectVisitor* v);
190 // Iterates over all handles.
191 void IterateAllRoots(ObjectVisitor* v);
193 // Iterates over all handles that have embedder-assigned class ID.
194 void IterateAllRootsWithClassIds(ObjectVisitor* v);
196 // Iterates over all handles in the new space that have embedder-assigned
198 void IterateAllRootsInNewSpaceWithClassIds(ObjectVisitor* v);
200 // Iterates over all weak roots in heap.
201 void IterateWeakRoots(ObjectVisitor* v);
203 // Find all weak handles satisfying the callback predicate, mark
205 void IdentifyWeakHandles(WeakSlotCallback f);
207 // NOTE: Three ...NewSpace... functions below are used during
208 // scavenge collections and iterate over sets of handles that are
209 // guaranteed to contain all handles holding new space objects (but
210 // may also include old space objects).
212 // Iterates over strong and dependent handles. See the node above.
213 void IterateNewSpaceStrongAndDependentRoots(ObjectVisitor* v);
215 // Finds weak independent or partially independent handles satisfying
216 // the callback predicate and marks them as pending. See the note above.
217 void IdentifyNewSpaceWeakIndependentHandles(WeakSlotCallbackWithHeap f);
219 // Iterates over weak independent or partially independent handles.
220 // See the note above.
221 void IterateNewSpaceWeakIndependentRoots(ObjectVisitor* v);
223 // Iterate over objects in object groups that have at least one object
224 // which requires visiting. The callback has to return true if objects
225 // can be skipped and false otherwise.
226 bool IterateObjectGroups(ObjectVisitor* v, WeakSlotCallbackWithHeap can_skip);
228 // Add an object group.
229 // Should be only used in GC callback function before a collection.
230 // All groups are destroyed after a garbage collection.
231 void AddObjectGroup(Object*** handles,
233 v8::RetainedObjectInfo* info);
235 // Associates handle with the object group represented by id.
236 // Should be only used in GC callback function before a collection.
237 // All groups are destroyed after a garbage collection.
238 void SetObjectGroupId(Object** handle, UniqueId id);
240 // Set RetainedObjectInfo for an object group. Should not be called more than
241 // once for a group. Should not be called for a group which contains no
243 void SetRetainedObjectInfo(UniqueId id, RetainedObjectInfo* info);
245 // Adds an implicit reference from a group to an object. Should be only used
246 // in GC callback function before a collection. All implicit references are
247 // destroyed after a mark-compact collection.
248 void SetReferenceFromGroup(UniqueId id, Object** child);
250 // Adds an implicit reference from a parent object to a child object. Should
251 // be only used in GC callback function before a collection. All implicit
252 // references are destroyed after a mark-compact collection.
253 void SetReference(HeapObject** parent, Object** child);
255 List<ObjectGroup*>* object_groups() {
256 ComputeObjectGroupsAndImplicitReferences();
257 return &object_groups_;
260 List<ImplicitRefGroup*>* implicit_ref_groups() {
261 ComputeObjectGroupsAndImplicitReferences();
262 return &implicit_ref_groups_;
265 // Remove bags, this should only happen after GC.
266 void RemoveObjectGroups();
267 void RemoveImplicitRefGroups();
269 // Tear down the global handle structure.
272 Isolate* isolate() { return isolate_; }
280 explicit GlobalHandles(Isolate* isolate);
282 // Migrates data from the internal representation (object_group_connections_,
283 // retainer_infos_ and implicit_ref_connections_) to the public and more
284 // efficient representation (object_groups_ and implicit_ref_groups_).
285 void ComputeObjectGroupsAndImplicitReferences();
287 // v8::internal::List is inefficient even for small number of elements, if we
288 // don't assign any initial capacity.
289 static const int kObjectGroupConnectionsCapacity = 20;
291 class PendingPhantomCallback;
293 // Helpers for PostGarbageCollectionProcessing.
294 static void InvokeSecondPassPhantomCallbacks(
295 List<PendingPhantomCallback>* callbacks, Isolate* isolate);
296 int PostScavengeProcessing(int initial_post_gc_processing_count);
297 int PostMarkSweepProcessing(int initial_post_gc_processing_count);
298 int DispatchPendingPhantomCallbacks(bool synchronous_second_pass);
299 void UpdateListOfNewSpaceNodes();
301 // Internal node structures.
305 class PendingPhantomCallbacksSecondPassTask;
309 // Field always containing the number of handles to global objects.
310 int number_of_global_handles_;
312 // List of all allocated node blocks.
313 NodeBlock* first_block_;
315 // List of node blocks with used nodes.
316 NodeBlock* first_used_block_;
318 // Free list of nodes.
321 // Contains all nodes holding new space objects. Note: when the list
322 // is accessed, some of the objects may have been promoted already.
323 List<Node*> new_space_nodes_;
325 int post_gc_processing_count_;
327 // Object groups and implicit references, public and more efficient
329 List<ObjectGroup*> object_groups_;
330 List<ImplicitRefGroup*> implicit_ref_groups_;
332 // Object groups and implicit references, temporary representation while
333 // constructing the groups.
334 List<ObjectGroupConnection> object_group_connections_;
335 List<ObjectGroupRetainerInfo> retainer_infos_;
336 List<ObjectGroupConnection> implicit_ref_connections_;
338 List<PendingPhantomCallback> pending_phantom_callbacks_;
340 friend class Isolate;
342 DISALLOW_COPY_AND_ASSIGN(GlobalHandles);
346 class GlobalHandles::PendingPhantomCallback {
348 typedef v8::WeakCallbackInfo<void> Data;
349 PendingPhantomCallback(
350 Node* node, Data::Callback callback, void* parameter,
351 void* internal_fields[v8::kInternalFieldsInWeakCallback])
352 : node_(node), callback_(callback), parameter_(parameter) {
353 for (int i = 0; i < v8::kInternalFieldsInWeakCallback; ++i) {
354 internal_fields_[i] = internal_fields[i];
358 void Invoke(Isolate* isolate);
360 Node* node() { return node_; }
361 Data::Callback callback() { return callback_; }
365 Data::Callback callback_;
367 void* internal_fields_[v8::kInternalFieldsInWeakCallback];
371 class EternalHandles {
373 enum SingletonHandle {
378 NUMBER_OF_SINGLETON_HANDLES
384 int NumberOfHandles() { return size_; }
386 // Create an EternalHandle, overwriting the index.
387 void Create(Isolate* isolate, Object* object, int* index);
389 // Grab the handle for an existing EternalHandle.
390 inline Handle<Object> Get(int index) {
391 return Handle<Object>(GetLocation(index));
394 // Grab the handle for an existing SingletonHandle.
395 inline Handle<Object> GetSingleton(SingletonHandle singleton) {
396 DCHECK(Exists(singleton));
397 return Get(singleton_handles_[singleton]);
400 // Checks whether a SingletonHandle has been assigned.
401 inline bool Exists(SingletonHandle singleton) {
402 return singleton_handles_[singleton] != kInvalidIndex;
405 // Assign a SingletonHandle to an empty slot and returns the handle.
406 Handle<Object> CreateSingleton(Isolate* isolate,
408 SingletonHandle singleton) {
409 Create(isolate, object, &singleton_handles_[singleton]);
410 return Get(singleton_handles_[singleton]);
413 // Iterates over all handles.
414 void IterateAllRoots(ObjectVisitor* visitor);
415 // Iterates over all handles which might be in new space.
416 void IterateNewSpaceRoots(ObjectVisitor* visitor);
417 // Rebuilds new space list.
418 void PostGarbageCollectionProcessing(Heap* heap);
421 static const int kInvalidIndex = -1;
422 static const int kShift = 8;
423 static const int kSize = 1 << kShift;
424 static const int kMask = 0xff;
426 // Gets the slot for an index
427 inline Object** GetLocation(int index) {
428 DCHECK(index >= 0 && index < size_);
429 return &blocks_[index >> kShift][index & kMask];
433 List<Object**> blocks_;
434 List<int> new_space_indices_;
435 int singleton_handles_[NUMBER_OF_SINGLETON_HANDLES];
437 DISALLOW_COPY_AND_ASSIGN(EternalHandles);
441 } // namespace internal
444 #endif // V8_GLOBAL_HANDLES_H_