1 //==- llvm/Support/Recycler.h - Recycling Allocator --------------*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the Recycler class template. See the doxygen comment for
11 // Recycler for more details.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SUPPORT_RECYCLER_H
16 #define LLVM_SUPPORT_RECYCLER_H
18 #include "llvm/ADT/ilist.h"
19 #include "llvm/Support/AlignOf.h"
24 /// PrintRecyclingAllocatorStats - Helper for RecyclingAllocator for
25 /// printing statistics.
27 void PrintRecyclerStats(size_t Size, size_t Align, size_t FreeListSize);
29 /// RecyclerStruct - Implementation detail for Recycler. This is a
30 /// class that the recycler imposes on free'd memory to carve out
31 /// next/prev pointers.
32 struct RecyclerStruct {
33 RecyclerStruct *Prev, *Next;
37 struct ilist_traits<RecyclerStruct> : ilist_default_traits<RecyclerStruct> {
38 static RecyclerStruct *getPrev(const RecyclerStruct *t) { return t->Prev; }
39 static RecyclerStruct *getNext(const RecyclerStruct *t) { return t->Next; }
40 static void setPrev(RecyclerStruct *t, RecyclerStruct *p) { t->Prev = p; }
41 static void setNext(RecyclerStruct *t, RecyclerStruct *n) { t->Next = n; }
43 mutable RecyclerStruct Sentinel;
44 RecyclerStruct *createSentinel() const {
47 static void destroySentinel(RecyclerStruct *) {}
49 static void deleteNode(RecyclerStruct *) {
50 assert(0 && "Recycler's ilist_traits shouldn't see a deleteNode call!");
54 /// Recycler - This class manages a linked-list of deallocated nodes
55 /// and facilitates reusing deallocated memory in place of allocating
58 template<class T, size_t Size = sizeof(T), size_t Align = AlignOf<T>::Alignment>
60 /// FreeList - Doubly-linked list of nodes that have deleted contents and
61 /// are not in active use.
63 iplist<RecyclerStruct> FreeList;
67 // If this fails, either the callee has lost track of some allocation,
68 // or the callee isn't tracking allocations and should just call
69 // clear() before deleting the Recycler.
70 assert(FreeList.empty() && "Non-empty recycler deleted!");
73 /// clear - Release all the tracked allocations to the allocator. The
74 /// recycler must be free of any tracked allocations before being
75 /// deleted; calling clear is one way to ensure this.
76 template<class AllocatorType>
77 void clear(AllocatorType &Allocator) {
78 while (!FreeList.empty()) {
79 T *t = reinterpret_cast<T *>(FreeList.remove(FreeList.begin()));
80 Allocator.Deallocate(t);
84 template<class SubClass, class AllocatorType>
85 SubClass *Allocate(AllocatorType &Allocator) {
86 assert(sizeof(SubClass) <= Size &&
87 "Recycler allocation size is less than object size!");
88 assert(AlignOf<SubClass>::Alignment <= Align &&
89 "Recycler allocation alignment is less than object alignment!");
90 return !FreeList.empty() ?
91 reinterpret_cast<SubClass *>(FreeList.remove(FreeList.begin())) :
92 static_cast<SubClass *>(Allocator.Allocate(Size, Align));
95 template<class AllocatorType>
96 T *Allocate(AllocatorType &Allocator) {
97 return Allocate<T>(Allocator);
100 template<class SubClass, class AllocatorType>
101 void Deallocate(AllocatorType & /*Allocator*/, SubClass* Element) {
102 FreeList.push_front(reinterpret_cast<RecyclerStruct *>(Element));
106 PrintRecyclerStats(Size, Align, FreeList.size());