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