RecyclingAllocator.h revision 207618
1178784Skmacy//==- llvm/Support/RecyclingAllocator.h - Recycling Allocator ----*- C++ -*-==// 2178784Skmacy// 3178784Skmacy// The LLVM Compiler Infrastructure 4178784Skmacy// 5178784Skmacy// This file is distributed under the University of Illinois Open Source 6178784Skmacy// License. See LICENSE.TXT for details. 7178784Skmacy// 8178784Skmacy//===----------------------------------------------------------------------===// 9178784Skmacy// 10178784Skmacy// This file defines the RecyclingAllocator class. See the doxygen comment for 11178784Skmacy// RecyclingAllocator for more details on the implementation. 12178784Skmacy// 13178784Skmacy//===----------------------------------------------------------------------===// 14178784Skmacy 15178784Skmacy#ifndef LLVM_SUPPORT_RECYCLINGALLOCATOR_H 16178784Skmacy#define LLVM_SUPPORT_RECYCLINGALLOCATOR_H 17178784Skmacy 18178784Skmacy#include "llvm/Support/Recycler.h" 19178784Skmacy 20178784Skmacynamespace llvm { 21178784Skmacy 22178784Skmacy/// RecyclingAllocator - This class wraps an Allocator, adding the 23178784Skmacy/// functionality of recycling deleted objects. 24178784Skmacy/// 25178784Skmacytemplate<class AllocatorType, class T, 26178784Skmacy size_t Size = sizeof(T), size_t Align = AlignOf<T>::Alignment> 27178784Skmacyclass RecyclingAllocator { 28178784Skmacyprivate: 29178784Skmacy /// Base - Implementation details. 30178784Skmacy /// 31178784Skmacy Recycler<T, Size, Align> Base; 32178784Skmacy 33178784Skmacy /// Allocator - The wrapped allocator. 34178784Skmacy /// 35178784Skmacy AllocatorType Allocator; 36178784Skmacy 37178784Skmacypublic: 38178784Skmacy ~RecyclingAllocator() { Base.clear(Allocator); } 39178784Skmacy 40178784Skmacy /// Allocate - Return a pointer to storage for an object of type 41178784Skmacy /// SubClass. The storage may be either newly allocated or recycled. 42178784Skmacy /// 43178784Skmacy template<class SubClass> 44178784Skmacy SubClass *Allocate() { return Base.template Allocate<SubClass>(Allocator); } 45178784Skmacy 46178784Skmacy T *Allocate() { return Base.Allocate(Allocator); } 47178784Skmacy 48178784Skmacy /// Deallocate - Release storage for the pointed-to object. The 49178784Skmacy /// storage will be kept track of and may be recycled. 50178784Skmacy /// 51178784Skmacy template<class SubClass> 52178784Skmacy void Deallocate(SubClass* E) { return Base.Deallocate(Allocator, E); } 53178784Skmacy 54178784Skmacy void PrintStats() { Base.PrintStats(); } 55178784Skmacy}; 56178784Skmacy 57178784Skmacy} 58178784Skmacy 59178784Skmacytemplate<class AllocatorType, class T, size_t Size, size_t Align> 60178784Skmacyinline void *operator new(size_t, 61178784Skmacy llvm::RecyclingAllocator<AllocatorType, 62178784Skmacy T, Size, Align> &Allocator) { 63178784Skmacy return Allocator.Allocate(); 64178784Skmacy} 65178784Skmacy 66178784Skmacytemplate<class AllocatorType, class T, size_t Size, size_t Align> 67178784Skmacyinline void operator delete(void *E, 68178784Skmacy llvm::RecyclingAllocator<AllocatorType, 69178784Skmacy T, Size, Align> &A) { 70178784Skmacy A.Deallocate(E); 71178784Skmacy} 72178784Skmacy 73178784Skmacy#endif 74178784Skmacy