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 
     41 // A wrapper subclass std::deque to make it easy to construct one
     42 // that uses a zone allocator.
     43 template <typename T>
     44 class ZoneDeque : public std::deque<T, zone_allocator<T>> {
     45  public:
     46   // Constructs an empty deque.
     47   explicit ZoneDeque(Zone* zone)
     48       : std::deque<T, zone_allocator<T>>(zone_allocator<T>(zone)) {}
     49 };
     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 // A wrapper subclass std::priority_queue to make it easy to construct one
     64 // that uses a zone allocator.
     65 template <typename T, typename Compare = std::less<T>>
     66 class ZonePriorityQueue
     67     : public std::priority_queue<T, ZoneVector<T>, Compare> {
     68  public:
     69   // Constructs an empty list.
     70   explicit ZonePriorityQueue(Zone* zone)
     71       : std::priority_queue<T, ZoneVector<T>, Compare>(Compare(),
     72                                                        ZoneVector<T>(zone)) {}
     73 };
     74 
     75 // A wrapper subclass for std::queue to make it easy to construct one
     76 // that uses a zone allocator.
     77 template <typename T>
     78 class ZoneQueue : public std::queue<T, ZoneDeque<T>> {
     79  public:
     80   // Constructs an empty queue.
     81   explicit ZoneQueue(Zone* zone)
     82       : std::queue<T, ZoneDeque<T>>(ZoneDeque<T>(zone)) {}
     83 };
     84 
     85 // A wrapper subclass for std::stack to make it easy to construct one that uses
     86 // a zone allocator.
     87 template <typename T>
     88 class ZoneStack : public std::stack<T, ZoneDeque<T>> {
     89  public:
     90   // Constructs an empty stack.
     91   explicit ZoneStack(Zone* zone)
     92       : std::stack<T, ZoneDeque<T>>(ZoneDeque<T>(zone)) {}
     93 };
     94 
     95 // A wrapper subclass for std::set to make it easy to construct one that uses
     96 // a zone allocator.
     97 template <typename K, typename Compare = std::less<K>>
     98 class ZoneSet : public std::set<K, Compare, zone_allocator<K>> {
     99  public:
    100   // Constructs an empty set.
    101   explicit ZoneSet(Zone* zone)
    102       : std::set<K, Compare, zone_allocator<K>>(Compare(),
    103                                                 zone_allocator<K>(zone)) {}
    104 };
    105 
    106 // A wrapper subclass for std::map to make it easy to construct one that uses
    107 // a zone allocator.
    108 template <typename K, typename V, typename Compare = std::less<K>>
    109 class ZoneMap
    110     : public std::map<K, V, Compare, zone_allocator<std::pair<const K, V>>> {
    111  public:
    112   // Constructs an empty map.
    113   explicit ZoneMap(Zone* zone)
    114       : std::map<K, V, Compare, zone_allocator<std::pair<const K, V>>>(
    115             Compare(), zone_allocator<std::pair<const K, V>>(zone)) {}
    116 };
    117 
    118 // A wrapper subclass for std::multimap to make it easy to construct one that
    119 // uses a zone allocator.
    120 template <typename K, typename V, typename Compare = std::less<K>>
    121 class ZoneMultimap
    122     : public std::multimap<K, V, Compare,
    123                            zone_allocator<std::pair<const K, V>>> {
    124  public:
    125   // Constructs an empty multimap.
    126   explicit ZoneMultimap(Zone* zone)
    127       : std::multimap<K, V, Compare, zone_allocator<std::pair<const K, V>>>(
    128             Compare(), zone_allocator<std::pair<const K, V>>(zone)) {}
    129 };
    130 
    131 // Typedefs to shorten commonly used vectors.
    132 typedef ZoneVector<bool> BoolVector;
    133 typedef ZoneVector<int> IntVector;
    134 
    135 }  // namespace internal
    136 }  // namespace v8
    137 
    138 #endif  // V8_SRC_ZONE_ZONE_CONTAINERS_H_
    139