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 | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 10 | #include <map> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11 | #include <queue> |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12 | #include <set> |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 13 | #include <stack> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14 | #include <vector> |
| 15 | |
| 16 | #include "src/zone-allocator.h" |
| 17 | |
| 18 | namespace v8 { |
| 19 | namespace internal { |
| 20 | |
| 21 | // A wrapper subclass for std::vector to make it easy to construct one |
| 22 | // that uses a zone allocator. |
| 23 | template <typename T> |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 24 | class ZoneVector : public std::vector<T, zone_allocator<T>> { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 25 | public: |
| 26 | // Constructs an empty vector. |
| 27 | explicit ZoneVector(Zone* zone) |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 28 | : 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 32 | ZoneVector(size_t size, Zone* zone) |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 33 | : std::vector<T, zone_allocator<T>>(size, T(), zone_allocator<T>(zone)) {} |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 34 | |
| 35 | // Constructs a new vector and fills it with {size} elements, each |
| 36 | // having the value {def}. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 37 | ZoneVector(size_t size, T def, Zone* zone) |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 38 | : std::vector<T, zone_allocator<T>>(size, def, zone_allocator<T>(zone)) {} |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 41 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 42 | // A wrapper subclass std::deque to make it easy to construct one |
| 43 | // that uses a zone allocator. |
| 44 | template <typename T> |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 45 | class ZoneDeque : public std::deque<T, zone_allocator<T>> { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 46 | public: |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 47 | // Constructs an empty deque. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 48 | explicit ZoneDeque(Zone* zone) |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 49 | : std::deque<T, zone_allocator<T>>(zone_allocator<T>(zone)) {} |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 52 | |
| 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. |
| 57 | template <typename T> |
| 58 | class 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 66 | // A wrapper subclass std::priority_queue to make it easy to construct one |
| 67 | // that uses a zone allocator. |
| 68 | template <typename T, typename Compare = std::less<T>> |
| 69 | class 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 79 | // A wrapper subclass for std::queue to make it easy to construct one |
| 80 | // that uses a zone allocator. |
| 81 | template <typename T> |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 82 | class ZoneQueue : public std::queue<T, ZoneDeque<T>> { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 83 | public: |
| 84 | // Constructs an empty queue. |
| 85 | explicit ZoneQueue(Zone* zone) |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 86 | : std::queue<T, ZoneDeque<T>>(ZoneDeque<T>(zone)) {} |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 87 | }; |
| 88 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 89 | |
| 90 | // A wrapper subclass for std::stack to make it easy to construct one that uses |
| 91 | // a zone allocator. |
| 92 | template <typename T> |
| 93 | class 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 101 | // A wrapper subclass for std::set to make it easy to construct one that uses |
| 102 | // a zone allocator. |
| 103 | template <typename K, typename Compare = std::less<K>> |
| 104 | class 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. |
| 115 | template <typename K, typename V, typename Compare = std::less<K>> |
| 116 | class 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 126 | // Typedefs to shorten commonly used vectors. |
| 127 | typedef ZoneVector<bool> BoolVector; |
| 128 | typedef ZoneVector<int> IntVector; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 129 | |
| 130 | } // namespace internal |
| 131 | } // namespace v8 |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 132 | |
| 133 | #endif // V8_ZONE_CONTAINERS_H_ |