Home | History | Annotate | Download | only in src
      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 "../include/v8-profiler.h"
     32 
     33 #include "list-inl.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_;
     67   v8::RetainedObjectInfo* info_;
     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.
     81 class ImplicitRefGroup {
     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 
     99   HeapObject** parent_;
    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 
    113 class GlobalHandles {
    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 
    135   // Returns the current number of weak handles.
    136   int NumberOfWeakHandles() { return number_of_weak_handles_; }
    137 
    138   void RecordStats(HeapStats* stats);
    139 
    140   // Returns the current number of weak handles to global objects.
    141   // These handles are also included in NumberOfWeakHandles().
    142   int NumberOfGlobalObjectWeakHandles() {
    143     return number_of_global_object_weak_handles_;
    144   }
    145 
    146   // Clear the weakness of a global handle.
    147   void ClearWeakness(Object** location);
    148 
    149   // Tells whether global handle is near death.
    150   static bool IsNearDeath(Object** location);
    151 
    152   // Tells whether global handle is weak.
    153   static bool IsWeak(Object** location);
    154 
    155   // Process pending weak handles.
    156   // Returns true if next major GC is likely to collect more garbage.
    157   bool PostGarbageCollectionProcessing();
    158 
    159   // Iterates over all strong handles.
    160   void IterateStrongRoots(ObjectVisitor* v);
    161 
    162   // Iterates over all handles.
    163   void IterateAllRoots(ObjectVisitor* v);
    164 
    165   // Iterates over all handles that have embedder-assigned class ID.
    166   void IterateAllRootsWithClassIds(ObjectVisitor* v);
    167 
    168   // Iterates over all weak roots in heap.
    169   void IterateWeakRoots(ObjectVisitor* v);
    170 
    171   // Iterates over weak roots that are bound to a given callback.
    172   void IterateWeakRoots(WeakReferenceGuest f,
    173                         WeakReferenceCallback callback);
    174 
    175   // Find all weak handles satisfying the callback predicate, mark
    176   // them as pending.
    177   void IdentifyWeakHandles(WeakSlotCallback f);
    178 
    179   // Add an object group.
    180   // Should be only used in GC callback function before a collection.
    181   // All groups are destroyed after a mark-compact collection.
    182   void AddObjectGroup(Object*** handles,
    183                       size_t length,
    184                       v8::RetainedObjectInfo* info);
    185 
    186   // Add an implicit references' group.
    187   // Should be only used in GC callback function before a collection.
    188   // All groups are destroyed after a mark-compact collection.
    189   void AddImplicitReferences(HeapObject** parent,
    190                              Object*** children,
    191                              size_t length);
    192 
    193   // Returns the object groups.
    194   List<ObjectGroup*>* object_groups() { return &object_groups_; }
    195 
    196   // Returns the implicit references' groups.
    197   List<ImplicitRefGroup*>* implicit_ref_groups() {
    198     return &implicit_ref_groups_;
    199   }
    200 
    201   // Remove bags, this should only happen after GC.
    202   void RemoveObjectGroups();
    203   void RemoveImplicitRefGroups();
    204 
    205   // Tear down the global handle structure.
    206   void TearDown();
    207 
    208   Isolate* isolate() { return isolate_; }
    209 
    210 #ifdef DEBUG
    211   void PrintStats();
    212   void Print();
    213 #endif
    214   class Pool;
    215  private:
    216   explicit GlobalHandles(Isolate* isolate);
    217 
    218   // Internal node structure, one for each global handle.
    219   class Node;
    220 
    221   Isolate* isolate_;
    222 
    223   // Field always containing the number of weak and near-death handles.
    224   int number_of_weak_handles_;
    225 
    226   // Field always containing the number of weak and near-death handles
    227   // to global objects.  These objects are also included in
    228   // number_of_weak_handles_.
    229   int number_of_global_object_weak_handles_;
    230 
    231   // Global handles are kept in a single linked list pointed to by head_.
    232   Node* head_;
    233   Node* head() { return head_; }
    234   void set_head(Node* value) { head_ = value; }
    235 
    236   // Free list for DESTROYED global handles not yet deallocated.
    237   Node* first_free_;
    238   Node* first_free() { return first_free_; }
    239   void set_first_free(Node* value) { first_free_ = value; }
    240 
    241   // List of deallocated nodes.
    242   // Deallocated nodes form a prefix of all the nodes and
    243   // |first_deallocated| points to last deallocated node before
    244   // |head|.  Those deallocated nodes are additionally linked
    245   // by |next_free|:
    246   //                                    1st deallocated  head
    247   //                                           |          |
    248   //                                           V          V
    249   //    node          node        ...         node       node
    250   //      .next      -> .next ->                .next ->
    251   //   <- .next_free <- .next_free           <- .next_free
    252   Node* first_deallocated_;
    253   Node* first_deallocated() { return first_deallocated_; }
    254   void set_first_deallocated(Node* value) {
    255     first_deallocated_ = value;
    256   }
    257 
    258   Pool* pool_;
    259   int post_gc_processing_count_;
    260   List<ObjectGroup*> object_groups_;
    261   List<ImplicitRefGroup*> implicit_ref_groups_;
    262 
    263   friend class Isolate;
    264 
    265   DISALLOW_COPY_AND_ASSIGN(GlobalHandles);
    266 };
    267 
    268 
    269 } }  // namespace v8::internal
    270 
    271 #endif  // V8_GLOBAL_HANDLES_H_
    272