V8 Project
global-handles.h
Go to the documentation of this file.
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.
4 
5 #ifndef V8_GLOBAL_HANDLES_H_
6 #define V8_GLOBAL_HANDLES_H_
7 
8 #include "include/v8.h"
9 #include "include/v8-profiler.h"
10 
11 #include "src/handles.h"
12 #include "src/list.h"
13 #include "src/utils.h"
14 
15 namespace v8 {
16 namespace internal {
17 
18 class HeapStats;
19 class ObjectVisitor;
20 
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 
27 // Data structures for tracking object groups and implicit references.
28 
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.
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];
42  }
43  ~ObjectGroup();
44 
47  size_t length;
48 };
49 
50 
53  : parent(parent), length(length) {
54  DCHECK(length > 0);
55  children = new Object**[length];
56  }
58 
61  size_t length;
62 };
63 
64 
65 // For internal bookkeeping.
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 
80 };
81 
82 
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 
97 };
98 
99 
101  public:
102  ~GlobalHandles();
103 
104  // Creates a new global handle that is alive until Destroy is called.
105  Handle<Object> Create(Object* value);
106 
107  // Copy a global handle
108  static Handle<Object> CopyGlobal(Object** location);
109 
110  // Destroy a global handle.
111  static void Destroy(Object** location);
112 
114 
115  // Make the global handle weak and set the callback parameter for the
116  // handle. When the garbage collector recognizes that only weak global
117  // handles point to an object the handles are cleared and the callback
118  // function is invoked (for each handle) with the handle and corresponding
119  // parameter as arguments. Note: cleared means set to Smi::FromInt(0). The
120  // reason is that Smi::FromInt(0) does not change during garage collection.
121  static void MakeWeak(Object** location,
122  void* parameter,
123  WeakCallback weak_callback);
124 
125  void RecordStats(HeapStats* stats);
126 
127  // Returns the current number of weak handles.
128  int NumberOfWeakHandles();
129 
130  // Returns the current number of weak handles to global objects.
131  // These handles are also included in NumberOfWeakHandles().
133 
134  // Returns the current number of handles to global objects.
135  int global_handles_count() const {
137  }
138 
139  // Clear the weakness of a global handle.
140  static void* ClearWeakness(Object** location);
141 
142  // Clear the weakness of a global handle.
143  static void MarkIndependent(Object** location);
144 
145  // Mark the reference to this object externaly unreachable.
146  static void MarkPartiallyDependent(Object** location);
147 
148  static bool IsIndependent(Object** location);
149 
150  // Tells whether global handle is near death.
151  static bool IsNearDeath(Object** location);
152 
153  // Tells whether global handle is weak.
154  static bool IsWeak(Object** location);
155 
156  // Process pending weak handles.
157  // Returns the number of freed nodes.
159 
160  // Iterates over all strong handles.
162 
163  // Iterates over all handles.
165 
166  // Iterates over all handles that have embedder-assigned class ID.
168 
169  // Iterates over all handles in the new space that have embedder-assigned
170  // class ID.
172 
173  // Iterates over all weak roots in heap.
175 
176  // Find all weak handles satisfying the callback predicate, mark
177  // them as pending.
179 
180  // NOTE: Three ...NewSpace... functions below are used during
181  // scavenge collections and iterate over sets of handles that are
182  // guaranteed to contain all handles holding new space objects (but
183  // may also include old space objects).
184 
185  // Iterates over strong and dependent handles. See the node above.
187 
188  // Finds weak independent or partially independent handles satisfying
189  // the callback predicate and marks them as pending. See the note above.
191 
192  // Iterates over weak independent or partially independent handles.
193  // See the note above.
195 
196  // Iterate over objects in object groups that have at least one object
197  // which requires visiting. The callback has to return true if objects
198  // can be skipped and false otherwise.
200 
201  // Add an object group.
202  // Should be only used in GC callback function before a collection.
203  // All groups are destroyed after a garbage collection.
204  void AddObjectGroup(Object*** handles,
205  size_t length,
206  v8::RetainedObjectInfo* info);
207 
208  // Associates handle with the object group represented by id.
209  // Should be only used in GC callback function before a collection.
210  // All groups are destroyed after a garbage collection.
212 
213  // Set RetainedObjectInfo for an object group. Should not be called more than
214  // once for a group. Should not be called for a group which contains no
215  // handles.
217 
218  // Adds an implicit reference from a group to an object. Should be only used
219  // in GC callback function before a collection. All implicit references are
220  // destroyed after a mark-compact collection.
221  void SetReferenceFromGroup(UniqueId id, Object** child);
222 
223  // Adds an implicit reference from a parent object to a child object. Should
224  // be only used in GC callback function before a collection. All implicit
225  // references are destroyed after a mark-compact collection.
226  void SetReference(HeapObject** parent, Object** child);
227 
230  return &object_groups_;
231  }
232 
235  return &implicit_ref_groups_;
236  }
237 
238  // Remove bags, this should only happen after GC.
239  void RemoveObjectGroups();
241 
242  // Tear down the global handle structure.
243  void TearDown();
244 
245  Isolate* isolate() { return isolate_; }
246 
247 #ifdef DEBUG
248  void PrintStats();
249  void Print();
250 #endif
251 
252  private:
253  explicit GlobalHandles(Isolate* isolate);
254 
255  // Migrates data from the internal representation (object_group_connections_,
256  // retainer_infos_ and implicit_ref_connections_) to the public and more
257  // efficient representation (object_groups_ and implicit_ref_groups_).
259 
260  // v8::internal::List is inefficient even for small number of elements, if we
261  // don't assign any initial capacity.
262  static const int kObjectGroupConnectionsCapacity = 20;
263 
264  // Internal node structures.
265  class Node;
266  class NodeBlock;
267  class NodeIterator;
268 
270 
271  // Field always containing the number of handles to global objects.
273 
274  // List of all allocated node blocks.
276 
277  // List of node blocks with used nodes.
279 
280  // Free list of nodes.
282 
283  // Contains all nodes holding new space objects. Note: when the list
284  // is accessed, some of the objects may have been promoted already.
286 
288 
289  // Object groups and implicit references, public and more efficient
290  // representation.
293 
294  // Object groups and implicit references, temporary representation while
295  // constructing the groups.
299 
300  friend class Isolate;
301 
303 };
304 
305 
307  public:
312 
314  };
315 
316  EternalHandles();
317  ~EternalHandles();
318 
319  int NumberOfHandles() { return size_; }
320 
321  // Create an EternalHandle, overwriting the index.
322  void Create(Isolate* isolate, Object* object, int* index);
323 
324  // Grab the handle for an existing EternalHandle.
325  inline Handle<Object> Get(int index) {
326  return Handle<Object>(GetLocation(index));
327  }
328 
329  // Grab the handle for an existing SingletonHandle.
331  DCHECK(Exists(singleton));
332  return Get(singleton_handles_[singleton]);
333  }
334 
335  // Checks whether a SingletonHandle has been assigned.
336  inline bool Exists(SingletonHandle singleton) {
337  return singleton_handles_[singleton] != kInvalidIndex;
338  }
339 
340  // Assign a SingletonHandle to an empty slot and returns the handle.
342  Object* object,
343  SingletonHandle singleton) {
344  Create(isolate, object, &singleton_handles_[singleton]);
345  return Get(singleton_handles_[singleton]);
346  }
347 
348  // Iterates over all handles.
349  void IterateAllRoots(ObjectVisitor* visitor);
350  // Iterates over all handles which might be in new space.
351  void IterateNewSpaceRoots(ObjectVisitor* visitor);
352  // Rebuilds new space list.
354 
355  private:
356  static const int kInvalidIndex = -1;
357  static const int kShift = 8;
358  static const int kSize = 1 << kShift;
359  static const int kMask = 0xff;
360 
361  // Gets the slot for an index
362  inline Object** GetLocation(int index) {
363  DCHECK(index >= 0 && index < size_);
364  return &blocks_[index >> kShift][index & kMask];
365  }
366 
367  int size_;
371 
373 };
374 
375 
376 } } // namespace v8::internal
377 
378 #endif // V8_GLOBAL_HANDLES_H_
Interface for providing information about embedder's objects held by global handles.
Definition: v8-profiler.h:545
General purpose unique identifier.
Definition: v8.h:144
bool Exists(SingletonHandle singleton)
Handle< Object > CreateSingleton(Isolate *isolate, Object *object, SingletonHandle singleton)
Handle< Object > Get(int index)
DISALLOW_COPY_AND_ASSIGN(EternalHandles)
static const int kInvalidIndex
void PostGarbageCollectionProcessing(Heap *heap)
void IterateAllRoots(ObjectVisitor *visitor)
void Create(Isolate *isolate, Object *object, int *index)
int singleton_handles_[NUMBER_OF_SINGLETON_HANDLES]
void IterateNewSpaceRoots(ObjectVisitor *visitor)
Object ** GetLocation(int index)
Handle< Object > GetSingleton(SingletonHandle singleton)
GlobalHandles(Isolate *isolate)
List< ImplicitRefGroup * > implicit_ref_groups_
WeakCallbackData< v8::Value, void >::Callback WeakCallback
void RecordStats(HeapStats *stats)
bool IterateObjectGroups(ObjectVisitor *v, WeakSlotCallbackWithHeap can_skip)
void IterateAllRootsInNewSpaceWithClassIds(ObjectVisitor *v)
void IterateAllRoots(ObjectVisitor *v)
static void MakeWeak(Object **location, void *parameter, WeakCallback weak_callback)
List< ObjectGroup * > object_groups_
void IterateNewSpaceWeakIndependentRoots(ObjectVisitor *v)
static void * ClearWeakness(Object **location)
static bool IsWeak(Object **location)
void SetRetainedObjectInfo(UniqueId id, RetainedObjectInfo *info)
static void MarkIndependent(Object **location)
static bool IsIndependent(Object **location)
DISALLOW_COPY_AND_ASSIGN(GlobalHandles)
List< ObjectGroup * > * object_groups()
void IterateNewSpaceStrongAndDependentRoots(ObjectVisitor *v)
void SetReference(HeapObject **parent, Object **child)
void AddObjectGroup(Object ***handles, size_t length, v8::RetainedObjectInfo *info)
int PostGarbageCollectionProcessing(GarbageCollector collector)
void IterateAllRootsWithClassIds(ObjectVisitor *v)
static void MarkPartiallyDependent(Object **location)
static bool IsNearDeath(Object **location)
void IdentifyNewSpaceWeakIndependentHandles(WeakSlotCallbackWithHeap f)
void IterateStrongRoots(ObjectVisitor *v)
void SetReferenceFromGroup(UniqueId id, Object **child)
void IdentifyWeakHandles(WeakSlotCallback f)
static Handle< Object > CopyGlobal(Object **location)
void SetObjectGroupId(Object **handle, UniqueId id)
List< ImplicitRefGroup * > * implicit_ref_groups()
static const int kObjectGroupConnectionsCapacity
List< ObjectGroupConnection > object_group_connections_
static void Destroy(Object **location)
List< ObjectGroupConnection > implicit_ref_connections_
Handle< Object > Create(Object *value)
void IterateWeakRoots(ObjectVisitor *v)
List< ObjectGroupRetainerInfo > retainer_infos_
enable harmony numeric enable harmony object literal extensions Optimize object Array DOM strings and string trace pretenuring decisions of HAllocate instructions Enables optimizations which favor memory size over execution speed maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining trace the tracking of allocation sites deoptimize every n garbage collections perform array bounds checks elimination analyze liveness of environment slots and zap dead values flushes the cache of optimized code for closures on every GC allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms do not emit check maps for constant values that have a leaf deoptimize the optimized code if the layout of the maps changes enable context specialization in TurboFan execution budget before interrupt is triggered max percentage of megamorphic generic ICs to allow optimization enable use of SAHF instruction if enable use of VFP3 instructions if available enable use of NEON instructions if enable use of SDIV and UDIV instructions if enable use of MLS instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of d16 d31 registers on ARM this requires VFP3 force all emitted branches to be in long enable alignment of csp to bytes on platforms which prefer the register to always be NULL
#define DCHECK(condition)
Definition: logging.h:205
bool(* WeakSlotCallback)(Object **pointer)
Definition: globals.h:349
bool(* WeakSlotCallbackWithHeap)(Heap *heap, Object **pointer)
Definition: globals.h:351
Handle< T > handle(T *t, Isolate *isolate)
Definition: handles.h:146
Debugger support for the V8 JavaScript engine.
Definition: accessors.cc:20
ImplicitRefGroup(HeapObject **parent, size_t length)
bool operator<(const ObjectGroupConnection &other) const
bool operator==(const ObjectGroupConnection &other) const
ObjectGroupConnection(UniqueId id, Object **object)
bool operator==(const ObjectGroupRetainerInfo &other) const
bool operator<(const ObjectGroupRetainerInfo &other) const
ObjectGroupRetainerInfo(UniqueId id, RetainedObjectInfo *info)
ObjectGroup(size_t length)
v8::RetainedObjectInfo * info