v8  3.14.5(node0.10.28)
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-profiler.h"
32 
33 #include "list.h"
34 
35 namespace v8 {
36 namespace internal {
37 
38 // Structure for tracking global handles.
39 // A single list keeps all the allocated global handles.
40 // Destroyed handles stay in the list but is added to the free list.
41 // At GC the destroyed global handles are removed from the free list
42 // and deallocated.
43 
44 // An object group is treated like a single JS object: if one of object in
45 // the group is alive, all objects in the same group are considered alive.
46 // An object group is used to simulate object relationship in a DOM tree.
47 class ObjectGroup {
48  public:
49  static ObjectGroup* New(Object*** handles,
50  size_t length,
51  v8::RetainedObjectInfo* info) {
52  ASSERT(length > 0);
53  ObjectGroup* group = reinterpret_cast<ObjectGroup*>(
54  malloc(OFFSET_OF(ObjectGroup, objects_[length])));
55  group->length_ = length;
56  group->info_ = info;
57  CopyWords(group->objects_, handles, static_cast<int>(length));
58  return group;
59  }
60 
61  void Dispose() {
62  if (info_ != NULL) info_->Dispose();
63  free(this);
64  }
65 
66  size_t length_;
68  Object** objects_[1]; // Variable sized array.
69 
70  private:
71  void* operator new(size_t size);
72  void operator delete(void* p);
73  ~ObjectGroup();
74  DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectGroup);
75 };
76 
77 
78 // An implicit references group consists of two parts: a parent object and
79 // a list of children objects. If the parent is alive, all the children
80 // are alive too.
82  public:
83  static ImplicitRefGroup* New(HeapObject** parent,
84  Object*** children,
85  size_t length) {
86  ASSERT(length > 0);
87  ImplicitRefGroup* group = reinterpret_cast<ImplicitRefGroup*>(
88  malloc(OFFSET_OF(ImplicitRefGroup, children_[length])));
89  group->parent_ = parent;
90  group->length_ = length;
91  CopyWords(group->children_, children, static_cast<int>(length));
92  return group;
93  }
94 
95  void Dispose() {
96  free(this);
97  }
98 
100  size_t length_;
101  Object** children_[1]; // Variable sized array.
102 
103  private:
104  void* operator new(size_t size);
105  void operator delete(void* p);
106  ~ImplicitRefGroup();
107  DISALLOW_IMPLICIT_CONSTRUCTORS(ImplicitRefGroup);
108 };
109 
110 
111 typedef void (*WeakReferenceGuest)(Object* object, void* parameter);
112 
114  public:
115  ~GlobalHandles();
116 
117  // Creates a new global handle that is alive until Destroy is called.
118  Handle<Object> Create(Object* value);
119 
120  // Destroy a global handle.
121  void Destroy(Object** location);
122 
123  // Make the global handle weak and set the callback parameter for the
124  // handle. When the garbage collector recognizes that only weak global
125  // handles point to an object the handles are cleared and the callback
126  // function is invoked (for each handle) with the handle and corresponding
127  // parameter as arguments. Note: cleared means set to Smi::FromInt(0). The
128  // reason is that Smi::FromInt(0) does not change during garage collection.
129  void MakeWeak(Object** location,
130  void* parameter,
131  WeakReferenceCallback callback);
132 
133  static void SetWrapperClassId(Object** location, uint16_t class_id);
134  static uint16_t GetWrapperClassId(Object** location);
135 
136  // Returns the current number of weak handles.
137  int NumberOfWeakHandles() { return number_of_weak_handles_; }
138 
139  void RecordStats(HeapStats* stats);
140 
141  // Returns the current number of weak handles to global objects.
142  // These handles are also included in NumberOfWeakHandles().
144  return number_of_global_object_weak_handles_;
145  }
146 
147  // Returns the current number of handles to global objects.
149  return number_of_global_handles_;
150  }
151 
152  // Clear the weakness of a global handle.
153  void ClearWeakness(Object** location);
154 
155  // Clear the weakness of a global handle.
156  void MarkIndependent(Object** location);
157 
158  static bool IsIndependent(Object** location);
159 
160  // Tells whether global handle is near death.
161  static bool IsNearDeath(Object** location);
162 
163  // Tells whether global handle is weak.
164  static bool IsWeak(Object** location);
165 
166  // Process pending weak handles.
167  // Returns true if next major GC is likely to collect more garbage.
169 
170  // Iterates over all strong handles.
171  void IterateStrongRoots(ObjectVisitor* v);
172 
173  // Iterates over all handles.
174  void IterateAllRoots(ObjectVisitor* v);
175 
176  // Iterates over all handles that have embedder-assigned class ID.
177  void IterateAllRootsWithClassIds(ObjectVisitor* v);
178 
179  // Iterates over all weak roots in heap.
180  void IterateWeakRoots(ObjectVisitor* v);
181 
182  // Iterates over weak roots that are bound to a given callback.
184  WeakReferenceCallback callback);
185 
186  // Find all weak handles satisfying the callback predicate, mark
187  // them as pending.
189 
190  // NOTE: Three ...NewSpace... functions below are used during
191  // scavenge collections and iterate over sets of handles that are
192  // guaranteed to contain all handles holding new space objects (but
193  // may also include old space objects).
194 
195  // Iterates over strong and dependent handles. See the node above.
196  void IterateNewSpaceStrongAndDependentRoots(ObjectVisitor* v);
197 
198  // Finds weak independent handles satisfying the callback predicate
199  // and marks them as pending. See the note above.
201 
202  // Iterates over weak independent handles. See the note above.
203  void IterateNewSpaceWeakIndependentRoots(ObjectVisitor* v);
204 
205  // Add an object group.
206  // Should be only used in GC callback function before a collection.
207  // All groups are destroyed after a mark-compact collection.
208  void AddObjectGroup(Object*** handles,
209  size_t length,
210  v8::RetainedObjectInfo* info);
211 
212  // Add an implicit references' group.
213  // Should be only used in GC callback function before a collection.
214  // All groups are destroyed after a mark-compact collection.
215  void AddImplicitReferences(HeapObject** parent,
216  Object*** children,
217  size_t length);
218 
219  // Returns the object groups.
220  List<ObjectGroup*>* object_groups() { return &object_groups_; }
221 
222  // Returns the implicit references' groups.
224  return &implicit_ref_groups_;
225  }
226 
227  // Remove bags, this should only happen after GC.
228  void RemoveObjectGroups();
230 
231  // Tear down the global handle structure.
232  void TearDown();
233 
234  Isolate* isolate() { return isolate_; }
235 
236 #ifdef DEBUG
237  void PrintStats();
238  void Print();
239 #endif
240 
241  private:
242  explicit GlobalHandles(Isolate* isolate);
243 
244  // Internal node structures.
245  class Node;
246  class NodeBlock;
247  class NodeIterator;
248 
249  Isolate* isolate_;
250 
251  // Field always containing the number of weak and near-death handles.
252  int number_of_weak_handles_;
253 
254  // Field always containing the number of weak and near-death handles
255  // to global objects. These objects are also included in
256  // number_of_weak_handles_.
257  int number_of_global_object_weak_handles_;
258 
259  // Field always containing the number of handles to global objects.
260  int number_of_global_handles_;
261 
262  // List of all allocated node blocks.
263  NodeBlock* first_block_;
264 
265  // List of node blocks with used nodes.
266  NodeBlock* first_used_block_;
267 
268  // Free list of nodes.
269  Node* first_free_;
270 
271  // Contains all nodes holding new space objects. Note: when the list
272  // is accessed, some of the objects may have been promoted already.
273  List<Node*> new_space_nodes_;
274 
275  int post_gc_processing_count_;
276 
277  List<ObjectGroup*> object_groups_;
278  List<ImplicitRefGroup*> implicit_ref_groups_;
279 
280  friend class Isolate;
281 
283 };
284 
285 
286 } } // namespace v8::internal
287 
288 #endif // V8_GLOBAL_HANDLES_H_
bool(* WeakSlotCallbackWithHeap)(Heap *heap, Object **pointer)
Definition: v8globals.h:167
List< ImplicitRefGroup * > * implicit_ref_groups()
void Destroy(Object **location)
static ObjectGroup * New(Object ***handles, size_t length, v8::RetainedObjectInfo *info)
void IdentifyNewSpaceWeakIndependentHandles(WeakSlotCallbackWithHeap f)
void AddImplicitReferences(HeapObject **parent, Object ***children, size_t length)
static bool IsNearDeath(Object **location)
void IterateWeakRoots(ObjectVisitor *v)
#define ASSERT(condition)
Definition: checks.h:270
void ClearWeakness(Object **location)
v8::Handle< v8::Value > Print(const v8::Arguments &args)
unsigned short uint16_t
Definition: unicode.cc:46
void IterateStrongRoots(ObjectVisitor *v)
void AddObjectGroup(Object ***handles, size_t length, v8::RetainedObjectInfo *info)
void CopyWords(T *dst, T *src, int num_words)
Definition: v8utils.h:127
bool PostGarbageCollectionProcessing(GarbageCollector collector)
#define OFFSET_OF(type, field)
Definition: globals.h:273
Handle< Object > Create(Object *value)
static uint16_t GetWrapperClassId(Object **location)
static bool IsIndependent(Object **location)
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: globals.h:307
void IterateAllRoots(ObjectVisitor *v)
static void SetWrapperClassId(Object **location, uint16_t class_id)
void(* WeakReferenceCallback)(Persistent< Value > object, void *parameter)
Definition: v8.h:138
void IterateAllRootsWithClassIds(ObjectVisitor *v)
void IterateNewSpaceStrongAndDependentRoots(ObjectVisitor *v)
List< ObjectGroup * > * object_groups()
virtual void Dispose()=0
void IdentifyWeakHandles(WeakSlotCallback f)
void RecordStats(HeapStats *stats)
void(* WeakReferenceGuest)(Object *object, void *parameter)
static ImplicitRefGroup * New(HeapObject **parent, Object ***children, size_t length)
void MarkIndependent(Object **location)
static bool IsWeak(Object **location)
void IterateNewSpaceWeakIndependentRoots(ObjectVisitor *v)
activate correct semantics for inheriting readonliness enable harmony semantics for typeof enable harmony enable harmony proxies enable all harmony harmony_scoping harmony_proxies harmony_scoping tracks arrays with only smi values automatically unbox arrays of doubles use crankshaft use hydrogen range analysis use hydrogen global value numbering use function inlining maximum number of AST nodes considered for a single inlining loop invariant code motion print statistics for hydrogen trace generated IR for specified phases trace register allocator trace range analysis trace representation types environment for every instruction put a break point before deoptimizing polymorphic inlining perform array bounds checks elimination use dead code elimination trace on stack replacement optimize closures cache optimized code for closures functions with arguments object loop weight for representation inference allow uint32 values on optimize frames if they are used only in safe operations track parallel recompilation enable all profiler experiments number of stack frames inspected by the profiler call recompile stub directly when self optimizing trigger profiler ticks based on counting instead of timing weight back edges by jump distance for interrupt triggering percentage of ICs that must have type info to allow optimization watch_ic_patching retry_self_opt interrupt_at_exit extra verbose compilation tracing generate extra emit comments in code disassembly enable use of SSE3 instructions if available enable use of CMOV instruction if available enable use of SAHF instruction if enable use of VFP3 instructions if available this implies enabling ARMv7 and VFP2 enable use of VFP2 instructions if available enable use of SDIV and UDIV instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of MIPS FPU instructions if NULL
Definition: flags.cc:301
void MakeWeak(Object **location, void *parameter, WeakReferenceCallback callback)
bool(* WeakSlotCallback)(Object **pointer)
Definition: v8globals.h:165
v8::RetainedObjectInfo * info_