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