LLVM 20.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"
20#include <cassert>
21
22namespace llvm {
23
24/// PrintRecyclingAllocatorStats - Helper for RecyclingAllocator for
25/// printing statistics.
26///
27void PrintRecyclerStats(size_t Size, size_t Align, size_t FreeListSize);
28
29/// Recycler - This class manages a linked-list of deallocated nodes
30/// and facilitates reusing deallocated memory in place of allocating
31/// new memory.
32///
33template <class T, size_t Size = sizeof(T), size_t Align = alignof(T)>
34class Recycler {
35 struct FreeNode {
36 FreeNode *Next;
37 };
38
39 /// List of nodes that have deleted contents and are not in active use.
40 FreeNode *FreeList = nullptr;
41
42 FreeNode *pop_val() {
43 auto *Val = FreeList;
45 FreeList = FreeList->Next;
47 return Val;
48 }
49
50 void push(FreeNode *N) {
51 N->Next = FreeList;
52 FreeList = N;
54 }
55
56public:
58 // If this fails, either the callee has lost track of some allocation,
59 // or the callee isn't tracking allocations and should just call
60 // clear() before deleting the Recycler.
61 assert(!FreeList && "Non-empty recycler deleted!");
62 }
63 Recycler(const Recycler &) = delete;
65 : FreeList(std::exchange(Other.FreeList, nullptr)) {}
66 Recycler() = default;
67
68 /// clear - Release all the tracked allocations to the allocator. The
69 /// recycler must be free of any tracked allocations before being
70 /// deleted; calling clear is one way to ensure this.
71 template<class AllocatorType>
72 void clear(AllocatorType &Allocator) {
73 while (FreeList) {
74 T *t = reinterpret_cast<T *>(pop_val());
75 Allocator.Deallocate(t, Size, Align);
76 }
77 }
78
79 /// Special case for BumpPtrAllocator which has an empty Deallocate()
80 /// function.
81 ///
82 /// There is no need to traverse the free list, pulling all the objects into
83 /// cache.
84 void clear(BumpPtrAllocator &) { FreeList = nullptr; }
85
86 template<class SubClass, class AllocatorType>
87 SubClass *Allocate(AllocatorType &Allocator) {
88 static_assert(alignof(SubClass) <= Align,
89 "Recycler allocation alignment is less than object align!");
90 static_assert(sizeof(SubClass) <= Size,
91 "Recycler allocation size is less than object size!");
92 static_assert(Size >= sizeof(FreeNode) &&
93 "Recycler allocation size must be at least sizeof(FreeNode)");
94 return FreeList ? reinterpret_cast<SubClass *>(pop_val())
95 : static_cast<SubClass *>(Allocator.Allocate(Size, Align));
96 }
97
98 template<class AllocatorType>
99 T *Allocate(AllocatorType &Allocator) {
100 return Allocate<T>(Allocator);
101 }
102
103 template<class SubClass, class AllocatorType>
104 void Deallocate(AllocatorType & /*Allocator*/, SubClass* Element) {
105 push(reinterpret_cast<FreeNode *>(Element));
106 }
107
109};
110
111template <class T, size_t Size, size_t Align>
113 size_t S = 0;
114 for (auto *I = FreeList; I; I = I->Next)
115 ++S;
117}
118
119}
120
121#endif
This file defines the BumpPtrAllocator interface.
#define __asan_poison_memory_region(p, size)
Definition: Compiler.h:552
#define __asan_unpoison_memory_region(p, size)
Definition: Compiler.h:553
#define __msan_allocated_memory(p, size)
Definition: Compiler.h:527
uint64_t Size
#define I(x, y, z)
Definition: MD5.cpp:58
Basic Register Allocator
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:66
Recycler - This class manages a linked-list of deallocated nodes and facilitates reusing deallocated ...
Definition: Recycler.h:34
void PrintStats()
Definition: Recycler.h:112
void clear(BumpPtrAllocator &)
Special case for BumpPtrAllocator which has an empty Deallocate() function.
Definition: Recycler.h:84
void clear(AllocatorType &Allocator)
clear - Release all the tracked allocations to the allocator.
Definition: Recycler.h:72
Recycler(Recycler &&Other)
Definition: Recycler.h:64
Recycler()=default
void Deallocate(AllocatorType &, SubClass *Element)
Definition: Recycler.h:104
Recycler(const Recycler &)=delete
SubClass * Allocate(AllocatorType &Allocator)
Definition: Recycler.h:87
T * Allocate(AllocatorType &Allocator)
Definition: Recycler.h:99
This file defines classes to implement an intrusive doubly linked list class (i.e.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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.
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
#define N
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39