Home | History | Annotate | Download | only in src
      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>
      9 #include <list>
     10 #include <map>
     11 #include <queue>
     12 #include <set>
     13 #include <stack>
     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>
     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 
     42 // A wrapper subclass std::deque to make it easy to construct one
     43 // that uses a zone allocator.
     44 template <typename T>
     45 class ZoneDeque : public std::deque<T, zone_allocator<T>> {
     46  public:
     47   // Constructs an empty deque.
     48   explicit ZoneDeque(Zone* zone)
     49       : std::deque<T, zone_allocator<T>>(zone_allocator<T>(zone)) {}
     50 };
     51 
     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 
     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 
     79 // A wrapper subclass for std::queue to make it easy to construct one
     80 // that uses a zone allocator.
     81 template <typename T>
     82 class ZoneQueue : public std::queue<T, ZoneDeque<T>> {
     83  public:
     84   // Constructs an empty queue.
     85   explicit ZoneQueue(Zone* zone)
     86       : std::queue<T, ZoneDeque<T>>(ZoneDeque<T>(zone)) {}
     87 };
     88 
     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 
    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 
    126 // Typedefs to shorten commonly used vectors.
    127 typedef ZoneVector<bool> BoolVector;
    128 typedef ZoneVector<int> IntVector;
    129 
    130 }  // namespace internal
    131 }  // namespace v8
    132 
    133 #endif  // V8_ZONE_CONTAINERS_H_
    134