2 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3 * Copyright (C) 2001 Peter Kelly (pmk@post.com)
4 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include <wtf/HashCountedSet.h>
28 #include <wtf/HashSet.h>
29 #include <wtf/Noncopyable.h>
30 #include <wtf/OwnPtr.h>
31 #include <wtf/StdLibExtras.h>
32 #include <wtf/Threading.h>
34 #if ENABLE(JSC_MULTIPLE_THREADS)
39 #include <wtf/symbian/BlockAllocatorSymbian.h>
42 #define ASSERT_CLASS_FITS_IN_CELL(class) COMPILE_ASSERT(sizeof(class) <= CELL_SIZE, class_fits_in_cell)
50 class MarkedArgumentBuffer;
53 enum OperationInProgress { NoOperation, Allocation, Collection };
55 class LiveObjectIterator;
57 struct CollectorHeap {
60 CollectorBlock** blocks;
70 OperationInProgress operationInProgress;
73 class Heap : public Noncopyable {
79 void* allocateNumber(size_t);
80 void* allocate(size_t);
82 bool isBusy(); // true if an allocation or collection is in progress
83 void collectAllGarbage();
85 static const size_t minExtraCost = 256;
86 static const size_t maxExtraCost = 1024 * 1024;
88 void reportExtraMemoryCost(size_t cost);
90 size_t objectCount() const;
95 Statistics statistics() const;
97 void protect(JSValue);
98 void unprotect(JSValue);
100 static Heap* heap(JSValue); // 0 for immediate values
101 static Heap* heap(JSCell*);
103 size_t globalObjectCount();
104 size_t protectedObjectCount();
105 size_t protectedGlobalObjectCount();
106 HashCountedSet<const char*>* protectedObjectTypeCounts();
108 void registerThread(); // Only needs to be called by clients that can use the same heap from multiple threads.
110 static bool isCellMarked(const JSCell*);
111 static void markCell(JSCell*);
113 void markConservatively(MarkStack&, void* start, void* end);
115 HashSet<MarkedArgumentBuffer*>& markListSet() { if (!m_markListSet) m_markListSet = new HashSet<MarkedArgumentBuffer*>; return *m_markListSet; }
117 JSGlobalData* globalData() const { return m_globalData; }
118 static bool isNumber(JSCell*);
120 LiveObjectIterator primaryHeapBegin();
121 LiveObjectIterator primaryHeapEnd();
126 static CollectorBlock* cellBlock(const JSCell*);
127 static size_t cellOffset(const JSCell*);
129 friend class JSGlobalData;
133 NEVER_INLINE CollectorBlock* allocateBlock();
134 NEVER_INLINE void freeBlock(size_t);
135 NEVER_INLINE void freeBlockPtr(CollectorBlock*);
138 void growBlocks(size_t neededBlocks);
139 void shrinkBlocks(size_t neededBlocks);
140 void clearMarkBits();
141 void clearMarkBits(CollectorBlock*);
142 size_t markedCells(size_t startBlock = 0, size_t startCell = 0) const;
144 void recordExtraCost(size_t);
146 void addToStatistics(Statistics&) const;
149 void markProtectedObjects(MarkStack&);
150 void markCurrentThreadConservatively(MarkStack&);
151 void markCurrentThreadConservativelyInternal(MarkStack&);
152 void markOtherThreadConservatively(MarkStack&, Thread*);
153 void markStackObjectsConservatively(MarkStack&);
155 typedef HashCountedSet<JSCell*> ProtectCountSet;
157 CollectorHeap m_heap;
159 ProtectCountSet m_protectedValues;
161 HashSet<MarkedArgumentBuffer*>* m_markListSet;
163 #if ENABLE(JSC_MULTIPLE_THREADS)
164 void makeUsableFromMultipleThreads();
166 static void unregisterThread(void*);
167 void unregisterThread();
169 Mutex m_registeredThreadsMutex;
170 Thread* m_registeredThreads;
171 pthread_key_t m_currentThreadRegistrar;
175 // Allocates collector blocks with correct alignment
176 WTF::AlignedBlockAllocator m_blockallocator;
179 JSGlobalData* m_globalData;
182 // tunable parameters
183 template<size_t bytesPerWord> struct CellSize;
185 // cell size needs to be a power of two for certain optimizations in collector.cpp
187 template<> struct CellSize<sizeof(uint32_t)> { static const size_t m_value = 32; };
189 template<> struct CellSize<sizeof(uint32_t)> { static const size_t m_value = 64; };
191 template<> struct CellSize<sizeof(uint64_t)> { static const size_t m_value = 64; };
193 #if OS(WINCE) || OS(SYMBIAN)
194 const size_t BLOCK_SIZE = 64 * 1024; // 64k
196 const size_t BLOCK_SIZE = 64 * 4096; // 256k
200 const size_t BLOCK_OFFSET_MASK = BLOCK_SIZE - 1;
201 const size_t BLOCK_MASK = ~BLOCK_OFFSET_MASK;
202 const size_t MINIMUM_CELL_SIZE = CellSize<sizeof(void*)>::m_value;
203 const size_t CELL_ARRAY_LENGTH = (MINIMUM_CELL_SIZE / sizeof(double)) + (MINIMUM_CELL_SIZE % sizeof(double) != 0 ? sizeof(double) : 0);
204 const size_t CELL_SIZE = CELL_ARRAY_LENGTH * sizeof(double);
205 const size_t SMALL_CELL_SIZE = CELL_SIZE / 2;
206 const size_t CELL_MASK = CELL_SIZE - 1;
207 const size_t CELL_ALIGN_MASK = ~CELL_MASK;
208 const size_t CELLS_PER_BLOCK = (BLOCK_SIZE - sizeof(Heap*)) * 8 * CELL_SIZE / (8 * CELL_SIZE + 1) / CELL_SIZE; // one bitmap byte can represent 8 cells.
210 const size_t BITMAP_SIZE = (CELLS_PER_BLOCK + 7) / 8;
211 const size_t BITMAP_WORDS = (BITMAP_SIZE + 3) / sizeof(uint32_t);
213 struct CollectorBitmap {
214 uint32_t bits[BITMAP_WORDS];
215 bool get(size_t n) const { return !!(bits[n >> 5] & (1 << (n & 0x1F))); }
216 void set(size_t n) { bits[n >> 5] |= (1 << (n & 0x1F)); }
217 void clear(size_t n) { bits[n >> 5] &= ~(1 << (n & 0x1F)); }
218 void clearAll() { memset(bits, 0, sizeof(bits)); }
219 size_t count(size_t startCell = 0)
222 for ( ; (startCell & 0x1F) != 0; ++startCell) {
226 for (size_t i = startCell >> 5; i < BITMAP_WORDS; ++i)
227 result += WTF::bitCount(bits[i]);
230 size_t isEmpty() // Much more efficient than testing count() == 0.
232 for (size_t i = 0; i < BITMAP_WORDS; ++i)
239 struct CollectorCell {
240 double memory[CELL_ARRAY_LENGTH];
243 class CollectorBlock {
245 CollectorCell cells[CELLS_PER_BLOCK];
246 CollectorBitmap marked;
250 struct HeapConstants {
251 static const size_t cellSize = CELL_SIZE;
252 static const size_t cellsPerBlock = CELLS_PER_BLOCK;
253 typedef CollectorCell Cell;
254 typedef CollectorBlock Block;
257 inline CollectorBlock* Heap::cellBlock(const JSCell* cell)
259 return reinterpret_cast<CollectorBlock*>(reinterpret_cast<uintptr_t>(cell) & BLOCK_MASK);
262 inline size_t Heap::cellOffset(const JSCell* cell)
264 return (reinterpret_cast<uintptr_t>(cell) & BLOCK_OFFSET_MASK) / CELL_SIZE;
267 inline bool Heap::isCellMarked(const JSCell* cell)
269 return cellBlock(cell)->marked.get(cellOffset(cell));
272 inline void Heap::markCell(JSCell* cell)
274 cellBlock(cell)->marked.set(cellOffset(cell));
277 inline void Heap::reportExtraMemoryCost(size_t cost)
279 if (cost > minExtraCost)
280 recordExtraCost(cost);
283 inline void* Heap::allocateNumber(size_t s)
285 if (void* result = m_heap.nextNumber) {
286 m_heap.nextNumber = 0;
290 void* result = allocate(s);
291 m_heap.nextNumber = static_cast<char*>(result) + (CELL_SIZE / 2);
297 #endif /* Collector_h */