RecyclingAllocator.h revision 4f4b348437ff607b53c66ec7da8a011b637ebc99
1//==- llvm/Support/RecyclingAllocator.h - Recycling Allocator ----*- 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// This file defines the RecyclingAllocator class.  See the doxygen comment for
11// RecyclingAllocator for more details on the implementation.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_SUPPORT_RECYCLINGALLOCATOR_H
16#define LLVM_SUPPORT_RECYCLINGALLOCATOR_H
17
18#include "llvm/Support/Recycler.h"
19
20namespace llvm {
21
22/// RecyclingAllocator - This class wraps an Allocator, adding the
23/// functionality of recycling deleted objects.
24///
25template<class AllocatorType, class T, class LargestT = T>
26class RecyclingAllocator {
27private:
28  /// Base - Implementation details.
29  ///
30  Recycler<T, LargestT> Base;
31
32  /// Allocator - The wrapped allocator.
33  ///
34  AllocatorType Allocator;
35
36public:
37  ~RecyclingAllocator() { Base.clear(Allocator); }
38
39  /// Allocate - Return a pointer to storage for an object of type
40  /// SubClass. The storage may be either newly allocated or recycled.
41  ///
42  template<class SubClass>
43  SubClass *Allocate() { return Base.Allocate<SubClass>(Allocator); }
44
45  T *Allocate() { return Base.Allocate(Allocator); }
46
47  /// Deallocate - Release storage for the pointed-to object. The
48  /// storage will be kept track of and may be recycled.
49  ///
50  template<class SubClass>
51  void Deallocate(SubClass* E) { return Base.Deallocate(Allocator, E); }
52
53  void PrintStats() { Base.PrintStats(); }
54};
55
56}
57
58#endif
59