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