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#include <type_traits>
23
24namespace llvm {
25
26/// PrintRecyclingAllocatorStats - Helper for RecyclingAllocator for
27/// printing statistics.
28///
29LLVM_ABI void PrintRecyclerStats(size_t Size, size_t Align,
30 size_t FreeListSize);
31
32/// Recycler - This class manages a linked-list of deallocated nodes
33/// and facilitates reusing deallocated memory in place of allocating
34/// new memory.
35///
36template <class T, size_t Size = sizeof(T), size_t Align = alignof(T)>
37class Recycler {
38 struct FreeNode {
39 FreeNode *Next;
40 };
41
42 /// List of nodes that have deleted contents and are not in active use.
43 FreeNode *FreeList = nullptr;
44
45 FreeNode *pop_val() {
46 auto *Val = FreeList;
48 FreeList = FreeList->Next;
50 return Val;
51 }
52
53 void push(FreeNode *N) {
54 N->Next = FreeList;
55 FreeList = N;
57 }
58
59public:
61 // If this fails, either the callee has lost track of some allocation,
62 // or the callee isn't tracking allocations and should just call
63 // clear() before deleting the Recycler.
64 assert(!FreeList && "Non-empty recycler deleted!");
65 }
66 Recycler(const Recycler &) = delete;
68 : FreeList(std::exchange(Other.FreeList, nullptr)) {}
69 Recycler() = default;
70
71 /// clear - Release all the tracked allocations to the allocator. The
72 /// recycler must be free of any tracked allocations before being
73 /// deleted; calling clear is one way to ensure this.
74 template<class AllocatorType>
75 void clear(AllocatorType &Allocator) {
76 if constexpr (std::is_same_v<std::decay_t<AllocatorType>,
78 // For BumpPtrAllocator, Deallocate is a no-op, so just drop the free
79 // list.
80 FreeList = nullptr;
81 } else {
82 while (FreeList) {
83 T *t = reinterpret_cast<T *>(pop_val());
84 Allocator.Deallocate(t, Size, Align);
85 }
86 }
87 }
88
89 template<class SubClass, class AllocatorType>
90 SubClass *Allocate(AllocatorType &Allocator) {
91 static_assert(alignof(SubClass) <= Align,
92 "Recycler allocation alignment is less than object align!");
93 static_assert(sizeof(SubClass) <= Size,
94 "Recycler allocation size is less than object size!");
95 static_assert(Size >= sizeof(FreeNode) &&
96 "Recycler allocation size must be at least sizeof(FreeNode)");
97 return FreeList ? reinterpret_cast<SubClass *>(pop_val())
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 push(reinterpret_cast<FreeNode *>(Element));
109 }
110
112};
113
114template <class T, size_t Size, size_t Align>
116 size_t S = 0;
117 for (auto *I = FreeList; I; I = I->Next)
118 ++S;
120}
121
122}
123
124#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:115
void clear(AllocatorType &Allocator)
clear - Release all the tracked allocations to the allocator.
Definition Recycler.h:75
Recycler(Recycler &&Other)
Definition Recycler.h:67
Recycler()=default
void Deallocate(AllocatorType &, SubClass *Element)
Definition Recycler.h:107
Recycler(const Recycler &)=delete
SubClass * Allocate(AllocatorType &Allocator)
Definition Recycler.h:90
T * Allocate(AllocatorType &Allocator)
Definition Recycler.h:102
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
@ Other
Any other memory.
Definition ModRef.h:68
FunctionAddr VTableAddr Next
Definition InstrProf.h:141
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:383
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:867
#define N
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39