OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 10 matching lines...) Expand all Loading... |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 #ifndef V8_GLOBAL_HANDLES_H_ | 28 #ifndef V8_GLOBAL_HANDLES_H_ |
29 #define V8_GLOBAL_HANDLES_H_ | 29 #define V8_GLOBAL_HANDLES_H_ |
30 | 30 |
| 31 #include "../include/v8.h" |
31 #include "../include/v8-profiler.h" | 32 #include "../include/v8-profiler.h" |
32 | 33 |
33 #include "list.h" | 34 #include "list.h" |
34 #include "v8utils.h" | 35 #include "v8utils.h" |
35 | 36 |
36 namespace v8 { | 37 namespace v8 { |
37 namespace internal { | 38 namespace internal { |
38 | 39 |
39 class GCTracer; | 40 class GCTracer; |
40 class HeapStats; | 41 class HeapStats; |
41 class ObjectVisitor; | 42 class ObjectVisitor; |
42 | 43 |
43 // Structure for tracking global handles. | 44 // Structure for tracking global handles. |
44 // A single list keeps all the allocated global handles. | 45 // A single list keeps all the allocated global handles. |
45 // Destroyed handles stay in the list but is added to the free list. | 46 // Destroyed handles stay in the list but is added to the free list. |
46 // At GC the destroyed global handles are removed from the free list | 47 // At GC the destroyed global handles are removed from the free list |
47 // and deallocated. | 48 // and deallocated. |
48 | 49 |
| 50 // Data structures for tracking object groups and implicit references. |
| 51 |
49 // An object group is treated like a single JS object: if one of object in | 52 // An object group is treated like a single JS object: if one of object in |
50 // the group is alive, all objects in the same group are considered alive. | 53 // the group is alive, all objects in the same group are considered alive. |
51 // An object group is used to simulate object relationship in a DOM tree. | 54 // An object group is used to simulate object relationship in a DOM tree. |
52 class ObjectGroup { | 55 |
53 public: | 56 // An implicit references group consists of two parts: a parent object and a |
54 static ObjectGroup* New(Object*** handles, | 57 // list of children objects. If the parent is alive, all the children are alive |
55 size_t length, | 58 // too. |
56 v8::RetainedObjectInfo* info) { | 59 |
| 60 struct ObjectGroup { |
| 61 explicit ObjectGroup(size_t length) |
| 62 : info(NULL), length(length) { |
57 ASSERT(length > 0); | 63 ASSERT(length > 0); |
58 ObjectGroup* group = reinterpret_cast<ObjectGroup*>( | 64 objects = new Object**[length]; |
59 malloc(OFFSET_OF(ObjectGroup, objects_[length]))); | |
60 group->length_ = length; | |
61 group->info_ = info; | |
62 CopyWords(group->objects_, handles, static_cast<int>(length)); | |
63 return group; | |
64 } | 65 } |
| 66 ~ObjectGroup(); |
65 | 67 |
66 void Dispose() { | 68 v8::RetainedObjectInfo* info; |
67 if (info_ != NULL) info_->Dispose(); | 69 Object*** objects; |
68 free(this); | 70 size_t length; |
69 } | |
70 | |
71 size_t length_; | |
72 v8::RetainedObjectInfo* info_; | |
73 Object** objects_[1]; // Variable sized array. | |
74 | |
75 private: | |
76 void* operator new(size_t size); | |
77 void operator delete(void* p); | |
78 ~ObjectGroup(); | |
79 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectGroup); | |
80 }; | 71 }; |
81 | 72 |
82 | 73 |
83 // An implicit references group consists of two parts: a parent object and | 74 struct ImplicitRefGroup { |
84 // a list of children objects. If the parent is alive, all the children | 75 ImplicitRefGroup(HeapObject** parent, size_t length) |
85 // are alive too. | 76 : parent(parent), length(length) { |
86 class ImplicitRefGroup { | |
87 public: | |
88 static ImplicitRefGroup* New(HeapObject** parent, | |
89 Object*** children, | |
90 size_t length) { | |
91 ASSERT(length > 0); | 77 ASSERT(length > 0); |
92 ImplicitRefGroup* group = reinterpret_cast<ImplicitRefGroup*>( | 78 children = new Object**[length]; |
93 malloc(OFFSET_OF(ImplicitRefGroup, children_[length]))); | |
94 group->parent_ = parent; | |
95 group->length_ = length; | |
96 CopyWords(group->children_, children, length); | |
97 return group; | |
98 } | 79 } |
| 80 ~ImplicitRefGroup(); |
99 | 81 |
100 void Dispose() { | 82 HeapObject** parent; |
101 free(this); | 83 Object*** children; |
102 } | 84 size_t length; |
103 | |
104 HeapObject** parent_; | |
105 size_t length_; | |
106 Object** children_[1]; // Variable sized array. | |
107 | |
108 private: | |
109 void* operator new(size_t size); | |
110 void operator delete(void* p); | |
111 ~ImplicitRefGroup(); | |
112 DISALLOW_IMPLICIT_CONSTRUCTORS(ImplicitRefGroup); | |
113 }; | 85 }; |
114 | 86 |
115 | 87 |
| 88 // For internal bookkeeping. |
| 89 struct ObjectGroupConnection { |
| 90 ObjectGroupConnection(UniqueId id, Object** object) |
| 91 : id(id), object(object) {} |
| 92 |
| 93 bool operator==(const ObjectGroupConnection& other) const { |
| 94 return id == other.id; |
| 95 } |
| 96 |
| 97 bool operator<(const ObjectGroupConnection& other) const { |
| 98 return id < other.id; |
| 99 } |
| 100 |
| 101 UniqueId id; |
| 102 Object** object; |
| 103 }; |
| 104 |
| 105 |
| 106 struct ObjectGroupRetainerInfo { |
| 107 ObjectGroupRetainerInfo(UniqueId id, RetainedObjectInfo* info) |
| 108 : id(id), info(info) {} |
| 109 |
| 110 bool operator==(const ObjectGroupRetainerInfo& other) const { |
| 111 return id == other.id; |
| 112 } |
| 113 |
| 114 bool operator<(const ObjectGroupRetainerInfo& other) const { |
| 115 return id < other.id; |
| 116 } |
| 117 |
| 118 UniqueId id; |
| 119 RetainedObjectInfo* info; |
| 120 }; |
| 121 |
| 122 |
116 class GlobalHandles { | 123 class GlobalHandles { |
117 public: | 124 public: |
118 ~GlobalHandles(); | 125 ~GlobalHandles(); |
119 | 126 |
120 // Creates a new global handle that is alive until Destroy is called. | 127 // Creates a new global handle that is alive until Destroy is called. |
121 Handle<Object> Create(Object* value); | 128 Handle<Object> Create(Object* value); |
122 | 129 |
123 // Destroy a global handle. | 130 // Destroy a global handle. |
124 void Destroy(Object** location); | 131 void Destroy(Object** location); |
125 | 132 |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 // can be skipped and false otherwise. | 218 // can be skipped and false otherwise. |
212 bool IterateObjectGroups(ObjectVisitor* v, WeakSlotCallbackWithHeap can_skip); | 219 bool IterateObjectGroups(ObjectVisitor* v, WeakSlotCallbackWithHeap can_skip); |
213 | 220 |
214 // Add an object group. | 221 // Add an object group. |
215 // Should be only used in GC callback function before a collection. | 222 // Should be only used in GC callback function before a collection. |
216 // All groups are destroyed after a garbage collection. | 223 // All groups are destroyed after a garbage collection. |
217 void AddObjectGroup(Object*** handles, | 224 void AddObjectGroup(Object*** handles, |
218 size_t length, | 225 size_t length, |
219 v8::RetainedObjectInfo* info); | 226 v8::RetainedObjectInfo* info); |
220 | 227 |
| 228 // Associates handle with the object group represented by id. |
| 229 // Should be only used in GC callback function before a collection. |
| 230 // All groups are destroyed after a garbage collection. |
| 231 void SetObjectGroupId(Object** handle, UniqueId id); |
| 232 |
| 233 // Set RetainedObjectInfo for an object group. Should not be called more than |
| 234 // once for a group. Should not be called for a group which contains no |
| 235 // handles. |
| 236 void SetRetainedObjectInfo(UniqueId id, RetainedObjectInfo* info); |
| 237 |
221 // Add an implicit references' group. | 238 // Add an implicit references' group. |
222 // Should be only used in GC callback function before a collection. | 239 // Should be only used in GC callback function before a collection. |
223 // All groups are destroyed after a mark-compact collection. | 240 // All groups are destroyed after a mark-compact collection. |
224 void AddImplicitReferences(HeapObject** parent, | 241 void AddImplicitReferences(HeapObject** parent, |
225 Object*** children, | 242 Object*** children, |
226 size_t length); | 243 size_t length); |
227 | 244 |
228 // Returns the object groups. | 245 // Adds an implicit reference from a group to an object. Should be only used |
229 List<ObjectGroup*>* object_groups() { return &object_groups_; } | 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); |
230 | 249 |
231 // Returns the implicit references' groups. | 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); |
| 254 |
| 255 List<ObjectGroup*>* object_groups() { |
| 256 ComputeObjectGroupsAndImplicitReferences(); |
| 257 return &object_groups_; |
| 258 } |
| 259 |
232 List<ImplicitRefGroup*>* implicit_ref_groups() { | 260 List<ImplicitRefGroup*>* implicit_ref_groups() { |
| 261 ComputeObjectGroupsAndImplicitReferences(); |
233 return &implicit_ref_groups_; | 262 return &implicit_ref_groups_; |
234 } | 263 } |
235 | 264 |
236 // Remove bags, this should only happen after GC. | 265 // Remove bags, this should only happen after GC. |
237 void RemoveObjectGroups(); | 266 void RemoveObjectGroups(); |
238 void RemoveImplicitRefGroups(); | 267 void RemoveImplicitRefGroups(); |
239 | 268 |
240 // Tear down the global handle structure. | 269 // Tear down the global handle structure. |
241 void TearDown(); | 270 void TearDown(); |
242 | 271 |
243 Isolate* isolate() { return isolate_; } | 272 Isolate* isolate() { return isolate_; } |
244 | 273 |
245 #ifdef DEBUG | 274 #ifdef DEBUG |
246 void PrintStats(); | 275 void PrintStats(); |
247 void Print(); | 276 void Print(); |
248 #endif | 277 #endif |
249 | 278 |
250 private: | 279 private: |
251 explicit GlobalHandles(Isolate* isolate); | 280 explicit GlobalHandles(Isolate* isolate); |
252 | 281 |
| 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(); |
| 286 |
| 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; |
| 290 |
253 // Internal node structures. | 291 // Internal node structures. |
254 class Node; | 292 class Node; |
255 class NodeBlock; | 293 class NodeBlock; |
256 class NodeIterator; | 294 class NodeIterator; |
257 | 295 |
258 Isolate* isolate_; | 296 Isolate* isolate_; |
259 | 297 |
260 // Field always containing the number of handles to global objects. | 298 // Field always containing the number of handles to global objects. |
261 int number_of_global_handles_; | 299 int number_of_global_handles_; |
262 | 300 |
263 // List of all allocated node blocks. | 301 // List of all allocated node blocks. |
264 NodeBlock* first_block_; | 302 NodeBlock* first_block_; |
265 | 303 |
266 // List of node blocks with used nodes. | 304 // List of node blocks with used nodes. |
267 NodeBlock* first_used_block_; | 305 NodeBlock* first_used_block_; |
268 | 306 |
269 // Free list of nodes. | 307 // Free list of nodes. |
270 Node* first_free_; | 308 Node* first_free_; |
271 | 309 |
272 // Contains all nodes holding new space objects. Note: when the list | 310 // Contains all nodes holding new space objects. Note: when the list |
273 // is accessed, some of the objects may have been promoted already. | 311 // is accessed, some of the objects may have been promoted already. |
274 List<Node*> new_space_nodes_; | 312 List<Node*> new_space_nodes_; |
275 | 313 |
276 int post_gc_processing_count_; | 314 int post_gc_processing_count_; |
277 | 315 |
| 316 // Object groups and implicit references, public and more efficient |
| 317 // representation. |
278 List<ObjectGroup*> object_groups_; | 318 List<ObjectGroup*> object_groups_; |
279 List<ImplicitRefGroup*> implicit_ref_groups_; | 319 List<ImplicitRefGroup*> implicit_ref_groups_; |
280 | 320 |
| 321 // Object groups and implicit references, temporary representation while |
| 322 // constructing the groups. |
| 323 List<ObjectGroupConnection> object_group_connections_; |
| 324 List<ObjectGroupRetainerInfo> retainer_infos_; |
| 325 List<ObjectGroupConnection> implicit_ref_connections_; |
| 326 |
281 friend class Isolate; | 327 friend class Isolate; |
282 | 328 |
283 DISALLOW_COPY_AND_ASSIGN(GlobalHandles); | 329 DISALLOW_COPY_AND_ASSIGN(GlobalHandles); |
284 }; | 330 }; |
285 | 331 |
286 | 332 |
287 } } // namespace v8::internal | 333 } } // namespace v8::internal |
288 | 334 |
289 #endif // V8_GLOBAL_HANDLES_H_ | 335 #endif // V8_GLOBAL_HANDLES_H_ |
OLD | NEW |