Line data Source code
1 : // Copyright 2014 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #ifndef V8_ZONE_ZONE_CONTAINERS_H_
6 : #define V8_ZONE_ZONE_CONTAINERS_H_
7 :
8 : #include <deque>
9 : #include <forward_list>
10 : #include <list>
11 : #include <map>
12 : #include <queue>
13 : #include <set>
14 : #include <stack>
15 : #include <unordered_map>
16 : #include <unordered_set>
17 : #include <vector>
18 :
19 : #include "src/base/functional.h"
20 : #include "src/zone/zone-allocator.h"
21 :
22 : namespace v8 {
23 : namespace internal {
24 :
25 : // A wrapper subclass for std::vector to make it easy to construct one
26 : // that uses a zone allocator.
27 : template <typename T>
28 21993751 : class ZoneVector : public std::vector<T, ZoneAllocator<T>> {
29 : public:
30 : // Constructs an empty vector.
31 1098 : explicit ZoneVector(Zone* zone)
32 1098 : : std::vector<T, ZoneAllocator<T>>(ZoneAllocator<T>(zone)) {}
33 :
34 : // Constructs a new vector and fills it with {size} elements, each
35 : // constructed via the default constructor.
36 483149 : ZoneVector(size_t size, Zone* zone)
37 36534729 : : std::vector<T, ZoneAllocator<T>>(size, T(), ZoneAllocator<T>(zone)) {}
38 :
39 : // Constructs a new vector and fills it with {size} elements, each
40 : // having the value {def}.
41 : ZoneVector(size_t size, T def, Zone* zone)
42 33447203 : : std::vector<T, ZoneAllocator<T>>(size, def, ZoneAllocator<T>(zone)) {}
43 :
44 : // Constructs a new vector and fills it with the contents of the given
45 : // initializer list.
46 15172 : ZoneVector(std::initializer_list<T> list, Zone* zone)
47 15172 : : std::vector<T, ZoneAllocator<T>>(list, ZoneAllocator<T>(zone)) {}
48 :
49 : // Constructs a new vector and fills it with the contents of the range
50 : // [first, last).
51 : template <class InputIt>
52 35100 : ZoneVector(InputIt first, InputIt last, Zone* zone)
53 3728769 : : std::vector<T, ZoneAllocator<T>>(first, last, ZoneAllocator<T>(zone)) {}
54 : };
55 :
56 : // A wrapper subclass for std::deque to make it easy to construct one
57 : // that uses a zone allocator.
58 : template <typename T>
59 : class ZoneDeque : public std::deque<T, RecyclingZoneAllocator<T>> {
60 : public:
61 : // Constructs an empty deque.
62 : explicit ZoneDeque(Zone* zone)
63 : : std::deque<T, RecyclingZoneAllocator<T>>(
64 44587934 : RecyclingZoneAllocator<T>(zone)) {}
65 : };
66 :
67 : // A wrapper subclass for std::list to make it easy to construct one
68 : // that uses a zone allocator.
69 : // TODO(mstarzinger): This should be renamed to ZoneList once we got rid of our
70 : // own home-grown ZoneList that actually is a ZoneVector.
71 : template <typename T>
72 : class ZoneLinkedList : public std::list<T, ZoneAllocator<T>> {
73 : public:
74 : // Constructs an empty list.
75 : explicit ZoneLinkedList(Zone* zone)
76 : : std::list<T, ZoneAllocator<T>>(ZoneAllocator<T>(zone)) {}
77 : };
78 :
79 : // A wrapper subclass for std::forward_list to make it easy to construct one
80 : // that uses a zone allocator.
81 : template <typename T>
82 : class ZoneForwardList : public std::forward_list<T, ZoneAllocator<T>> {
83 : public:
84 : // Constructs an empty list.
85 : explicit ZoneForwardList(Zone* zone)
86 : : std::forward_list<T, ZoneAllocator<T>>(ZoneAllocator<T>(zone)) {}
87 : };
88 :
89 : // A wrapper subclass for std::priority_queue to make it easy to construct one
90 : // that uses a zone allocator.
91 : template <typename T, typename Compare = std::less<T>>
92 : class ZonePriorityQueue
93 : : public std::priority_queue<T, ZoneVector<T>, Compare> {
94 : public:
95 : // Constructs an empty list.
96 : explicit ZonePriorityQueue(Zone* zone)
97 : : std::priority_queue<T, ZoneVector<T>, Compare>(Compare(),
98 : ZoneVector<T>(zone)) {}
99 : };
100 :
101 : // A wrapper subclass for std::queue to make it easy to construct one
102 : // that uses a zone allocator.
103 : template <typename T>
104 : class ZoneQueue : public std::queue<T, ZoneDeque<T>> {
105 : public:
106 : // Constructs an empty queue.
107 15875697 : explicit ZoneQueue(Zone* zone)
108 15876274 : : std::queue<T, ZoneDeque<T>>(ZoneDeque<T>(zone)) {}
109 : };
110 :
111 : // A wrapper subclass for std::stack to make it easy to construct one that uses
112 : // a zone allocator.
113 : template <typename T>
114 : class ZoneStack : public std::stack<T, ZoneDeque<T>> {
115 : public:
116 : // Constructs an empty stack.
117 12567457 : explicit ZoneStack(Zone* zone)
118 12567851 : : std::stack<T, ZoneDeque<T>>(ZoneDeque<T>(zone)) {}
119 : };
120 :
121 : // A wrapper subclass for std::set to make it easy to construct one that uses
122 : // a zone allocator.
123 : template <typename K, typename Compare = std::less<K>>
124 : class ZoneSet : public std::set<K, Compare, ZoneAllocator<K>> {
125 : public:
126 : // Constructs an empty set.
127 : explicit ZoneSet(Zone* zone)
128 : : std::set<K, Compare, ZoneAllocator<K>>(Compare(),
129 : ZoneAllocator<K>(zone)) {}
130 : };
131 :
132 : // A wrapper subclass for std::multiset to make it easy to construct one that
133 : // uses a zone allocator.
134 : template <typename K, typename Compare = std::less<K>>
135 : class ZoneMultiset : public std::multiset<K, Compare, ZoneAllocator<K>> {
136 : public:
137 : // Constructs an empty set.
138 : explicit ZoneMultiset(Zone* zone)
139 : : std::multiset<K, Compare, ZoneAllocator<K>>(Compare(),
140 : ZoneAllocator<K>(zone)) {}
141 : };
142 :
143 : // A wrapper subclass for std::map to make it easy to construct one that uses
144 : // a zone allocator.
145 : template <typename K, typename V, typename Compare = std::less<K>>
146 : class ZoneMap
147 : : public std::map<K, V, Compare, ZoneAllocator<std::pair<const K, V>>> {
148 : public:
149 : // Constructs an empty map.
150 : explicit ZoneMap(Zone* zone)
151 : : std::map<K, V, Compare, ZoneAllocator<std::pair<const K, V>>>(
152 : Compare(), ZoneAllocator<std::pair<const K, V>>(zone)) {}
153 : };
154 :
155 : // A wrapper subclass for std::unordered_map to make it easy to construct one
156 : // that uses a zone allocator.
157 : template <typename K, typename V, typename Hash = base::hash<K>,
158 : typename KeyEqual = std::equal_to<K>>
159 : class ZoneUnorderedMap
160 : : public std::unordered_map<K, V, Hash, KeyEqual,
161 : ZoneAllocator<std::pair<const K, V>>> {
162 : public:
163 : // Constructs an empty map.
164 6158560 : explicit ZoneUnorderedMap(Zone* zone, size_t bucket_count = 100)
165 : : std::unordered_map<K, V, Hash, KeyEqual,
166 : ZoneAllocator<std::pair<const K, V>>>(
167 : bucket_count, Hash(), KeyEqual(),
168 6158487 : ZoneAllocator<std::pair<const K, V>>(zone)) {}
169 : };
170 :
171 : // A wrapper subclass for std::unordered_set to make it easy to construct one
172 : // that uses a zone allocator.
173 : template <typename K, typename Hash = base::hash<K>,
174 : typename KeyEqual = std::equal_to<K>>
175 : class ZoneUnorderedSet
176 : : public std::unordered_set<K, Hash, KeyEqual, ZoneAllocator<K>> {
177 : public:
178 : // Constructs an empty map.
179 7841529 : explicit ZoneUnorderedSet(Zone* zone)
180 : : std::unordered_set<K, Hash, KeyEqual, ZoneAllocator<K>>(
181 7841533 : 100, Hash(), KeyEqual(), ZoneAllocator<K>(zone)) {}
182 : };
183 :
184 : // A wrapper subclass for std::multimap to make it easy to construct one that
185 : // uses a zone allocator.
186 : template <typename K, typename V, typename Compare = std::less<K>>
187 : class ZoneMultimap
188 : : public std::multimap<K, V, Compare,
189 : ZoneAllocator<std::pair<const K, V>>> {
190 : public:
191 : // Constructs an empty multimap.
192 : explicit ZoneMultimap(Zone* zone)
193 : : std::multimap<K, V, Compare, ZoneAllocator<std::pair<const K, V>>>(
194 : Compare(), ZoneAllocator<std::pair<const K, V>>(zone)) {}
195 : };
196 :
197 : // Typedefs to shorten commonly used vectors.
198 : typedef ZoneVector<bool> BoolVector;
199 : typedef ZoneVector<int> IntVector;
200 :
201 : } // namespace internal
202 : } // namespace v8
203 :
204 : #endif // V8_ZONE_ZONE_CONTAINERS_H_
|