CopiedAllocator.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 CopiedAllocator_h
  26. #define CopiedAllocator_h
  27. #include "CopiedBlock.h"
  28. #include <wtf/CheckedBoolean.h>
  29. #include <wtf/DataLog.h>
  30. namespace JSC {
  31. class CopiedAllocator {
  32. public:
  33. CopiedAllocator();
  34. bool fastPathShouldSucceed(size_t bytes) const;
  35. CheckedBoolean tryAllocate(size_t bytes, void** outPtr);
  36. CheckedBoolean tryReallocate(void *oldPtr, size_t oldBytes, size_t newBytes);
  37. void* forceAllocate(size_t bytes);
  38. CopiedBlock* resetCurrentBlock();
  39. void setCurrentBlock(CopiedBlock*);
  40. size_t currentCapacity();
  41. bool isValid() { return !!m_currentBlock; }
  42. CopiedBlock* currentBlock() { return m_currentBlock; }
  43. // Yes, these are public. No, that doesn't mean you can play with them.
  44. // If I had made them private then I'd have to list off all of the JIT
  45. // classes and functions that are entitled to modify these directly, and
  46. // that would have been gross.
  47. size_t m_currentRemaining;
  48. char* m_currentPayloadEnd;
  49. CopiedBlock* m_currentBlock;
  50. };
  51. inline CopiedAllocator::CopiedAllocator()
  52. : m_currentRemaining(0)
  53. , m_currentPayloadEnd(0)
  54. , m_currentBlock(0)
  55. {
  56. }
  57. inline bool CopiedAllocator::fastPathShouldSucceed(size_t bytes) const
  58. {
  59. ASSERT(is8ByteAligned(reinterpret_cast<void*>(bytes)));
  60. return bytes <= m_currentRemaining;
  61. }
  62. inline CheckedBoolean CopiedAllocator::tryAllocate(size_t bytes, void** outPtr)
  63. {
  64. ASSERT(is8ByteAligned(reinterpret_cast<void*>(bytes)));
  65. // This code is written in a gratuitously low-level manner, in order to
  66. // serve as a kind of template for what the JIT would do. Note that the
  67. // way it's written it ought to only require one register, which doubles
  68. // as the result, provided that the compiler does a minimal amount of
  69. // control flow simplification and the bytes argument is a constant.
  70. size_t currentRemaining = m_currentRemaining;
  71. if (bytes > currentRemaining)
  72. return false;
  73. currentRemaining -= bytes;
  74. m_currentRemaining = currentRemaining;
  75. *outPtr = m_currentPayloadEnd - currentRemaining - bytes;
  76. ASSERT(is8ByteAligned(*outPtr));
  77. return true;
  78. }
  79. inline CheckedBoolean CopiedAllocator::tryReallocate(
  80. void* oldPtr, size_t oldBytes, size_t newBytes)
  81. {
  82. ASSERT(is8ByteAligned(oldPtr));
  83. ASSERT(is8ByteAligned(reinterpret_cast<void*>(oldBytes)));
  84. ASSERT(is8ByteAligned(reinterpret_cast<void*>(newBytes)));
  85. ASSERT(newBytes > oldBytes);
  86. size_t additionalBytes = newBytes - oldBytes;
  87. size_t currentRemaining = m_currentRemaining;
  88. if (m_currentPayloadEnd - currentRemaining - oldBytes != static_cast<char*>(oldPtr))
  89. return false;
  90. if (additionalBytes > currentRemaining)
  91. return false;
  92. m_currentRemaining = currentRemaining - additionalBytes;
  93. return true;
  94. }
  95. inline void* CopiedAllocator::forceAllocate(size_t bytes)
  96. {
  97. void* result = 0; // Needed because compilers don't realize this will always be assigned.
  98. CheckedBoolean didSucceed = tryAllocate(bytes, &result);
  99. ASSERT(didSucceed);
  100. return result;
  101. }
  102. inline CopiedBlock* CopiedAllocator::resetCurrentBlock()
  103. {
  104. CopiedBlock* result = m_currentBlock;
  105. if (result) {
  106. result->m_remaining = m_currentRemaining;
  107. m_currentBlock = 0;
  108. m_currentRemaining = 0;
  109. m_currentPayloadEnd = 0;
  110. }
  111. return result;
  112. }
  113. inline void CopiedAllocator::setCurrentBlock(CopiedBlock* newBlock)
  114. {
  115. ASSERT(!m_currentBlock);
  116. m_currentBlock = newBlock;
  117. ASSERT(newBlock);
  118. m_currentRemaining = newBlock->m_remaining;
  119. m_currentPayloadEnd = newBlock->payloadEnd();
  120. }
  121. inline size_t CopiedAllocator::currentCapacity()
  122. {
  123. if (!m_currentBlock)
  124. return 0;
  125. return m_currentBlock->capacity();
  126. }
  127. } // namespace JSC
  128. #endif