LLVM API Documentation
00001 //==- llvm/Support/Recycler.h - Recycling Allocator --------------*- C++ -*-==// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines the Recycler class template. See the doxygen comment for 00011 // Recycler for more details. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #ifndef LLVM_SUPPORT_RECYCLER_H 00016 #define LLVM_SUPPORT_RECYCLER_H 00017 00018 #include "llvm/ADT/ilist.h" 00019 #include "llvm/Support/AlignOf.h" 00020 #include <cassert> 00021 00022 namespace llvm { 00023 00024 /// PrintRecyclingAllocatorStats - Helper for RecyclingAllocator for 00025 /// printing statistics. 00026 /// 00027 void PrintRecyclerStats(size_t Size, size_t Align, size_t FreeListSize); 00028 00029 /// RecyclerStruct - Implementation detail for Recycler. This is a 00030 /// class that the recycler imposes on free'd memory to carve out 00031 /// next/prev pointers. 00032 struct RecyclerStruct { 00033 RecyclerStruct *Prev, *Next; 00034 }; 00035 00036 template<> 00037 struct ilist_traits<RecyclerStruct> : ilist_default_traits<RecyclerStruct> { 00038 static RecyclerStruct *getPrev(const RecyclerStruct *t) { return t->Prev; } 00039 static RecyclerStruct *getNext(const RecyclerStruct *t) { return t->Next; } 00040 static void setPrev(RecyclerStruct *t, RecyclerStruct *p) { t->Prev = p; } 00041 static void setNext(RecyclerStruct *t, RecyclerStruct *n) { t->Next = n; } 00042 00043 mutable RecyclerStruct Sentinel; 00044 RecyclerStruct *createSentinel() const { 00045 return &Sentinel; 00046 } 00047 static void destroySentinel(RecyclerStruct *) {} 00048 00049 static void deleteNode(RecyclerStruct *) { 00050 assert(0 && "Recycler's ilist_traits shouldn't see a deleteNode call!"); 00051 } 00052 }; 00053 00054 /// Recycler - This class manages a linked-list of deallocated nodes 00055 /// and facilitates reusing deallocated memory in place of allocating 00056 /// new memory. 00057 /// 00058 template<class T, size_t Size = sizeof(T), size_t Align = AlignOf<T>::Alignment> 00059 class Recycler { 00060 /// FreeList - Doubly-linked list of nodes that have deleted contents and 00061 /// are not in active use. 00062 /// 00063 iplist<RecyclerStruct> FreeList; 00064 00065 public: 00066 ~Recycler() { 00067 // If this fails, either the callee has lost track of some allocation, 00068 // or the callee isn't tracking allocations and should just call 00069 // clear() before deleting the Recycler. 00070 assert(FreeList.empty() && "Non-empty recycler deleted!"); 00071 } 00072 00073 /// clear - Release all the tracked allocations to the allocator. The 00074 /// recycler must be free of any tracked allocations before being 00075 /// deleted; calling clear is one way to ensure this. 00076 template<class AllocatorType> 00077 void clear(AllocatorType &Allocator) { 00078 while (!FreeList.empty()) { 00079 T *t = reinterpret_cast<T *>(FreeList.remove(FreeList.begin())); 00080 Allocator.Deallocate(t); 00081 } 00082 } 00083 00084 template<class SubClass, class AllocatorType> 00085 SubClass *Allocate(AllocatorType &Allocator) { 00086 assert(sizeof(SubClass) <= Size && 00087 "Recycler allocation size is less than object size!"); 00088 assert(AlignOf<SubClass>::Alignment <= Align && 00089 "Recycler allocation alignment is less than object alignment!"); 00090 return !FreeList.empty() ? 00091 reinterpret_cast<SubClass *>(FreeList.remove(FreeList.begin())) : 00092 static_cast<SubClass *>(Allocator.Allocate(Size, Align)); 00093 } 00094 00095 template<class AllocatorType> 00096 T *Allocate(AllocatorType &Allocator) { 00097 return Allocate<T>(Allocator); 00098 } 00099 00100 template<class SubClass, class AllocatorType> 00101 void Deallocate(AllocatorType & /*Allocator*/, SubClass* Element) { 00102 FreeList.push_front(reinterpret_cast<RecyclerStruct *>(Element)); 00103 } 00104 00105 void PrintStats() { 00106 PrintRecyclerStats(Size, Align, FreeList.size()); 00107 } 00108 }; 00109 00110 } 00111 00112 #endif