blob: feb95bf2a3dfe7d73ecd4d2e1e401d07c22e3e90 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2007-2008 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 "list-inl.h"
32
33namespace v8 {
34namespace internal {
35
36// Structure for tracking global handles.
37// A single list keeps all the allocated global handles.
38// Destroyed handles stay in the list but is added to the free list.
39// At GC the destroyed global handles are removed from the free list
40// and deallocated.
41
42// Callback function on handling weak global handles.
43// typedef bool (*WeakSlotCallback)(Object** pointer);
44
45// An object group is treated like a single JS object: if one of object in
46// the group is alive, all objects in the same group are considered alive.
47// An object group is used to simulate object relationship in a DOM tree.
48class ObjectGroup : public Malloced {
49 public:
50 ObjectGroup() : objects_(4) {}
51 explicit ObjectGroup(size_t capacity) : objects_(capacity) {}
52
53 List<Object**> objects_;
54};
55
56
Steve Block3ce2e202009-11-05 08:53:23 +000057typedef void (*WeakReferenceGuest)(Object* object, void* parameter);
58
Steve Blocka7e24c12009-10-30 11:49:00 +000059class GlobalHandles : public AllStatic {
60 public:
61 // Creates a new global handle that is alive until Destroy is called.
62 static Handle<Object> Create(Object* value);
63
64 // Destroy a global handle.
65 static void Destroy(Object** location);
66
67 // Make the global handle weak and set the callback parameter for the
68 // handle. When the garbage collector recognizes that only weak global
69 // handles point to an object the handles are cleared and the callback
70 // function is invoked (for each handle) with the handle and corresponding
71 // parameter as arguments. Note: cleared means set to Smi::FromInt(0). The
72 // reason is that Smi::FromInt(0) does not change during garage collection.
73 static void MakeWeak(Object** location,
74 void* parameter,
75 WeakReferenceCallback callback);
76
77 // Returns the current number of weak handles.
78 static int NumberOfWeakHandles() { return number_of_weak_handles_; }
79
80 // Returns the current number of weak handles to global objects.
81 // These handles are also included in NumberOfWeakHandles().
82 static int NumberOfGlobalObjectWeakHandles() {
83 return number_of_global_object_weak_handles_;
84 }
85
86 // Clear the weakness of a global handle.
87 static void ClearWeakness(Object** location);
88
89 // Tells whether global handle is near death.
90 static bool IsNearDeath(Object** location);
91
92 // Tells whether global handle is weak.
93 static bool IsWeak(Object** location);
94
95 // Process pending weak handles.
96 static void PostGarbageCollectionProcessing();
97
98 // Iterates over all handles.
99 static void IterateRoots(ObjectVisitor* v);
100
101 // Iterates over all weak roots in heap.
102 static void IterateWeakRoots(ObjectVisitor* v);
103
Steve Block3ce2e202009-11-05 08:53:23 +0000104 // Iterates over weak roots that are bound to a given callback.
105 static void IterateWeakRoots(WeakReferenceGuest f,
106 WeakReferenceCallback callback);
107
Steve Blocka7e24c12009-10-30 11:49:00 +0000108 // Find all weak handles satisfying the callback predicate, mark
109 // them as pending.
110 static void IdentifyWeakHandles(WeakSlotCallback f);
111
112 // Add an object group.
113 // Should only used in GC callback function before a collection.
114 // All groups are destroyed after a mark-compact collection.
115 static void AddGroup(Object*** handles, size_t length);
116
117 // Returns the object groups.
118 static List<ObjectGroup*>* ObjectGroups();
119
120 // Remove bags, this should only happen after GC.
121 static void RemoveObjectGroups();
122
123 // Tear down the global handle structure.
124 static void TearDown();
125
126#ifdef DEBUG
127 static void PrintStats();
128 static void Print();
129#endif
130 private:
131 // Internal node structure, one for each global handle.
132 class Node;
133
134 // Field always containing the number of weak and near-death handles.
135 static int number_of_weak_handles_;
136
137 // Field always containing the number of weak and near-death handles
138 // to global objects. These objects are also included in
139 // number_of_weak_handles_.
140 static int number_of_global_object_weak_handles_;
141
142 // Global handles are kept in a single linked list pointed to by head_.
143 static Node* head_;
144 static Node* head() { return head_; }
145 static void set_head(Node* value) { head_ = value; }
146
147 // Free list for DESTROYED global handles not yet deallocated.
148 static Node* first_free_;
149 static Node* first_free() { return first_free_; }
150 static void set_first_free(Node* value) { first_free_ = value; }
151};
152
153
154} } // namespace v8::internal
155
156#endif // V8_GLOBAL_HANDLES_H_