21 #ifndef LLVM_SUPPORT_ALLOCATOR_H
22 #define LLVM_SUPPORT_ALLOCATOR_H
33 #include <type_traits>
50 static_assert(
static_cast<void *(
AllocatorBase::*)(
size_t,
size_t)
>(
52 static_cast<void *(DerivedT::*)(
size_t,
size_t)
>(
54 "Class derives from AllocatorBase without implementing the "
55 "core Allocate(size_t, size_t) overload!");
57 return static_cast<DerivedT *
>(
this)->
Allocate(Size, Alignment);
64 static_assert(
static_cast<void (
AllocatorBase::*)(
const void *,
size_t)
>(
66 static_cast<void (DerivedT::*)(
const void *,
size_t)
>(
67 &DerivedT::Deallocate),
68 "Class derives from AllocatorBase without implementing the "
69 "core Deallocate(void *) overload!");
71 return static_cast<DerivedT *
>(
this)->
Deallocate(Ptr, Size);
79 return static_cast<T *
>(
Allocate(Num *
sizeof(
T),
alignof(
T)));
84 typename std::enable_if<
85 !std::is_same<typename std::remove_cv<T>::type,
void>::value,
void>::type
87 Deallocate(static_cast<const void *>(Ptr), Num *
sizeof(
T));
104 free(const_cast<void *>(Ptr));
136 template <
typename AllocatorT = MallocAllocator,
size_t SlabSize = 4096,
137 size_t SizeThreshold = SlabSize>
140 BumpPtrAllocatorImpl<AllocatorT, SlabSize, SizeThreshold>> {
142 static_assert(SizeThreshold <= SlabSize,
143 "The SizeThreshold must be at most the SlabSize to ensure "
144 "that objects larger than a slab go into their own memory "
149 template <
typename T>
151 : Allocator(std::forward<
T &&>(Allocator)) {}
156 : CurPtr(Old.CurPtr), End(Old.End), Slabs(std::move(Old.Slabs)),
157 CustomSizedSlabs(std::move(Old.CustomSizedSlabs)),
158 BytesAllocated(Old.BytesAllocated), RedZoneSize(Old.RedZoneSize),
159 Allocator(std::move(Old.Allocator)) {
160 Old.CurPtr = Old.End =
nullptr;
161 Old.BytesAllocated = 0;
163 Old.CustomSizedSlabs.clear();
167 DeallocateSlabs(Slabs.
begin(), Slabs.
end());
168 DeallocateCustomSizedSlabs();
172 DeallocateSlabs(Slabs.
begin(), Slabs.
end());
173 DeallocateCustomSizedSlabs();
177 BytesAllocated = RHS.BytesAllocated;
178 RedZoneSize = RHS.RedZoneSize;
179 Slabs = std::move(RHS.Slabs);
180 CustomSizedSlabs = std::move(RHS.CustomSizedSlabs);
181 Allocator = std::move(RHS.Allocator);
183 RHS.CurPtr = RHS.End =
nullptr;
184 RHS.BytesAllocated = 0;
186 RHS.CustomSizedSlabs.clear();
194 DeallocateCustomSizedSlabs();
195 CustomSizedSlabs.
clear();
202 CurPtr = (
char *)Slabs.
front();
203 End = CurPtr + SlabSize;
206 DeallocateSlabs(std::next(Slabs.
begin()), Slabs.
end());
213 assert(Alignment > 0 &&
"0-byte alignnment is not allowed. Use 1 instead.");
216 BytesAllocated +=
Size;
219 assert(Adjustment + Size >= Size &&
"Adjustment + Size must not overflow");
221 size_t SizeToAllocate =
Size;
222 #if LLVM_ADDRESS_SANITIZER_BUILD
224 SizeToAllocate += RedZoneSize;
228 if (Adjustment + SizeToAllocate <=
size_t(End - CurPtr)) {
229 char *AlignedPtr = CurPtr + Adjustment;
230 CurPtr = AlignedPtr + SizeToAllocate;
241 size_t PaddedSize = SizeToAllocate + Alignment - 1;
242 if (PaddedSize > SizeThreshold) {
243 void *NewSlab = Allocator.Allocate(PaddedSize, 0);
247 CustomSizedSlabs.
push_back(std::make_pair(NewSlab, PaddedSize));
249 uintptr_t AlignedAddr =
alignAddr(NewSlab, Alignment);
250 assert(AlignedAddr + Size <= (uintptr_t)NewSlab + PaddedSize);
251 char *AlignedPtr = (
char*)AlignedAddr;
259 uintptr_t AlignedAddr =
alignAddr(CurPtr, Alignment);
260 assert(AlignedAddr + SizeToAllocate <= (uintptr_t)End &&
261 "Unable to allocate memory!");
262 char *AlignedPtr = (
char*)AlignedAddr;
263 CurPtr = AlignedPtr + SizeToAllocate;
282 size_t TotalMemory = 0;
284 TotalMemory += computeSlabSize(std::distance(Slabs.
begin(),
I));
285 for (
auto &PtrAndSize : CustomSizedSlabs)
286 TotalMemory += PtrAndSize.second;
293 RedZoneSize = NewSize;
305 char *CurPtr =
nullptr;
319 size_t BytesAllocated = 0;
323 size_t RedZoneSize = 1;
326 AllocatorT Allocator;
328 static size_t computeSlabSize(
unsigned SlabIdx) {
333 return SlabSize * ((
size_t)1 << std::min<size_t>(30, SlabIdx / 128));
338 void StartNewSlab() {
339 size_t AllocatedSlabSize = computeSlabSize(Slabs.
size());
341 void *NewSlab = Allocator.Allocate(AllocatedSlabSize, 0);
347 CurPtr = (
char *)(NewSlab);
348 End = ((
char *)NewSlab) + AllocatedSlabSize;
354 for (; I !=
E; ++
I) {
355 size_t AllocatedSlabSize =
356 computeSlabSize(std::distance(Slabs.
begin(),
I));
357 Allocator.Deallocate(*I, AllocatedSlabSize);
362 void DeallocateCustomSizedSlabs() {
363 for (
auto &PtrAndSize : CustomSizedSlabs) {
364 void *
Ptr = PtrAndSize.first;
365 size_t Size = PtrAndSize.second;
366 Allocator.Deallocate(Ptr, Size);
392 : Allocator(std::move(Old.Allocator)) {}
396 Allocator = std::move(RHS.Allocator);
404 auto DestroyElements = [](
char *Begin,
char *
End) {
407 reinterpret_cast<T *
>(
Ptr)->~
T();
410 for (
auto I = Allocator.Slabs.
begin(), E = Allocator.Slabs.
end(); I !=
E;
412 size_t AllocatedSlabSize = BumpPtrAllocator::computeSlabSize(
413 std::distance(Allocator.Slabs.
begin(),
I));
414 char *Begin = (
char *)
alignAddr(*I,
alignof(
T));
415 char *
End = *I == Allocator.Slabs.
back() ? Allocator.CurPtr
416 : (
char *)*I + AllocatedSlabSize;
418 DestroyElements(Begin, End);
421 for (
auto &PtrAndSize : Allocator.CustomSizedSlabs) {
422 void *
Ptr = PtrAndSize.first;
423 size_t Size = PtrAndSize.second;
424 DestroyElements((
char *)
alignAddr(Ptr,
alignof(
T)), (
char *)Ptr + Size);
436 template <
typename AllocatorT,
size_t SlabSize,
size_t SizeThreshold>
437 void *
operator new(
size_t Size,
453 template <
typename AllocatorT,
size_t SlabSize,
size_t SizeThreshold>
454 void operator delete(
458 #endif // LLVM_SUPPORT_ALLOCATOR_H
void push_back(const T &Elt)
#define __msan_allocated_memory(p, size)
size_t getBytesAllocated() const
Maximum length of the test input libFuzzer tries to guess a good value based on the corpus and reports it always prefer smaller inputs during the corpus shuffle When libFuzzer itself reports a bug this exit code will be used If indicates the maximal total time in seconds to run the fuzzer minimizes the provided crash input Use with tries to cleanse the provided crash input to make it contain fewer original bytes Use with exact_artifact_path to specify the output Experimental Use value profile to guide fuzzing Number of jobs to run If min(jobs, NumberOfCpuCores()/2)\" is used.") FUZZER_FLAG_INT(reload
void Deallocate(const void *Ptr, size_t Size)
Deallocate Ptr to Size bytes of memory allocated by this allocator.
size_t getTotalMemory() const
void printBumpPtrAllocatorStats(unsigned NumSlabs, size_t BytesAllocated, size_t TotalMemory)
void * Allocate(size_t Size, size_t Alignment)
Allocate Size bytes of Alignment aligned memory.
BumpPtrAllocatorImpl()=default
void Reset()
Deallocate all but the current slab and reset the current pointer to the beginning of it...
BumpPtrAllocatorImpl & operator=(BumpPtrAllocatorImpl &&RHS)
size_t GetNumSlabs() const
void Deallocate(const void *Ptr, size_t Size)
LLVM_NODISCARD bool empty() const
void DestroyAll()
Call the destructor of each allocated object and deallocate all but the current slab and reset the cu...
BumpPtrAllocatorImpl BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
#define __asan_unpoison_memory_region(p, size)
Allocate memory in an ever growing pool, as if by bump-pointer.
size_t alignmentAdjustment(const void *Ptr, size_t Alignment)
Returns the necessary adjustment for aligning Ptr to Alignment bytes, rounding up.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void * Allocate(size_t Size, size_t Alignment)
Allocate space at the specified alignment.
static const unsigned End
void Deallocate(const void *Ptr, size_t)
T * Allocate(size_t num=1)
Allocate space for an array of objects without constructing them.
void setRedZoneSize(size_t NewSize)
#define LLVM_ATTRIBUTE_RETURNS_NONNULL
~SpecificBumpPtrAllocator()
uint64_t NextPowerOf2(uint64_t A)
Returns the next power of two (in 64-bits) that is strictly greater than A.
iterator erase(const_iterator CI)
SpecificBumpPtrAllocator & operator=(SpecificBumpPtrAllocator &&RHS)
SpecificBumpPtrAllocator(SpecificBumpPtrAllocator &&Old)
SpecificBumpPtrAllocator()
A BumpPtrAllocator that allows only elements of a specific type to be allocated.
#define LLVM_ATTRIBUTE_RETURNS_NOALIAS
LLVM_ATTRIBUTE_RETURNS_NOALIAS Used to mark a function as returning a pointer that does not alias any...
typename SuperClass::iterator iterator
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
uintptr_t alignAddr(const void *Addr, size_t Alignment)
Aligns Addr to Alignment bytes, rounding up.
LLVM_ATTRIBUTE_RETURNS_NONNULL void * Allocate(size_t Size, size_t)
T * Allocate(size_t Num=1)
Allocate space for a sequence of objects without constructing them.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
std::enable_if< !std::is_same< typename std::remove_cv< T >::type, void >::value, void >::type Deallocate(T *Ptr, size_t Num=1)
Deallocate space for a sequence of objects without constructing them.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
#define __asan_poison_memory_region(p, size)
BumpPtrAllocatorImpl(T &&Allocator)
BumpPtrAllocatorImpl(BumpPtrAllocatorImpl &&Old)
CRTP base class providing obvious overloads for the core Allocate() methods of LLVM-style allocators...