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
22namespace v8 {
23namespace internal {
24
25// A wrapper subclass for std::vector to make it easy to construct one
26// that uses a zone allocator.
27template <typename T>
28class ZoneVector : public std::vector<T, ZoneAllocator<T>> {
29 public:
30  // Constructs an empty vector.
31  explicit ZoneVector(Zone* zone)
32      : 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  ZoneVector(size_t size, Zone* zone)
37      : 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      : 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  ZoneVector(std::initializer_list<T> list, Zone* zone)
47      : 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  ZoneVector(InputIt first, InputIt last, Zone* zone)
53      : 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.
58template <typename T>
59class 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            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(all): This should be renamed to ZoneList once we got rid of our own
70// home-grown ZoneList that actually is a ZoneVector.
71template <typename T>
72class 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.
81template <typename T>
82class 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.
91template <typename T, typename Compare = std::less<T>>
92class 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.
103template <typename T>
104class ZoneQueue : public std::queue<T, ZoneDeque<T>> {
105 public:
106  // Constructs an empty queue.
107  explicit ZoneQueue(Zone* zone)
108      : 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.
113template <typename T>
114class ZoneStack : public std::stack<T, ZoneDeque<T>> {
115 public:
116  // Constructs an empty stack.
117  explicit ZoneStack(Zone* zone)
118      : 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.
123template <typename K, typename Compare = std::less<K>>
124class 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.
134template <typename K, typename Compare = std::less<K>>
135class 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.
145template <typename K, typename V, typename Compare = std::less<K>>
146class 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.
157template <typename K, typename V, typename Hash = base::hash<K>,
158          typename KeyEqual = std::equal_to<K>>
159class 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  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            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.
173template <typename K, typename Hash = base::hash<K>,
174          typename KeyEqual = std::equal_to<K>>
175class ZoneUnorderedSet
176    : public std::unordered_set<K, Hash, KeyEqual, ZoneAllocator<K>> {
177 public:
178  // Constructs an empty map.
179  explicit ZoneUnorderedSet(Zone* zone, size_t bucket_count = 100)
180      : std::unordered_set<K, Hash, KeyEqual, ZoneAllocator<K>>(
181            bucket_count, 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.
186template <typename K, typename V, typename Compare = std::less<K>>
187class 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.
198using BoolVector = ZoneVector<bool>;
199using IntVector = ZoneVector<int>;
200
201}  // namespace internal
202}  // namespace v8
203
204#endif  // V8_ZONE_ZONE_CONTAINERS_H_
205