Home | History | Annotate | Download | only in zone
      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_SRC_ZONE_ZONE_CONTAINERS_H_
      6 #define V8_SRC_ZONE_ZONE_CONTAINERS_H_
      7 
      8 #include <deque>
      9 #include <list>
     10 #include <map>
     11 #include <queue>
     12 #include <set>
     13 #include <stack>
     14 #include <vector>
     15 
     16 #include "src/zone/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>
     24 class ZoneVector : public std::vector<T, zone_allocator<T>> {
     25  public:
     26   // Constructs an empty vector.
     27   explicit ZoneVector(Zone* zone)
     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.
     32   ZoneVector(size_t size, Zone* zone)
     33       : std::vector<T, zone_allocator<T>>(size, T(), zone_allocator<T>(zone)) {}
     34 
     35   // Constructs a new vector and fills it with {size} elements, each
     36   // having the value {def}.
     37   ZoneVector(size_t size, T def, Zone* zone)
     38       : std::vector<T, zone_allocator<T>>(size, def, zone_allocator<T>(zone)) {}
     39 
     40   // Constructs a new vector and fills it with the contents of the range
     41   // [first, last).
     42   template <class InputIt>
     43   ZoneVector(InputIt first, InputIt last, Zone* zone)
     44       : std::vector<T, zone_allocator<T>>(first, last,
     45                                           zone_allocator<T>(zone)) {}
     46 };
     47 
     48 // A wrapper subclass std::deque to make it easy to construct one
     49 // that uses a zone allocator.
     50 template <typename T>
     51 class ZoneDeque : public std::deque<T, zone_allocator<T>> {
     52  public:
     53   // Constructs an empty deque.
     54   explicit ZoneDeque(Zone* zone)
     55       : std::deque<T, zone_allocator<T>>(zone_allocator<T>(zone)) {}
     56 };
     57 
     58 // A wrapper subclass std::list to make it easy to construct one
     59 // that uses a zone allocator.
     60 // TODO(mstarzinger): This should be renamed to ZoneList once we got rid of our
     61 // own home-grown ZoneList that actually is a ZoneVector.
     62 template <typename T>
     63 class ZoneLinkedList : public std::list<T, zone_allocator<T>> {
     64  public:
     65   // Constructs an empty list.
     66   explicit ZoneLinkedList(Zone* zone)
     67       : std::list<T, zone_allocator<T>>(zone_allocator<T>(zone)) {}
     68 };
     69 
     70 // A wrapper subclass std::priority_queue to make it easy to construct one
     71 // that uses a zone allocator.
     72 template <typename T, typename Compare = std::less<T>>
     73 class ZonePriorityQueue
     74     : public std::priority_queue<T, ZoneVector<T>, Compare> {
     75  public:
     76   // Constructs an empty list.
     77   explicit ZonePriorityQueue(Zone* zone)
     78       : std::priority_queue<T, ZoneVector<T>, Compare>(Compare(),
     79                                                        ZoneVector<T>(zone)) {}
     80 };
     81 
     82 // A wrapper subclass for std::queue to make it easy to construct one
     83 // that uses a zone allocator.
     84 template <typename T>
     85 class ZoneQueue : public std::queue<T, ZoneDeque<T>> {
     86  public:
     87   // Constructs an empty queue.
     88   explicit ZoneQueue(Zone* zone)
     89       : std::queue<T, ZoneDeque<T>>(ZoneDeque<T>(zone)) {}
     90 };
     91 
     92 // A wrapper subclass for std::stack to make it easy to construct one that uses
     93 // a zone allocator.
     94 template <typename T>
     95 class ZoneStack : public std::stack<T, ZoneDeque<T>> {
     96  public:
     97   // Constructs an empty stack.
     98   explicit ZoneStack(Zone* zone)
     99       : std::stack<T, ZoneDeque<T>>(ZoneDeque<T>(zone)) {}
    100 };
    101 
    102 // A wrapper subclass for std::set to make it easy to construct one that uses
    103 // a zone allocator.
    104 template <typename K, typename Compare = std::less<K>>
    105 class ZoneSet : public std::set<K, Compare, zone_allocator<K>> {
    106  public:
    107   // Constructs an empty set.
    108   explicit ZoneSet(Zone* zone)
    109       : std::set<K, Compare, zone_allocator<K>>(Compare(),
    110                                                 zone_allocator<K>(zone)) {}
    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 // A wrapper subclass for std::multimap to make it easy to construct one that
    126 // uses a zone allocator.
    127 template <typename K, typename V, typename Compare = std::less<K>>
    128 class ZoneMultimap
    129     : public std::multimap<K, V, Compare,
    130                            zone_allocator<std::pair<const K, V>>> {
    131  public:
    132   // Constructs an empty multimap.
    133   explicit ZoneMultimap(Zone* zone)
    134       : std::multimap<K, V, Compare, zone_allocator<std::pair<const K, V>>>(
    135             Compare(), zone_allocator<std::pair<const K, V>>(zone)) {}
    136 };
    137 
    138 // Typedefs to shorten commonly used vectors.
    139 typedef ZoneVector<bool> BoolVector;
    140 typedef ZoneVector<int> IntVector;
    141 
    142 }  // namespace internal
    143 }  // namespace v8
    144 
    145 #endif  // V8_SRC_ZONE_ZONE_CONTAINERS_H_
    146