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_SRC_ZONE_ZONE_CONTAINERS_H_
6 : #define V8_SRC_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 1468672 : class ZoneVector : public std::vector<T, ZoneAllocator<T>> {
29 : public:
30 : // Constructs an empty vector.
31 718 : explicit ZoneVector(Zone* zone)
32 718 : : 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 443355 : ZoneVector(size_t size, Zone* zone)
37 29416811 : : 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 21442099 : : std::vector<T, ZoneAllocator<T>>(size, def, ZoneAllocator<T>(zone)) {}
43 :
44 : // Constructs a new vector and fills it with the contents of the range
45 : // [first, last).
46 : template <class InputIt>
47 : ZoneVector(InputIt first, InputIt last, Zone* zone)
48 653316 : : std::vector<T, ZoneAllocator<T>>(first, last, ZoneAllocator<T>(zone)) {}
49 : };
50 :
51 : // A wrapper subclass for std::deque to make it easy to construct one
52 : // that uses a zone allocator.
53 : template <typename T>
54 : class ZoneDeque : public std::deque<T, RecyclingZoneAllocator<T>> {
55 : public:
56 : // Constructs an empty deque.
57 : explicit ZoneDeque(Zone* zone)
58 : : std::deque<T, RecyclingZoneAllocator<T>>(
59 37703259 : RecyclingZoneAllocator<T>(zone)) {}
60 : };
61 :
62 : // A wrapper subclass for std::list to make it easy to construct one
63 : // that uses a zone allocator.
64 : // TODO(mstarzinger): This should be renamed to ZoneList once we got rid of our
65 : // own home-grown ZoneList that actually is a ZoneVector.
66 : template <typename T>
67 : class ZoneLinkedList : public std::list<T, ZoneAllocator<T>> {
68 : public:
69 : // Constructs an empty list.
70 : explicit ZoneLinkedList(Zone* zone)
71 : : std::list<T, ZoneAllocator<T>>(ZoneAllocator<T>(zone)) {}
72 : };
73 :
74 : // A wrapper subclass for std::forward_list to make it easy to construct one
75 : // that uses a zone allocator.
76 : template <typename T>
77 : class ZoneForwardList : public std::forward_list<T, ZoneAllocator<T>> {
78 : public:
79 : // Constructs an empty list.
80 : explicit ZoneForwardList(Zone* zone)
81 : : std::forward_list<T, ZoneAllocator<T>>(ZoneAllocator<T>(zone)) {}
82 : };
83 :
84 : // A wrapper subclass for std::priority_queue to make it easy to construct one
85 : // that uses a zone allocator.
86 : template <typename T, typename Compare = std::less<T>>
87 : class ZonePriorityQueue
88 : : public std::priority_queue<T, ZoneVector<T>, Compare> {
89 : public:
90 : // Constructs an empty list.
91 : explicit ZonePriorityQueue(Zone* zone)
92 : : std::priority_queue<T, ZoneVector<T>, Compare>(Compare(),
93 : ZoneVector<T>(zone)) {}
94 : };
95 :
96 : // A wrapper subclass for std::queue to make it easy to construct one
97 : // that uses a zone allocator.
98 : template <typename T>
99 : class ZoneQueue : public std::queue<T, ZoneDeque<T>> {
100 : public:
101 : // Constructs an empty queue.
102 12378008 : explicit ZoneQueue(Zone* zone)
103 12378053 : : std::queue<T, ZoneDeque<T>>(ZoneDeque<T>(zone)) {}
104 : };
105 :
106 : // A wrapper subclass for std::stack to make it easy to construct one that uses
107 : // a zone allocator.
108 : template <typename T>
109 : class ZoneStack : public std::stack<T, ZoneDeque<T>> {
110 : public:
111 : // Constructs an empty stack.
112 9843301 : explicit ZoneStack(Zone* zone)
113 9843409 : : std::stack<T, ZoneDeque<T>>(ZoneDeque<T>(zone)) {}
114 : };
115 :
116 : // A wrapper subclass for std::set to make it easy to construct one that uses
117 : // a zone allocator.
118 : template <typename K, typename Compare = std::less<K>>
119 : class ZoneSet : public std::set<K, Compare, ZoneAllocator<K>> {
120 : public:
121 : // Constructs an empty set.
122 : explicit ZoneSet(Zone* zone)
123 : : std::set<K, Compare, ZoneAllocator<K>>(Compare(),
124 : ZoneAllocator<K>(zone)) {}
125 : };
126 :
127 : // A wrapper subclass for std::map to make it easy to construct one that uses
128 : // a zone allocator.
129 : template <typename K, typename V, typename Compare = std::less<K>>
130 : class ZoneMap
131 : : public std::map<K, V, Compare, ZoneAllocator<std::pair<const K, V>>> {
132 : public:
133 : // Constructs an empty map.
134 : explicit ZoneMap(Zone* zone)
135 : : std::map<K, V, Compare, ZoneAllocator<std::pair<const K, V>>>(
136 : Compare(), ZoneAllocator<std::pair<const K, V>>(zone)) {}
137 : };
138 :
139 : // A wrapper subclass for std::unordered_map to make it easy to construct one
140 : // that uses a zone allocator.
141 : template <typename K, typename V, typename Hash = base::hash<K>,
142 : typename KeyEqual = std::equal_to<K>>
143 : class ZoneUnorderedMap
144 : : public std::unordered_map<K, V, Hash, KeyEqual,
145 : ZoneAllocator<std::pair<const K, V>>> {
146 : public:
147 : // Constructs an empty map.
148 886718 : explicit ZoneUnorderedMap(Zone* zone)
149 : : std::unordered_map<K, V, Hash, KeyEqual,
150 : ZoneAllocator<std::pair<const K, V>>>(
151 : 100, Hash(), KeyEqual(),
152 886718 : ZoneAllocator<std::pair<const K, V>>(zone)) {}
153 : };
154 :
155 : // A wrapper subclass for std::unordered_set to make it easy to construct one
156 : // that uses a zone allocator.
157 : template <typename K, typename Hash = base::hash<K>,
158 : typename KeyEqual = std::equal_to<K>>
159 : class ZoneUnorderedSet
160 : : public std::unordered_set<K, Hash, KeyEqual, ZoneAllocator<K>> {
161 : public:
162 : // Constructs an empty map.
163 443359 : explicit ZoneUnorderedSet(Zone* zone)
164 : : std::unordered_set<K, Hash, KeyEqual, ZoneAllocator<K>>(
165 443359 : 100, Hash(), KeyEqual(), ZoneAllocator<K>(zone)) {}
166 : };
167 :
168 : // A wrapper subclass for std::multimap to make it easy to construct one that
169 : // uses a zone allocator.
170 : template <typename K, typename V, typename Compare = std::less<K>>
171 : class ZoneMultimap
172 : : public std::multimap<K, V, Compare,
173 : ZoneAllocator<std::pair<const K, V>>> {
174 : public:
175 : // Constructs an empty multimap.
176 : explicit ZoneMultimap(Zone* zone)
177 : : std::multimap<K, V, Compare, ZoneAllocator<std::pair<const K, V>>>(
178 : Compare(), ZoneAllocator<std::pair<const K, V>>(zone)) {}
179 : };
180 :
181 : // Typedefs to shorten commonly used vectors.
182 : typedef ZoneVector<bool> BoolVector;
183 : typedef ZoneVector<int> IntVector;
184 :
185 : } // namespace internal
186 : } // namespace v8
187 :
188 : #endif // V8_SRC_ZONE_ZONE_CONTAINERS_H_
|