v8  3.25.30(node0.11.13)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
global-handles.h
Go to the documentation of this file.
1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
27 
28 #ifndef V8_GLOBAL_HANDLES_H_
29 #define V8_GLOBAL_HANDLES_H_
30 
31 #include "../include/v8.h"
32 #include "../include/v8-profiler.h"
33 
34 #include "handles.h"
35 #include "list.h"
36 #include "v8utils.h"
37 
38 namespace v8 {
39 namespace internal {
40 
41 class GCTracer;
42 class HeapStats;
43 class ObjectVisitor;
44 
45 // Structure for tracking global handles.
46 // A single list keeps all the allocated global handles.
47 // Destroyed handles stay in the list but is added to the free list.
48 // At GC the destroyed global handles are removed from the free list
49 // and deallocated.
50 
51 // Data structures for tracking object groups and implicit references.
52 
53 // An object group is treated like a single JS object: if one of object in
54 // the group is alive, all objects in the same group are considered alive.
55 // An object group is used to simulate object relationship in a DOM tree.
56 
57 // An implicit references group consists of two parts: a parent object and a
58 // list of children objects. If the parent is alive, all the children are alive
59 // too.
60 
61 struct ObjectGroup {
62  explicit ObjectGroup(size_t length)
63  : info(NULL), length(length) {
64  ASSERT(length > 0);
65  objects = new Object**[length];
66  }
67  ~ObjectGroup();
68 
71  size_t length;
72 };
73 
74 
77  : parent(parent), length(length) {
78  ASSERT(length > 0);
79  children = new Object**[length];
80  }
82 
85  size_t length;
86 };
87 
88 
89 // For internal bookkeeping.
92  : id(id), object(object) {}
93 
94  bool operator==(const ObjectGroupConnection& other) const {
95  return id == other.id;
96  }
97 
98  bool operator<(const ObjectGroupConnection& other) const {
99  return id < other.id;
100  }
101 
104 };
105 
106 
109  : id(id), info(info) {}
110 
111  bool operator==(const ObjectGroupRetainerInfo& other) const {
112  return id == other.id;
113  }
114 
115  bool operator<(const ObjectGroupRetainerInfo& other) const {
116  return id < other.id;
117  }
118 
121 };
122 
123 
125  public:
126  ~GlobalHandles();
127 
128  // Creates a new global handle that is alive until Destroy is called.
129  Handle<Object> Create(Object* value);
130 
131  // Copy a global handle
132  static Handle<Object> CopyGlobal(Object** location);
133 
134  // Destroy a global handle.
135  static void Destroy(Object** location);
136 
138 
139  // Make the global handle weak and set the callback parameter for the
140  // handle. When the garbage collector recognizes that only weak global
141  // handles point to an object the handles are cleared and the callback
142  // function is invoked (for each handle) with the handle and corresponding
143  // parameter as arguments. Note: cleared means set to Smi::FromInt(0). The
144  // reason is that Smi::FromInt(0) does not change during garage collection.
145  static void MakeWeak(Object** location,
146  void* parameter,
147  WeakCallback weak_callback);
148 
149  void RecordStats(HeapStats* stats);
150 
151  // Returns the current number of weak handles.
152  int NumberOfWeakHandles();
153 
154  // Returns the current number of weak handles to global objects.
155  // These handles are also included in NumberOfWeakHandles().
157 
158  // Returns the current number of handles to global objects.
159  int global_handles_count() const {
160  return number_of_global_handles_;
161  }
162 
163  // Clear the weakness of a global handle.
164  static void* ClearWeakness(Object** location);
165 
166  // Clear the weakness of a global handle.
167  static void MarkIndependent(Object** location);
168 
169  // Mark the reference to this object externaly unreachable.
170  static void MarkPartiallyDependent(Object** location);
171 
172  static bool IsIndependent(Object** location);
173 
174  // Tells whether global handle is near death.
175  static bool IsNearDeath(Object** location);
176 
177  // Tells whether global handle is weak.
178  static bool IsWeak(Object** location);
179 
180  // Process pending weak handles.
181  // Returns true if next major GC is likely to collect more garbage.
183  GCTracer* tracer);
184 
185  // Iterates over all strong handles.
186  void IterateStrongRoots(ObjectVisitor* v);
187 
188  // Iterates over all handles.
189  void IterateAllRoots(ObjectVisitor* v);
190 
191  // Iterates over all handles that have embedder-assigned class ID.
192  void IterateAllRootsWithClassIds(ObjectVisitor* v);
193 
194  // Iterates over all handles in the new space that have embedder-assigned
195  // class ID.
196  void IterateAllRootsInNewSpaceWithClassIds(ObjectVisitor* v);
197 
198  // Iterates over all weak roots in heap.
199  void IterateWeakRoots(ObjectVisitor* v);
200 
201  // Find all weak handles satisfying the callback predicate, mark
202  // them as pending.
204 
205  // NOTE: Three ...NewSpace... functions below are used during
206  // scavenge collections and iterate over sets of handles that are
207  // guaranteed to contain all handles holding new space objects (but
208  // may also include old space objects).
209 
210  // Iterates over strong and dependent handles. See the node above.
211  void IterateNewSpaceStrongAndDependentRoots(ObjectVisitor* v);
212 
213  // Finds weak independent or partially independent handles satisfying
214  // the callback predicate and marks them as pending. See the note above.
216 
217  // Iterates over weak independent or partially independent handles.
218  // See the note above.
219  void IterateNewSpaceWeakIndependentRoots(ObjectVisitor* v);
220 
221  // Iterate over objects in object groups that have at least one object
222  // which requires visiting. The callback has to return true if objects
223  // can be skipped and false otherwise.
224  bool IterateObjectGroups(ObjectVisitor* v, WeakSlotCallbackWithHeap can_skip);
225 
226  // Add an object group.
227  // Should be only used in GC callback function before a collection.
228  // All groups are destroyed after a garbage collection.
229  void AddObjectGroup(Object*** handles,
230  size_t length,
232 
233  // Associates handle with the object group represented by id.
234  // Should be only used in GC callback function before a collection.
235  // All groups are destroyed after a garbage collection.
237 
238  // Set RetainedObjectInfo for an object group. Should not be called more than
239  // once for a group. Should not be called for a group which contains no
240  // handles.
242 
243  // Add an implicit references' group.
244  // Should be only used in GC callback function before a collection.
245  // All groups are destroyed after a mark-compact collection.
246  void AddImplicitReferences(HeapObject** parent,
247  Object*** children,
248  size_t length);
249 
250  // Adds an implicit reference from a group to an object. Should be only used
251  // in GC callback function before a collection. All implicit references are
252  // destroyed after a mark-compact collection.
253  void SetReferenceFromGroup(UniqueId id, Object** child);
254 
255  // Adds an implicit reference from a parent object to a child object. Should
256  // be only used in GC callback function before a collection. All implicit
257  // references are destroyed after a mark-compact collection.
258  void SetReference(HeapObject** parent, Object** child);
259 
261  ComputeObjectGroupsAndImplicitReferences();
262  return &object_groups_;
263  }
264 
266  ComputeObjectGroupsAndImplicitReferences();
267  return &implicit_ref_groups_;
268  }
269 
270  // Remove bags, this should only happen after GC.
271  void RemoveObjectGroups();
273 
274  // Tear down the global handle structure.
275  void TearDown();
276 
277  Isolate* isolate() { return isolate_; }
278 
279 #ifdef DEBUG
280  void PrintStats();
281  void Print();
282 #endif
283 
284  private:
285  explicit GlobalHandles(Isolate* isolate);
286 
287  // Migrates data from the internal representation (object_group_connections_,
288  // retainer_infos_ and implicit_ref_connections_) to the public and more
289  // efficient representation (object_groups_ and implicit_ref_groups_).
290  void ComputeObjectGroupsAndImplicitReferences();
291 
292  // v8::internal::List is inefficient even for small number of elements, if we
293  // don't assign any initial capacity.
294  static const int kObjectGroupConnectionsCapacity = 20;
295 
296  // Internal node structures.
297  class Node;
298  class NodeBlock;
299  class NodeIterator;
300 
301  Isolate* isolate_;
302 
303  // Field always containing the number of handles to global objects.
304  int number_of_global_handles_;
305 
306  // List of all allocated node blocks.
307  NodeBlock* first_block_;
308 
309  // List of node blocks with used nodes.
310  NodeBlock* first_used_block_;
311 
312  // Free list of nodes.
313  Node* first_free_;
314 
315  // Contains all nodes holding new space objects. Note: when the list
316  // is accessed, some of the objects may have been promoted already.
317  List<Node*> new_space_nodes_;
318 
319  int post_gc_processing_count_;
320 
321  // Object groups and implicit references, public and more efficient
322  // representation.
323  List<ObjectGroup*> object_groups_;
324  List<ImplicitRefGroup*> implicit_ref_groups_;
325 
326  // Object groups and implicit references, temporary representation while
327  // constructing the groups.
328  List<ObjectGroupConnection> object_group_connections_;
329  List<ObjectGroupRetainerInfo> retainer_infos_;
330  List<ObjectGroupConnection> implicit_ref_connections_;
331 
332  friend class Isolate;
333 
335 };
336 
337 
339  public:
344 
346  };
347 
348  EternalHandles();
349  ~EternalHandles();
350 
351  int NumberOfHandles() { return size_; }
352 
353  // Create an EternalHandle, overwriting the index.
354  void Create(Isolate* isolate, Object* object, int* index);
355 
356  // Grab the handle for an existing EternalHandle.
357  inline Handle<Object> Get(int index) {
358  return Handle<Object>(GetLocation(index));
359  }
360 
361  // Grab the handle for an existing SingletonHandle.
363  ASSERT(Exists(singleton));
364  return Get(singleton_handles_[singleton]);
365  }
366 
367  // Checks whether a SingletonHandle has been assigned.
368  inline bool Exists(SingletonHandle singleton) {
369  return singleton_handles_[singleton] != kInvalidIndex;
370  }
371 
372  // Assign a SingletonHandle to an empty slot and returns the handle.
374  Object* object,
375  SingletonHandle singleton) {
376  Create(isolate, object, &singleton_handles_[singleton]);
377  return Get(singleton_handles_[singleton]);
378  }
379 
380  // Iterates over all handles.
381  void IterateAllRoots(ObjectVisitor* visitor);
382  // Iterates over all handles which might be in new space.
383  void IterateNewSpaceRoots(ObjectVisitor* visitor);
384  // Rebuilds new space list.
386 
387  private:
388  static const int kInvalidIndex = -1;
389  static const int kShift = 8;
390  static const int kSize = 1 << kShift;
391  static const int kMask = 0xff;
392 
393  // Gets the slot for an index
394  inline Object** GetLocation(int index) {
395  ASSERT(index >= 0 && index < size_);
396  return &blocks_[index >> kShift][index & kMask];
397  }
398 
399  int size_;
400  List<Object**> blocks_;
401  List<int> new_space_indices_;
402  int singleton_handles_[NUMBER_OF_SINGLETON_HANDLES];
403 
404  DISALLOW_COPY_AND_ASSIGN(EternalHandles);
405 };
406 
407 
408 } } // namespace v8::internal
409 
410 #endif // V8_GLOBAL_HANDLES_H_
enable upcoming ES6 features enable harmony block scoping enable harmony enable harmony proxies enable harmony generators enable harmony numeric enable harmony string enable harmony math functions harmony_scoping harmony_symbols harmony_collections harmony_iteration harmony_strings harmony_scoping harmony_maths tracks arrays with only smi values Optimize object Array DOM strings and string pretenure call new trace pretenuring decisions of HAllocate instructions track fields with only smi values track fields with heap values track_fields track_fields Enables optimizations which favor memory size over execution speed use string slices optimization filter maximum number of GVN fix point iterations use function inlining use allocation folding eliminate write barriers targeting allocations in optimized code maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining crankshaft harvests type feedback from stub cache trace check elimination phase hydrogen tracing filter NULL
Definition: flags.cc:269
v8::RetainedObjectInfo * info
bool(* WeakSlotCallbackWithHeap)(Heap *heap, Object **pointer)
Definition: v8globals.h:171
List< ImplicitRefGroup * > * implicit_ref_groups()
static void Destroy(Object **location)
void IdentifyNewSpaceWeakIndependentHandles(WeakSlotCallbackWithHeap f)
Handle< Object > CreateSingleton(Isolate *isolate, Object *object, SingletonHandle singleton)
void PostGarbageCollectionProcessing(Heap *heap)
bool operator==(const ObjectGroupRetainerInfo &other) const
void AddImplicitReferences(HeapObject **parent, Object ***children, size_t length)
bool operator==(const ObjectGroupConnection &other) const
static bool IsNearDeath(Object **location)
void SetReferenceFromGroup(UniqueId id, Object **child)
void IterateWeakRoots(ObjectVisitor *v)
#define ASSERT(condition)
Definition: checks.h:329
void Create(Isolate *isolate, Object *object, int *index)
void IterateStrongRoots(ObjectVisitor *v)
void AddObjectGroup(Object ***handles, size_t length, v8::RetainedObjectInfo *info)
bool operator<(const ObjectGroupConnection &other) const
Handle< Object > Create(Object *value)
static bool IsIndependent(Object **location)
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: globals.h:359
static void * ClearWeakness(Object **location)
static Handle< Object > CopyGlobal(Object **location)
ObjectGroupConnection(UniqueId id, Object **object)
void IterateAllRoots(ObjectVisitor *v)
void SetReference(HeapObject **parent, Object **child)
void IterateAllRootsWithClassIds(ObjectVisitor *v)
void IterateNewSpaceRoots(ObjectVisitor *visitor)
void IterateNewSpaceStrongAndDependentRoots(ObjectVisitor *v)
List< ObjectGroup * > * object_groups()
bool IterateObjectGroups(ObjectVisitor *v, WeakSlotCallbackWithHeap can_skip)
void SetRetainedObjectInfo(UniqueId id, RetainedObjectInfo *info)
bool PostGarbageCollectionProcessing(GarbageCollector collector, GCTracer *tracer)
bool operator<(const ObjectGroupRetainerInfo &other) const
Handle< T > handle(T *t, Isolate *isolate)
Definition: handles.h:103
enable upcoming ES6 features enable harmony block scoping enable harmony enable harmony proxies enable harmony generators enable harmony numeric enable harmony string enable harmony math functions harmony_scoping harmony_symbols harmony_collections harmony_iteration harmony_strings harmony_scoping harmony_maths tracks arrays with only smi values Optimize object Array DOM strings and string pretenure call new trace pretenuring decisions of HAllocate instructions track fields with only smi values track fields with heap values track_fields track_fields Enables optimizations which favor memory size over execution speed use string slices optimization filter maximum number of GVN fix point iterations use function inlining use allocation folding eliminate write barriers targeting allocations in optimized code maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining crankshaft harvests type feedback from stub cache trace check elimination phase hydrogen tracing filter trace hydrogen to given file name trace inlining decisions trace store elimination trace all use positions trace global value numbering trace hydrogen escape analysis trace the tracking of allocation sites trace map generalization environment for every instruction deoptimize every n garbage collections put a break point before deoptimizing deoptimize uncommon cases use on stack replacement trace array bounds check elimination perform array index dehoisting use load elimination use store elimination use constant folding eliminate unreachable code number of stress runs when picking a function to watch for shared function info
Definition: flags.cc:317
void IdentifyWeakHandles(WeakSlotCallback f)
Handle< Object > Get(int index)
void RecordStats(HeapStats *stats)
static void MakeWeak(Object **location, void *parameter, WeakCallback weak_callback)
static void MarkPartiallyDependent(Object **location)
void SetObjectGroupId(Object **handle, UniqueId id)
void IterateAllRoots(ObjectVisitor *visitor)
Handle< Object > GetSingleton(SingletonHandle singleton)
ObjectGroupRetainerInfo(UniqueId id, RetainedObjectInfo *info)
static void MarkIndependent(Object **location)
static bool IsWeak(Object **location)
void Print(const v8::FunctionCallbackInfo< v8::Value > &args)
void IterateAllRootsInNewSpaceWithClassIds(ObjectVisitor *v)
void IterateNewSpaceWeakIndependentRoots(ObjectVisitor *v)
WeakCallbackData< v8::Value, void >::Callback WeakCallback
bool Exists(SingletonHandle singleton)
ObjectGroup(size_t length)
bool(* WeakSlotCallback)(Object **pointer)
Definition: v8globals.h:169
ImplicitRefGroup(HeapObject **parent, size_t length)