Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4 | |
| 5 | #ifndef V8_GLOBAL_HANDLES_H_ |
| 6 | #define V8_GLOBAL_HANDLES_H_ |
| 7 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8 | #include "include/v8.h" |
| 9 | #include "include/v8-profiler.h" |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11 | #include "src/handles.h" |
| 12 | #include "src/list.h" |
| 13 | #include "src/utils.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14 | |
| 15 | namespace v8 { |
| 16 | namespace internal { |
| 17 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18 | class HeapStats; |
| 19 | class ObjectVisitor; |
| 20 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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 |
| 25 | // and deallocated. |
| 26 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 27 | // Data structures for tracking object groups and implicit references. |
| 28 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 32 | |
| 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 |
| 35 | // too. |
| 36 | |
| 37 | struct ObjectGroup { |
| 38 | explicit ObjectGroup(size_t length) |
| 39 | : info(NULL), length(length) { |
| 40 | DCHECK(length > 0); |
| 41 | objects = new Object**[length]; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 42 | } |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 43 | ~ObjectGroup(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 44 | |
| 45 | v8::RetainedObjectInfo* info; |
| 46 | Object*** objects; |
| 47 | size_t length; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 51 | struct ImplicitRefGroup { |
| 52 | ImplicitRefGroup(HeapObject** parent, size_t length) |
| 53 | : parent(parent), length(length) { |
| 54 | DCHECK(length > 0); |
| 55 | children = new Object**[length]; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 56 | } |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 57 | ~ImplicitRefGroup(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 58 | |
| 59 | HeapObject** parent; |
| 60 | Object*** children; |
| 61 | size_t length; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 65 | // For internal bookkeeping. |
| 66 | struct ObjectGroupConnection { |
| 67 | ObjectGroupConnection(UniqueId id, Object** object) |
| 68 | : id(id), object(object) {} |
| 69 | |
| 70 | bool operator==(const ObjectGroupConnection& other) const { |
| 71 | return id == other.id; |
| 72 | } |
| 73 | |
| 74 | bool operator<(const ObjectGroupConnection& other) const { |
| 75 | return id < other.id; |
| 76 | } |
| 77 | |
| 78 | UniqueId id; |
| 79 | Object** object; |
| 80 | }; |
| 81 | |
| 82 | |
| 83 | struct ObjectGroupRetainerInfo { |
| 84 | ObjectGroupRetainerInfo(UniqueId id, RetainedObjectInfo* info) |
| 85 | : id(id), info(info) {} |
| 86 | |
| 87 | bool operator==(const ObjectGroupRetainerInfo& other) const { |
| 88 | return id == other.id; |
| 89 | } |
| 90 | |
| 91 | bool operator<(const ObjectGroupRetainerInfo& other) const { |
| 92 | return id < other.id; |
| 93 | } |
| 94 | |
| 95 | UniqueId id; |
| 96 | RetainedObjectInfo* info; |
| 97 | }; |
| 98 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 99 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 100 | enum WeaknessType { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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. |
| 107 | PHANTOM_WEAK, |
| 108 | PHANTOM_WEAK_2_INTERNAL_FIELDS |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 112 | class GlobalHandles { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 113 | public: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 114 | ~GlobalHandles(); |
| 115 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 116 | // Creates a new global handle that is alive until Destroy is called. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 117 | Handle<Object> Create(Object* value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 118 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 119 | // Copy a global handle |
| 120 | static Handle<Object> CopyGlobal(Object** location); |
| 121 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 122 | // Destroy a global handle. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 123 | static void Destroy(Object** location); |
| 124 | |
| 125 | typedef WeakCallbackData<v8::Value, void>::Callback WeakCallback; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 126 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 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 }; |
| 132 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 133 | // Make the global handle weak and set the callback parameter for the |
| 134 | // handle. When the garbage collector recognizes that only weak global |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 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, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 143 | WeakCallback weak_callback); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 144 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 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. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 147 | static void MakeWeak(Object** location, void* parameter, |
| 148 | WeakCallbackInfo<void>::Callback weak_callback, |
| 149 | v8::WeakCallbackType type); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 150 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 151 | void RecordStats(HeapStats* stats); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 152 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 153 | // Returns the current number of weak handles. |
| 154 | int NumberOfWeakHandles(); |
| 155 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 156 | // Returns the current number of weak handles to global objects. |
| 157 | // These handles are also included in NumberOfWeakHandles(). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 158 | int NumberOfGlobalObjectWeakHandles(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 159 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 160 | // Returns the current number of handles to global objects. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 161 | int global_handles_count() const { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 162 | return number_of_global_handles_; |
| 163 | } |
| 164 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 165 | // Clear the weakness of a global handle. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 166 | static void* ClearWeakness(Object** location); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 167 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 168 | // Clear the weakness of a global handle. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 169 | static void MarkIndependent(Object** location); |
| 170 | |
| 171 | // Mark the reference to this object externaly unreachable. |
| 172 | static void MarkPartiallyDependent(Object** location); |
| 173 | |
| 174 | static bool IsIndependent(Object** location); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 175 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 176 | // Tells whether global handle is near death. |
| 177 | static bool IsNearDeath(Object** location); |
| 178 | |
| 179 | // Tells whether global handle is weak. |
| 180 | static bool IsWeak(Object** location); |
| 181 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 182 | // Process pending weak handles. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 183 | // Returns the number of freed nodes. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 184 | int PostGarbageCollectionProcessing( |
| 185 | GarbageCollector collector, const v8::GCCallbackFlags gc_callback_flags); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 186 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 187 | // Iterates over all strong handles. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 188 | void IterateStrongRoots(ObjectVisitor* v); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 189 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 190 | // Iterates over all handles. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 191 | void IterateAllRoots(ObjectVisitor* v); |
| 192 | |
| 193 | // Iterates over all handles that have embedder-assigned class ID. |
| 194 | void IterateAllRootsWithClassIds(ObjectVisitor* v); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 195 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 196 | // Iterates over all handles in the new space that have embedder-assigned |
| 197 | // class ID. |
| 198 | void IterateAllRootsInNewSpaceWithClassIds(ObjectVisitor* v); |
| 199 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 200 | // Iterate over all handles in the new space that are weak, unmodified |
| 201 | // and have class IDs |
| 202 | void IterateWeakRootsInNewSpaceWithClassIds(ObjectVisitor* v); |
| 203 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 204 | // Iterates over all weak roots in heap. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 205 | void IterateWeakRoots(ObjectVisitor* v); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 206 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 207 | // Find all weak handles satisfying the callback predicate, mark |
| 208 | // them as pending. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 209 | void IdentifyWeakHandles(WeakSlotCallback f); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 210 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 211 | // NOTE: Five ...NewSpace... functions below are used during |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 212 | // scavenge collections and iterate over sets of handles that are |
| 213 | // guaranteed to contain all handles holding new space objects (but |
| 214 | // may also include old space objects). |
| 215 | |
| 216 | // Iterates over strong and dependent handles. See the node above. |
| 217 | void IterateNewSpaceStrongAndDependentRoots(ObjectVisitor* v); |
| 218 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 219 | // Finds weak independent or partially independent handles satisfying |
| 220 | // the callback predicate and marks them as pending. See the note above. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 221 | void IdentifyNewSpaceWeakIndependentHandles(WeakSlotCallbackWithHeap f); |
| 222 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 223 | // Iterates over weak independent or partially independent handles. |
| 224 | // See the note above. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 225 | void IterateNewSpaceWeakIndependentRoots(ObjectVisitor* v); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 226 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 227 | // Finds weak independent or unmodified handles satisfying |
| 228 | // the callback predicate and marks them as pending. See the note above. |
| 229 | void MarkNewSpaceWeakUnmodifiedObjectsPending( |
| 230 | WeakSlotCallbackWithHeap is_unscavenged); |
| 231 | |
| 232 | // Iterates over weak independent or unmodified handles. |
| 233 | // See the note above. |
| 234 | void IterateNewSpaceWeakUnmodifiedRoots(ObjectVisitor* v); |
| 235 | |
| 236 | // Identify unmodified objects that are in weak state and marks them |
| 237 | // unmodified |
| 238 | void IdentifyWeakUnmodifiedObjects(WeakSlotCallback is_unmodified); |
| 239 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 240 | // Iterate over objects in object groups that have at least one object |
| 241 | // which requires visiting. The callback has to return true if objects |
| 242 | // can be skipped and false otherwise. |
| 243 | bool IterateObjectGroups(ObjectVisitor* v, WeakSlotCallbackWithHeap can_skip); |
| 244 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 245 | // Add an object group. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 246 | // Should be only used in GC callback function before a collection. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 247 | // All groups are destroyed after a garbage collection. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 248 | void AddObjectGroup(Object*** handles, |
| 249 | size_t length, |
| 250 | v8::RetainedObjectInfo* info); |
| 251 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 252 | // Associates handle with the object group represented by id. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 253 | // Should be only used in GC callback function before a collection. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 254 | // All groups are destroyed after a garbage collection. |
| 255 | void SetObjectGroupId(Object** handle, UniqueId id); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 256 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 257 | // Set RetainedObjectInfo for an object group. Should not be called more than |
| 258 | // once for a group. Should not be called for a group which contains no |
| 259 | // handles. |
| 260 | void SetRetainedObjectInfo(UniqueId id, RetainedObjectInfo* info); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 261 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 262 | // Adds an implicit reference from a group to an object. Should be only used |
| 263 | // in GC callback function before a collection. All implicit references are |
| 264 | // destroyed after a mark-compact collection. |
| 265 | void SetReferenceFromGroup(UniqueId id, Object** child); |
| 266 | |
| 267 | // Adds an implicit reference from a parent object to a child object. Should |
| 268 | // be only used in GC callback function before a collection. All implicit |
| 269 | // references are destroyed after a mark-compact collection. |
| 270 | void SetReference(HeapObject** parent, Object** child); |
| 271 | |
| 272 | List<ObjectGroup*>* object_groups() { |
| 273 | ComputeObjectGroupsAndImplicitReferences(); |
| 274 | return &object_groups_; |
| 275 | } |
| 276 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 277 | List<ImplicitRefGroup*>* implicit_ref_groups() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 278 | ComputeObjectGroupsAndImplicitReferences(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 279 | return &implicit_ref_groups_; |
| 280 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 281 | |
| 282 | // Remove bags, this should only happen after GC. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 283 | void RemoveObjectGroups(); |
| 284 | void RemoveImplicitRefGroups(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 285 | |
| 286 | // Tear down the global handle structure. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 287 | void TearDown(); |
| 288 | |
| 289 | Isolate* isolate() { return isolate_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 290 | |
| 291 | #ifdef DEBUG |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 292 | void PrintStats(); |
| 293 | void Print(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 294 | #endif |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 295 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 296 | private: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 297 | explicit GlobalHandles(Isolate* isolate); |
| 298 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 299 | // Migrates data from the internal representation (object_group_connections_, |
| 300 | // retainer_infos_ and implicit_ref_connections_) to the public and more |
| 301 | // efficient representation (object_groups_ and implicit_ref_groups_). |
| 302 | void ComputeObjectGroupsAndImplicitReferences(); |
| 303 | |
| 304 | // v8::internal::List is inefficient even for small number of elements, if we |
| 305 | // don't assign any initial capacity. |
| 306 | static const int kObjectGroupConnectionsCapacity = 20; |
| 307 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 308 | class PendingPhantomCallback; |
| 309 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 310 | // Helpers for PostGarbageCollectionProcessing. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 311 | static void InvokeSecondPassPhantomCallbacks( |
| 312 | List<PendingPhantomCallback>* callbacks, Isolate* isolate); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 313 | int PostScavengeProcessing(int initial_post_gc_processing_count); |
| 314 | int PostMarkSweepProcessing(int initial_post_gc_processing_count); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 315 | int DispatchPendingPhantomCallbacks(bool synchronous_second_pass); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 316 | void UpdateListOfNewSpaceNodes(); |
| 317 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 318 | // Internal node structures. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 319 | class Node; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 320 | class NodeBlock; |
| 321 | class NodeIterator; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 322 | class PendingPhantomCallbacksSecondPassTask; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 323 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 324 | Isolate* isolate_; |
| 325 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 326 | // Field always containing the number of handles to global objects. |
| 327 | int number_of_global_handles_; |
| 328 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 329 | // List of all allocated node blocks. |
| 330 | NodeBlock* first_block_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 331 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 332 | // List of node blocks with used nodes. |
| 333 | NodeBlock* first_used_block_; |
| 334 | |
| 335 | // Free list of nodes. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 336 | Node* first_free_; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 337 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 338 | // Contains all nodes holding new space objects. Note: when the list |
| 339 | // is accessed, some of the objects may have been promoted already. |
| 340 | List<Node*> new_space_nodes_; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 341 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 342 | int post_gc_processing_count_; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 343 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 344 | // Object groups and implicit references, public and more efficient |
| 345 | // representation. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 346 | List<ObjectGroup*> object_groups_; |
| 347 | List<ImplicitRefGroup*> implicit_ref_groups_; |
| 348 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 349 | // Object groups and implicit references, temporary representation while |
| 350 | // constructing the groups. |
| 351 | List<ObjectGroupConnection> object_group_connections_; |
| 352 | List<ObjectGroupRetainerInfo> retainer_infos_; |
| 353 | List<ObjectGroupConnection> implicit_ref_connections_; |
| 354 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 355 | List<PendingPhantomCallback> pending_phantom_callbacks_; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 356 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 357 | friend class Isolate; |
| 358 | |
| 359 | DISALLOW_COPY_AND_ASSIGN(GlobalHandles); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 360 | }; |
| 361 | |
| 362 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 363 | class GlobalHandles::PendingPhantomCallback { |
| 364 | public: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 365 | typedef v8::WeakCallbackInfo<void> Data; |
| 366 | PendingPhantomCallback( |
| 367 | Node* node, Data::Callback callback, void* parameter, |
| 368 | void* internal_fields[v8::kInternalFieldsInWeakCallback]) |
| 369 | : node_(node), callback_(callback), parameter_(parameter) { |
| 370 | for (int i = 0; i < v8::kInternalFieldsInWeakCallback; ++i) { |
| 371 | internal_fields_[i] = internal_fields[i]; |
| 372 | } |
| 373 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 374 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 375 | void Invoke(Isolate* isolate); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 376 | |
| 377 | Node* node() { return node_; } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 378 | Data::Callback callback() { return callback_; } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 379 | |
| 380 | private: |
| 381 | Node* node_; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 382 | Data::Callback callback_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 383 | void* parameter_; |
| 384 | void* internal_fields_[v8::kInternalFieldsInWeakCallback]; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 385 | }; |
| 386 | |
| 387 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 388 | class EternalHandles { |
| 389 | public: |
| 390 | enum SingletonHandle { |
| 391 | I18N_TEMPLATE_ONE, |
| 392 | I18N_TEMPLATE_TWO, |
| 393 | DATE_CACHE_VERSION, |
| 394 | |
| 395 | NUMBER_OF_SINGLETON_HANDLES |
| 396 | }; |
| 397 | |
| 398 | EternalHandles(); |
| 399 | ~EternalHandles(); |
| 400 | |
| 401 | int NumberOfHandles() { return size_; } |
| 402 | |
| 403 | // Create an EternalHandle, overwriting the index. |
| 404 | void Create(Isolate* isolate, Object* object, int* index); |
| 405 | |
| 406 | // Grab the handle for an existing EternalHandle. |
| 407 | inline Handle<Object> Get(int index) { |
| 408 | return Handle<Object>(GetLocation(index)); |
| 409 | } |
| 410 | |
| 411 | // Grab the handle for an existing SingletonHandle. |
| 412 | inline Handle<Object> GetSingleton(SingletonHandle singleton) { |
| 413 | DCHECK(Exists(singleton)); |
| 414 | return Get(singleton_handles_[singleton]); |
| 415 | } |
| 416 | |
| 417 | // Checks whether a SingletonHandle has been assigned. |
| 418 | inline bool Exists(SingletonHandle singleton) { |
| 419 | return singleton_handles_[singleton] != kInvalidIndex; |
| 420 | } |
| 421 | |
| 422 | // Assign a SingletonHandle to an empty slot and returns the handle. |
| 423 | Handle<Object> CreateSingleton(Isolate* isolate, |
| 424 | Object* object, |
| 425 | SingletonHandle singleton) { |
| 426 | Create(isolate, object, &singleton_handles_[singleton]); |
| 427 | return Get(singleton_handles_[singleton]); |
| 428 | } |
| 429 | |
| 430 | // Iterates over all handles. |
| 431 | void IterateAllRoots(ObjectVisitor* visitor); |
| 432 | // Iterates over all handles which might be in new space. |
| 433 | void IterateNewSpaceRoots(ObjectVisitor* visitor); |
| 434 | // Rebuilds new space list. |
| 435 | void PostGarbageCollectionProcessing(Heap* heap); |
| 436 | |
| 437 | private: |
| 438 | static const int kInvalidIndex = -1; |
| 439 | static const int kShift = 8; |
| 440 | static const int kSize = 1 << kShift; |
| 441 | static const int kMask = 0xff; |
| 442 | |
| 443 | // Gets the slot for an index |
| 444 | inline Object** GetLocation(int index) { |
| 445 | DCHECK(index >= 0 && index < size_); |
| 446 | return &blocks_[index >> kShift][index & kMask]; |
| 447 | } |
| 448 | |
| 449 | int size_; |
| 450 | List<Object**> blocks_; |
| 451 | List<int> new_space_indices_; |
| 452 | int singleton_handles_[NUMBER_OF_SINGLETON_HANDLES]; |
| 453 | |
| 454 | DISALLOW_COPY_AND_ASSIGN(EternalHandles); |
| 455 | }; |
| 456 | |
| 457 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 458 | } // namespace internal |
| 459 | } // namespace v8 |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 460 | |
| 461 | #endif // V8_GLOBAL_HANDLES_H_ |