LLVM  3.7.0
Recycler.h
Go to the documentation of this file.
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/Allocator.h"
22 #include <cassert>
23 
24 namespace llvm {
25 
26 /// PrintRecyclingAllocatorStats - Helper for RecyclingAllocator for
27 /// printing statistics.
28 ///
29 void PrintRecyclerStats(size_t Size, size_t Align, size_t FreeListSize);
30 
31 /// RecyclerStruct - Implementation detail for Recycler. This is a
32 /// class that the recycler imposes on free'd memory to carve out
33 /// next/prev pointers.
36 };
37 
38 template<>
40  public ilist_default_traits<RecyclerStruct> {
41  static RecyclerStruct *getPrev(const RecyclerStruct *t) { return t->Prev; }
42  static RecyclerStruct *getNext(const RecyclerStruct *t) { return t->Next; }
43  static void setPrev(RecyclerStruct *t, RecyclerStruct *p) { t->Prev = p; }
44  static void setNext(RecyclerStruct *t, RecyclerStruct *n) { t->Next = n; }
45 
48  return &Sentinel;
49  }
50  static void destroySentinel(RecyclerStruct *) {}
51 
55 
56  static void deleteNode(RecyclerStruct *) {
57  llvm_unreachable("Recycler's ilist_traits shouldn't see a deleteNode call!");
58  }
59 };
60 
61 /// Recycler - This class manages a linked-list of deallocated nodes
62 /// and facilitates reusing deallocated memory in place of allocating
63 /// new memory.
64 ///
65 template<class T, size_t Size = sizeof(T), size_t Align = AlignOf<T>::Alignment>
66 class Recycler {
67  /// FreeList - Doubly-linked list of nodes that have deleted contents and
68  /// are not in active use.
69  ///
70  iplist<RecyclerStruct> FreeList;
71 
72 public:
74  // If this fails, either the callee has lost track of some allocation,
75  // or the callee isn't tracking allocations and should just call
76  // clear() before deleting the Recycler.
77  assert(FreeList.empty() && "Non-empty recycler deleted!");
78  }
79 
80  /// clear - Release all the tracked allocations to the allocator. The
81  /// recycler must be free of any tracked allocations before being
82  /// deleted; calling clear is one way to ensure this.
83  template<class AllocatorType>
84  void clear(AllocatorType &Allocator) {
85  while (!FreeList.empty()) {
86  T *t = reinterpret_cast<T *>(FreeList.remove(FreeList.begin()));
87  Allocator.Deallocate(t);
88  }
89  }
90 
91  /// Special case for BumpPtrAllocator which has an empty Deallocate()
92  /// function.
93  ///
94  /// There is no need to traverse the free list, pulling all the objects into
95  /// cache.
97  FreeList.clearAndLeakNodesUnsafely();
98  }
99 
100  template<class SubClass, class AllocatorType>
101  SubClass *Allocate(AllocatorType &Allocator) {
102  static_assert(AlignOf<SubClass>::Alignment <= Align,
103  "Recycler allocation alignment is less than object align!");
104  static_assert(sizeof(SubClass) <= Size,
105  "Recycler allocation size is less than object size!");
106  return !FreeList.empty() ?
107  reinterpret_cast<SubClass *>(FreeList.remove(FreeList.begin())) :
108  static_cast<SubClass *>(Allocator.Allocate(Size, Align));
109  }
110 
111  template<class AllocatorType>
112  T *Allocate(AllocatorType &Allocator) {
113  return Allocate<T>(Allocator);
114  }
115 
116  template<class SubClass, class AllocatorType>
117  void Deallocate(AllocatorType & /*Allocator*/, SubClass* Element) {
118  FreeList.push_front(reinterpret_cast<RecyclerStruct *>(Element));
119  }
120 
121  void PrintStats() {
122  PrintRecyclerStats(Size, Align, FreeList.size());
123  }
124 };
125 
126 }
127 
128 #endif
void PrintStats()
Definition: Recycler.h:121
AlignOf - A templated class that contains an enum value representing the alignment of the template ar...
Definition: AlignOf.h:46
void PrintRecyclerStats(size_t Size, size_t Align, size_t FreeListSize)
PrintRecyclingAllocatorStats - Helper for RecyclingAllocator for printing statistics.
Definition: Allocator.cpp:32
static RecyclerStruct * getNext(const RecyclerStruct *t)
Definition: Recycler.h:42
static void setNext(RecyclerStruct *t, RecyclerStruct *n)
Definition: Recycler.h:44
This file defines the MallocAllocator and BumpPtrAllocator interfaces.
void clear(BumpPtrAllocator &)
Special case for BumpPtrAllocator which has an empty Deallocate() function.
Definition: Recycler.h:96
static void setPrev(RecyclerStruct *t, RecyclerStruct *p)
Definition: Recycler.h:43
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:98
static void destroySentinel(RecyclerStruct *)
Definition: Recycler.h:50
ilist_default_traits - Default template traits for intrusive list.
Definition: ilist.h:127
RecyclerStruct * Next
Definition: Recycler.h:35
static RecyclerStruct * getPrev(const RecyclerStruct *t)
Definition: Recycler.h:41
SubClass * Allocate(AllocatorType &Allocator)
Definition: Recycler.h:101
static void deleteNode(RecyclerStruct *)
Definition: Recycler.h:56
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:135
iplist - The subset of list functionality that can safely be used on nodes of polymorphic types...
Definition: ilist.h:49
RecyclerStruct * provideInitialHead() const
Definition: Recycler.h:52
T * Allocate(AllocatorType &Allocator)
Definition: Recycler.h:112
Recycler - This class manages a linked-list of deallocated nodes and facilitates reusing deallocated ...
Definition: Recycler.h:66
static cl::opt< AlignMode > Align(cl::desc("Load/store alignment support"), cl::Hidden, cl::init(NoStrictAlign), cl::values(clEnumValN(StrictAlign,"aarch64-strict-align","Disallow all unaligned memory accesses"), clEnumValN(NoStrictAlign,"aarch64-no-strict-align","Allow unaligned memory accesses"), clEnumValEnd))
RecyclerStruct * ensureHead(RecyclerStruct *) const
Definition: Recycler.h:53
RecyclerStruct * createSentinel() const
Definition: Recycler.h:47
RecyclerStruct * Prev
Definition: Recycler.h:35
void Deallocate(AllocatorType &, SubClass *Element)
Definition: Recycler.h:117
static NodeTy * createSentinel()
createSentinel - create the dynamic sentinel
Definition: ilist.h:78
void clear(AllocatorType &Allocator)
clear - Release all the tracked allocations to the allocator.
Definition: Recycler.h:84
RecyclerStruct - Implementation detail for Recycler.
Definition: Recycler.h:34
static void noteHead(RecyclerStruct *, RecyclerStruct *)
Definition: Recycler.h:54