diff options
author | Dan Gohman <gohman@apple.com> | 2008-07-28 21:51:04 +0000 |
---|---|---|
committer | Dan Gohman <gohman@apple.com> | 2008-07-28 21:51:04 +0000 |
commit | 2fcbc7e8303dfee61147d76bbf16ed0297133c77 (patch) | |
tree | 7ec1a6f6b2a8a37e054b84505502b3346c6680c7 /include/llvm/Support/Recycler.h | |
parent | 7fb99c747552e43a9adc0bdc2396fa74374978be (diff) | |
download | external_llvm-2fcbc7e8303dfee61147d76bbf16ed0297133c77.zip external_llvm-2fcbc7e8303dfee61147d76bbf16ed0297133c77.tar.gz external_llvm-2fcbc7e8303dfee61147d76bbf16ed0297133c77.tar.bz2 |
Fold the useful features of alist and alist_node into ilist, and
a new ilist_node class, and remove them. Unlike alist_node,
ilist_node doesn't attempt to manage storage itself, so it avoids
the associated problems, including being opaque in gdb.
Adjust the Recycler class so that it doesn't depend on alist_node.
Also, change it to use explicit Size and Align parameters, allowing
it to work when the largest-sized node doesn't have the greatest
alignment requirement.
Change MachineInstr's MachineMemOperand list from a pool-backed
alist to a std::list for now.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@54146 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'include/llvm/Support/Recycler.h')
-rw-r--r-- | include/llvm/Support/Recycler.h | 92 |
1 files changed, 54 insertions, 38 deletions
diff --git a/include/llvm/Support/Recycler.h b/include/llvm/Support/Recycler.h index 4750a6d..3f235b6 100644 --- a/include/llvm/Support/Recycler.h +++ b/include/llvm/Support/Recycler.h @@ -15,62 +15,81 @@ #ifndef LLVM_SUPPORT_RECYCLER_H #define LLVM_SUPPORT_RECYCLER_H -#include "llvm/ADT/alist_node.h" +#include "llvm/ADT/ilist.h" +#include "llvm/Support/AlignOf.h" +#include <cassert> namespace llvm { /// PrintRecyclingAllocatorStats - Helper for RecyclingAllocator for /// printing statistics. /// -void PrintRecyclerStats(size_t LargestTypeSize, size_t FreeListSize); +void PrintRecyclerStats(size_t Size, size_t Align, size_t FreeListSize); + +/// RecyclerStruct - Implementation detail for Recycler. This is a +/// class that the recycler imposes on free'd memory to carve out +/// next/prev pointers. +struct RecyclerStruct { + RecyclerStruct *Prev, *Next; +}; + +template<> +struct ilist_traits<RecyclerStruct> : ilist_default_traits<RecyclerStruct> { + static RecyclerStruct *getPrev(const RecyclerStruct *t) { return t->Prev; } + static RecyclerStruct *getNext(const RecyclerStruct *t) { return t->Next; } + static void setPrev(RecyclerStruct *t, RecyclerStruct *p) { t->Prev = p; } + static void setNext(RecyclerStruct *t, RecyclerStruct *n) { t->Next = n; } + + mutable RecyclerStruct Sentinel; + RecyclerStruct *createSentinel() const { + return &Sentinel; + } + static void destroySentinel(RecyclerStruct *) {} + + static void deleteNode(RecyclerStruct *) { + assert(0 && "Recycler's ilist_traits shouldn't see a deleteNode call!"); + } +}; /// Recycler - This class manages a linked-list of deallocated nodes /// and facilitates reusing deallocated memory in place of allocating -/// new memory. The objects it allocates are stored in alist_node -/// containers, so they may be used in alists. +/// new memory. /// -template<class T, class LargestT = T> +template<class T, size_t Size = sizeof(T), size_t Align = AlignOf<T>::Alignment> class Recycler { - typedef alist_node<T, LargestT> NodeTy; - - /// FreeListTraits - ilist traits for FreeList. - /// - struct FreeListTraits : ilist_traits<alist_node<T, LargestT> > { - NodeTy &getSentinel() { return this->Sentinel; } - }; - /// FreeList - Doubly-linked list of nodes that have deleted contents and /// are not in active use. /// - iplist<NodeTy, FreeListTraits> FreeList; - - /// CreateNewNode - Allocate a new node object and initialize its - /// prev and next pointers to 0. - /// - template<class AllocatorType> - NodeTy *CreateNewNode(AllocatorType &Allocator) { - // Note that we're calling new on the *node*, to initialize its - // Next/Prev pointers, not new on the end-user object. - return new (Allocator.Allocate<NodeTy>()) NodeTy(); - } + iplist<RecyclerStruct> FreeList; public: - ~Recycler() { assert(FreeList.empty()); } + ~Recycler() { + // If this fails, either the callee has lost track of some allocation, + // or the callee isn't tracking allocations and should just call + // clear() before deleting the Recycler. + assert(FreeList.empty() && "Non-empty recycler deleted!"); + } + /// clear - Release all the tracked allocations to the allocator. The + /// recycler must be free of any tracked allocations before being + /// deleted; calling clear is one way to ensure this. template<class AllocatorType> void clear(AllocatorType &Allocator) { - while (!FreeList.empty()) - Allocator.Deallocate(FreeList.remove(FreeList.begin())); + while (!FreeList.empty()) { + T *t = reinterpret_cast<T *>(FreeList.remove(FreeList.begin())); + Allocator.Deallocate(t); + } } template<class SubClass, class AllocatorType> SubClass *Allocate(AllocatorType &Allocator) { - NodeTy *N = !FreeList.empty() ? - FreeList.remove(FreeList.front()) : - CreateNewNode(Allocator); - assert(N->getPrev() == 0); - assert(N->getNext() == 0); - return N->getElement((SubClass*)0); + assert(sizeof(SubClass) <= Size && + "Recycler allocation size is less than object size!"); + assert(AlignOf<SubClass>::Alignment <= Align && + "Recycler allocation alignment is less than object alignment!"); + return !FreeList.empty() ? + reinterpret_cast<SubClass *>(FreeList.remove(FreeList.begin())) : + static_cast<SubClass *>(Allocator.Allocate(Size, Align)); } template<class AllocatorType> @@ -80,14 +99,11 @@ public: template<class SubClass, class AllocatorType> void Deallocate(AllocatorType & /*Allocator*/, SubClass* Element) { - NodeTy *N = NodeTy::getNode(Element); - assert(N->getPrev() == 0); - assert(N->getNext() == 0); - FreeList.push_front(N); + FreeList.push_front(reinterpret_cast<RecyclerStruct *>(Element)); } void PrintStats() { - PrintRecyclerStats(sizeof(LargestT), FreeList.size()); + PrintRecyclerStats(Size, Align, FreeList.size()); } }; |