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