zone-containers.h 2.74 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 <queue>
11
#include <stack>
12
#include <vector>
13

14
#include "src/zone-allocator.h"
15 16 17 18

namespace v8 {
namespace internal {

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

28 29
  // Constructs a new vector and fills it with {size} elements, each
  // constructed via the default constructor.
30
  ZoneVector(size_t size, Zone* zone)
31
      : std::vector<T, zone_allocator<T>>(size, T(), zone_allocator<T>(zone)) {}
32

33 34
  // Constructs a new vector and fills it with {size} elements, each
  // having the value {def}.
35
  ZoneVector(size_t size, T def, Zone* zone)
36
      : std::vector<T, zone_allocator<T>>(size, def, zone_allocator<T>(zone)) {}
37 38
};

39

40 41 42
// A wrapper subclass std::deque to make it easy to construct one
// that uses a zone allocator.
template <typename T>
43
class ZoneDeque : public std::deque<T, zone_allocator<T>> {
44
 public:
45
  // Constructs an empty deque.
46
  explicit ZoneDeque(Zone* zone)
47 48 49 50 51 52 53 54 55 56 57 58 59 60
      : 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)) {}
61 62
};

63

64 65 66
// A wrapper subclass for std::queue to make it easy to construct one
// that uses a zone allocator.
template <typename T>
67
class ZoneQueue : public std::queue<T, ZoneDeque<T>> {
68 69 70
 public:
  // Constructs an empty queue.
  explicit ZoneQueue(Zone* zone)
71
      : std::queue<T, ZoneDeque<T>>(ZoneDeque<T>(zone)) {}
72 73
};

74 75 76 77 78 79 80 81 82 83 84 85

// 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)) {}
};


86 87 88
// Typedefs to shorten commonly used vectors.
typedef ZoneVector<bool> BoolVector;
typedef ZoneVector<int> IntVector;
89 90 91

}  // namespace internal
}  // namespace v8
92 93

#endif  // V8_ZONE_CONTAINERS_H_