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 { |
| 101 | NORMAL_WEAK, // Embedder gets a handle to the dying object. |
| 102 | PHANTOM_WEAK, // Embedder gets the parameter they passed in earlier. |
| 103 | INTERNAL_FIELDS_WEAK // Embedder gets 2 internal fields from dying object. |
| 104 | }; |
| 105 | |
| 106 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 107 | class GlobalHandles { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 108 | public: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 109 | ~GlobalHandles(); |
| 110 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 111 | // Creates a new global handle that is alive until Destroy is called. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 112 | Handle<Object> Create(Object* value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 113 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 114 | // Copy a global handle |
| 115 | static Handle<Object> CopyGlobal(Object** location); |
| 116 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 117 | // Destroy a global handle. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 118 | static void Destroy(Object** location); |
| 119 | |
| 120 | typedef WeakCallbackData<v8::Value, void>::Callback WeakCallback; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 121 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 122 | // For a phantom weak reference, the callback does not have access to the |
| 123 | // dying object. Phantom weak references are preferred because they allow |
| 124 | // memory to be reclaimed in one GC cycle rather than two. However, for |
| 125 | // historical reasons the default is non-phantom. |
| 126 | enum PhantomState { Nonphantom, Phantom }; |
| 127 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 128 | // Make the global handle weak and set the callback parameter for the |
| 129 | // handle. When the garbage collector recognizes that only weak global |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 130 | // handles point to an object the callback function is invoked (for each |
| 131 | // handle) with the handle and corresponding parameter as arguments. By |
| 132 | // default the handle still contains a pointer to the object that is being |
| 133 | // collected. For this reason the object is not collected until the next |
| 134 | // GC. For a phantom weak handle the handle is cleared (set to a Smi) |
| 135 | // before the callback is invoked, but the handle can still be identified |
| 136 | // in the callback by using the location() of the handle. |
| 137 | static void MakeWeak(Object** location, void* parameter, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 138 | WeakCallback weak_callback); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 139 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 140 | // It would be nice to template this one, but it's really hard to get |
| 141 | // the template instantiator to work right if you do. |
| 142 | static void MakePhantom(Object** location, void* parameter, |
| 143 | PhantomCallbackData<void>::Callback weak_callback); |
| 144 | |
| 145 | static void MakePhantom( |
| 146 | Object** location, |
| 147 | v8::InternalFieldsCallbackData<void, void>::Callback weak_callback, |
| 148 | int16_t internal_field_index1, |
| 149 | int16_t internal_field_index2 = v8::Object::kNoInternalFieldIndex); |
| 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 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 165 | // Collect up data for the weak handle callbacks after GC has completed, but |
| 166 | // before memory is reclaimed. |
| 167 | void CollectPhantomCallbackData(); |
| 168 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 169 | // Clear the weakness of a global handle. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 170 | static void* ClearWeakness(Object** location); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 171 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 172 | // Clear the weakness of a global handle. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 173 | static void MarkIndependent(Object** location); |
| 174 | |
| 175 | // Mark the reference to this object externaly unreachable. |
| 176 | static void MarkPartiallyDependent(Object** location); |
| 177 | |
| 178 | static bool IsIndependent(Object** location); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 179 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 180 | // Tells whether global handle is near death. |
| 181 | static bool IsNearDeath(Object** location); |
| 182 | |
| 183 | // Tells whether global handle is weak. |
| 184 | static bool IsWeak(Object** location); |
| 185 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 186 | // Process pending weak handles. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 187 | // Returns the number of freed nodes. |
| 188 | int PostGarbageCollectionProcessing(GarbageCollector collector); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 189 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 190 | // Iterates over all strong handles. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 191 | void IterateStrongRoots(ObjectVisitor* v); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 192 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 193 | // Iterates over all handles. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 194 | void IterateAllRoots(ObjectVisitor* v); |
| 195 | |
| 196 | // Iterates over all handles that have embedder-assigned class ID. |
| 197 | void IterateAllRootsWithClassIds(ObjectVisitor* v); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 198 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 199 | // Iterates over all handles in the new space that have embedder-assigned |
| 200 | // class ID. |
| 201 | void IterateAllRootsInNewSpaceWithClassIds(ObjectVisitor* v); |
| 202 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 203 | // Iterates over all weak roots in heap. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 204 | void IterateWeakRoots(ObjectVisitor* v); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 205 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 206 | // Find all weak handles satisfying the callback predicate, mark |
| 207 | // them as pending. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 208 | void IdentifyWeakHandles(WeakSlotCallback f); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 209 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 210 | // NOTE: Three ...NewSpace... functions below are used during |
| 211 | // scavenge collections and iterate over sets of handles that are |
| 212 | // guaranteed to contain all handles holding new space objects (but |
| 213 | // may also include old space objects). |
| 214 | |
| 215 | // Iterates over strong and dependent handles. See the node above. |
| 216 | void IterateNewSpaceStrongAndDependentRoots(ObjectVisitor* v); |
| 217 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 218 | // Finds weak independent or partially independent handles satisfying |
| 219 | // the callback predicate and marks them as pending. See the note above. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 220 | void IdentifyNewSpaceWeakIndependentHandles(WeakSlotCallbackWithHeap f); |
| 221 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 222 | // Iterates over weak independent or partially independent handles. |
| 223 | // See the note above. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 224 | void IterateNewSpaceWeakIndependentRoots(ObjectVisitor* v); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 225 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 226 | // Iterate over objects in object groups that have at least one object |
| 227 | // which requires visiting. The callback has to return true if objects |
| 228 | // can be skipped and false otherwise. |
| 229 | bool IterateObjectGroups(ObjectVisitor* v, WeakSlotCallbackWithHeap can_skip); |
| 230 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 231 | // Add an object group. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 232 | // Should be only used in GC callback function before a collection. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 233 | // All groups are destroyed after a garbage collection. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 234 | void AddObjectGroup(Object*** handles, |
| 235 | size_t length, |
| 236 | v8::RetainedObjectInfo* info); |
| 237 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 238 | // Associates handle with the object group represented by id. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 239 | // Should be only used in GC callback function before a collection. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 240 | // All groups are destroyed after a garbage collection. |
| 241 | void SetObjectGroupId(Object** handle, UniqueId id); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 242 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 243 | // Set RetainedObjectInfo for an object group. Should not be called more than |
| 244 | // once for a group. Should not be called for a group which contains no |
| 245 | // handles. |
| 246 | void SetRetainedObjectInfo(UniqueId id, RetainedObjectInfo* info); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 247 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 248 | // Adds an implicit reference from a group to an object. Should be only used |
| 249 | // in GC callback function before a collection. All implicit references are |
| 250 | // destroyed after a mark-compact collection. |
| 251 | void SetReferenceFromGroup(UniqueId id, Object** child); |
| 252 | |
| 253 | // Adds an implicit reference from a parent object to a child object. Should |
| 254 | // be only used in GC callback function before a collection. All implicit |
| 255 | // references are destroyed after a mark-compact collection. |
| 256 | void SetReference(HeapObject** parent, Object** child); |
| 257 | |
| 258 | List<ObjectGroup*>* object_groups() { |
| 259 | ComputeObjectGroupsAndImplicitReferences(); |
| 260 | return &object_groups_; |
| 261 | } |
| 262 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 263 | List<ImplicitRefGroup*>* implicit_ref_groups() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 264 | ComputeObjectGroupsAndImplicitReferences(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 265 | return &implicit_ref_groups_; |
| 266 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 267 | |
| 268 | // Remove bags, this should only happen after GC. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 269 | void RemoveObjectGroups(); |
| 270 | void RemoveImplicitRefGroups(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 271 | |
| 272 | // Tear down the global handle structure. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 273 | void TearDown(); |
| 274 | |
| 275 | Isolate* isolate() { return isolate_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 276 | |
| 277 | #ifdef DEBUG |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 278 | void PrintStats(); |
| 279 | void Print(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 280 | #endif |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 281 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 282 | private: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 283 | explicit GlobalHandles(Isolate* isolate); |
| 284 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 285 | // Migrates data from the internal representation (object_group_connections_, |
| 286 | // retainer_infos_ and implicit_ref_connections_) to the public and more |
| 287 | // efficient representation (object_groups_ and implicit_ref_groups_). |
| 288 | void ComputeObjectGroupsAndImplicitReferences(); |
| 289 | |
| 290 | // v8::internal::List is inefficient even for small number of elements, if we |
| 291 | // don't assign any initial capacity. |
| 292 | static const int kObjectGroupConnectionsCapacity = 20; |
| 293 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 294 | // Helpers for PostGarbageCollectionProcessing. |
| 295 | int PostScavengeProcessing(int initial_post_gc_processing_count); |
| 296 | int PostMarkSweepProcessing(int initial_post_gc_processing_count); |
| 297 | int DispatchPendingPhantomCallbacks(); |
| 298 | void UpdateListOfNewSpaceNodes(); |
| 299 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 300 | // Internal node structures. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 301 | class Node; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 302 | class NodeBlock; |
| 303 | class NodeIterator; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 304 | class PendingPhantomCallback; |
| 305 | class PendingInternalFieldsCallback; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 306 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 307 | Isolate* isolate_; |
| 308 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 309 | // Field always containing the number of handles to global objects. |
| 310 | int number_of_global_handles_; |
| 311 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 312 | // List of all allocated node blocks. |
| 313 | NodeBlock* first_block_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 314 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 315 | // List of node blocks with used nodes. |
| 316 | NodeBlock* first_used_block_; |
| 317 | |
| 318 | // Free list of nodes. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 319 | Node* first_free_; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 320 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 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_; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 324 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 325 | int post_gc_processing_count_; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 326 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 327 | // Object groups and implicit references, public and more efficient |
| 328 | // representation. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 329 | List<ObjectGroup*> object_groups_; |
| 330 | List<ImplicitRefGroup*> implicit_ref_groups_; |
| 331 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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_; |
| 337 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 338 | List<PendingPhantomCallback> pending_phantom_callbacks_; |
| 339 | List<PendingInternalFieldsCallback> pending_internal_fields_callbacks_; |
| 340 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 341 | friend class Isolate; |
| 342 | |
| 343 | DISALLOW_COPY_AND_ASSIGN(GlobalHandles); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 344 | }; |
| 345 | |
| 346 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 347 | class GlobalHandles::PendingPhantomCallback { |
| 348 | public: |
| 349 | typedef PhantomCallbackData<void> Data; |
| 350 | PendingPhantomCallback(Node* node, Data data, Data::Callback callback) |
| 351 | : node_(node), data_(data), callback_(callback) {} |
| 352 | |
| 353 | void invoke(); |
| 354 | |
| 355 | Node* node() { return node_; } |
| 356 | |
| 357 | private: |
| 358 | Node* node_; |
| 359 | Data data_; |
| 360 | Data::Callback callback_; |
| 361 | }; |
| 362 | |
| 363 | |
| 364 | class GlobalHandles::PendingInternalFieldsCallback { |
| 365 | public: |
| 366 | typedef InternalFieldsCallbackData<void, void> Data; |
| 367 | PendingInternalFieldsCallback(Data data, Data::Callback callback) |
| 368 | : data_(data), callback_(callback) {} |
| 369 | |
| 370 | void invoke() { callback_(data_); } |
| 371 | |
| 372 | private: |
| 373 | Data data_; |
| 374 | Data::Callback callback_; |
| 375 | }; |
| 376 | |
| 377 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 378 | class EternalHandles { |
| 379 | public: |
| 380 | enum SingletonHandle { |
| 381 | I18N_TEMPLATE_ONE, |
| 382 | I18N_TEMPLATE_TWO, |
| 383 | DATE_CACHE_VERSION, |
| 384 | |
| 385 | NUMBER_OF_SINGLETON_HANDLES |
| 386 | }; |
| 387 | |
| 388 | EternalHandles(); |
| 389 | ~EternalHandles(); |
| 390 | |
| 391 | int NumberOfHandles() { return size_; } |
| 392 | |
| 393 | // Create an EternalHandle, overwriting the index. |
| 394 | void Create(Isolate* isolate, Object* object, int* index); |
| 395 | |
| 396 | // Grab the handle for an existing EternalHandle. |
| 397 | inline Handle<Object> Get(int index) { |
| 398 | return Handle<Object>(GetLocation(index)); |
| 399 | } |
| 400 | |
| 401 | // Grab the handle for an existing SingletonHandle. |
| 402 | inline Handle<Object> GetSingleton(SingletonHandle singleton) { |
| 403 | DCHECK(Exists(singleton)); |
| 404 | return Get(singleton_handles_[singleton]); |
| 405 | } |
| 406 | |
| 407 | // Checks whether a SingletonHandle has been assigned. |
| 408 | inline bool Exists(SingletonHandle singleton) { |
| 409 | return singleton_handles_[singleton] != kInvalidIndex; |
| 410 | } |
| 411 | |
| 412 | // Assign a SingletonHandle to an empty slot and returns the handle. |
| 413 | Handle<Object> CreateSingleton(Isolate* isolate, |
| 414 | Object* object, |
| 415 | SingletonHandle singleton) { |
| 416 | Create(isolate, object, &singleton_handles_[singleton]); |
| 417 | return Get(singleton_handles_[singleton]); |
| 418 | } |
| 419 | |
| 420 | // Iterates over all handles. |
| 421 | void IterateAllRoots(ObjectVisitor* visitor); |
| 422 | // Iterates over all handles which might be in new space. |
| 423 | void IterateNewSpaceRoots(ObjectVisitor* visitor); |
| 424 | // Rebuilds new space list. |
| 425 | void PostGarbageCollectionProcessing(Heap* heap); |
| 426 | |
| 427 | private: |
| 428 | static const int kInvalidIndex = -1; |
| 429 | static const int kShift = 8; |
| 430 | static const int kSize = 1 << kShift; |
| 431 | static const int kMask = 0xff; |
| 432 | |
| 433 | // Gets the slot for an index |
| 434 | inline Object** GetLocation(int index) { |
| 435 | DCHECK(index >= 0 && index < size_); |
| 436 | return &blocks_[index >> kShift][index & kMask]; |
| 437 | } |
| 438 | |
| 439 | int size_; |
| 440 | List<Object**> blocks_; |
| 441 | List<int> new_space_indices_; |
| 442 | int singleton_handles_[NUMBER_OF_SINGLETON_HANDLES]; |
| 443 | |
| 444 | DISALLOW_COPY_AND_ASSIGN(EternalHandles); |
| 445 | }; |
| 446 | |
| 447 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 448 | } } // namespace v8::internal |
| 449 | |
| 450 | #endif // V8_GLOBAL_HANDLES_H_ |