1 //==- llvm/Support/Recycler.h - Recycling Allocator --------------*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the Recycler class template. See the doxygen comment for
11 // Recycler for more details.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SUPPORT_RECYCLER_H
16 #define LLVM_SUPPORT_RECYCLER_H
18 #include "llvm/ADT/ilist.h"
19 #include "llvm/Support/AlignOf.h"
20 #include "llvm/Support/Allocator.h"
21 #include "llvm/Support/ErrorHandling.h"
26 /// PrintRecyclingAllocatorStats - Helper for RecyclingAllocator for
27 /// printing statistics.
29 void PrintRecyclerStats(size_t Size, size_t Align, size_t FreeListSize);
31 /// Recycler - This class manages a linked-list of deallocated nodes
32 /// and facilitates reusing deallocated memory in place of allocating
35 template<class T, size_t Size = sizeof(T), size_t Align = AlignOf<T>::Alignment>
41 /// List of nodes that have deleted contents and are not in active use.
42 FreeNode *FreeList = nullptr;
46 FreeList = FreeList->Next;
50 void push(FreeNode *N) {
57 // If this fails, either the callee has lost track of some allocation,
58 // or the callee isn't tracking allocations and should just call
59 // clear() before deleting the Recycler.
60 assert(!FreeList && "Non-empty recycler deleted!");
63 /// clear - Release all the tracked allocations to the allocator. The
64 /// recycler must be free of any tracked allocations before being
65 /// deleted; calling clear is one way to ensure this.
66 template<class AllocatorType>
67 void clear(AllocatorType &Allocator) {
69 T *t = reinterpret_cast<T *>(pop_val());
70 Allocator.Deallocate(t);
74 /// Special case for BumpPtrAllocator which has an empty Deallocate()
77 /// There is no need to traverse the free list, pulling all the objects into
79 void clear(BumpPtrAllocator &) { FreeList = nullptr; }
81 template<class SubClass, class AllocatorType>
82 SubClass *Allocate(AllocatorType &Allocator) {
83 static_assert(AlignOf<SubClass>::Alignment <= Align,
84 "Recycler allocation alignment is less than object align!");
85 static_assert(sizeof(SubClass) <= Size,
86 "Recycler allocation size is less than object size!");
87 return FreeList ? reinterpret_cast<SubClass *>(pop_val())
88 : static_cast<SubClass *>(Allocator.Allocate(Size, Align));
91 template<class AllocatorType>
92 T *Allocate(AllocatorType &Allocator) {
93 return Allocate<T>(Allocator);
96 template<class SubClass, class AllocatorType>
97 void Deallocate(AllocatorType & /*Allocator*/, SubClass* Element) {
98 push(reinterpret_cast<FreeNode *>(Element));
104 template <class T, size_t Size, size_t Align>
105 void Recycler<T, Size, Align>::PrintStats() {
107 for (auto *I = FreeList; I; I = I->Next)
109 PrintRecyclerStats(Size, Align, S);