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