QtScript/JSC on Symbian: Enhanced memory allocator for Collector heap
[qt:qt.git] / src / 3rdparty / javascriptcore / JavaScriptCore / runtime / Collector.cpp
1 /*
2  *  Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
3  *  Copyright (C) 2007 Eric Seidel <eric@webkit.org>
4  *
5  *  This library is free software; you can redistribute it and/or
6  *  modify it under the terms of the GNU Lesser General Public
7  *  License as published by the Free Software Foundation; either
8  *  version 2 of the License, or (at your option) any later version.
9  *
10  *  This library is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *  Lesser General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Lesser General Public
16  *  License along with this library; if not, write to the Free Software
17  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  */
20
21 #include "config.h"
22 #include "Collector.h"
23
24 #include "ArgList.h"
25 #include "CallFrame.h"
26 #include "CodeBlock.h"
27 #include "CollectorHeapIterator.h"
28 #include "Interpreter.h"
29 #include "JSArray.h"
30 #include "JSGlobalObject.h"
31 #include "JSLock.h"
32 #include "JSONObject.h"
33 #include "JSString.h"
34 #include "JSValue.h"
35 #include "JSZombie.h"
36 #include "MarkStack.h"
37 #include "Nodes.h"
38 #include "Tracing.h"
39 #include <algorithm>
40 #include <limits.h>
41 #include <setjmp.h>
42 #include <stdlib.h>
43 #include <wtf/FastMalloc.h>
44 #include <wtf/HashCountedSet.h>
45 #include <wtf/UnusedParam.h>
46 #include <wtf/VMTags.h>
47
48 #if OS(DARWIN)
49
50 #include <mach/mach_init.h>
51 #include <mach/mach_port.h>
52 #include <mach/task.h>
53 #include <mach/thread_act.h>
54 #include <mach/vm_map.h>
55
56 #elif OS(WINDOWS)
57
58 #include <windows.h>
59 #include <malloc.h>
60
61 #elif OS(HAIKU)
62
63 #include <OS.h>
64
65 #elif OS(UNIX)
66
67 #include <stdlib.h>
68 #if !OS(HAIKU)
69 #include <sys/mman.h>
70 #endif
71 #include <unistd.h>
72
73 #if OS(SOLARIS)
74 #include <thread.h>
75 #else
76 #include <pthread.h>
77 #endif
78
79 #if HAVE(PTHREAD_NP_H)
80 #include <pthread_np.h>
81 #endif
82
83 #if OS(QNX)
84 #include <fcntl.h>
85 #include <sys/procfs.h>
86 #include <stdio.h>
87 #include <errno.h>
88 #endif
89
90 #endif
91
92 #define COLLECT_ON_EVERY_ALLOCATION 0
93
94 using std::max;
95
96 namespace JSC {
97
98 // tunable parameters
99
100 const size_t GROWTH_FACTOR = 2;
101 const size_t LOW_WATER_FACTOR = 4;
102 const size_t ALLOCATIONS_PER_COLLECTION = 3600;
103 // This value has to be a macro to be used in max() without introducing
104 // a PIC branch in Mach-O binaries, see <rdar://problem/5971391>.
105 #define MIN_ARRAY_SIZE (static_cast<size_t>(14))
106
107 #if ENABLE(JSC_MULTIPLE_THREADS)
108
109 #if OS(DARWIN)
110 typedef mach_port_t PlatformThread;
111 #elif OS(WINDOWS)
112 typedef HANDLE PlatformThread;
113 #endif
114
115 class Heap::Thread {
116 public:
117     Thread(pthread_t pthread, const PlatformThread& platThread, void* base) 
118         : posixThread(pthread)
119         , platformThread(platThread)
120         , stackBase(base)
121     {
122     }
123
124     Thread* next;
125     pthread_t posixThread;
126     PlatformThread platformThread;
127     void* stackBase;
128 };
129
130 #endif
131
132 Heap::Heap(JSGlobalData* globalData)
133     : m_markListSet(0)
134 #if ENABLE(JSC_MULTIPLE_THREADS)
135     , m_registeredThreads(0)
136     , m_currentThreadRegistrar(0)
137 #endif
138     , m_globalData(globalData)
139 #if OS(SYMBIAN)
140     , m_blockallocator(JSCCOLLECTOR_VIRTUALMEM_RESERVATION, BLOCK_SIZE)
141 #endif
142 {
143     ASSERT(globalData);
144     memset(&m_heap, 0, sizeof(CollectorHeap));
145     allocateBlock();
146 }
147
148 Heap::~Heap()
149 {
150     // The destroy function must already have been called, so assert this.
151     ASSERT(!m_globalData);
152 }
153
154 void Heap::destroy()
155 {
156     JSLock lock(SilenceAssertionsOnly);
157
158     if (!m_globalData)
159         return;
160
161     ASSERT(!m_globalData->dynamicGlobalObject);
162     ASSERT(!isBusy());
163     
164     // The global object is not GC protected at this point, so sweeping may delete it
165     // (and thus the global data) before other objects that may use the global data.
166     RefPtr<JSGlobalData> protect(m_globalData);
167
168     delete m_markListSet;
169     m_markListSet = 0;
170
171     freeBlocks();
172
173 #if ENABLE(JSC_MULTIPLE_THREADS)
174     if (m_currentThreadRegistrar) {
175         int error = pthread_key_delete(m_currentThreadRegistrar);
176         ASSERT_UNUSED(error, !error);
177     }
178
179     MutexLocker registeredThreadsLock(m_registeredThreadsMutex);
180     for (Heap::Thread* t = m_registeredThreads; t;) {
181         Heap::Thread* next = t->next;
182         delete t;
183         t = next;
184     }
185 #endif
186 #if OS(SYMBIAN)
187     m_blockallocator.destroy();
188 #endif
189     m_globalData = 0;
190 }
191
192 NEVER_INLINE CollectorBlock* Heap::allocateBlock()
193 {
194 #if OS(DARWIN)
195     vm_address_t address = 0;
196     vm_map(current_task(), &address, BLOCK_SIZE, BLOCK_OFFSET_MASK, VM_FLAGS_ANYWHERE | VM_TAG_FOR_COLLECTOR_MEMORY, MEMORY_OBJECT_NULL, 0, FALSE, VM_PROT_DEFAULT, VM_PROT_DEFAULT, VM_INHERIT_DEFAULT);
197 #elif OS(SYMBIAN)
198     void* address = m_blockallocator.alloc();  
199     if (!address)
200         CRASH();
201 #elif OS(WINCE)
202     void* address = VirtualAlloc(NULL, BLOCK_SIZE, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
203 #elif OS(WINDOWS)
204 #if COMPILER(MINGW) && !COMPILER(MINGW64)
205     void* address = __mingw_aligned_malloc(BLOCK_SIZE, BLOCK_SIZE);
206 #else
207     void* address = _aligned_malloc(BLOCK_SIZE, BLOCK_SIZE);
208 #endif
209     memset(address, 0, BLOCK_SIZE);
210 #elif HAVE(POSIX_MEMALIGN)
211     void* address;
212     posix_memalign(&address, BLOCK_SIZE, BLOCK_SIZE);
213 #else
214
215 #if ENABLE(JSC_MULTIPLE_THREADS)
216 #error Need to initialize pagesize safely.
217 #endif
218     static size_t pagesize = getpagesize();
219
220     size_t extra = 0;
221     if (BLOCK_SIZE > pagesize)
222         extra = BLOCK_SIZE - pagesize;
223
224     void* mmapResult = mmap(NULL, BLOCK_SIZE + extra, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
225     uintptr_t address = reinterpret_cast<uintptr_t>(mmapResult);
226
227     size_t adjust = 0;
228     if ((address & BLOCK_OFFSET_MASK) != 0)
229         adjust = BLOCK_SIZE - (address & BLOCK_OFFSET_MASK);
230
231     if (adjust > 0)
232         munmap(reinterpret_cast<char*>(address), adjust);
233
234     if (adjust < extra)
235         munmap(reinterpret_cast<char*>(address + adjust + BLOCK_SIZE), extra - adjust);
236
237     address += adjust;
238 #endif
239
240     // Initialize block.
241
242     CollectorBlock* block = reinterpret_cast<CollectorBlock*>(address);
243     block->heap = this;
244     clearMarkBits(block);
245
246     Structure* dummyMarkableCellStructure = m_globalData->dummyMarkableCellStructure.get();
247     for (size_t i = 0; i < HeapConstants::cellsPerBlock; ++i)
248         new (block->cells + i) JSCell(dummyMarkableCellStructure);
249     
250     // Add block to blocks vector.
251
252     size_t numBlocks = m_heap.numBlocks;
253     if (m_heap.usedBlocks == numBlocks) {
254         static const size_t maxNumBlocks = ULONG_MAX / sizeof(CollectorBlock*) / GROWTH_FACTOR;
255         if (numBlocks > maxNumBlocks)
256             CRASH();
257         numBlocks = max(MIN_ARRAY_SIZE, numBlocks * GROWTH_FACTOR);
258         m_heap.numBlocks = numBlocks;
259         m_heap.blocks = static_cast<CollectorBlock**>(fastRealloc(m_heap.blocks, numBlocks * sizeof(CollectorBlock*)));
260     }
261     m_heap.blocks[m_heap.usedBlocks++] = block;
262
263     return block;
264 }
265
266 NEVER_INLINE void Heap::freeBlock(size_t block)
267 {
268     m_heap.didShrink = true;
269
270     ObjectIterator it(m_heap, block);
271     ObjectIterator end(m_heap, block + 1);
272     for ( ; it != end; ++it)
273         (*it)->~JSCell();
274     freeBlockPtr(m_heap.blocks[block]);
275
276     // swap with the last block so we compact as we go
277     m_heap.blocks[block] = m_heap.blocks[m_heap.usedBlocks - 1];
278     m_heap.usedBlocks--;
279
280     if (m_heap.numBlocks > MIN_ARRAY_SIZE && m_heap.usedBlocks < m_heap.numBlocks / LOW_WATER_FACTOR) {
281         m_heap.numBlocks = m_heap.numBlocks / GROWTH_FACTOR; 
282         m_heap.blocks = static_cast<CollectorBlock**>(fastRealloc(m_heap.blocks, m_heap.numBlocks * sizeof(CollectorBlock*)));
283     }
284 }
285
286 NEVER_INLINE void Heap::freeBlockPtr(CollectorBlock* block)
287 {
288 #if OS(DARWIN)    
289     vm_deallocate(current_task(), reinterpret_cast<vm_address_t>(block), BLOCK_SIZE);
290 #elif OS(SYMBIAN)
291     m_blockallocator.free(reinterpret_cast<void*>(block));
292 #elif OS(WINCE)
293     VirtualFree(block, 0, MEM_RELEASE);
294 #elif OS(WINDOWS)
295 #if COMPILER(MINGW) && !COMPILER(MINGW64)
296     __mingw_aligned_free(block);
297 #else
298     _aligned_free(block);
299 #endif
300 #elif HAVE(POSIX_MEMALIGN)
301     free(block);
302 #else
303     munmap(reinterpret_cast<char*>(block), BLOCK_SIZE);
304 #endif
305 }
306
307 void Heap::freeBlocks()
308 {
309     ProtectCountSet protectedValuesCopy = m_protectedValues;
310
311     clearMarkBits();
312     ProtectCountSet::iterator protectedValuesEnd = protectedValuesCopy.end();
313     for (ProtectCountSet::iterator it = protectedValuesCopy.begin(); it != protectedValuesEnd; ++it)
314         markCell(it->first);
315
316     m_heap.nextCell = 0;
317     m_heap.nextBlock = 0;
318     DeadObjectIterator it(m_heap, m_heap.nextBlock, m_heap.nextCell);
319     DeadObjectIterator end(m_heap, m_heap.usedBlocks);
320     for ( ; it != end; ++it)
321         (*it)->~JSCell();
322
323     ASSERT(!protectedObjectCount());
324
325     protectedValuesEnd = protectedValuesCopy.end();
326     for (ProtectCountSet::iterator it = protectedValuesCopy.begin(); it != protectedValuesEnd; ++it)
327         it->first->~JSCell();
328
329     for (size_t block = 0; block < m_heap.usedBlocks; ++block)
330         freeBlockPtr(m_heap.blocks[block]);
331
332     fastFree(m_heap.blocks);
333
334     memset(&m_heap, 0, sizeof(CollectorHeap));
335 }
336
337 void Heap::recordExtraCost(size_t cost)
338 {
339     // Our frequency of garbage collection tries to balance memory use against speed
340     // by collecting based on the number of newly created values. However, for values
341     // that hold on to a great deal of memory that's not in the form of other JS values,
342     // that is not good enough - in some cases a lot of those objects can pile up and
343     // use crazy amounts of memory without a GC happening. So we track these extra
344     // memory costs. Only unusually large objects are noted, and we only keep track
345     // of this extra cost until the next GC. In garbage collected languages, most values
346     // are either very short lived temporaries, or have extremely long lifetimes. So
347     // if a large value survives one garbage collection, there is not much point to
348     // collecting more frequently as long as it stays alive.
349
350     if (m_heap.extraCost > maxExtraCost && m_heap.extraCost > m_heap.usedBlocks * BLOCK_SIZE / 2) {
351         // If the last iteration through the heap deallocated blocks, we need
352         // to clean up remaining garbage before marking. Otherwise, the conservative
353         // marking mechanism might follow a pointer to unmapped memory.
354         if (m_heap.didShrink)
355             sweep();
356         reset();
357     }
358     m_heap.extraCost += cost;
359 }
360
361 void* Heap::allocate(size_t s)
362 {
363     typedef HeapConstants::Block Block;
364     typedef HeapConstants::Cell Cell;
365     
366     ASSERT(JSLock::lockCount() > 0);
367     ASSERT(JSLock::currentThreadIsHoldingLock());
368     ASSERT_UNUSED(s, s <= HeapConstants::cellSize);
369
370     ASSERT(m_heap.operationInProgress == NoOperation);
371
372 #if COLLECT_ON_EVERY_ALLOCATION
373     collectAllGarbage();
374     ASSERT(m_heap.operationInProgress == NoOperation);
375 #endif
376
377 allocate:
378
379     // Fast case: find the next garbage cell and recycle it.
380
381     do {
382         ASSERT(m_heap.nextBlock < m_heap.usedBlocks);
383         Block* block = reinterpret_cast<Block*>(m_heap.blocks[m_heap.nextBlock]);
384         do {
385             ASSERT(m_heap.nextCell < HeapConstants::cellsPerBlock);
386             if (!block->marked.get(m_heap.nextCell)) { // Always false for the last cell in the block
387                 Cell* cell = block->cells + m_heap.nextCell;
388
389                 m_heap.operationInProgress = Allocation;
390                 JSCell* imp = reinterpret_cast<JSCell*>(cell);
391                 imp->~JSCell();
392                 m_heap.operationInProgress = NoOperation;
393
394                 ++m_heap.nextCell;
395                 return cell;
396             }
397         } while (++m_heap.nextCell != HeapConstants::cellsPerBlock);
398         m_heap.nextCell = 0;
399     } while (++m_heap.nextBlock != m_heap.usedBlocks);
400
401     // Slow case: reached the end of the heap. Mark live objects and start over.
402
403     reset();
404     goto allocate;
405 }
406
407 void Heap::resizeBlocks()
408 {
409     m_heap.didShrink = false;
410
411     size_t usedCellCount = markedCells();
412     size_t minCellCount = usedCellCount + max(ALLOCATIONS_PER_COLLECTION, usedCellCount);
413     size_t minBlockCount = (minCellCount + HeapConstants::cellsPerBlock - 1) / HeapConstants::cellsPerBlock;
414
415     size_t maxCellCount = 1.25f * minCellCount;
416     size_t maxBlockCount = (maxCellCount + HeapConstants::cellsPerBlock - 1) / HeapConstants::cellsPerBlock;
417
418     if (m_heap.usedBlocks < minBlockCount)
419         growBlocks(minBlockCount);
420     else if (m_heap.usedBlocks > maxBlockCount)
421         shrinkBlocks(maxBlockCount);
422 }
423
424 void Heap::growBlocks(size_t neededBlocks)
425 {
426     ASSERT(m_heap.usedBlocks < neededBlocks);
427     while (m_heap.usedBlocks < neededBlocks)
428         allocateBlock();
429 }
430
431 void Heap::shrinkBlocks(size_t neededBlocks)
432 {
433     ASSERT(m_heap.usedBlocks > neededBlocks);
434     
435     // Clear the always-on last bit, so isEmpty() isn't fooled by it.
436     for (size_t i = 0; i < m_heap.usedBlocks; ++i)
437         m_heap.blocks[i]->marked.clear(HeapConstants::cellsPerBlock - 1);
438
439     for (size_t i = 0; i != m_heap.usedBlocks && m_heap.usedBlocks != neededBlocks; ) {
440         if (m_heap.blocks[i]->marked.isEmpty()) {
441             freeBlock(i);
442         } else
443             ++i;
444     }
445
446     // Reset the always-on last bit.
447     for (size_t i = 0; i < m_heap.usedBlocks; ++i)
448         m_heap.blocks[i]->marked.set(HeapConstants::cellsPerBlock - 1);
449 }
450
451 #if OS(WINCE)
452 void* g_stackBase = 0;
453
454 inline bool isPageWritable(void* page)
455 {
456     MEMORY_BASIC_INFORMATION memoryInformation;
457     DWORD result = VirtualQuery(page, &memoryInformation, sizeof(memoryInformation));
458
459     // return false on error, including ptr outside memory
460     if (result != sizeof(memoryInformation))
461         return false;
462
463     DWORD protect = memoryInformation.Protect & ~(PAGE_GUARD | PAGE_NOCACHE);
464     return protect == PAGE_READWRITE
465         || protect == PAGE_WRITECOPY
466         || protect == PAGE_EXECUTE_READWRITE
467         || protect == PAGE_EXECUTE_WRITECOPY;
468 }
469
470 static void* getStackBase(void* previousFrame)
471 {
472     // find the address of this stack frame by taking the address of a local variable
473     bool isGrowingDownward;
474     void* thisFrame = (void*)(&isGrowingDownward);
475
476     isGrowingDownward = previousFrame < &thisFrame;
477     static DWORD pageSize = 0;
478     if (!pageSize) {
479         SYSTEM_INFO systemInfo;
480         GetSystemInfo(&systemInfo);
481         pageSize = systemInfo.dwPageSize;
482     }
483
484     // scan all of memory starting from this frame, and return the last writeable page found
485     register char* currentPage = (char*)((DWORD)thisFrame & ~(pageSize - 1));
486     if (isGrowingDownward) {
487         while (currentPage > 0) {
488             // check for underflow
489             if (currentPage >= (char*)pageSize)
490                 currentPage -= pageSize;
491             else
492                 currentPage = 0;
493             if (!isPageWritable(currentPage))
494                 return currentPage + pageSize;
495         }
496         return 0;
497     } else {
498         while (true) {
499             // guaranteed to complete because isPageWritable returns false at end of memory
500             currentPage += pageSize;
501             if (!isPageWritable(currentPage))
502                 return currentPage;
503         }
504     }
505 }
506 #endif
507
508 #if OS(HPUX)
509 struct hpux_get_stack_base_data
510 {
511     pthread_t thread;
512     _pthread_stack_info info;
513 };
514
515 static void *hpux_get_stack_base_internal(void *d)
516 {
517     hpux_get_stack_base_data *data = static_cast<hpux_get_stack_base_data *>(d);
518
519     // _pthread_stack_info_np requires the target thread to be suspended
520     // in order to get information about it
521     pthread_suspend(data->thread);
522
523     // _pthread_stack_info_np returns an errno code in case of failure
524     // or zero on success
525     if (_pthread_stack_info_np(data->thread, &data->info)) {
526         // failed
527         return 0;
528     }
529
530     pthread_continue(data->thread);
531     return data;
532 }
533
534 static void *hpux_get_stack_base()
535 {
536     hpux_get_stack_base_data data;
537     data.thread = pthread_self();
538
539     // We cannot get the stack information for the current thread
540     // So we start a new thread to get that information and return it to us
541     pthread_t other;
542     pthread_create(&other, 0, hpux_get_stack_base_internal, &data);
543
544     void *result;
545     pthread_join(other, &result);
546     if (result)
547        return data.info.stk_stack_base;
548     return 0;
549 }
550 #endif
551
552 #if OS(QNX)
553 static inline void *currentThreadStackBaseQNX()
554 {
555     static void* stackBase = 0;
556     static size_t stackSize = 0;
557     static pthread_t stackThread;
558     pthread_t thread = pthread_self();
559     if (stackBase == 0 || thread != stackThread) {
560         struct _debug_thread_info threadInfo;
561         memset(&threadInfo, 0, sizeof(threadInfo));
562         threadInfo.tid = pthread_self();
563         int fd = open("/proc/self", O_RDONLY);
564         if (fd == -1) {
565             LOG_ERROR("Unable to open /proc/self (errno: %d)", errno);
566             return 0;
567         }
568         devctl(fd, DCMD_PROC_TIDSTATUS, &threadInfo, sizeof(threadInfo), 0);
569         close(fd);
570         stackBase = reinterpret_cast<void*>(threadInfo.stkbase);
571         stackSize = threadInfo.stksize;
572         ASSERT(stackBase);
573         stackThread = thread;
574     }
575     return static_cast<char*>(stackBase) + stackSize;
576 }
577 #endif
578
579 static inline void* currentThreadStackBase()
580 {
581 #if OS(DARWIN)
582     pthread_t thread = pthread_self();
583     return pthread_get_stackaddr_np(thread);
584 #elif OS(WINDOWS) && CPU(X86) && COMPILER(MSVC)
585     // offset 0x18 from the FS segment register gives a pointer to
586     // the thread information block for the current thread
587     NT_TIB* pTib;
588     __asm {
589         MOV EAX, FS:[18h]
590         MOV pTib, EAX
591     }
592     return static_cast<void*>(pTib->StackBase);
593 #elif OS(WINDOWS) && CPU(X86_64) && (COMPILER(MSVC) || COMPILER(GCC))
594     // FIXME: why only for MSVC?
595     PNT_TIB64 pTib = reinterpret_cast<PNT_TIB64>(NtCurrentTeb());
596     return reinterpret_cast<void*>(pTib->StackBase);
597 #elif OS(WINDOWS) && CPU(X86) && COMPILER(GCC)
598     // offset 0x18 from the FS segment register gives a pointer to
599     // the thread information block for the current thread
600     NT_TIB* pTib;
601     asm ( "movl %%fs:0x18, %0\n"
602           : "=r" (pTib)
603         );
604     return static_cast<void*>(pTib->StackBase);
605 #elif OS(HPUX)
606     return hpux_get_stack_base();
607 #elif OS(QNX)
608     AtomicallyInitializedStatic(Mutex&, mutex = *new Mutex);
609     MutexLocker locker(mutex);
610     return currentThreadStackBaseQNX();
611 #elif OS(SOLARIS)
612     stack_t s;
613     thr_stksegment(&s);
614     return s.ss_sp;
615 #elif OS(AIX)
616     pthread_t thread = pthread_self();
617     struct __pthrdsinfo threadinfo;
618     char regbuf[256];
619     int regbufsize = sizeof regbuf;
620
621     if (pthread_getthrds_np(&thread, PTHRDSINFO_QUERY_ALL,
622                             &threadinfo, sizeof threadinfo,
623                             &regbuf, &regbufsize) == 0)
624         return threadinfo.__pi_stackaddr;
625
626     return 0;
627 #elif OS(OPENBSD)
628     pthread_t thread = pthread_self();
629     stack_t stack;
630     pthread_stackseg_np(thread, &stack);
631     return stack.ss_sp;
632 #elif OS(SYMBIAN)
633     TThreadStackInfo info;
634     RThread thread;
635     thread.StackInfo(info);
636     return (void*)info.iBase;
637 #elif OS(HAIKU)
638     thread_info threadInfo;
639     get_thread_info(find_thread(NULL), &threadInfo);
640     return threadInfo.stack_end;
641 #elif OS(UNIX)
642     AtomicallyInitializedStatic(Mutex&, mutex = *new Mutex);
643     MutexLocker locker(mutex);
644     static void* stackBase = 0;
645     static size_t stackSize = 0;
646     static pthread_t stackThread;
647     pthread_t thread = pthread_self();
648     if (stackBase == 0 || thread != stackThread) {
649         pthread_attr_t sattr;
650         pthread_attr_init(&sattr);
651 #if HAVE(PTHREAD_NP_H) || OS(NETBSD)
652         // e.g. on FreeBSD 5.4, neundorf@kde.org
653         pthread_attr_get_np(thread, &sattr);
654 #else
655         // FIXME: this function is non-portable; other POSIX systems may have different np alternatives
656         pthread_getattr_np(thread, &sattr);
657 #endif
658         int rc = pthread_attr_getstack(&sattr, &stackBase, &stackSize);
659         (void)rc; // FIXME: Deal with error code somehow? Seems fatal.
660         ASSERT(stackBase);
661         pthread_attr_destroy(&sattr);
662         stackThread = thread;
663     }
664     return static_cast<char*>(stackBase) + stackSize;
665 #elif OS(WINCE)
666     AtomicallyInitializedStatic(Mutex&, mutex = *new Mutex);
667     MutexLocker locker(mutex);
668     if (g_stackBase)
669         return g_stackBase;
670     else {
671         int dummy;
672         return getStackBase(&dummy);
673     }
674 #else
675 #error Need a way to get the stack base on this platform
676 #endif
677 }
678
679 #if ENABLE(JSC_MULTIPLE_THREADS)
680
681 static inline PlatformThread getCurrentPlatformThread()
682 {
683 #if OS(DARWIN)
684     return pthread_mach_thread_np(pthread_self());
685 #elif OS(WINDOWS)
686     return pthread_getw32threadhandle_np(pthread_self());
687 #endif
688 }
689
690 void Heap::makeUsableFromMultipleThreads()
691 {
692     if (m_currentThreadRegistrar)
693         return;
694
695     int error = pthread_key_create(&m_currentThreadRegistrar, unregisterThread);
696     if (error)
697         CRASH();
698 }
699
700 void Heap::registerThread()
701 {
702     ASSERT(!m_globalData->mainThreadOnly || isMainThread());
703
704     if (!m_currentThreadRegistrar || pthread_getspecific(m_currentThreadRegistrar))
705         return;
706
707     pthread_setspecific(m_currentThreadRegistrar, this);
708     Heap::Thread* thread = new Heap::Thread(pthread_self(), getCurrentPlatformThread(), currentThreadStackBase());
709
710     MutexLocker lock(m_registeredThreadsMutex);
711
712     thread->next = m_registeredThreads;
713     m_registeredThreads = thread;
714 }
715
716 void Heap::unregisterThread(void* p)
717 {
718     if (p)
719         static_cast<Heap*>(p)->unregisterThread();
720 }
721
722 void Heap::unregisterThread()
723 {
724     pthread_t currentPosixThread = pthread_self();
725
726     MutexLocker lock(m_registeredThreadsMutex);
727
728     if (pthread_equal(currentPosixThread, m_registeredThreads->posixThread)) {
729         Thread* t = m_registeredThreads;
730         m_registeredThreads = m_registeredThreads->next;
731         delete t;
732     } else {
733         Heap::Thread* last = m_registeredThreads;
734         Heap::Thread* t;
735         for (t = m_registeredThreads->next; t; t = t->next) {
736             if (pthread_equal(t->posixThread, currentPosixThread)) {
737                 last->next = t->next;
738                 break;
739             }
740             last = t;
741         }
742         ASSERT(t); // If t is NULL, we never found ourselves in the list.
743         delete t;
744     }
745 }
746
747 #else // ENABLE(JSC_MULTIPLE_THREADS)
748
749 void Heap::registerThread()
750 {
751 }
752
753 #endif
754
755 inline bool isPointerAligned(void* p)
756 {
757     return (((intptr_t)(p) & (sizeof(char*) - 1)) == 0);
758 }
759
760 // Cell size needs to be a power of two for isPossibleCell to be valid.
761 COMPILE_ASSERT(sizeof(CollectorCell) % 2 == 0, Collector_cell_size_is_power_of_two);
762
763 #if USE(JSVALUE32)
764 static bool isHalfCellAligned(void *p)
765 {
766     return (((intptr_t)(p) & (CELL_MASK >> 1)) == 0);
767 }
768
769 static inline bool isPossibleCell(void* p)
770 {
771     return isHalfCellAligned(p) && p;
772 }
773
774 #else
775
776 static inline bool isCellAligned(void *p)
777 {
778     return (((intptr_t)(p) & CELL_MASK) == 0);
779 }
780
781 static inline bool isPossibleCell(void* p)
782 {
783     return isCellAligned(p) && p;
784 }
785 #endif // USE(JSVALUE32)
786
787 void Heap::markConservatively(MarkStack& markStack, void* start, void* end)
788 {
789     if (start > end) {
790         void* tmp = start;
791         start = end;
792         end = tmp;
793     }
794
795     ASSERT((static_cast<char*>(end) - static_cast<char*>(start)) < 0x1000000);
796     ASSERT(isPointerAligned(start));
797     ASSERT(isPointerAligned(end));
798
799     char** p = static_cast<char**>(start);
800     char** e = static_cast<char**>(end);
801
802     CollectorBlock** blocks = m_heap.blocks;
803     while (p != e) {
804         char* x = *p++;
805         if (isPossibleCell(x)) {
806             size_t usedBlocks;
807             uintptr_t xAsBits = reinterpret_cast<uintptr_t>(x);
808             xAsBits &= CELL_ALIGN_MASK;
809
810             uintptr_t offset = xAsBits & BLOCK_OFFSET_MASK;
811             const size_t lastCellOffset = sizeof(CollectorCell) * (CELLS_PER_BLOCK - 1);
812             if (offset > lastCellOffset)
813                 continue;
814
815             CollectorBlock* blockAddr = reinterpret_cast<CollectorBlock*>(xAsBits - offset);
816             usedBlocks = m_heap.usedBlocks;
817             for (size_t block = 0; block < usedBlocks; block++) {
818                 if (blocks[block] != blockAddr)
819                     continue;
820                 markStack.append(reinterpret_cast<JSCell*>(xAsBits));
821                 markStack.drain();
822             }
823         }
824     }
825 }
826
827 void NEVER_INLINE Heap::markCurrentThreadConservativelyInternal(MarkStack& markStack)
828 {
829     void* dummy;
830     void* stackPointer = &dummy;
831     void* stackBase = currentThreadStackBase();
832     markConservatively(markStack, stackPointer, stackBase);
833 }
834
835 #if COMPILER(GCC)
836 #define REGISTER_BUFFER_ALIGNMENT __attribute__ ((aligned (sizeof(void*))))
837 #else
838 #define REGISTER_BUFFER_ALIGNMENT
839 #endif
840
841 void Heap::markCurrentThreadConservatively(MarkStack& markStack)
842 {
843     // setjmp forces volatile registers onto the stack
844     jmp_buf registers REGISTER_BUFFER_ALIGNMENT;
845 #if COMPILER(MSVC)
846 #pragma warning(push)
847 #pragma warning(disable: 4611)
848 #endif
849     setjmp(registers);
850 #if COMPILER(MSVC)
851 #pragma warning(pop)
852 #endif
853
854     markCurrentThreadConservativelyInternal(markStack);
855 }
856
857 #if ENABLE(JSC_MULTIPLE_THREADS)
858
859 static inline void suspendThread(const PlatformThread& platformThread)
860 {
861 #if OS(DARWIN)
862     thread_suspend(platformThread);
863 #elif OS(WINDOWS)
864     SuspendThread(platformThread);
865 #else
866 #error Need a way to suspend threads on this platform
867 #endif
868 }
869
870 static inline void resumeThread(const PlatformThread& platformThread)
871 {
872 #if OS(DARWIN)
873     thread_resume(platformThread);
874 #elif OS(WINDOWS)
875     ResumeThread(platformThread);
876 #else
877 #error Need a way to resume threads on this platform
878 #endif
879 }
880
881 typedef unsigned long usword_t; // word size, assumed to be either 32 or 64 bit
882
883 #if OS(DARWIN)
884
885 #if CPU(X86)
886 typedef i386_thread_state_t PlatformThreadRegisters;
887 #elif CPU(X86_64)
888 typedef x86_thread_state64_t PlatformThreadRegisters;
889 #elif CPU(PPC)
890 typedef ppc_thread_state_t PlatformThreadRegisters;
891 #elif CPU(PPC64)
892 typedef ppc_thread_state64_t PlatformThreadRegisters;
893 #elif CPU(ARM)
894 typedef arm_thread_state_t PlatformThreadRegisters;
895 #else
896 #error Unknown Architecture
897 #endif
898
899 #elif OS(WINDOWS) && CPU(X86)
900 typedef CONTEXT PlatformThreadRegisters;
901 #else
902 #error Need a thread register struct for this platform
903 #endif
904
905 static size_t getPlatformThreadRegisters(const PlatformThread& platformThread, PlatformThreadRegisters& regs)
906 {
907 #if OS(DARWIN)
908
909 #if CPU(X86)
910     unsigned user_count = sizeof(regs)/sizeof(int);
911     thread_state_flavor_t flavor = i386_THREAD_STATE;
912 #elif CPU(X86_64)
913     unsigned user_count = x86_THREAD_STATE64_COUNT;
914     thread_state_flavor_t flavor = x86_THREAD_STATE64;
915 #elif CPU(PPC) 
916     unsigned user_count = PPC_THREAD_STATE_COUNT;
917     thread_state_flavor_t flavor = PPC_THREAD_STATE;
918 #elif CPU(PPC64)
919     unsigned user_count = PPC_THREAD_STATE64_COUNT;
920     thread_state_flavor_t flavor = PPC_THREAD_STATE64;
921 #elif CPU(ARM)
922     unsigned user_count = ARM_THREAD_STATE_COUNT;
923     thread_state_flavor_t flavor = ARM_THREAD_STATE;
924 #else
925 #error Unknown Architecture
926 #endif
927
928     kern_return_t result = thread_get_state(platformThread, flavor, (thread_state_t)&regs, &user_count);
929     if (result != KERN_SUCCESS) {
930         WTFReportFatalError(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, 
931                             "JavaScript garbage collection failed because thread_get_state returned an error (%d). This is probably the result of running inside Rosetta, which is not supported.", result);
932         CRASH();
933     }
934     return user_count * sizeof(usword_t);
935 // end OS(DARWIN)
936
937 #elif OS(WINDOWS) && CPU(X86)
938     regs.ContextFlags = CONTEXT_INTEGER | CONTEXT_CONTROL | CONTEXT_SEGMENTS;
939     GetThreadContext(platformThread, &regs);
940     return sizeof(CONTEXT);
941 #else
942 #error Need a way to get thread registers on this platform
943 #endif
944 }
945
946 static inline void* otherThreadStackPointer(const PlatformThreadRegisters& regs)
947 {
948 #if OS(DARWIN)
949
950 #if __DARWIN_UNIX03
951
952 #if CPU(X86)
953     return reinterpret_cast<void*>(regs.__esp);
954 #elif CPU(X86_64)
955     return reinterpret_cast<void*>(regs.__rsp);
956 #elif CPU(PPC) || CPU(PPC64)
957     return reinterpret_cast<void*>(regs.__r1);
958 #elif CPU(ARM)
959     return reinterpret_cast<void*>(regs.__sp);
960 #else
961 #error Unknown Architecture
962 #endif
963
964 #else // !__DARWIN_UNIX03
965
966 #if CPU(X86)
967     return reinterpret_cast<void*>(regs.esp);
968 #elif CPU(X86_64)
969     return reinterpret_cast<void*>(regs.rsp);
970 #elif CPU(PPC) || CPU(PPC64)
971     return reinterpret_cast<void*>(regs.r1);
972 #else
973 #error Unknown Architecture
974 #endif
975
976 #endif // __DARWIN_UNIX03
977
978 // end OS(DARWIN)
979 #elif CPU(X86) && OS(WINDOWS)
980     return reinterpret_cast<void*>((uintptr_t) regs.Esp);
981 #else
982 #error Need a way to get the stack pointer for another thread on this platform
983 #endif
984 }
985
986 void Heap::markOtherThreadConservatively(MarkStack& markStack, Thread* thread)
987 {
988     suspendThread(thread->platformThread);
989
990     PlatformThreadRegisters regs;
991     size_t regSize = getPlatformThreadRegisters(thread->platformThread, regs);
992
993     // mark the thread's registers
994     markConservatively(markStack, static_cast<void*>(&regs), static_cast<void*>(reinterpret_cast<char*>(&regs) + regSize));
995
996     void* stackPointer = otherThreadStackPointer(regs);
997     markConservatively(markStack, stackPointer, thread->stackBase);
998
999     resumeThread(thread->platformThread);
1000 }
1001
1002 #endif
1003
1004 void Heap::markStackObjectsConservatively(MarkStack& markStack)
1005 {
1006     markCurrentThreadConservatively(markStack);
1007
1008 #if ENABLE(JSC_MULTIPLE_THREADS)
1009
1010     if (m_currentThreadRegistrar) {
1011
1012         MutexLocker lock(m_registeredThreadsMutex);
1013
1014 #ifndef NDEBUG
1015         // Forbid malloc during the mark phase. Marking a thread suspends it, so 
1016         // a malloc inside markChildren() would risk a deadlock with a thread that had been 
1017         // suspended while holding the malloc lock.
1018         fastMallocForbid();
1019 #endif
1020         // It is safe to access the registeredThreads list, because we earlier asserted that locks are being held,
1021         // and since this is a shared heap, they are real locks.
1022         for (Thread* thread = m_registeredThreads; thread; thread = thread->next) {
1023             if (!pthread_equal(thread->posixThread, pthread_self()))
1024                 markOtherThreadConservatively(markStack, thread);
1025         }
1026 #ifndef NDEBUG
1027         fastMallocAllow();
1028 #endif
1029     }
1030 #endif
1031 }
1032
1033 void Heap::protect(JSValue k)
1034 {
1035     ASSERT(k);
1036     ASSERT(JSLock::currentThreadIsHoldingLock() || !m_globalData->isSharedInstance);
1037
1038     if (!k.isCell())
1039         return;
1040
1041     m_protectedValues.add(k.asCell());
1042 }
1043
1044 void Heap::unprotect(JSValue k)
1045 {
1046     ASSERT(k);
1047     ASSERT(JSLock::currentThreadIsHoldingLock() || !m_globalData->isSharedInstance);
1048
1049     if (!k.isCell())
1050         return;
1051
1052     m_protectedValues.remove(k.asCell());
1053 }
1054
1055 void Heap::markProtectedObjects(MarkStack& markStack)
1056 {
1057     ProtectCountSet::iterator end = m_protectedValues.end();
1058     for (ProtectCountSet::iterator it = m_protectedValues.begin(); it != end; ++it) {
1059         markStack.append(it->first);
1060         markStack.drain();
1061     }
1062 }
1063
1064 void Heap::clearMarkBits()
1065 {
1066     for (size_t i = 0; i < m_heap.usedBlocks; ++i)
1067         clearMarkBits(m_heap.blocks[i]);
1068 }
1069
1070 void Heap::clearMarkBits(CollectorBlock* block)
1071 {
1072     // allocate assumes that the last cell in every block is marked.
1073     block->marked.clearAll();
1074     block->marked.set(HeapConstants::cellsPerBlock - 1);
1075 }
1076
1077 size_t Heap::markedCells(size_t startBlock, size_t startCell) const
1078 {
1079     ASSERT(startBlock <= m_heap.usedBlocks);
1080     ASSERT(startCell < HeapConstants::cellsPerBlock);
1081
1082     if (startBlock >= m_heap.usedBlocks)
1083         return 0;
1084
1085     size_t result = 0;
1086     result += m_heap.blocks[startBlock]->marked.count(startCell);
1087     for (size_t i = startBlock + 1; i < m_heap.usedBlocks; ++i)
1088         result += m_heap.blocks[i]->marked.count();
1089
1090     return result;
1091 }
1092
1093 void Heap::sweep()
1094 {
1095     ASSERT(m_heap.operationInProgress == NoOperation);
1096     if (m_heap.operationInProgress != NoOperation)
1097         CRASH();
1098     m_heap.operationInProgress = Collection;
1099     
1100 #if !ENABLE(JSC_ZOMBIES)
1101     Structure* dummyMarkableCellStructure = m_globalData->dummyMarkableCellStructure.get();
1102 #endif
1103
1104     DeadObjectIterator it(m_heap, m_heap.nextBlock, m_heap.nextCell);
1105     DeadObjectIterator end(m_heap, m_heap.usedBlocks);
1106     for ( ; it != end; ++it) {
1107         JSCell* cell = *it;
1108 #if ENABLE(JSC_ZOMBIES)
1109         if (!cell->isZombie()) {
1110             const ClassInfo* info = cell->classInfo();
1111             cell->~JSCell();
1112             new (cell) JSZombie(info, JSZombie::leakedZombieStructure());
1113             Heap::markCell(cell);
1114         }
1115 #else
1116         cell->~JSCell();
1117         // Callers of sweep assume it's safe to mark any cell in the heap.
1118         new (cell) JSCell(dummyMarkableCellStructure);
1119 #endif
1120     }
1121
1122     m_heap.operationInProgress = NoOperation;
1123 }
1124
1125 void Heap::markRoots()
1126 {
1127 #ifndef NDEBUG
1128     if (m_globalData->isSharedInstance) {
1129         ASSERT(JSLock::lockCount() > 0);
1130         ASSERT(JSLock::currentThreadIsHoldingLock());
1131     }
1132 #endif
1133
1134     ASSERT(m_heap.operationInProgress == NoOperation);
1135     if (m_heap.operationInProgress != NoOperation)
1136         CRASH();
1137
1138     m_heap.operationInProgress = Collection;
1139
1140     MarkStack& markStack = m_globalData->markStack;
1141
1142     // Reset mark bits.
1143     clearMarkBits();
1144
1145     // Mark stack roots.
1146     markStackObjectsConservatively(markStack);
1147     m_globalData->interpreter->registerFile().markCallFrames(markStack, this);
1148
1149     // Mark explicitly registered roots.
1150     markProtectedObjects(markStack);
1151
1152 #if QT_BUILD_SCRIPT_LIB
1153     if (m_globalData->clientData)
1154         m_globalData->clientData->mark(markStack);
1155 #endif
1156
1157     // Mark misc. other roots.
1158     if (m_markListSet && m_markListSet->size())
1159         MarkedArgumentBuffer::markLists(markStack, *m_markListSet);
1160     if (m_globalData->exception)
1161         markStack.append(m_globalData->exception);
1162     m_globalData->smallStrings.markChildren(markStack);
1163     if (m_globalData->functionCodeBlockBeingReparsed)
1164         m_globalData->functionCodeBlockBeingReparsed->markAggregate(markStack);
1165     if (m_globalData->firstStringifierToMark)
1166         JSONObject::markStringifiers(markStack, m_globalData->firstStringifierToMark);
1167
1168     markStack.drain();
1169     markStack.compact();
1170
1171     m_heap.operationInProgress = NoOperation;
1172 }
1173
1174 size_t Heap::objectCount() const
1175 {
1176     return m_heap.nextBlock * HeapConstants::cellsPerBlock // allocated full blocks
1177            + m_heap.nextCell // allocated cells in current block
1178            + markedCells(m_heap.nextBlock, m_heap.nextCell) // marked cells in remainder of m_heap
1179            - m_heap.usedBlocks; // 1 cell per block is a dummy sentinel
1180 }
1181
1182 void Heap::addToStatistics(Heap::Statistics& statistics) const
1183 {
1184     statistics.size += m_heap.usedBlocks * BLOCK_SIZE;
1185     statistics.free += m_heap.usedBlocks * BLOCK_SIZE - (objectCount() * HeapConstants::cellSize);
1186 }
1187
1188 Heap::Statistics Heap::statistics() const
1189 {
1190     Statistics statistics = { 0, 0 };
1191     addToStatistics(statistics);
1192     return statistics;
1193 }
1194
1195 size_t Heap::globalObjectCount()
1196 {
1197     size_t count = 0;
1198     if (JSGlobalObject* head = m_globalData->head) {
1199         JSGlobalObject* o = head;
1200         do {
1201             ++count;
1202             o = o->next();
1203         } while (o != head);
1204     }
1205     return count;
1206 }
1207
1208 size_t Heap::protectedGlobalObjectCount()
1209 {
1210     size_t count = 0;
1211     if (JSGlobalObject* head = m_globalData->head) {
1212         JSGlobalObject* o = head;
1213         do {
1214             if (m_protectedValues.contains(o))
1215                 ++count;
1216             o = o->next();
1217         } while (o != head);
1218     }
1219
1220     return count;
1221 }
1222
1223 size_t Heap::protectedObjectCount()
1224 {
1225     return m_protectedValues.size();
1226 }
1227
1228 static const char* typeName(JSCell* cell)
1229 {
1230     if (cell->isString())
1231         return "string";
1232 #if USE(JSVALUE32)
1233     if (cell->isNumber())
1234         return "number";
1235 #endif
1236     if (cell->isGetterSetter())
1237         return "gettersetter";
1238     if (cell->isAPIValueWrapper())
1239         return "value wrapper";
1240     if (cell->isPropertyNameIterator())
1241         return "for-in iterator";
1242     ASSERT(cell->isObject());
1243     const ClassInfo* info = cell->classInfo();
1244     return info ? info->className : "Object";
1245 }
1246
1247 HashCountedSet<const char*>* Heap::protectedObjectTypeCounts()
1248 {
1249     HashCountedSet<const char*>* counts = new HashCountedSet<const char*>;
1250
1251     ProtectCountSet::iterator end = m_protectedValues.end();
1252     for (ProtectCountSet::iterator it = m_protectedValues.begin(); it != end; ++it)
1253         counts->add(typeName(it->first));
1254
1255     return counts;
1256 }
1257
1258 bool Heap::isBusy()
1259 {
1260     return m_heap.operationInProgress != NoOperation;
1261 }
1262
1263 void Heap::reset()
1264 {
1265     JAVASCRIPTCORE_GC_BEGIN();
1266
1267     markRoots();
1268
1269     JAVASCRIPTCORE_GC_MARKED();
1270
1271     m_heap.nextCell = 0;
1272     m_heap.nextBlock = 0;
1273     m_heap.nextNumber = 0;
1274     m_heap.extraCost = 0;
1275 #if ENABLE(JSC_ZOMBIES)
1276     sweep();
1277 #endif
1278     resizeBlocks();
1279
1280     JAVASCRIPTCORE_GC_END();
1281 }
1282
1283 void Heap::collectAllGarbage()
1284 {
1285     JAVASCRIPTCORE_GC_BEGIN();
1286
1287     // If the last iteration through the heap deallocated blocks, we need
1288     // to clean up remaining garbage before marking. Otherwise, the conservative
1289     // marking mechanism might follow a pointer to unmapped memory.
1290     if (m_heap.didShrink)
1291         sweep();
1292
1293     markRoots();
1294
1295     JAVASCRIPTCORE_GC_MARKED();
1296
1297     m_heap.nextCell = 0;
1298     m_heap.nextBlock = 0;
1299     m_heap.nextNumber = 0;
1300     m_heap.extraCost = 0;
1301     sweep();
1302     resizeBlocks();
1303
1304     JAVASCRIPTCORE_GC_END();
1305 }
1306
1307 LiveObjectIterator Heap::primaryHeapBegin()
1308 {
1309     return LiveObjectIterator(m_heap, 0);
1310 }
1311
1312 LiveObjectIterator Heap::primaryHeapEnd()
1313 {
1314     return LiveObjectIterator(m_heap, m_heap.usedBlocks);
1315 }
1316
1317 } // namespace JSC