1 //==- llvm/Support/RecyclingAllocator.h - Recycling Allocator ----*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the RecyclingAllocator class. See the doxygen comment for
11 // RecyclingAllocator for more details on the implementation.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SUPPORT_RECYCLINGALLOCATOR_H
16 #define LLVM_SUPPORT_RECYCLINGALLOCATOR_H
19 #include "llvm/Support/Recycler.h"
20 #include "llvm/ADT/STLExtras.h"
24 /// RecyclingAllocator - This class wraps an Allocator, adding the
25 /// functionality of recycling deleted objects.
27 template<class AllocatorType, class T, class LargestT = T>
28 class RecyclingAllocator {
30 /// Base - Implementation details.
32 Recycler<T, LargestT> Base;
34 /// Allocator - The wrapped allocator.
36 AllocatorType Allocator;
39 ~RecyclingAllocator() { Base.clear(Allocator); }
41 /// Allocate - Return a pointer to storage for an object of type
42 /// SubClass. The storage may be either newly allocated or recycled.
44 template<class SubClass>
45 SubClass *Allocate() { return Base.Allocate<SubClass>(Allocator); }
47 T *Allocate() { return Base.Allocate(Allocator); }
49 /// Deallocate - Release storage for the pointed-to object. The
50 /// storage will be kept track of and may be recycled.
52 template<class SubClass>
53 void Deallocate(SubClass* E) { return Base.Deallocate(Allocator, E); }
55 void PrintStats() { Base.PrintStats(); }