blob: 79b168c37eab7a16969d1b91384ba1f74e3d0423 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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_CONTAINERS_H_
6#define V8_ZONE_CONTAINERS_H_
7
8#include <deque>
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009#include <list>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010#include <map>
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011#include <queue>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012#include <set>
Emily Bernierd0a1eb72015-03-24 16:35:39 -040013#include <stack>
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014#include <vector>
15
16#include "src/zone-allocator.h"
17
18namespace v8 {
19namespace internal {
20
21// A wrapper subclass for std::vector to make it easy to construct one
22// that uses a zone allocator.
23template <typename T>
Emily Bernierd0a1eb72015-03-24 16:35:39 -040024class ZoneVector : public std::vector<T, zone_allocator<T>> {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000025 public:
26 // Constructs an empty vector.
27 explicit ZoneVector(Zone* zone)
Emily Bernierd0a1eb72015-03-24 16:35:39 -040028 : std::vector<T, zone_allocator<T>>(zone_allocator<T>(zone)) {}
29
30 // Constructs a new vector and fills it with {size} elements, each
31 // constructed via the default constructor.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000032 ZoneVector(size_t size, Zone* zone)
Emily Bernierd0a1eb72015-03-24 16:35:39 -040033 : std::vector<T, zone_allocator<T>>(size, T(), zone_allocator<T>(zone)) {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +000034
35 // Constructs a new vector and fills it with {size} elements, each
36 // having the value {def}.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000037 ZoneVector(size_t size, T def, Zone* zone)
Emily Bernierd0a1eb72015-03-24 16:35:39 -040038 : std::vector<T, zone_allocator<T>>(size, def, zone_allocator<T>(zone)) {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +000039};
40
Emily Bernierd0a1eb72015-03-24 16:35:39 -040041
Ben Murdochb8a8cc12014-11-26 15:28:44 +000042// A wrapper subclass std::deque to make it easy to construct one
43// that uses a zone allocator.
44template <typename T>
Emily Bernierd0a1eb72015-03-24 16:35:39 -040045class ZoneDeque : public std::deque<T, zone_allocator<T>> {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000046 public:
Emily Bernierd0a1eb72015-03-24 16:35:39 -040047 // Constructs an empty deque.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000048 explicit ZoneDeque(Zone* zone)
Emily Bernierd0a1eb72015-03-24 16:35:39 -040049 : std::deque<T, zone_allocator<T>>(zone_allocator<T>(zone)) {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +000050};
51
Emily Bernierd0a1eb72015-03-24 16:35:39 -040052
53// A wrapper subclass std::list to make it easy to construct one
54// that uses a zone allocator.
55// TODO(mstarzinger): This should be renamed to ZoneList once we got rid of our
56// own home-grown ZoneList that actually is a ZoneVector.
57template <typename T>
58class ZoneLinkedList : public std::list<T, zone_allocator<T>> {
59 public:
60 // Constructs an empty list.
61 explicit ZoneLinkedList(Zone* zone)
62 : std::list<T, zone_allocator<T>>(zone_allocator<T>(zone)) {}
63};
64
65
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000066// A wrapper subclass std::priority_queue to make it easy to construct one
67// that uses a zone allocator.
68template <typename T, typename Compare = std::less<T>>
69class ZonePriorityQueue
70 : public std::priority_queue<T, ZoneVector<T>, Compare> {
71 public:
72 // Constructs an empty list.
73 explicit ZonePriorityQueue(Zone* zone)
74 : std::priority_queue<T, ZoneVector<T>, Compare>(Compare(),
75 ZoneVector<T>(zone)) {}
76};
77
78
Ben Murdochb8a8cc12014-11-26 15:28:44 +000079// A wrapper subclass for std::queue to make it easy to construct one
80// that uses a zone allocator.
81template <typename T>
Emily Bernierd0a1eb72015-03-24 16:35:39 -040082class ZoneQueue : public std::queue<T, ZoneDeque<T>> {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000083 public:
84 // Constructs an empty queue.
85 explicit ZoneQueue(Zone* zone)
Emily Bernierd0a1eb72015-03-24 16:35:39 -040086 : std::queue<T, ZoneDeque<T>>(ZoneDeque<T>(zone)) {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +000087};
88
Emily Bernierd0a1eb72015-03-24 16:35:39 -040089
90// A wrapper subclass for std::stack to make it easy to construct one that uses
91// a zone allocator.
92template <typename T>
93class ZoneStack : public std::stack<T, ZoneDeque<T>> {
94 public:
95 // Constructs an empty stack.
96 explicit ZoneStack(Zone* zone)
97 : std::stack<T, ZoneDeque<T>>(ZoneDeque<T>(zone)) {}
98};
99
100
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000101// A wrapper subclass for std::set to make it easy to construct one that uses
102// a zone allocator.
103template <typename K, typename Compare = std::less<K>>
104class ZoneSet : public std::set<K, Compare, zone_allocator<K>> {
105 public:
106 // Constructs an empty set.
107 explicit ZoneSet(Zone* zone)
108 : std::set<K, Compare, zone_allocator<K>>(Compare(),
109 zone_allocator<K>(zone)) {}
110};
111
112
113// A wrapper subclass for std::map to make it easy to construct one that uses
114// a zone allocator.
115template <typename K, typename V, typename Compare = std::less<K>>
116class ZoneMap
117 : public std::map<K, V, Compare, zone_allocator<std::pair<const K, V>>> {
118 public:
119 // Constructs an empty map.
120 explicit ZoneMap(Zone* zone)
121 : std::map<K, V, Compare, zone_allocator<std::pair<const K, V>>>(
122 Compare(), zone_allocator<std::pair<const K, V>>(zone)) {}
123};
124
125
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000126// Typedefs to shorten commonly used vectors.
127typedef ZoneVector<bool> BoolVector;
128typedef ZoneVector<int> IntVector;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400129
130} // namespace internal
131} // namespace v8
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000132
133#endif // V8_ZONE_CONTAINERS_H_