• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "llvm/Support/ErrorHandling.h"
21 #include <cassert>
22 
23 namespace llvm {
24 
25 /// PrintRecyclingAllocatorStats - Helper for RecyclingAllocator for
26 /// printing statistics.
27 ///
28 void PrintRecyclerStats(size_t Size, size_t Align, size_t FreeListSize);
29 
30 /// RecyclerStruct - Implementation detail for Recycler. This is a
31 /// class that the recycler imposes on free'd memory to carve out
32 /// next/prev pointers.
33 struct RecyclerStruct {
34   RecyclerStruct *Prev, *Next;
35 };
36 
37 template<>
38 struct ilist_traits<RecyclerStruct> :
39     public ilist_default_traits<RecyclerStruct> {
40   static RecyclerStruct *getPrev(const RecyclerStruct *t) { return t->Prev; }
41   static RecyclerStruct *getNext(const RecyclerStruct *t) { return t->Next; }
42   static void setPrev(RecyclerStruct *t, RecyclerStruct *p) { t->Prev = p; }
43   static void setNext(RecyclerStruct *t, RecyclerStruct *n) { t->Next = n; }
44 
45   mutable RecyclerStruct Sentinel;
46   RecyclerStruct *createSentinel() const {
47     return &Sentinel;
48   }
49   static void destroySentinel(RecyclerStruct *) {}
50 
51   RecyclerStruct *provideInitialHead() const { return createSentinel(); }
52   RecyclerStruct *ensureHead(RecyclerStruct*) const { return createSentinel(); }
53   static void noteHead(RecyclerStruct*, RecyclerStruct*) {}
54 
55   static void deleteNode(RecyclerStruct *) {
56     llvm_unreachable("Recycler's ilist_traits shouldn't see a deleteNode call!");
57   }
58 };
59 
60 /// Recycler - This class manages a linked-list of deallocated nodes
61 /// and facilitates reusing deallocated memory in place of allocating
62 /// new memory.
63 ///
64 template<class T, size_t Size = sizeof(T), size_t Align = AlignOf<T>::Alignment>
65 class Recycler {
66   /// FreeList - Doubly-linked list of nodes that have deleted contents and
67   /// are not in active use.
68   ///
69   iplist<RecyclerStruct> FreeList;
70 
71 public:
72   ~Recycler() {
73     // If this fails, either the callee has lost track of some allocation,
74     // or the callee isn't tracking allocations and should just call
75     // clear() before deleting the Recycler.
76     assert(FreeList.empty() && "Non-empty recycler deleted!");
77   }
78 
79   /// clear - Release all the tracked allocations to the allocator. The
80   /// recycler must be free of any tracked allocations before being
81   /// deleted; calling clear is one way to ensure this.
82   template<class AllocatorType>
83   void clear(AllocatorType &Allocator) {
84     while (!FreeList.empty()) {
85       T *t = reinterpret_cast<T *>(FreeList.remove(FreeList.begin()));
86       Allocator.Deallocate(t);
87     }
88   }
89 
90   template<class SubClass, class AllocatorType>
91   SubClass *Allocate(AllocatorType &Allocator) {
92     assert(sizeof(SubClass) <= Size &&
93            "Recycler allocation size is less than object size!");
94     assert(AlignOf<SubClass>::Alignment <= Align &&
95            "Recycler allocation alignment is less than object alignment!");
96     return !FreeList.empty() ?
97            reinterpret_cast<SubClass *>(FreeList.remove(FreeList.begin())) :
98            static_cast<SubClass *>(Allocator.Allocate(Size, Align));
99   }
100 
101   template<class AllocatorType>
102   T *Allocate(AllocatorType &Allocator) {
103     return Allocate<T>(Allocator);
104   }
105 
106   template<class SubClass, class AllocatorType>
107   void Deallocate(AllocatorType & /*Allocator*/, SubClass* Element) {
108     FreeList.push_front(reinterpret_cast<RecyclerStruct *>(Element));
109   }
110 
111   void PrintStats() {
112     PrintRecyclerStats(Size, Align, FreeList.size());
113   }
114 };
115 
116 }
117 
118 #endif
119