OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_ZONE_CONTAINERS_H_ | 5 #ifndef V8_ZONE_CONTAINERS_H_ |
6 #define V8_ZONE_CONTAINERS_H_ | 6 #define V8_ZONE_CONTAINERS_H_ |
7 | 7 |
8 #include <deque> | 8 #include <deque> |
9 #include <queue> | 9 #include <queue> |
10 #include <stack> | |
11 #include <vector> | 10 #include <vector> |
12 | 11 |
13 #include "src/zone-allocator.h" | 12 #include "src/zone-allocator.h" |
14 | 13 |
15 namespace v8 { | 14 namespace v8 { |
16 namespace internal { | 15 namespace internal { |
17 | 16 |
18 // A wrapper subclass for std::vector to make it easy to construct one | 17 // A wrapper subclass for std::vector to make it easy to construct one |
19 // that uses a zone allocator. | 18 // that uses a zone allocator. |
20 template <typename T> | 19 template <typename T> |
21 class ZoneVector : public std::vector<T, zone_allocator<T> > { | 20 class ZoneVector : public std::vector<T, zone_allocator<T> > { |
22 public: | 21 public: |
23 // Constructs an empty vector. | 22 // Constructs an empty vector. |
24 explicit ZoneVector(Zone* zone) | 23 explicit ZoneVector(Zone* zone) |
25 : std::vector<T, zone_allocator<T> >(zone_allocator<T>(zone)) {} | 24 : std::vector<T, zone_allocator<T> >(zone_allocator<T>(zone)) {} |
26 | 25 |
27 // Constructs a new vector and fills it with {size} elements, each | 26 // Constructs a new vector and fills it with {size} elements, each |
28 // constructed via the default constructor. | 27 // constructed via the default constructor. |
29 ZoneVector(int size, Zone* zone) | 28 ZoneVector(int size, Zone* zone) |
30 : std::vector<T, zone_allocator<T> >(size, T(), zone_allocator<T>(zone)) { | 29 : std::vector<T, zone_allocator<T> >(size, T(), zone_allocator<T>(zone)) { |
31 } | 30 } |
32 | 31 |
33 // Constructs a new vector and fills it with {size} elements, each | 32 // Constructs a new vector and fills it with {size} elements, each |
34 // having the value {def}. | 33 // having the value {def}. |
35 ZoneVector(int size, T def, Zone* zone) | 34 ZoneVector(int size, T def, Zone* zone) |
36 : std::vector<T, zone_allocator<T> >(size, def, zone_allocator<T>(zone)) { | 35 : std::vector<T, zone_allocator<T> >(size, def, zone_allocator<T>(zone)) { |
37 } | 36 } |
38 }; | 37 }; |
39 | 38 |
40 | |
41 // A wrapper subclass std::deque to make it easy to construct one | 39 // A wrapper subclass std::deque to make it easy to construct one |
42 // that uses a zone allocator. | 40 // that uses a zone allocator. |
43 template <typename T> | 41 template <typename T> |
44 class ZoneDeque : public std::deque<T, zone_allocator<T> > { | 42 class ZoneDeque : public std::deque<T, zone_allocator<T> > { |
45 public: | 43 public: |
46 // Constructs an empty deque. | |
47 explicit ZoneDeque(Zone* zone) | 44 explicit ZoneDeque(Zone* zone) |
48 : std::deque<T, zone_allocator<T> >(zone_allocator<T>(zone)) {} | 45 : std::deque<T, zone_allocator<T> >(zone_allocator<T>(zone)) {} |
49 }; | 46 }; |
50 | 47 |
51 | |
52 // A wrapper subclass for std::queue to make it easy to construct one | 48 // A wrapper subclass for std::queue to make it easy to construct one |
53 // that uses a zone allocator. | 49 // that uses a zone allocator. |
54 template <typename T> | 50 template <typename T> |
55 class ZoneQueue : public std::queue<T, ZoneDeque<T>> { | 51 class ZoneQueue : public std::queue<T, std::deque<T, zone_allocator<T> > > { |
56 public: | 52 public: |
57 // Constructs an empty queue. | 53 // Constructs an empty queue. |
58 explicit ZoneQueue(Zone* zone) | 54 explicit ZoneQueue(Zone* zone) |
59 : std::queue<T, ZoneDeque<T>>(ZoneDeque<T>(zone)) {} | 55 : std::queue<T, std::deque<T, zone_allocator<T> > >( |
| 56 std::deque<T, zone_allocator<T> >(zone_allocator<T>(zone))) {} |
60 }; | 57 }; |
61 | 58 |
62 | |
63 // A wrapper subclass for std::stack to make it easy to construct one that uses | |
64 // a zone allocator. | |
65 template <typename T> | |
66 class ZoneStack : public std::stack<T, ZoneDeque<T>> { | |
67 public: | |
68 // Constructs an empty stack. | |
69 explicit ZoneStack(Zone* zone) | |
70 : std::stack<T, ZoneDeque<T>>(ZoneDeque<T>(zone)) {} | |
71 }; | |
72 | |
73 | |
74 // Typedefs to shorten commonly used vectors. | 59 // Typedefs to shorten commonly used vectors. |
75 typedef ZoneVector<bool> BoolVector; | 60 typedef ZoneVector<bool> BoolVector; |
76 typedef ZoneVector<int> IntVector; | 61 typedef ZoneVector<int> IntVector; |
77 | 62 } } // namespace v8::internal |
78 } // namespace internal | |
79 } // namespace v8 | |
80 | 63 |
81 #endif // V8_ZONE_CONTAINERS_H_ | 64 #endif // V8_ZONE_CONTAINERS_H_ |
OLD | NEW |