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 <list>
10 : #include <map>
11 : #include <queue>
12 : #include <set>
13 : #include <stack>
14 : #include <vector>
15 :
16 : #include "src/zone/zone-allocator.h"
17 :
18 : namespace v8 {
19 : namespace internal {
20 :
21 : // A wrapper subclass for std::vector to make it easy to construct one
22 : // that uses a zone allocator.
23 : template <typename T>
24 5625947 : class ZoneVector : public std::vector<T, ZoneAllocator<T>> {
25 : public:
26 : // Constructs an empty vector.
27 982 : explicit ZoneVector(Zone* zone)
28 982 : : std::vector<T, ZoneAllocator<T>>(ZoneAllocator<T>(zone)) {}
29 :
30 : // Constructs a new vector and fills it with {size} elements, each
31 : // constructed via the default constructor.
32 395303 : ZoneVector(size_t size, Zone* zone)
33 20894929 : : std::vector<T, ZoneAllocator<T>>(size, T(), ZoneAllocator<T>(zone)) {}
34 :
35 : // Constructs a new vector and fills it with {size} elements, each
36 : // having the value {def}.
37 : ZoneVector(size_t size, T def, Zone* zone)
38 16265432 : : std::vector<T, ZoneAllocator<T>>(size, def, ZoneAllocator<T>(zone)) {}
39 :
40 : // Constructs a new vector and fills it with the contents of the range
41 : // [first, last).
42 : template <class InputIt>
43 : ZoneVector(InputIt first, InputIt last, Zone* zone)
44 641693 : : std::vector<T, ZoneAllocator<T>>(first, last, ZoneAllocator<T>(zone)) {}
45 : };
46 :
47 : // A wrapper subclass std::deque to make it easy to construct one
48 : // that uses a zone allocator.
49 : template <typename T>
50 : class ZoneDeque : public std::deque<T, RecyclingZoneAllocator<T>> {
51 : public:
52 : // Constructs an empty deque.
53 : explicit ZoneDeque(Zone* zone)
54 : : std::deque<T, RecyclingZoneAllocator<T>>(
55 23135919 : RecyclingZoneAllocator<T>(zone)) {}
56 : };
57 :
58 : // A wrapper subclass std::list to make it easy to construct one
59 : // that uses a zone allocator.
60 : // TODO(mstarzinger): This should be renamed to ZoneList once we got rid of our
61 : // own home-grown ZoneList that actually is a ZoneVector.
62 : template <typename T>
63 : class ZoneLinkedList : public std::list<T, ZoneAllocator<T>> {
64 : public:
65 : // Constructs an empty list.
66 : explicit ZoneLinkedList(Zone* zone)
67 : : std::list<T, ZoneAllocator<T>>(ZoneAllocator<T>(zone)) {}
68 : };
69 :
70 : // A wrapper subclass std::priority_queue to make it easy to construct one
71 : // that uses a zone allocator.
72 : template <typename T, typename Compare = std::less<T>>
73 : class ZonePriorityQueue
74 : : public std::priority_queue<T, ZoneVector<T>, Compare> {
75 : public:
76 : // Constructs an empty list.
77 : explicit ZonePriorityQueue(Zone* zone)
78 : : std::priority_queue<T, ZoneVector<T>, Compare>(Compare(),
79 : ZoneVector<T>(zone)) {}
80 : };
81 :
82 : // A wrapper subclass for std::queue to make it easy to construct one
83 : // that uses a zone allocator.
84 : template <typename T>
85 : class ZoneQueue : public std::queue<T, ZoneDeque<T>> {
86 : public:
87 : // Constructs an empty queue.
88 6577566 : explicit ZoneQueue(Zone* zone)
89 6577627 : : std::queue<T, ZoneDeque<T>>(ZoneDeque<T>(zone)) {}
90 : };
91 :
92 : // A wrapper subclass for std::stack to make it easy to construct one that uses
93 : // a zone allocator.
94 : template <typename T>
95 : class ZoneStack : public std::stack<T, ZoneDeque<T>> {
96 : public:
97 : // Constructs an empty stack.
98 10352046 : explicit ZoneStack(Zone* zone)
99 10352175 : : std::stack<T, ZoneDeque<T>>(ZoneDeque<T>(zone)) {}
100 : };
101 :
102 : // A wrapper subclass for std::set to make it easy to construct one that uses
103 : // a zone allocator.
104 : template <typename K, typename Compare = std::less<K>>
105 : class ZoneSet : public std::set<K, Compare, ZoneAllocator<K>> {
106 : public:
107 : // Constructs an empty set.
108 : explicit ZoneSet(Zone* zone)
109 : : std::set<K, Compare, ZoneAllocator<K>>(Compare(),
110 : ZoneAllocator<K>(zone)) {}
111 : };
112 :
113 : // A wrapper subclass for std::map to make it easy to construct one that uses
114 : // a zone allocator.
115 : template <typename K, typename V, typename Compare = std::less<K>>
116 : class ZoneMap
117 : : public std::map<K, V, Compare, ZoneAllocator<std::pair<const K, V>>> {
118 : public:
119 : // Constructs an empty map.
120 : explicit ZoneMap(Zone* zone)
121 : : std::map<K, V, Compare, ZoneAllocator<std::pair<const K, V>>>(
122 : Compare(), ZoneAllocator<std::pair<const K, V>>(zone)) {}
123 : };
124 :
125 : // A wrapper subclass for std::multimap to make it easy to construct one that
126 : // uses a zone allocator.
127 : template <typename K, typename V, typename Compare = std::less<K>>
128 : class ZoneMultimap
129 : : public std::multimap<K, V, Compare,
130 : ZoneAllocator<std::pair<const K, V>>> {
131 : public:
132 : // Constructs an empty multimap.
133 : explicit ZoneMultimap(Zone* zone)
134 : : std::multimap<K, V, Compare, ZoneAllocator<std::pair<const K, V>>>(
135 : Compare(), ZoneAllocator<std::pair<const K, V>>(zone)) {}
136 : };
137 :
138 : // Typedefs to shorten commonly used vectors.
139 : typedef ZoneVector<bool> BoolVector;
140 : typedef ZoneVector<int> IntVector;
141 :
142 : } // namespace internal
143 : } // namespace v8
144 :
145 : #endif // V8_SRC_ZONE_ZONE_CONTAINERS_H_
|