blob: b0ff7b6cf1bc68297ded6eae7e4bb76f322b18a9 [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 Murdochb8a8cc12014-11-26 15:28:44 +000010#include <queue>
Emily Bernierd0a1eb72015-03-24 16:35:39 -040011#include <stack>
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012#include <vector>
13
14#include "src/zone-allocator.h"
15
16namespace v8 {
17namespace internal {
18
19// A wrapper subclass for std::vector to make it easy to construct one
20// that uses a zone allocator.
21template <typename T>
Emily Bernierd0a1eb72015-03-24 16:35:39 -040022class ZoneVector : public std::vector<T, zone_allocator<T>> {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000023 public:
24 // Constructs an empty vector.
25 explicit ZoneVector(Zone* zone)
Emily Bernierd0a1eb72015-03-24 16:35:39 -040026 : std::vector<T, zone_allocator<T>>(zone_allocator<T>(zone)) {}
27
28 // Constructs a new vector and fills it with {size} elements, each
29 // constructed via the default constructor.
30 ZoneVector(int size, Zone* zone)
31 : std::vector<T, zone_allocator<T>>(size, T(), zone_allocator<T>(zone)) {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +000032
33 // Constructs a new vector and fills it with {size} elements, each
34 // having the value {def}.
35 ZoneVector(int size, T def, Zone* zone)
Emily Bernierd0a1eb72015-03-24 16:35:39 -040036 : std::vector<T, zone_allocator<T>>(size, def, zone_allocator<T>(zone)) {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +000037};
38
Emily Bernierd0a1eb72015-03-24 16:35:39 -040039
Ben Murdochb8a8cc12014-11-26 15:28:44 +000040// A wrapper subclass std::deque to make it easy to construct one
41// that uses a zone allocator.
42template <typename T>
Emily Bernierd0a1eb72015-03-24 16:35:39 -040043class ZoneDeque : public std::deque<T, zone_allocator<T>> {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000044 public:
Emily Bernierd0a1eb72015-03-24 16:35:39 -040045 // Constructs an empty deque.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000046 explicit ZoneDeque(Zone* zone)
Emily Bernierd0a1eb72015-03-24 16:35:39 -040047 : std::deque<T, zone_allocator<T>>(zone_allocator<T>(zone)) {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +000048};
49
Emily Bernierd0a1eb72015-03-24 16:35:39 -040050
51// A wrapper subclass std::list to make it easy to construct one
52// that uses a zone allocator.
53// TODO(mstarzinger): This should be renamed to ZoneList once we got rid of our
54// own home-grown ZoneList that actually is a ZoneVector.
55template <typename T>
56class ZoneLinkedList : public std::list<T, zone_allocator<T>> {
57 public:
58 // Constructs an empty list.
59 explicit ZoneLinkedList(Zone* zone)
60 : std::list<T, zone_allocator<T>>(zone_allocator<T>(zone)) {}
61};
62
63
Ben Murdochb8a8cc12014-11-26 15:28:44 +000064// A wrapper subclass for std::queue to make it easy to construct one
65// that uses a zone allocator.
66template <typename T>
Emily Bernierd0a1eb72015-03-24 16:35:39 -040067class ZoneQueue : public std::queue<T, ZoneDeque<T>> {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000068 public:
69 // Constructs an empty queue.
70 explicit ZoneQueue(Zone* zone)
Emily Bernierd0a1eb72015-03-24 16:35:39 -040071 : std::queue<T, ZoneDeque<T>>(ZoneDeque<T>(zone)) {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +000072};
73
Emily Bernierd0a1eb72015-03-24 16:35:39 -040074
75// A wrapper subclass for std::stack to make it easy to construct one that uses
76// a zone allocator.
77template <typename T>
78class ZoneStack : public std::stack<T, ZoneDeque<T>> {
79 public:
80 // Constructs an empty stack.
81 explicit ZoneStack(Zone* zone)
82 : std::stack<T, ZoneDeque<T>>(ZoneDeque<T>(zone)) {}
83};
84
85
Ben Murdochb8a8cc12014-11-26 15:28:44 +000086// Typedefs to shorten commonly used vectors.
87typedef ZoneVector<bool> BoolVector;
88typedef ZoneVector<int> IntVector;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040089
90} // namespace internal
91} // namespace v8
Ben Murdochb8a8cc12014-11-26 15:28:44 +000092
93#endif // V8_ZONE_CONTAINERS_H_