LLVM API Documentation

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