]>
git.saurik.com Git - apple/javascriptcore.git/blob - heap/GCSegmentedArray.h
2 * Copyright (C) 2014 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. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
26 #ifndef GCSegmentedArray_h
27 #define GCSegmentedArray_h
29 #include "HeapBlock.h"
30 #include <wtf/Vector.h>
38 class GCArraySegment
: public HeapBlock
<GCArraySegment
<T
>> {
40 GCArraySegment(Region
* region
)
41 : HeapBlock
<GCArraySegment
>(region
)
48 static GCArraySegment
* create(DeadBlock
*);
52 return bitwise_cast
<T
*>(this + 1);
55 static const size_t blockSize
= 4 * KB
;
62 template <typename T
> class GCSegmentedArrayIterator
;
65 class GCSegmentedArray
{
66 friend class GCSegmentedArrayIterator
<T
>;
67 friend class GCSegmentedArrayIterator
<const T
>;
69 GCSegmentedArray(BlockAllocator
&);
81 void fillVector(Vector
<T
>&);
84 typedef GCSegmentedArrayIterator
<T
> iterator
;
85 iterator
begin() const { return GCSegmentedArrayIterator
<T
>(m_segments
.head(), m_top
); }
86 iterator
end() const { return GCSegmentedArrayIterator
<T
>(); }
89 template <size_t size
> struct CapacityFromSize
{
90 static const size_t value
= (size
- sizeof(GCArraySegment
<T
>)) / sizeof(T
);
97 void setTopForFullSegment();
98 void setTopForEmptySegment();
101 void validatePrevious();
103 DoublyLinkedList
<GCArraySegment
<T
>> m_segments
;
104 BlockAllocator
& m_blockAllocator
;
106 JS_EXPORT_PRIVATE
static const size_t s_segmentCapacity
= CapacityFromSize
<GCArraySegment
<T
>::blockSize
>::value
;
108 size_t m_numberOfSegments
;
111 template <typename T
>
112 class GCSegmentedArrayIterator
{
113 friend class GCSegmentedArray
<T
>;
115 GCSegmentedArrayIterator()
116 : m_currentSegment(0)
121 T
& get() { return m_currentSegment
->data()[m_currentOffset
]; }
122 T
& operator*() { return get(); }
123 T
& operator->() { return get(); }
125 bool operator==(const GCSegmentedArrayIterator
& other
)
127 return m_currentSegment
== other
.m_currentSegment
&& m_currentOffset
== other
.m_currentOffset
;
130 bool operator!=(const GCSegmentedArrayIterator
& other
)
132 return !(*this == other
);
135 GCSegmentedArrayIterator
& operator++()
137 ASSERT(m_currentSegment
);
141 if (m_currentOffset
>= m_offsetLimit
) {
142 m_currentSegment
= m_currentSegment
->next();
144 m_offsetLimit
= GCSegmentedArray
<T
>::s_segmentCapacity
;
151 GCSegmentedArrayIterator(GCArraySegment
<T
>* start
, size_t top
)
152 : m_currentSegment(start
)
157 m_currentSegment
= nullptr;
160 GCArraySegment
<T
>* m_currentSegment
;
161 size_t m_currentOffset
;
162 size_t m_offsetLimit
;
167 #endif // GCSegmentedArray_h