zone-containers.h 4.12 KB
Newer Older
1
// Copyright 2014 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4 5 6 7

#ifndef V8_ZONE_CONTAINERS_H_
#define V8_ZONE_CONTAINERS_H_

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 19 20

namespace v8 {
namespace internal {

21 22 23
// A wrapper subclass for std::vector to make it easy to construct one
// that uses a zone allocator.
template <typename T>
24
class ZoneVector : public std::vector<T, zone_allocator<T>> {
25 26 27
 public:
  // Constructs an empty vector.
  explicit ZoneVector(Zone* zone)
28
      : std::vector<T, zone_allocator<T>>(zone_allocator<T>(zone)) {}
29

30 31
  // Constructs a new vector and fills it with {size} elements, each
  // 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 36
  // Constructs a new vector and fills it with {size} elements, each
  // 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 43 44
// A wrapper subclass std::deque to make it easy to construct one
// that uses a zone allocator.
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 50 51 52 53 54 55 56 57 58 59 60 61 62
      : std::deque<T, zone_allocator<T>>(zone_allocator<T>(zone)) {}
};


// A wrapper subclass std::list to make it easy to construct one
// that uses a zone allocator.
// TODO(mstarzinger): This should be renamed to ZoneList once we got rid of our
// own home-grown ZoneList that actually is a ZoneVector.
template <typename T>
class ZoneLinkedList : public std::list<T, zone_allocator<T>> {
 public:
  // Constructs an empty list.
  explicit ZoneLinkedList(Zone* zone)
      : std::list<T, zone_allocator<T>>(zone_allocator<T>(zone)) {}
63 64
};

65

66 67 68
// A wrapper subclass std::priority_queue to make it easy to construct one
// that uses a zone allocator.
template <typename T, typename Compare = std::less<T>>
69 70
class ZonePriorityQueue
    : public std::priority_queue<T, ZoneVector<T>, Compare> {
71 72 73
 public:
  // Constructs an empty list.
  explicit ZonePriorityQueue(Zone* zone)
74 75
      : std::priority_queue<T, ZoneVector<T>, Compare>(Compare(),
                                                       ZoneVector<T>(zone)) {}
76 77 78
};


79 80 81
// A wrapper subclass for std::queue to make it easy to construct one
// that uses a zone allocator.
template <typename T>
82
class ZoneQueue : public std::queue<T, ZoneDeque<T>> {
83 84 85
 public:
  // Constructs an empty queue.
  explicit ZoneQueue(Zone* zone)
86
      : std::queue<T, ZoneDeque<T>>(ZoneDeque<T>(zone)) {}
87 88
};

89 90 91 92 93 94 95 96 97 98 99 100

// A wrapper subclass for std::stack to make it easy to construct one that uses
// a zone allocator.
template <typename T>
class ZoneStack : public std::stack<T, ZoneDeque<T>> {
 public:
  // Constructs an empty stack.
  explicit ZoneStack(Zone* zone)
      : std::stack<T, ZoneDeque<T>>(ZoneDeque<T>(zone)) {}
};


101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
// A wrapper subclass for std::set to make it easy to construct one that uses
// a zone allocator.
template <typename K, typename Compare = std::less<K>>
class ZoneSet : public std::set<K, Compare, zone_allocator<K>> {
 public:
  // Constructs an empty set.
  explicit ZoneSet(Zone* zone)
      : std::set<K, Compare, zone_allocator<K>>(Compare(),
                                                zone_allocator<K>(zone)) {}
};


// A wrapper subclass for std::map to make it easy to construct one that uses
// a zone allocator.
template <typename K, typename V, typename Compare = std::less<K>>
class ZoneMap
117
    : public std::map<K, V, Compare, zone_allocator<std::pair<const K, V>>> {
118 119 120
 public:
  // Constructs an empty map.
  explicit ZoneMap(Zone* zone)
121 122
      : std::map<K, V, Compare, zone_allocator<std::pair<const K, V>>>(
            Compare(), zone_allocator<std::pair<const K, V>>(zone)) {}
123 124 125
};


126 127 128
// Typedefs to shorten commonly used vectors.
typedef ZoneVector<bool> BoolVector;
typedef ZoneVector<int> IntVector;
129 130 131

}  // namespace internal
}  // namespace v8
132 133

#endif  // V8_ZONE_CONTAINERS_H_