a338c0eb6397c272e9fa723dc70318eef5e2b977
[oota-llvm.git] / include / llvm / Support / RecyclingAllocator.h
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 <cassert>
19 #include "llvm/Support/Recycler.h"
20 #include "llvm/ADT/STLExtras.h"
21
22 namespace llvm {
23
24 /// RecyclingAllocator - This class wraps an Allocator, adding the
25 /// functionality of recycling deleted objects.
26 ///
27 template<class AllocatorType, class T, class LargestT = T>
28 class RecyclingAllocator {
29 private:
30   /// Base - Implementation details.
31   ///
32   Recycler<T, LargestT> Base;
33
34   /// Allocator - The wrapped allocator.
35   ///
36   AllocatorType Allocator;
37
38 public:
39   ~RecyclingAllocator() { Base.clear(Allocator); }
40
41   /// Allocate - Return a pointer to storage for an object of type
42   /// SubClass. The storage may be either newly allocated or recycled.
43   ///
44   template<class SubClass>
45   SubClass *Allocate() { return Base.Allocate<SubClass>(Allocator); }
46
47   T *Allocate() { return Base.Allocate(Allocator); }
48
49   /// Deallocate - Release storage for the pointed-to object. The
50   /// storage will be kept track of and may be recycled.
51   ///
52   template<class SubClass>
53   void Deallocate(SubClass* E) { return Base.Deallocate(Allocator, E); }
54
55   void PrintStats() { Base.PrintStats(); }
56 };
57
58 }
59
60 #endif