LLVM 22.0.0git
Recycler.h
Go to the documentation of this file.
1//==- llvm/Support/Recycler.h - Recycling Allocator --------------*- C++ -*-==//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the Recycler class template. See the doxygen comment for
10// Recycler for more details.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_SUPPORT_RECYCLER_H
15#define LLVM_SUPPORT_RECYCLER_H
16
17#include "llvm/ADT/ilist.h"
21#include <cassert>
22
23namespace llvm {
24
25/// PrintRecyclingAllocatorStats - Helper for RecyclingAllocator for
26/// printing statistics.
27///
28LLVM_ABI void PrintRecyclerStats(size_t Size, size_t Align,
29 size_t FreeListSize);
30
31/// Recycler - This class manages a linked-list of deallocated nodes
32/// and facilitates reusing deallocated memory in place of allocating
33/// new memory.
34///
35template <class T, size_t Size = sizeof(T), size_t Align = alignof(T)>
36class Recycler {
37 struct FreeNode {
38 FreeNode *Next;
39 };
40
41 /// List of nodes that have deleted contents and are not in active use.
42 FreeNode *FreeList = nullptr;
43
44 FreeNode *pop_val() {
45 auto *Val = FreeList;
47 FreeList = FreeList->Next;
49 return Val;
50 }
51
52 void push(FreeNode *N) {
53 N->Next = FreeList;
54 FreeList = N;
56 }
57
58public:
60 // If this fails, either the callee has lost track of some allocation,
61 // or the callee isn't tracking allocations and should just call
62 // clear() before deleting the Recycler.
63 assert(!FreeList && "Non-empty recycler deleted!");
64 }
65 Recycler(const Recycler &) = delete;
67 : FreeList(std::exchange(Other.FreeList, nullptr)) {}
68 Recycler() = default;
69
70 /// clear - Release all the tracked allocations to the allocator. The
71 /// recycler must be free of any tracked allocations before being
72 /// deleted; calling clear is one way to ensure this.
73 template<class AllocatorType>
74 void clear(AllocatorType &Allocator) {
75 while (FreeList) {
76 T *t = reinterpret_cast<T *>(pop_val());
77 Allocator.Deallocate(t, Size, Align);
78 }
79 }
80
81 /// Special case for BumpPtrAllocator which has an empty Deallocate()
82 /// function.
83 ///
84 /// There is no need to traverse the free list, pulling all the objects into
85 /// cache.
86 void clear(BumpPtrAllocator &) { FreeList = nullptr; }
87
88 template<class SubClass, class AllocatorType>
89 SubClass *Allocate(AllocatorType &Allocator) {
90 static_assert(alignof(SubClass) <= Align,
91 "Recycler allocation alignment is less than object align!");
92 static_assert(sizeof(SubClass) <= Size,
93 "Recycler allocation size is less than object size!");
94 static_assert(Size >= sizeof(FreeNode) &&
95 "Recycler allocation size must be at least sizeof(FreeNode)");
96 return FreeList ? reinterpret_cast<SubClass *>(pop_val())
97 : static_cast<SubClass *>(Allocator.Allocate(Size, Align));
98 }
99
100 template<class AllocatorType>
101 T *Allocate(AllocatorType &Allocator) {
102 return Allocate<T>(Allocator);
103 }
104
105 template<class SubClass, class AllocatorType>
106 void Deallocate(AllocatorType & /*Allocator*/, SubClass* Element) {
107 push(reinterpret_cast<FreeNode *>(Element));
108 }
109
111};
112
113template <class T, size_t Size, size_t Align>
115 size_t S = 0;
116 for (auto *I = FreeList; I; I = I->Next)
117 ++S;
119}
120
121}
122
123#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the BumpPtrAllocator interface.
#define LLVM_ABI
Definition Compiler.h:213
#define __asan_poison_memory_region(p, size)
Definition Compiler.h:568
#define __asan_unpoison_memory_region(p, size)
Definition Compiler.h:569
#define __msan_allocated_memory(p, size)
Definition Compiler.h:543
#define I(x, y, z)
Definition MD5.cpp:58
#define T
Basic Register Allocator
void PrintStats()
Definition Recycler.h:114
void clear(BumpPtrAllocator &)
Special case for BumpPtrAllocator which has an empty Deallocate() function.
Definition Recycler.h:86
void clear(AllocatorType &Allocator)
clear - Release all the tracked allocations to the allocator.
Definition Recycler.h:74
Recycler(Recycler &&Other)
Definition Recycler.h:66
Recycler()=default
void Deallocate(AllocatorType &, SubClass *Element)
Definition Recycler.h:106
Recycler(const Recycler &)=delete
SubClass * Allocate(AllocatorType &Allocator)
Definition Recycler.h:89
T * Allocate(AllocatorType &Allocator)
Definition Recycler.h:101
This file defines classes to implement an intrusive doubly linked list class (i.e.
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI void PrintRecyclerStats(size_t Size, size_t Align, size_t FreeListSize)
PrintRecyclingAllocatorStats - Helper for RecyclingAllocator for printing statistics.
Definition Allocator.cpp:31
BumpPtrAllocatorImpl BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:383
@ Other
Any other memory.
Definition ModRef.h:68
FunctionAddr VTableAddr Next
Definition InstrProf.h:141
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:851
#define N
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39