RecyclingAllocator.h revision 205407
1193323Sed//==- llvm/Support/RecyclingAllocator.h - Recycling Allocator ----*- C++ -*-==//
2193323Sed//
3193323Sed//                     The LLVM Compiler Infrastructure
4193323Sed//
5193323Sed// This file is distributed under the University of Illinois Open Source
6193323Sed// License. See LICENSE.TXT for details.
7193323Sed//
8193323Sed//===----------------------------------------------------------------------===//
9193323Sed//
10193323Sed// This file defines the RecyclingAllocator class.  See the doxygen comment for
11193323Sed// RecyclingAllocator for more details on the implementation.
12193323Sed//
13193323Sed//===----------------------------------------------------------------------===//
14193323Sed
15193323Sed#ifndef LLVM_SUPPORT_RECYCLINGALLOCATOR_H
16193323Sed#define LLVM_SUPPORT_RECYCLINGALLOCATOR_H
17193323Sed
18193323Sed#include "llvm/Support/Recycler.h"
19193323Sed
20193323Sednamespace llvm {
21193323Sed
22193323Sed/// RecyclingAllocator - This class wraps an Allocator, adding the
23193323Sed/// functionality of recycling deleted objects.
24193323Sed///
25193323Sedtemplate<class AllocatorType, class T,
26193323Sed         size_t Size = sizeof(T), size_t Align = AlignOf<T>::Alignment>
27193323Sedclass RecyclingAllocator {
28193323Sedprivate:
29193323Sed  /// Base - Implementation details.
30193323Sed  ///
31193323Sed  Recycler<T, Size, Align> Base;
32193323Sed
33193323Sed  /// Allocator - The wrapped allocator.
34193323Sed  ///
35193323Sed  AllocatorType Allocator;
36193323Sed
37193323Sedpublic:
38193323Sed  ~RecyclingAllocator() { Base.clear(Allocator); }
39193323Sed
40193323Sed  /// Allocate - Return a pointer to storage for an object of type
41193323Sed  /// SubClass. The storage may be either newly allocated or recycled.
42193323Sed  ///
43193323Sed  template<class SubClass>
44198953Srdivacky  SubClass *Allocate() { return Base.template Allocate<SubClass>(Allocator); }
45193323Sed
46193323Sed  T *Allocate() { return Base.Allocate(Allocator); }
47193323Sed
48193323Sed  /// Deallocate - Release storage for the pointed-to object. The
49193323Sed  /// storage will be kept track of and may be recycled.
50193323Sed  ///
51193323Sed  template<class SubClass>
52193323Sed  void Deallocate(SubClass* E) { return Base.Deallocate(Allocator, E); }
53193323Sed
54193323Sed  void PrintStats() { Base.PrintStats(); }
55193323Sed};
56193323Sed
57193323Sed}
58193323Sed
59205407Srdivackytemplate<class AllocatorType, class T, size_t Size, size_t Align>
60205407Srdivackyinline void *operator new(size_t,
61205407Srdivacky                          llvm::RecyclingAllocator<AllocatorType,
62205407Srdivacky                                                   T, Size, Align> &Allocator) {
63205407Srdivacky  return Allocator.Allocate();
64205407Srdivacky}
65205407Srdivacky
66193323Sed#endif
67