Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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_CONTAINERS_H_ |
| 6 | #define V8_ZONE_CONTAINERS_H_ |
| 7 | |
| 8 | #include <deque> |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 9 | #include <list> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10 | #include <queue> |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 11 | #include <stack> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12 | #include <vector> |
| 13 | |
| 14 | #include "src/zone-allocator.h" |
| 15 | |
| 16 | namespace v8 { |
| 17 | namespace internal { |
| 18 | |
| 19 | // A wrapper subclass for std::vector to make it easy to construct one |
| 20 | // that uses a zone allocator. |
| 21 | template <typename T> |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 22 | class ZoneVector : public std::vector<T, zone_allocator<T>> { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 23 | public: |
| 24 | // Constructs an empty vector. |
| 25 | explicit ZoneVector(Zone* zone) |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 26 | : 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 32 | |
| 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 Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 36 | : std::vector<T, zone_allocator<T>>(size, def, zone_allocator<T>(zone)) {} |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 37 | }; |
| 38 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 39 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 40 | // A wrapper subclass std::deque to make it easy to construct one |
| 41 | // that uses a zone allocator. |
| 42 | template <typename T> |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 43 | class ZoneDeque : public std::deque<T, zone_allocator<T>> { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 44 | public: |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 45 | // Constructs an empty deque. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 46 | explicit ZoneDeque(Zone* zone) |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 47 | : std::deque<T, zone_allocator<T>>(zone_allocator<T>(zone)) {} |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 48 | }; |
| 49 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 50 | |
| 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. |
| 55 | template <typename T> |
| 56 | class 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 64 | // A wrapper subclass for std::queue to make it easy to construct one |
| 65 | // that uses a zone allocator. |
| 66 | template <typename T> |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 67 | class ZoneQueue : public std::queue<T, ZoneDeque<T>> { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 68 | public: |
| 69 | // Constructs an empty queue. |
| 70 | explicit ZoneQueue(Zone* zone) |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 71 | : std::queue<T, ZoneDeque<T>>(ZoneDeque<T>(zone)) {} |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 72 | }; |
| 73 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 74 | |
| 75 | // A wrapper subclass for std::stack to make it easy to construct one that uses |
| 76 | // a zone allocator. |
| 77 | template <typename T> |
| 78 | class 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 86 | // Typedefs to shorten commonly used vectors. |
| 87 | typedef ZoneVector<bool> BoolVector; |
| 88 | typedef ZoneVector<int> IntVector; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 89 | |
| 90 | } // namespace internal |
| 91 | } // namespace v8 |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 92 | |
| 93 | #endif // V8_ZONE_CONTAINERS_H_ |