39c992bc5370cddb82abbb959ed4b3dca6aab114
[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
52   static void deleteNode(RecyclerStruct *) {
53     assert(0 && "Recycler's ilist_traits shouldn't see a deleteNode call!");
54   }
55 };
56
57 /// Recycler - This class manages a linked-list of deallocated nodes
58 /// and facilitates reusing deallocated memory in place of allocating
59 /// new memory.
60 ///
61 template<class T, size_t Size = sizeof(T), size_t Align = AlignOf<T>::Alignment>
62 class Recycler {
63   /// FreeList - Doubly-linked list of nodes that have deleted contents and
64   /// are not in active use.
65   ///
66   iplist<RecyclerStruct> FreeList;
67
68 public:
69   ~Recycler() {
70     // If this fails, either the callee has lost track of some allocation,
71     // or the callee isn't tracking allocations and should just call
72     // clear() before deleting the Recycler.
73     assert(FreeList.empty() && "Non-empty recycler deleted!");
74   }
75
76   /// clear - Release all the tracked allocations to the allocator. The
77   /// recycler must be free of any tracked allocations before being
78   /// deleted; calling clear is one way to ensure this.
79   template<class AllocatorType>
80   void clear(AllocatorType &Allocator) {
81     while (!FreeList.empty()) {
82       T *t = reinterpret_cast<T *>(FreeList.remove(FreeList.begin()));
83       Allocator.Deallocate(t);
84     }
85   }
86
87   template<class SubClass, class AllocatorType>
88   SubClass *Allocate(AllocatorType &Allocator) {
89     assert(sizeof(SubClass) <= Size &&
90            "Recycler allocation size is less than object size!");
91     assert(AlignOf<SubClass>::Alignment <= Align &&
92            "Recycler allocation alignment is less than object alignment!");
93     return !FreeList.empty() ?
94            reinterpret_cast<SubClass *>(FreeList.remove(FreeList.begin())) :
95            static_cast<SubClass *>(Allocator.Allocate(Size, Align));
96   }
97
98   template<class AllocatorType>
99   T *Allocate(AllocatorType &Allocator) {
100     return Allocate<T>(Allocator);
101   }
102
103   template<class SubClass, class AllocatorType>
104   void Deallocate(AllocatorType & /*Allocator*/, SubClass* Element) {
105     FreeList.push_front(reinterpret_cast<RecyclerStruct *>(Element));
106   }
107
108   void PrintStats() {
109     PrintRecyclerStats(Size, Align, FreeList.size());
110   }
111 };
112
113 }
114
115 #endif