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
18 #include "llvm/Support/Recycler.h"
22 /// RecyclingAllocator - This class wraps an Allocator, adding the
23 /// functionality of recycling deleted objects.
25 template<class AllocatorType, class T,
26 size_t Size = sizeof(T), size_t Align = AlignOf<T>::Alignment>
27 class RecyclingAllocator {
29 /// Base - Implementation details.
31 Recycler<T, Size, Align> Base;
33 /// Allocator - The wrapped allocator.
35 AllocatorType Allocator;
38 ~RecyclingAllocator() { Base.clear(Allocator); }
40 /// Allocate - Return a pointer to storage for an object of type
41 /// SubClass. The storage may be either newly allocated or recycled.
43 template<class SubClass>
44 SubClass *Allocate() { return Base.template Allocate<SubClass>(Allocator); }
46 T *Allocate() { return Base.Allocate(Allocator); }
48 /// Deallocate - Release storage for the pointed-to object. The
49 /// storage will be kept track of and may be recycled.
51 template<class SubClass>
52 void Deallocate(SubClass* E) { return Base.Deallocate(Allocator, E); }
54 void PrintStats() { Base.PrintStats(); }