1 // 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 33 namespace v8 { 34 namespace 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. 48 class ObjectGroup : public Malloced { 49 public: ObjectGroup()50 ObjectGroup() : objects_(4) {} ObjectGroup(size_t capacity)51 explicit ObjectGroup(size_t capacity) : objects_(capacity) {} 52 53 List<Object**> objects_; 54 }; 55 56 57 class 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. NumberOfWeakHandles()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(). NumberOfGlobalObjectWeakHandles()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 // Find all weak handles satisfying the callback predicate, mark 103 // them as pending. 104 static void IdentifyWeakHandles(WeakSlotCallback f); 105 106 // Add an object group. 107 // Should only used in GC callback function before a collection. 108 // All groups are destroyed after a mark-compact collection. 109 static void AddGroup(Object*** handles, size_t length); 110 111 // Returns the object groups. 112 static List<ObjectGroup*>* ObjectGroups(); 113 114 // Remove bags, this should only happen after GC. 115 static void RemoveObjectGroups(); 116 117 // Tear down the global handle structure. 118 static void TearDown(); 119 120 #ifdef DEBUG 121 static void PrintStats(); 122 static void Print(); 123 #endif 124 private: 125 // Internal node structure, one for each global handle. 126 class Node; 127 128 // Field always containing the number of weak and near-death handles. 129 static int number_of_weak_handles_; 130 131 // Field always containing the number of weak and near-death handles 132 // to global objects. These objects are also included in 133 // number_of_weak_handles_. 134 static int number_of_global_object_weak_handles_; 135 136 // Global handles are kept in a single linked list pointed to by head_. 137 static Node* head_; head()138 static Node* head() { return head_; } set_head(Node * value)139 static void set_head(Node* value) { head_ = value; } 140 141 // Free list for DESTROYED global handles not yet deallocated. 142 static Node* first_free_; first_free()143 static Node* first_free() { return first_free_; } set_first_free(Node * value)144 static void set_first_free(Node* value) { first_free_ = value; } 145 }; 146 147 148 } } // namespace v8::internal 149 150 #endif // V8_GLOBAL_HANDLES_H_ 151