webengine/osswebengine/JavaScriptCore/kjs/collector.h
changeset 0 dd21522fd290
child 92 e1bea15f9a39
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 // -*- c-basic-offset: 2 -*-
       
     2 /*
       
     3  *  This file is part of the KDE libraries
       
     4  *  Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
       
     5  *  Copyright (C) 2001 Peter Kelly (pmk@post.com)
       
     6  *  Copyright (C) 2003, 2004, 2005, 2006, 2007 Apple Inc. All rights reserved.
       
     7  *
       
     8  *  This library is free software; you can redistribute it and/or
       
     9  *  modify it under the terms of the GNU Lesser General Public
       
    10  *  License as published by the Free Software Foundation; either
       
    11  *  version 2 of the License, or (at your option) any later version.
       
    12  *
       
    13  *  This library is distributed in the hope that it will be useful,
       
    14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    16  *  Lesser General Public License for more details.
       
    17  *
       
    18  *  You should have received a copy of the GNU Lesser General Public
       
    19  *  License along with this library; if not, write to the Free Software
       
    20  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
       
    21  *
       
    22  */
       
    23 
       
    24 #ifndef KJSCOLLECTOR_H_
       
    25 #define KJSCOLLECTOR_H_
       
    26 
       
    27 #include <string.h>
       
    28 #include <wtf/HashCountedSet.h>
       
    29 #include <wtf/Vector.h>
       
    30 
       
    31 #if PLATFORM(SYMBIAN)
       
    32 #include <MemoryManager.h>
       
    33 #endif
       
    34 
       
    35 #define KJS_MEM_LIMIT 500000
       
    36 
       
    37 namespace KJS {
       
    38 
       
    39   class JSCell;
       
    40   class JSValue;
       
    41   class CollectorBlock;
       
    42 
       
    43   class Collector {
       
    44   public:
       
    45     static void* allocate(size_t s);
       
    46     IMPORT static bool collect();
       
    47     static bool isBusy(); // true if an allocation or collection is in progress
       
    48 
       
    49     static const size_t minExtraCostSize = 256;
       
    50 
       
    51     static void reportExtraMemoryCost(size_t cost);
       
    52 
       
    53     static size_t size();
       
    54 #if PLATFORM(SYMBIAN)
       
    55     static bool isOutOfMemory() { return memoryFull || MemoryManager::Status() != ENoOOM; }
       
    56 #else
       
    57     static bool isOutOfMemory() { return memoryFull; }
       
    58 #endif
       
    59 
       
    60 #if PLATFORM(SYMBIAN)
       
    61     static unsigned int CallStackGrowthThresh;
       
    62     static Vector<JSCell *> recursivelyOrphanedMarkTops;
       
    63 #endif // PLATFORM(SYMBIAN)
       
    64     
       
    65     IMPORT static void protect(JSValue*);
       
    66     IMPORT static void unprotect(JSValue*);
       
    67     
       
    68     IMPORT static void collectOnMainThreadOnly(JSValue*);
       
    69 
       
    70     static size_t numInterpreters();
       
    71     static size_t numProtectedObjects();
       
    72     static HashCountedSet<const char*>* rootObjectTypeCounts();
       
    73 
       
    74     class Thread;
       
    75     static void registerThread();
       
    76     
       
    77     static void registerAsMainThread();
       
    78 
       
    79     static bool isCellMarked(const JSCell*);
       
    80     static void markCell(JSCell*);
       
    81 
       
    82   private:
       
    83     IMPORT static const CollectorBlock* cellBlock(const JSCell*);
       
    84     IMPORT static CollectorBlock* cellBlock(JSCell*);
       
    85     IMPORT static size_t cellOffset(const JSCell*);
       
    86 
       
    87     Collector();
       
    88 
       
    89     static void recordExtraCost(size_t);
       
    90     static void markProtectedObjects();
       
    91     static void markMainThreadOnlyObjects();
       
    92     static void markCurrentThreadConservatively();
       
    93     static void markOtherThreadConservatively(Thread*);
       
    94     static void markStackObjectsConservatively();
       
    95     static void markStackObjectsConservatively(void* start, void* end);
       
    96 
       
    97     static void markRecursivelyOrphanedCells();
       
    98     
       
    99     static size_t mainThreadOnlyObjectCount;
       
   100     static bool memoryFull;
       
   101   };
       
   102 
       
   103   // tunable parameters
       
   104   template<size_t bytesPerWord> struct CellSize;
       
   105 
       
   106   // cell size needs to be a power of two for certain optimizations in collector.cpp
       
   107   template<> struct CellSize<sizeof(uint32_t)> { static const size_t m_value = 32; }; // 32-bit
       
   108   template<> struct CellSize<sizeof(uint64_t)> { static const size_t m_value = 64; }; // 64-bit
       
   109   const size_t BLOCK_SIZE = 16 * 4096; // 64k
       
   110   
       
   111   // derived constants
       
   112   const size_t BLOCK_OFFSET_MASK = BLOCK_SIZE - 1;
       
   113   const size_t BLOCK_MASK = ~BLOCK_OFFSET_MASK;
       
   114   const size_t MINIMUM_CELL_SIZE = CellSize<sizeof(void*)>::m_value;
       
   115   const size_t CELL_ARRAY_LENGTH = (MINIMUM_CELL_SIZE / sizeof(double)) + (MINIMUM_CELL_SIZE % sizeof(double) != 0 ? sizeof(double) : 0);
       
   116   const size_t CELL_SIZE = CELL_ARRAY_LENGTH * sizeof(double);
       
   117   const size_t CELL_MASK = CELL_SIZE - 1;
       
   118   const size_t CELLS_PER_BLOCK = (BLOCK_SIZE * 8 - sizeof(uint32_t) * 8 - sizeof(void *) * 8 - 2 * (7 + 3 * 8)) / (CELL_SIZE * 8 + 2);
       
   119   const size_t BITMAP_SIZE = (CELLS_PER_BLOCK + 7) / 8;
       
   120   const size_t BITMAP_WORDS = (BITMAP_SIZE + 3) / sizeof(uint32_t);
       
   121   
       
   122   struct CollectorBitmap {
       
   123     uint32_t bits[BITMAP_WORDS];
       
   124     bool get(size_t n) const { return !!(bits[n >> 5] & (1 << (n & 0x1F))); } 
       
   125     void set(size_t n) { bits[n >> 5] |= (1 << (n & 0x1F)); } 
       
   126     void clear(size_t n) { bits[n >> 5] &= ~(1 << (n & 0x1F)); } 
       
   127     void clearAll() { memset(bits, 0, sizeof(bits)); }
       
   128   };
       
   129   
       
   130   struct CollectorCell {
       
   131     union {
       
   132       double memory[CELL_ARRAY_LENGTH];
       
   133       struct {
       
   134         void* zeroIfFree;
       
   135         ptrdiff_t next;
       
   136       } freeCell;
       
   137     } u;
       
   138   };
       
   139 
       
   140   class CollectorBlock {
       
   141   public:
       
   142     CollectorCell cells[CELLS_PER_BLOCK];
       
   143     uint32_t usedCells;
       
   144     CollectorCell* freeList;
       
   145     CollectorBitmap marked;
       
   146     CollectorBitmap collectOnMainThreadOnly;
       
   147   };
       
   148 
       
   149 #if PLATFORM(SYMBIAN)
       
   150   // fixme need to figure out a way to allocate 64KB-aligned blocks in symbian,
       
   151   // current implementation is very slow :((
       
   152 #else
       
   153   inline const CollectorBlock* Collector::cellBlock(const JSCell* cell)
       
   154   {
       
   155     return reinterpret_cast<const CollectorBlock*>(reinterpret_cast<uintptr_t>(cell) & BLOCK_MASK);
       
   156   }
       
   157 
       
   158   inline CollectorBlock* Collector::cellBlock(JSCell* cell)
       
   159   {
       
   160     return const_cast<CollectorBlock*>(cellBlock(const_cast<const JSCell*>(cell)));
       
   161   }
       
   162 
       
   163   inline size_t Collector::cellOffset(const JSCell* cell)
       
   164   {
       
   165     return (reinterpret_cast<uintptr_t>(cell) & BLOCK_OFFSET_MASK) / CELL_SIZE;
       
   166   }
       
   167 #endif
       
   168 
       
   169   inline bool Collector::isCellMarked(const JSCell* cell)
       
   170   {
       
   171     return cellBlock(cell)->marked.get(cellOffset(cell));
       
   172   }
       
   173 
       
   174   inline void Collector::markCell(JSCell* cell)
       
   175   {
       
   176     cellBlock(cell)->marked.set(cellOffset(cell));
       
   177   }
       
   178 
       
   179   inline void Collector::reportExtraMemoryCost(size_t cost)
       
   180   { 
       
   181 #if !PLATFORM(SYMBIAN)
       
   182     if (cost > minExtraCostSize) 
       
   183       recordExtraCost(cost / (CELL_SIZE * 2)); 
       
   184 #endif
       
   185   }
       
   186 
       
   187 } // namespace KJS
       
   188 
       
   189 #endif /* KJSCOLLECTOR_H_ */