]>
git.saurik.com Git - apple/javascriptcore.git/blob - tools/TieredMMapArray.h
2 * Copyright (C) 2012 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #ifndef TieredMMapArray_h
27 #define TieredMMapArray_h
29 #include <wtf/OSAllocator.h>
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).
38 class TieredMMapArray
{
39 static const size_t entriesPerBlock
= 4096;
43 : m_directoryCount(4096)
44 , m_directory(static_cast<T
**>(OSAllocator::reserveAndCommit(m_directoryCount
* sizeof(T
*))))
47 for (size_t block
= 0; block
< m_directoryCount
; ++block
)
48 m_directory
[block
] = 0;
53 size_t usedCount
= (m_size
+ (entriesPerBlock
- 1)) / entriesPerBlock
;
54 ASSERT(usedCount
== m_directoryCount
|| !m_directory
[usedCount
]);
56 for (size_t block
= 0; block
< usedCount
; ++block
) {
57 ASSERT(m_directory
[block
]);
58 OSAllocator::decommitAndRelease(m_directory
[block
], entriesPerBlock
* sizeof(T
));
61 OSAllocator::decommitAndRelease(m_directory
, m_directoryCount
* sizeof(T
*));
64 T
& operator[](size_t index
)
66 ASSERT(index
< m_size
);
67 size_t block
= index
/ entriesPerBlock
;
68 size_t offset
= index
% entriesPerBlock
;
70 ASSERT(m_directory
[block
]);
71 return m_directory
[block
][offset
];
74 void append(const T
& value
)
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
);
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
;
91 size_t index
= m_size
;
92 size_t block
= index
/ entriesPerBlock
;
93 size_t offset
= index
% entriesPerBlock
;
96 ASSERT(!m_directory
[block
]);
97 m_directory
[block
] = static_cast<T
*>(OSAllocator::reserveAndCommit(entriesPerBlock
* sizeof(T
)));
100 ASSERT(m_directory
[block
]);
102 m_directory
[block
][offset
] = value
;
105 size_t size() const { return m_size
; }
108 size_t m_directoryCount
;
115 #endif // TieredMMapArray_h