21 #ifndef LLVM_SUPPORT_ALLOCATOR_H 22 #define LLVM_SUPPORT_ALLOCATOR_H 35 #include <type_traits> 52 static_assert(
static_cast<void *(
AllocatorBase::*)(
size_t,
size_t)
>(
54 static_cast<void *(DerivedT::*)(
size_t,
size_t)
>(
56 "Class derives from AllocatorBase without implementing the " 57 "core Allocate(size_t, size_t) overload!");
59 return static_cast<DerivedT *
>(
this)->
Allocate(Size, Alignment);
66 static_assert(
static_cast<void (
AllocatorBase::*)(
const void *,
size_t)
>(
68 static_cast<void (DerivedT::*)(
const void *,
size_t)
>(
69 &DerivedT::Deallocate),
70 "Class derives from AllocatorBase without implementing the " 71 "core Deallocate(void *) overload!");
73 return static_cast<DerivedT *
>(
this)->
Deallocate(Ptr, Size);
81 return static_cast<T *
>(
Allocate(Num *
sizeof(
T),
alignof(
T)));
86 typename std::enable_if<
87 !std::is_same<typename std::remove_cv<T>::type,
void>::value,
void>
::type 89 Deallocate(static_cast<const void *>(Ptr), Num *
sizeof(
T));
106 free(const_cast<void *>(Ptr));
138 template <
typename AllocatorT =
MallocAllocator,
size_t SlabSize = 4096,
139 size_t SizeThreshold = SlabSize>
142 BumpPtrAllocatorImpl<AllocatorT, SlabSize, SizeThreshold>> {
144 static_assert(SizeThreshold <= SlabSize,
145 "The SizeThreshold must be at most the SlabSize to ensure " 146 "that objects larger than a slab go into their own memory " 151 template <
typename T>
158 : CurPtr(Old.CurPtr), End(Old.End), Slabs(
std::move(Old.Slabs)),
159 CustomSizedSlabs(
std::move(Old.CustomSizedSlabs)),
160 BytesAllocated(Old.BytesAllocated), RedZoneSize(Old.RedZoneSize),
162 Old.CurPtr = Old.End =
nullptr;
163 Old.BytesAllocated = 0;
165 Old.CustomSizedSlabs.clear();
169 DeallocateSlabs(Slabs.begin(), Slabs.end());
170 DeallocateCustomSizedSlabs();
174 DeallocateSlabs(Slabs.begin(), Slabs.end());
175 DeallocateCustomSizedSlabs();
179 BytesAllocated = RHS.BytesAllocated;
180 RedZoneSize = RHS.RedZoneSize;
181 Slabs = std::move(RHS.Slabs);
182 CustomSizedSlabs = std::move(RHS.CustomSizedSlabs);
185 RHS.CurPtr = RHS.End =
nullptr;
186 RHS.BytesAllocated = 0;
188 RHS.CustomSizedSlabs.clear();
196 DeallocateCustomSizedSlabs();
197 CustomSizedSlabs.clear();
204 CurPtr = (
char *)Slabs.front();
205 End = CurPtr + SlabSize;
208 DeallocateSlabs(std::next(Slabs.begin()), Slabs.end());
209 Slabs.erase(std::next(Slabs.begin()), Slabs.end());
215 assert(Alignment > 0 &&
"0-byte alignnment is not allowed. Use 1 instead.");
218 BytesAllocated +=
Size;
221 assert(Adjustment + Size >= Size &&
"Adjustment + Size must not overflow");
223 size_t SizeToAllocate =
Size;
224 #if LLVM_ADDRESS_SANITIZER_BUILD 226 SizeToAllocate += RedZoneSize;
230 if (Adjustment + SizeToAllocate <=
size_t(End - CurPtr)) {
231 char *AlignedPtr = CurPtr + Adjustment;
232 CurPtr = AlignedPtr + SizeToAllocate;
243 size_t PaddedSize = SizeToAllocate + Alignment - 1;
244 if (PaddedSize > SizeThreshold) {
245 void *NewSlab =
Allocator.Allocate(PaddedSize, 0);
249 CustomSizedSlabs.push_back(std::make_pair(NewSlab, PaddedSize));
251 uintptr_t AlignedAddr =
alignAddr(NewSlab, Alignment);
252 assert(AlignedAddr + Size <= (uintptr_t)NewSlab + PaddedSize);
253 char *AlignedPtr = (
char*)AlignedAddr;
261 uintptr_t AlignedAddr =
alignAddr(CurPtr, Alignment);
262 assert(AlignedAddr + SizeToAllocate <= (uintptr_t)End &&
263 "Unable to allocate memory!");
264 char *AlignedPtr = (
char*)AlignedAddr;
265 CurPtr = AlignedPtr + SizeToAllocate;
284 size_t GetNumSlabs()
const {
return Slabs.size() + CustomSizedSlabs.size(); }
287 size_t TotalMemory = 0;
288 for (
auto I = Slabs.begin(),
E = Slabs.end();
I !=
E; ++
I)
289 TotalMemory += computeSlabSize(std::distance(Slabs.begin(),
I));
290 for (
auto &PtrAndSize : CustomSizedSlabs)
291 TotalMemory += PtrAndSize.second;
298 RedZoneSize = NewSize;
310 char *CurPtr =
nullptr;
324 size_t BytesAllocated = 0;
328 size_t RedZoneSize = 1;
333 static size_t computeSlabSize(
unsigned SlabIdx) {
338 return SlabSize * ((
size_t)1 << std::min<size_t>(30, SlabIdx / 128));
343 void StartNewSlab() {
344 size_t AllocatedSlabSize = computeSlabSize(Slabs.
size());
346 void *NewSlab = Allocator.Allocate(AllocatedSlabSize, 0);
352 CurPtr = (
char *)(NewSlab);
353 End = ((
char *)NewSlab) + AllocatedSlabSize;
359 for (; I !=
E; ++
I) {
360 size_t AllocatedSlabSize =
361 computeSlabSize(std::distance(Slabs.
begin(),
I));
362 Allocator.Deallocate(*I, AllocatedSlabSize);
367 void DeallocateCustomSizedSlabs() {
368 for (
auto &PtrAndSize : CustomSizedSlabs) {
369 void *
Ptr = PtrAndSize.first;
370 size_t Size = PtrAndSize.second;
371 Allocator.Deallocate(Ptr, Size);
397 : Allocator(
std::move(Old.Allocator)) {}
401 Allocator = std::move(RHS.Allocator);
409 auto DestroyElements = [](
char *Begin,
char *End) {
411 for (
char *
Ptr = Begin;
Ptr +
sizeof(
T) <= End;
Ptr +=
sizeof(
T))
412 reinterpret_cast<T *
>(Ptr)->~
T();
415 for (
auto I = Allocator.Slabs.
begin(),
E = Allocator.Slabs.
end();
I !=
E;
417 size_t AllocatedSlabSize = BumpPtrAllocator::computeSlabSize(
418 std::distance(Allocator.Slabs.
begin(),
I));
420 char *End = *
I == Allocator.Slabs.
back() ? Allocator.CurPtr
421 : (
char *)*
I + AllocatedSlabSize;
423 DestroyElements(Begin, End);
426 for (
auto &PtrAndSize : Allocator.CustomSizedSlabs) {
427 void *
Ptr = PtrAndSize.first;
428 size_t Size = PtrAndSize.second;
429 DestroyElements((
char *)
alignAddr(Ptr,
alignof(
T)), (
char *)Ptr + Size);
441 template <
typename AllocatorT,
size_t SlabSize,
size_t SizeThreshold>
442 void *
operator new(
size_t Size,
458 template <
typename AllocatorT,
size_t SlabSize,
size_t SizeThreshold>
459 void operator delete(
463 #endif // LLVM_SUPPORT_ALLOCATOR_H void push_back(const T &Elt)
#define LLVM_ATTRIBUTE_RETURNS_NOALIAS
LLVM_ATTRIBUTE_RETURNS_NOALIAS Used to mark a function as returning a pointer that does not alias any...
Compute iterated dominance frontiers using a linear time algorithm.
void Deallocate(const void *Ptr, size_t Size)
Deallocate Ptr to Size bytes of memory allocated by this allocator.
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.
#define __asan_unpoison_memory_region(p, size)
void Reset()
Deallocate all but the current slab and reset the current pointer to the beginning of it...
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
BumpPtrAllocatorImpl & operator=(BumpPtrAllocatorImpl &&RHS)
This file defines counterparts of C library allocation functions defined in the namespace 'std'...
void Deallocate(const void *Ptr, size_t Size)
void DestroyAll()
Call the destructor of each allocated object and deallocate all but the current slab and reset the cu...
size_t getBytesAllocated() const
BumpPtrAllocatorImpl BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
#define LLVM_ATTRIBUTE_RETURNS_NONNULL
Allocate memory in an ever growing pool, as if by bump-pointer.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
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.
#define offsetof(TYPE, MEMBER)
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)
~SpecificBumpPtrAllocator()
uint64_t NextPowerOf2(uint64_t A)
Returns the next power of two (in 64-bits) that is strictly greater than A.
SpecificBumpPtrAllocator & operator=(SpecificBumpPtrAllocator &&RHS)
LLVM_ATTRIBUTE_RETURNS_NONNULL void * safe_malloc(size_t Sz)
SpecificBumpPtrAllocator(SpecificBumpPtrAllocator &&Old)
SpecificBumpPtrAllocator()
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
A BumpPtrAllocator that allows only elements of a specific type to be allocated.
size_t GetNumSlabs() const
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()
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.
size_t getTotalMemory() const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
#define __msan_allocated_memory(p, size)
BumpPtrAllocatorImpl(T &&Allocator)
BumpPtrAllocatorImpl(BumpPtrAllocatorImpl &&Old)
#define __asan_poison_memory_region(p, size)
CRTP base class providing obvious overloads for the core Allocate() methods of LLVM-style allocators...