CopiedSpaceInlines.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (C) 2011 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef CopiedSpaceInlines_h
  26. #define CopiedSpaceInlines_h
  27. #include "CopiedBlock.h"
  28. #include "CopiedSpace.h"
  29. #include "Heap.h"
  30. #include "HeapBlock.h"
  31. #include "VM.h"
  32. #include <wtf/CheckedBoolean.h>
  33. namespace JSC {
  34. inline bool CopiedSpace::contains(CopiedBlock* block)
  35. {
  36. return !m_blockFilter.ruleOut(reinterpret_cast<Bits>(block)) && m_blockSet.contains(block);
  37. }
  38. inline bool CopiedSpace::contains(void* ptr, CopiedBlock*& result)
  39. {
  40. CopiedBlock* block = blockFor(ptr);
  41. if (contains(block)) {
  42. result = block;
  43. return true;
  44. }
  45. result = 0;
  46. return false;
  47. }
  48. inline void CopiedSpace::pin(CopiedBlock* block)
  49. {
  50. block->pin();
  51. }
  52. inline void CopiedSpace::pinIfNecessary(void* opaquePointer)
  53. {
  54. // Pointers into the copied space come in the following varieties:
  55. // 1) Pointers to the start of a span of memory. This is the most
  56. // natural though not necessarily the most common.
  57. // 2) Pointers to one value-sized (8 byte) word past the end of
  58. // a span of memory. This currently occurs with semi-butterflies
  59. // and should be fixed soon, once the other half of the
  60. // butterfly lands.
  61. // 3) Pointers to the innards arising from loop induction variable
  62. // optimizations (either manual ones or automatic, by the
  63. // compiler).
  64. // 4) Pointers to the end of a span of memory in arising from
  65. // induction variable optimizations combined with the
  66. // GC-to-compiler contract laid out in the C spec: a pointer to
  67. // the end of a span of memory must be considered to be a
  68. // pointer to that memory.
  69. EncodedJSValue* pointer = reinterpret_cast<EncodedJSValue*>(opaquePointer);
  70. CopiedBlock* block;
  71. // Handle (1) and (3).
  72. if (contains(pointer, block))
  73. pin(block);
  74. // Handle (4). We don't have to explicitly check and pin the block under this
  75. // pointer because it cannot possibly point to something that cases (1) and
  76. // (3) above or case (2) below wouldn't already catch.
  77. pointer--;
  78. // Handle (2)
  79. pointer--;
  80. if (contains(pointer, block))
  81. pin(block);
  82. }
  83. inline void CopiedSpace::recycleEvacuatedBlock(CopiedBlock* block)
  84. {
  85. ASSERT(block);
  86. ASSERT(block->canBeRecycled());
  87. ASSERT(!block->m_isPinned);
  88. {
  89. SpinLockHolder locker(&m_toSpaceLock);
  90. m_blockSet.remove(block);
  91. m_fromSpace->remove(block);
  92. }
  93. m_heap->blockAllocator().deallocate(CopiedBlock::destroy(block));
  94. }
  95. inline void CopiedSpace::recycleBorrowedBlock(CopiedBlock* block)
  96. {
  97. m_heap->blockAllocator().deallocate(CopiedBlock::destroy(block));
  98. {
  99. MutexLocker locker(m_loanedBlocksLock);
  100. ASSERT(m_numberOfLoanedBlocks > 0);
  101. ASSERT(m_inCopyingPhase);
  102. m_numberOfLoanedBlocks--;
  103. if (!m_numberOfLoanedBlocks)
  104. m_loanedBlocksCondition.signal();
  105. }
  106. }
  107. inline CopiedBlock* CopiedSpace::allocateBlockForCopyingPhase()
  108. {
  109. ASSERT(m_inCopyingPhase);
  110. CopiedBlock* block = CopiedBlock::createNoZeroFill(m_heap->blockAllocator().allocate<CopiedBlock>());
  111. {
  112. MutexLocker locker(m_loanedBlocksLock);
  113. m_numberOfLoanedBlocks++;
  114. }
  115. ASSERT(!block->dataSize());
  116. return block;
  117. }
  118. inline void CopiedSpace::allocateBlock()
  119. {
  120. if (m_heap->shouldCollect())
  121. m_heap->collect(Heap::DoNotSweep);
  122. m_allocator.resetCurrentBlock();
  123. CopiedBlock* block = CopiedBlock::create(m_heap->blockAllocator().allocate<CopiedBlock>());
  124. m_toSpace->push(block);
  125. m_blockFilter.add(reinterpret_cast<Bits>(block));
  126. m_blockSet.add(block);
  127. m_allocator.setCurrentBlock(block);
  128. }
  129. inline CheckedBoolean CopiedSpace::tryAllocate(size_t bytes, void** outPtr)
  130. {
  131. #if ENABLE(GC_VALIDATION)
  132. ASSERT(!m_heap->vm()->isInitializingObject());
  133. #endif
  134. if (!m_allocator.tryAllocate(bytes, outPtr))
  135. return tryAllocateSlowCase(bytes, outPtr);
  136. ASSERT(*outPtr);
  137. return true;
  138. }
  139. inline bool CopiedSpace::isOversize(size_t bytes)
  140. {
  141. return bytes > s_maxAllocationSize;
  142. }
  143. inline bool CopiedSpace::isPinned(void* ptr)
  144. {
  145. return blockFor(ptr)->m_isPinned;
  146. }
  147. inline CopiedBlock* CopiedSpace::blockFor(void* ptr)
  148. {
  149. return reinterpret_cast<CopiedBlock*>(reinterpret_cast<size_t>(ptr) & s_blockMask);
  150. }
  151. } // namespace JSC
  152. #endif // CopiedSpaceInlines_h