ooops, forgot to include the pointless-write eliminator in my previous checkin
[oota-llvm.git] / include / llvm / Support / Recycler.h
1 //==- llvm/Support/Recycler.h - Recycling Allocator --------------*- C++ -*-==//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the Recycler class template.  See the doxygen comment for
11 // Recycler for more details.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_SUPPORT_RECYCLER_H
16 #define LLVM_SUPPORT_RECYCLER_H
17
18 #include "llvm/ADT/ilist.h"
19 #include "llvm/Support/AlignOf.h"
20 #include <cassert>
21
22 namespace llvm {
23
24 /// PrintRecyclingAllocatorStats - Helper for RecyclingAllocator for
25 /// printing statistics.
26 ///
27 void PrintRecyclerStats(size_t Size, size_t Align, size_t FreeListSize);
28
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;
34 };
35
36 template<>
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; }
42
43   mutable RecyclerStruct Sentinel;
44   RecyclerStruct *createSentinel() const {
45     return &Sentinel;
46   }
47   static void destroySentinel(RecyclerStruct *) {}
48
49   RecyclerStruct *provideInitialHead() const { return createSentinel(); }
50   RecyclerStruct *ensureHead(RecyclerStruct*) const { return createSentinel(); }
51   static void noteHead(RecyclerStruct*, RecyclerStruct*) {}
52
53   static void deleteNode(RecyclerStruct *) {
54     assert(0 && "Recycler's ilist_traits shouldn't see a deleteNode call!");
55   }
56 };
57
58 /// Recycler - This class manages a linked-list of deallocated nodes
59 /// and facilitates reusing deallocated memory in place of allocating
60 /// new memory.
61 ///
62 template<class T, size_t Size = sizeof(T), size_t Align = AlignOf<T>::Alignment>
63 class Recycler {
64   /// FreeList - Doubly-linked list of nodes that have deleted contents and
65   /// are not in active use.
66   ///
67   iplist<RecyclerStruct> FreeList;
68
69 public:
70   ~Recycler() {
71     // If this fails, either the callee has lost track of some allocation,
72     // or the callee isn't tracking allocations and should just call
73     // clear() before deleting the Recycler.
74     assert(FreeList.empty() && "Non-empty recycler deleted!");
75   }
76
77   /// clear - Release all the tracked allocations to the allocator. The
78   /// recycler must be free of any tracked allocations before being
79   /// deleted; calling clear is one way to ensure this.
80   template<class AllocatorType>
81   void clear(AllocatorType &Allocator) {
82     while (!FreeList.empty()) {
83       T *t = reinterpret_cast<T *>(FreeList.remove(FreeList.begin()));
84       Allocator.Deallocate(t);
85     }
86   }
87
88   template<class SubClass, class AllocatorType>
89   SubClass *Allocate(AllocatorType &Allocator) {
90     assert(sizeof(SubClass) <= Size &&
91            "Recycler allocation size is less than object size!");
92     assert(AlignOf<SubClass>::Alignment <= Align &&
93            "Recycler allocation alignment is less than object alignment!");
94     return !FreeList.empty() ?
95            reinterpret_cast<SubClass *>(FreeList.remove(FreeList.begin())) :
96            static_cast<SubClass *>(Allocator.Allocate(Size, Align));
97   }
98
99   template<class AllocatorType>
100   T *Allocate(AllocatorType &Allocator) {
101     return Allocate<T>(Allocator);
102   }
103
104   template<class SubClass, class AllocatorType>
105   void Deallocate(AllocatorType & /*Allocator*/, SubClass* Element) {
106     FreeList.push_front(reinterpret_cast<RecyclerStruct *>(Element));
107   }
108
109   void PrintStats() {
110     PrintRecyclerStats(Size, Align, FreeList.size());
111   }
112 };
113
114 }
115
116 #endif