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_ZONE_CONTAINERS_H_ 6 #define V8_ZONE_ZONE_CONTAINERS_H_ 7 8 #include <deque> 9 #include <forward_list> 10 #include <list> 11 #include <map> 12 #include <queue> 13 #include <set> 14 #include <stack> 15 #include <unordered_map> 16 #include <unordered_set> 17 #include <vector> 18 19 #include "src/base/functional.h" 20 #include "src/zone/zone-allocator.h" 21 22 namespace v8 { 23 namespace internal { 24 25 // A wrapper subclass for std::vector to make it easy to construct one 26 // that uses a zone allocator. 27 template <typename T> 28 class ZoneVector : public std::vector<T, ZoneAllocator<T>> { 29 public: 30 // Constructs an empty vector. ZoneVector(Zone * zone)31 explicit ZoneVector(Zone* zone) 32 : std::vector<T, ZoneAllocator<T>>(ZoneAllocator<T>(zone)) {} 33 34 // Constructs a new vector and fills it with {size} elements, each 35 // constructed via the default constructor. ZoneVector(size_t size,Zone * zone)36 ZoneVector(size_t size, Zone* zone) 37 : std::vector<T, ZoneAllocator<T>>(size, T(), ZoneAllocator<T>(zone)) {} 38 39 // Constructs a new vector and fills it with {size} elements, each 40 // having the value {def}. ZoneVector(size_t size,T def,Zone * zone)41 ZoneVector(size_t size, T def, Zone* zone) 42 : std::vector<T, ZoneAllocator<T>>(size, def, ZoneAllocator<T>(zone)) {} 43 44 // Constructs a new vector and fills it with the contents of the given 45 // initializer list. ZoneVector(std::initializer_list<T> list,Zone * zone)46 ZoneVector(std::initializer_list<T> list, Zone* zone) 47 : std::vector<T, ZoneAllocator<T>>(list, ZoneAllocator<T>(zone)) {} 48 49 // Constructs a new vector and fills it with the contents of the range 50 // [first, last). 51 template <class InputIt> ZoneVector(InputIt first,InputIt last,Zone * zone)52 ZoneVector(InputIt first, InputIt last, Zone* zone) 53 : std::vector<T, ZoneAllocator<T>>(first, last, ZoneAllocator<T>(zone)) {} 54 }; 55 56 // A wrapper subclass for std::deque to make it easy to construct one 57 // that uses a zone allocator. 58 template <typename T> 59 class ZoneDeque : public std::deque<T, RecyclingZoneAllocator<T>> { 60 public: 61 // Constructs an empty deque. ZoneDeque(Zone * zone)62 explicit ZoneDeque(Zone* zone) 63 : std::deque<T, RecyclingZoneAllocator<T>>( 64 RecyclingZoneAllocator<T>(zone)) {} 65 }; 66 67 // A wrapper subclass for std::list to make it easy to construct one 68 // that uses a zone allocator. 69 // TODO(mstarzinger): This should be renamed to ZoneList once we got rid of our 70 // own home-grown ZoneList that actually is a ZoneVector. 71 template <typename T> 72 class ZoneLinkedList : public std::list<T, ZoneAllocator<T>> { 73 public: 74 // Constructs an empty list. ZoneLinkedList(Zone * zone)75 explicit ZoneLinkedList(Zone* zone) 76 : std::list<T, ZoneAllocator<T>>(ZoneAllocator<T>(zone)) {} 77 }; 78 79 // A wrapper subclass for std::forward_list to make it easy to construct one 80 // that uses a zone allocator. 81 template <typename T> 82 class ZoneForwardList : public std::forward_list<T, ZoneAllocator<T>> { 83 public: 84 // Constructs an empty list. ZoneForwardList(Zone * zone)85 explicit ZoneForwardList(Zone* zone) 86 : std::forward_list<T, ZoneAllocator<T>>(ZoneAllocator<T>(zone)) {} 87 }; 88 89 // A wrapper subclass for std::priority_queue to make it easy to construct one 90 // that uses a zone allocator. 91 template <typename T, typename Compare = std::less<T>> 92 class ZonePriorityQueue 93 : public std::priority_queue<T, ZoneVector<T>, Compare> { 94 public: 95 // Constructs an empty list. ZonePriorityQueue(Zone * zone)96 explicit ZonePriorityQueue(Zone* zone) 97 : std::priority_queue<T, ZoneVector<T>, Compare>(Compare(), 98 ZoneVector<T>(zone)) {} 99 }; 100 101 // A wrapper subclass for std::queue to make it easy to construct one 102 // that uses a zone allocator. 103 template <typename T> 104 class ZoneQueue : public std::queue<T, ZoneDeque<T>> { 105 public: 106 // Constructs an empty queue. ZoneQueue(Zone * zone)107 explicit ZoneQueue(Zone* zone) 108 : std::queue<T, ZoneDeque<T>>(ZoneDeque<T>(zone)) {} 109 }; 110 111 // A wrapper subclass for std::stack to make it easy to construct one that uses 112 // a zone allocator. 113 template <typename T> 114 class ZoneStack : public std::stack<T, ZoneDeque<T>> { 115 public: 116 // Constructs an empty stack. ZoneStack(Zone * zone)117 explicit ZoneStack(Zone* zone) 118 : std::stack<T, ZoneDeque<T>>(ZoneDeque<T>(zone)) {} 119 }; 120 121 // A wrapper subclass for std::set to make it easy to construct one that uses 122 // a zone allocator. 123 template <typename K, typename Compare = std::less<K>> 124 class ZoneSet : public std::set<K, Compare, ZoneAllocator<K>> { 125 public: 126 // Constructs an empty set. ZoneSet(Zone * zone)127 explicit ZoneSet(Zone* zone) 128 : std::set<K, Compare, ZoneAllocator<K>>(Compare(), 129 ZoneAllocator<K>(zone)) {} 130 }; 131 132 // A wrapper subclass for std::multiset to make it easy to construct one that 133 // uses a zone allocator. 134 template <typename K, typename Compare = std::less<K>> 135 class ZoneMultiset : public std::multiset<K, Compare, ZoneAllocator<K>> { 136 public: 137 // Constructs an empty set. ZoneMultiset(Zone * zone)138 explicit ZoneMultiset(Zone* zone) 139 : std::multiset<K, Compare, ZoneAllocator<K>>(Compare(), 140 ZoneAllocator<K>(zone)) {} 141 }; 142 143 // A wrapper subclass for std::map to make it easy to construct one that uses 144 // a zone allocator. 145 template <typename K, typename V, typename Compare = std::less<K>> 146 class ZoneMap 147 : public std::map<K, V, Compare, ZoneAllocator<std::pair<const K, V>>> { 148 public: 149 // Constructs an empty map. ZoneMap(Zone * zone)150 explicit ZoneMap(Zone* zone) 151 : std::map<K, V, Compare, ZoneAllocator<std::pair<const K, V>>>( 152 Compare(), ZoneAllocator<std::pair<const K, V>>(zone)) {} 153 }; 154 155 // A wrapper subclass for std::unordered_map to make it easy to construct one 156 // that uses a zone allocator. 157 template <typename K, typename V, typename Hash = base::hash<K>, 158 typename KeyEqual = std::equal_to<K>> 159 class ZoneUnorderedMap 160 : public std::unordered_map<K, V, Hash, KeyEqual, 161 ZoneAllocator<std::pair<const K, V>>> { 162 public: 163 // Constructs an empty map. ZoneUnorderedMap(Zone * zone)164 explicit ZoneUnorderedMap(Zone* zone) 165 : std::unordered_map<K, V, Hash, KeyEqual, 166 ZoneAllocator<std::pair<const K, V>>>( 167 100, Hash(), KeyEqual(), 168 ZoneAllocator<std::pair<const K, V>>(zone)) {} 169 }; 170 171 // A wrapper subclass for std::unordered_set to make it easy to construct one 172 // that uses a zone allocator. 173 template <typename K, typename Hash = base::hash<K>, 174 typename KeyEqual = std::equal_to<K>> 175 class ZoneUnorderedSet 176 : public std::unordered_set<K, Hash, KeyEqual, ZoneAllocator<K>> { 177 public: 178 // Constructs an empty map. ZoneUnorderedSet(Zone * zone)179 explicit ZoneUnorderedSet(Zone* zone) 180 : std::unordered_set<K, Hash, KeyEqual, ZoneAllocator<K>>( 181 100, Hash(), KeyEqual(), ZoneAllocator<K>(zone)) {} 182 }; 183 184 // A wrapper subclass for std::multimap to make it easy to construct one that 185 // uses a zone allocator. 186 template <typename K, typename V, typename Compare = std::less<K>> 187 class ZoneMultimap 188 : public std::multimap<K, V, Compare, 189 ZoneAllocator<std::pair<const K, V>>> { 190 public: 191 // Constructs an empty multimap. ZoneMultimap(Zone * zone)192 explicit ZoneMultimap(Zone* zone) 193 : std::multimap<K, V, Compare, ZoneAllocator<std::pair<const K, V>>>( 194 Compare(), ZoneAllocator<std::pair<const K, V>>(zone)) {} 195 }; 196 197 // Typedefs to shorten commonly used vectors. 198 typedef ZoneVector<bool> BoolVector; 199 typedef ZoneVector<int> IntVector; 200 201 } // namespace internal 202 } // namespace v8 203 204 #endif // V8_ZONE_ZONE_CONTAINERS_H_ 205