LLVM  6.0.0svn
AllocatorList.h
Go to the documentation of this file.
1 //===- llvm/ADT/AllocatorList.h - Custom allocator list ---------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_ADT_ALLOCATORLIST_H
11 #define LLVM_ADT_ALLOCATORLIST_H
12 
13 #include "llvm/ADT/ilist_node.h"
14 #include "llvm/ADT/iterator.h"
15 #include "llvm/ADT/simple_ilist.h"
16 #include "llvm/Support/Allocator.h"
17 #include <algorithm>
18 #include <cassert>
19 #include <cstddef>
20 #include <iterator>
21 #include <type_traits>
22 #include <utility>
23 
24 namespace llvm {
25 
26 /// A linked-list with a custom, local allocator.
27 ///
28 /// Expose a std::list-like interface that owns and uses a custom LLVM-style
29 /// allocator (e.g., BumpPtrAllocator), leveraging \a simple_ilist for the
30 /// implementation details.
31 ///
32 /// Because this list owns the allocator, calling \a splice() with a different
33 /// list isn't generally safe. As such, \a splice has been left out of the
34 /// interface entirely.
35 template <class T, class AllocatorT> class AllocatorList : AllocatorT {
36  struct Node : ilist_node<Node> {
37  Node(Node &&) = delete;
38  Node(const Node &) = delete;
39  Node &operator=(Node &&) = delete;
40  Node &operator=(const Node &) = delete;
41 
42  Node(T &&V) : V(std::move(V)) {}
43  Node(const T &V) : V(V) {}
44  template <class... Ts> Node(Ts &&... Vs) : V(std::forward<Ts>(Vs)...) {}
45  T V;
46  };
47 
49 
50  list_type List;
51 
52  AllocatorT &getAlloc() { return *this; }
53  const AllocatorT &getAlloc() const { return *this; }
54 
55  template <class... ArgTs> Node *create(ArgTs &&... Args) {
56  return new (getAlloc()) Node(std::forward<ArgTs>(Args)...);
57  }
58 
59  struct Cloner {
61 
62  Cloner(AllocatorList &AL) : AL(AL) {}
63 
64  Node *operator()(const Node &N) const { return AL.create(N.V); }
65  };
66 
67  struct Disposer {
69 
70  Disposer(AllocatorList &AL) : AL(AL) {}
71 
72  void operator()(Node *N) const {
73  N->~Node();
74  AL.getAlloc().Deallocate(N);
75  }
76  };
77 
78 public:
79  using value_type = T;
80  using pointer = T *;
81  using reference = T &;
82  using const_pointer = const T *;
83  using const_reference = const T &;
84  using size_type = typename list_type::size_type;
86 
87 private:
88  template <class ValueT, class IteratorBase>
89  class IteratorImpl
90  : public iterator_adaptor_base<IteratorImpl<ValueT, IteratorBase>,
91  IteratorBase,
92  std::bidirectional_iterator_tag, ValueT> {
93  template <class OtherValueT, class OtherIteratorBase>
94  friend class IteratorImpl;
95  friend AllocatorList;
96 
97  using base_type =
99  std::bidirectional_iterator_tag, ValueT>;
100 
101  public:
102  using value_type = ValueT;
103  using pointer = ValueT *;
104  using reference = ValueT &;
105 
106  IteratorImpl() = default;
107  IteratorImpl(const IteratorImpl &) = default;
108  IteratorImpl &operator=(const IteratorImpl &) = default;
109 
110  explicit IteratorImpl(const IteratorBase &I) : base_type(I) {}
111 
112  template <class OtherValueT, class OtherIteratorBase>
113  IteratorImpl(const IteratorImpl<OtherValueT, OtherIteratorBase> &X,
114  typename std::enable_if<std::is_convertible<
115  OtherIteratorBase, IteratorBase>::value>::type * = nullptr)
116  : base_type(X.wrapped()) {}
117 
118  ~IteratorImpl() = default;
119 
120  reference operator*() const { return base_type::wrapped()->V; }
121  pointer operator->() const { return &operator*(); }
122 
123  friend bool operator==(const IteratorImpl &L, const IteratorImpl &R) {
124  return L.wrapped() == R.wrapped();
125  }
126  friend bool operator!=(const IteratorImpl &L, const IteratorImpl &R) {
127  return !(L == R);
128  }
129  };
130 
131 public:
132  using iterator = IteratorImpl<T, typename list_type::iterator>;
133  using reverse_iterator =
134  IteratorImpl<T, typename list_type::reverse_iterator>;
135  using const_iterator =
136  IteratorImpl<const T, typename list_type::const_iterator>;
137  using const_reverse_iterator =
138  IteratorImpl<const T, typename list_type::const_reverse_iterator>;
139 
140  AllocatorList() = default;
142  : AllocatorT(std::move(X.getAlloc())), List(std::move(X.List)) {}
143 
145  List.cloneFrom(X.List, Cloner(*this), Disposer(*this));
146  }
147 
149  clear(); // Dispose of current nodes explicitly.
150  List = std::move(X.List);
151  getAlloc() = std::move(X.getAlloc());
152  return *this;
153  }
154 
156  List.cloneFrom(X.List, Cloner(*this), Disposer(*this));
157  return *this;
158  }
159 
161 
162  void swap(AllocatorList &RHS) {
163  List.swap(RHS.List);
164  std::swap(getAlloc(), RHS.getAlloc());
165  }
166 
167  bool empty() { return List.empty(); }
168  size_t size() { return List.size(); }
169 
170  iterator begin() { return iterator(List.begin()); }
171  iterator end() { return iterator(List.end()); }
172  const_iterator begin() const { return const_iterator(List.begin()); }
173  const_iterator end() const { return const_iterator(List.end()); }
177  return const_reverse_iterator(List.rbegin());
178  }
180  return const_reverse_iterator(List.rend());
181  }
182 
183  T &back() { return List.back().V; }
184  T &front() { return List.front().V; }
185  const T &back() const { return List.back().V; }
186  const T &front() const { return List.front().V; }
187 
188  template <class... Ts> iterator emplace(iterator I, Ts &&... Vs) {
189  return iterator(List.insert(I.wrapped(), *create(std::forward<Ts>(Vs)...)));
190  }
191 
193  return iterator(List.insert(I.wrapped(), *create(std::move(V))));
194  }
195  iterator insert(iterator I, const T &V) {
196  return iterator(List.insert(I.wrapped(), *create(V)));
197  }
198 
199  template <class Iterator>
200  void insert(iterator I, Iterator First, Iterator Last) {
201  for (; First != Last; ++First)
202  List.insert(I.wrapped(), *create(*First));
203  }
204 
206  return iterator(List.eraseAndDispose(I.wrapped(), Disposer(*this)));
207  }
208 
210  return iterator(
211  List.eraseAndDispose(First.wrapped(), Last.wrapped(), Disposer(*this)));
212  }
213 
214  void clear() { List.clearAndDispose(Disposer(*this)); }
215  void pop_back() { List.eraseAndDispose(--List.end(), Disposer(*this)); }
216  void pop_front() { List.eraseAndDispose(List.begin(), Disposer(*this)); }
217  void push_back(T &&V) { insert(end(), std::move(V)); }
218  void push_front(T &&V) { insert(begin(), std::move(V)); }
219  void push_back(const T &V) { insert(end(), V); }
220  void push_front(const T &V) { insert(begin(), V); }
221  template <class... Ts> void emplace_back(Ts &&... Vs) {
222  emplace(end(), std::forward<Ts>(Vs)...);
223  }
224  template <class... Ts> void emplace_front(Ts &&... Vs) {
225  emplace(begin(), std::forward<Ts>(Vs)...);
226  }
227 
228  /// Reset the underlying allocator.
229  ///
230  /// \pre \c empty()
231  void resetAlloc() {
232  assert(empty() && "Cannot reset allocator if not empty");
233  getAlloc().Reset();
234  }
235 };
236 
238 
239 } // end namespace llvm
240 
241 #endif // LLVM_ADT_ALLOCATORLIST_H
reverse_iterator rend()
void cloneFrom(const simple_ilist &L2, Cloner clone, Disposer dispose)
Clone another list.
Definition: simple_ilist.h:174
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
AllocatorList & operator=(AllocatorList &&X)
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
A linked-list with a custom, local allocator.
Definition: AllocatorList.h:35
void swap(simple_ilist &X)
Swap with another list in place using std::swap.
Definition: simple_ilist.h:157
void push_front(T &&V)
void emplace_front(Ts &&... Vs)
void push_front(const T &V)
const T & front() const
void insert(iterator I, Iterator First, Iterator Last)
void resetAlloc()
Reset the underlying allocator.
const_iterator end() const
const T & back() const
reference back()
Definition: simple_ilist.h:141
This file defines the MallocAllocator and BumpPtrAllocator interfaces.
void push_back(T &&V)
Definition: BitVector.h:920
iterator erase(iterator I)
AllocatorList()=default
const_iterator begin() const
void emplace_back(Ts &&... Vs)
APInt operator*(APInt a, uint64_t RHS)
Definition: APInt.h:2070
IteratorImpl< Token, typename list_type::reverse_iterator > reverse_iterator
#define T
const_reverse_iterator rend() const
AllocatorList & operator=(const AllocatorList &X)
const_reverse_iterator rbegin() const
IteratorImpl< Token, typename list_type::iterator > iterator
CRTP base class for adapting an iterator to a different type.
Definition: iterator.h:208
iterator eraseAndDispose(iterator I, Disposer dispose)
Remove a node by iterator and dispose of it.
Definition: simple_ilist.h:213
reverse_iterator rend()
Definition: simple_ilist.h:126
LLVM_NODISCARD size_type size() const
Calculate the size of the list in linear time.
Definition: simple_ilist.h:135
typename list_type::size_type size_type
Definition: AllocatorList.h:84
AllocatorList(AllocatorList &&X)
iterator erase(iterator First, iterator Last)
iterator insert(iterator I, T &&V)
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:923
iterator insert(iterator I, const T &V)
LLVM_NODISCARD bool empty() const
Check if the list is empty in constant time.
Definition: simple_ilist.h:132
void clearAndDispose(Disposer dispose)
Clear the list and dispose of the nodes.
Definition: simple_ilist.h:234
AllocatorList(const AllocatorList &X)
bool operator!=(uint64_t V1, const APInt &V2)
Definition: APInt.h:1948
void swap(AllocatorList &RHS)
#define I(x, y, z)
Definition: MD5.cpp:58
#define N
IteratorImpl< const Token, typename list_type::const_reverse_iterator > const_reverse_iterator
reference front()
Definition: simple_ilist.h:139
iterator insert(iterator I, reference Node)
Insert a node by reference; never copies.
Definition: simple_ilist.h:160
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
iterator emplace(iterator I, Ts &&... Vs)
IteratorImpl< const Token, typename list_type::const_iterator > const_iterator
reverse_iterator rbegin()
bool operator==(uint64_t V1, const APInt &V2)
Definition: APInt.h:1946
typename list_type::difference_type difference_type
Definition: AllocatorList.h:85
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
reverse_iterator rbegin()
Definition: simple_ilist.h:122
void push_back(const T &V)