LLVM API Documentation

Recycler.h
Go to the documentation of this file.
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 "llvm/Support/ErrorHandling.h"
00021 #include <cassert>
00022 
00023 namespace llvm {
00024 
00025 class BumpPtrAllocator;
00026 
00027 /// PrintRecyclingAllocatorStats - Helper for RecyclingAllocator for
00028 /// printing statistics.
00029 ///
00030 void PrintRecyclerStats(size_t Size, size_t Align, size_t FreeListSize);
00031 
00032 /// RecyclerStruct - Implementation detail for Recycler. This is a
00033 /// class that the recycler imposes on free'd memory to carve out
00034 /// next/prev pointers.
00035 struct RecyclerStruct {
00036   RecyclerStruct *Prev, *Next;
00037 };
00038 
00039 template<>
00040 struct ilist_traits<RecyclerStruct> :
00041     public ilist_default_traits<RecyclerStruct> {
00042   static RecyclerStruct *getPrev(const RecyclerStruct *t) { return t->Prev; }
00043   static RecyclerStruct *getNext(const RecyclerStruct *t) { return t->Next; }
00044   static void setPrev(RecyclerStruct *t, RecyclerStruct *p) { t->Prev = p; }
00045   static void setNext(RecyclerStruct *t, RecyclerStruct *n) { t->Next = n; }
00046 
00047   mutable RecyclerStruct Sentinel;
00048   RecyclerStruct *createSentinel() const {
00049     return &Sentinel;
00050   }
00051   static void destroySentinel(RecyclerStruct *) {}
00052 
00053   RecyclerStruct *provideInitialHead() const { return createSentinel(); }
00054   RecyclerStruct *ensureHead(RecyclerStruct*) const { return createSentinel(); }
00055   static void noteHead(RecyclerStruct*, RecyclerStruct*) {}
00056 
00057   static void deleteNode(RecyclerStruct *) {
00058     llvm_unreachable("Recycler's ilist_traits shouldn't see a deleteNode call!");
00059   }
00060 };
00061 
00062 /// Recycler - This class manages a linked-list of deallocated nodes
00063 /// and facilitates reusing deallocated memory in place of allocating
00064 /// new memory.
00065 ///
00066 template<class T, size_t Size = sizeof(T), size_t Align = AlignOf<T>::Alignment>
00067 class Recycler {
00068   /// FreeList - Doubly-linked list of nodes that have deleted contents and
00069   /// are not in active use.
00070   ///
00071   iplist<RecyclerStruct> FreeList;
00072 
00073 public:
00074   ~Recycler() {
00075     // If this fails, either the callee has lost track of some allocation,
00076     // or the callee isn't tracking allocations and should just call
00077     // clear() before deleting the Recycler.
00078     assert(FreeList.empty() && "Non-empty recycler deleted!");
00079   }
00080 
00081   /// clear - Release all the tracked allocations to the allocator. The
00082   /// recycler must be free of any tracked allocations before being
00083   /// deleted; calling clear is one way to ensure this.
00084   template<class AllocatorType>
00085   void clear(AllocatorType &Allocator) {
00086     while (!FreeList.empty()) {
00087       T *t = reinterpret_cast<T *>(FreeList.remove(FreeList.begin()));
00088       Allocator.Deallocate(t);
00089     }
00090   }
00091 
00092   /// Special case for BumpPtrAllocator which has an empty Deallocate()
00093   /// function.
00094   ///
00095   /// There is no need to traverse the free list, pulling all the objects into
00096   /// cache.
00097   void clear(BumpPtrAllocator&) {
00098     FreeList.clearAndLeakNodesUnsafely();
00099   }
00100 
00101   template<class SubClass, class AllocatorType>
00102   SubClass *Allocate(AllocatorType &Allocator) {
00103     assert(sizeof(SubClass) <= Size &&
00104            "Recycler allocation size is less than object size!");
00105     assert(AlignOf<SubClass>::Alignment <= Align &&
00106            "Recycler allocation alignment is less than object alignment!");
00107     return !FreeList.empty() ?
00108            reinterpret_cast<SubClass *>(FreeList.remove(FreeList.begin())) :
00109            static_cast<SubClass *>(Allocator.Allocate(Size, Align));
00110   }
00111 
00112   template<class AllocatorType>
00113   T *Allocate(AllocatorType &Allocator) {
00114     return Allocate<T>(Allocator);
00115   }
00116 
00117   template<class SubClass, class AllocatorType>
00118   void Deallocate(AllocatorType & /*Allocator*/, SubClass* Element) {
00119     FreeList.push_front(reinterpret_cast<RecyclerStruct *>(Element));
00120   }
00121 
00122   void PrintStats() {
00123     PrintRecyclerStats(Size, Align, FreeList.size());
00124   }
00125 };
00126 
00127 }
00128 
00129 #endif