]>
git.saurik.com Git - apple/javascriptcore.git/blob - heap/GCSegmentedArray.h
8aeba1025817eeedf87e336f21a19af40bd68ffc
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 <wtf/DoublyLinkedList.h>
30 #include <wtf/Vector.h>
35 class GCArraySegment
: public DoublyLinkedListNode
<GCArraySegment
<T
>> {
36 friend class WTF::DoublyLinkedListNode
<GCArraySegment
<T
>>;
39 : DoublyLinkedListNode
<GCArraySegment
<T
>>()
46 static GCArraySegment
* create();
47 static void destroy(GCArraySegment
*);
51 return bitwise_cast
<T
*>(this + 1);
54 static const size_t blockSize
= 4 * KB
;
56 GCArraySegment
* m_prev
;
57 GCArraySegment
* m_next
;
63 template <typename T
> class GCSegmentedArrayIterator
;
66 class GCSegmentedArray
{
67 friend class GCSegmentedArrayIterator
<T
>;
68 friend class GCSegmentedArrayIterator
<const T
>;
82 void fillVector(Vector
<T
>&);
85 typedef GCSegmentedArrayIterator
<T
> iterator
;
86 iterator
begin() const { return GCSegmentedArrayIterator
<T
>(m_segments
.head(), m_top
); }
87 iterator
end() const { return GCSegmentedArrayIterator
<T
>(); }
90 template <size_t size
> struct CapacityFromSize
{
91 static const size_t value
= (size
- sizeof(GCArraySegment
<T
>)) / sizeof(T
);
98 void setTopForFullSegment();
99 void setTopForEmptySegment();
102 void validatePrevious();
104 DoublyLinkedList
<GCArraySegment
<T
>> m_segments
;
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