TieredMMapArray.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (C) 2012 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 TieredMMapArray_h
  26. #define TieredMMapArray_h
  27. #include <wtf/OSAllocator.h>
  28. namespace JSC {
  29. // This class implements a simple array class that can be grown by appending items to the end.
  30. // This class is implemented purely in terms of system allocations, with no malloc/free, so that
  31. // it can safely be used from a secondary thread whilst the main thrad is paused (potentially
  32. // holding the fast malloc heap lock).
  33. template<typename T>
  34. class TieredMMapArray {
  35. static const size_t entriesPerBlock = 4096;
  36. public:
  37. TieredMMapArray()
  38. : m_directoryCount(4096)
  39. , m_directory(static_cast<T**>(OSAllocator::reserveAndCommit(m_directoryCount * sizeof(T*))))
  40. , m_size(0)
  41. {
  42. for (size_t block = 0; block < m_directoryCount; ++block)
  43. m_directory[block] = 0;
  44. }
  45. ~TieredMMapArray()
  46. {
  47. size_t usedCount = (m_size + (entriesPerBlock - 1)) / entriesPerBlock;
  48. ASSERT(usedCount == m_directoryCount || !m_directory[usedCount]);
  49. for (size_t block = 0; block < usedCount; ++block) {
  50. ASSERT(m_directory[block]);
  51. OSAllocator::decommitAndRelease(m_directory[block], entriesPerBlock * sizeof(T));
  52. }
  53. OSAllocator::decommitAndRelease(m_directory, m_directoryCount * sizeof(T*));
  54. }
  55. T& operator[](size_t index)
  56. {
  57. ASSERT(index < m_size);
  58. size_t block = index / entriesPerBlock;
  59. size_t offset = index % entriesPerBlock;
  60. ASSERT(m_directory[block]);
  61. return m_directory[block][offset];
  62. }
  63. void append(const T& value)
  64. {
  65. // Check if the array is completely full, if so create more capacity in the directory.
  66. if (m_size == m_directoryCount * entriesPerBlock) {
  67. // Reallocate the directory.
  68. size_t oldDirectorySize = m_directoryCount * sizeof(T*);
  69. size_t newDirectorySize = oldDirectorySize * 2;
  70. RELEASE_ASSERT(newDirectorySize < oldDirectorySize);
  71. m_directory = OSAllocator::reallocateCommitted(m_directory, oldDirectorySize, newDirectorySize);
  72. //
  73. size_t newDirectoryCount = m_directoryCount * 2;
  74. for (size_t block = m_directoryCount; block < newDirectoryCount; ++block)
  75. m_directory[block] = 0;
  76. m_directoryCount = newDirectoryCount;
  77. }
  78. size_t index = m_size;
  79. size_t block = index / entriesPerBlock;
  80. size_t offset = index % entriesPerBlock;
  81. if (!offset) {
  82. ASSERT(!m_directory[block]);
  83. m_directory[block] = static_cast<T*>(OSAllocator::reserveAndCommit(entriesPerBlock * sizeof(T)));
  84. }
  85. ASSERT(m_directory[block]);
  86. ++m_size;
  87. m_directory[block][offset] = value;
  88. }
  89. size_t size() const { return m_size; }
  90. private:
  91. size_t m_directoryCount;
  92. T** m_directory;
  93. size_t m_size;
  94. };
  95. }
  96. #endif // TieredMMapArray_h