20 #ifndef LLVM_SUPPORT_ALLOCATOR_H 21 #define LLVM_SUPPORT_ALLOCATOR_H 36 #include <type_traits> 53 static_assert(
static_cast<void *(
AllocatorBase::*)(
size_t,
size_t)
>(
55 static_cast<void *(DerivedT::*)(
size_t,
size_t)
>(
57 "Class derives from AllocatorBase without implementing the " 58 "core Allocate(size_t, size_t) overload!");
60 return static_cast<DerivedT *
>(
this)->
Allocate(Size, Alignment);
67 static_assert(
static_cast<void (
AllocatorBase::*)(
const void *,
size_t)
>(
69 static_cast<void (DerivedT::*)(
const void *,
size_t)
>(
70 &DerivedT::Deallocate),
71 "Class derives from AllocatorBase without implementing the " 72 "core Deallocate(void *) overload!");
74 return static_cast<DerivedT *
>(
this)->
Deallocate(Ptr, Size);
82 return static_cast<T *
>(
Allocate(Num *
sizeof(
T),
alignof(
T)));
87 typename std::enable_if<
88 !std::is_same<typename std::remove_cv<T>::type,
void>::value,
void>
::type 90 Deallocate(static_cast<const void *>(Ptr), Num *
sizeof(
T));
107 free(const_cast<void *>(Ptr));
139 template <
typename AllocatorT =
MallocAllocator,
size_t SlabSize = 4096,
140 size_t SizeThreshold = SlabSize>
143 BumpPtrAllocatorImpl<AllocatorT, SlabSize, SizeThreshold>> {
145 static_assert(SizeThreshold <= SlabSize,
146 "The SizeThreshold must be at most the SlabSize to ensure " 147 "that objects larger than a slab go into their own memory " 152 template <
typename T>
159 : CurPtr(Old.CurPtr), End(Old.End), Slabs(
std::move(Old.Slabs)),
160 CustomSizedSlabs(
std::move(Old.CustomSizedSlabs)),
161 BytesAllocated(Old.BytesAllocated), RedZoneSize(Old.RedZoneSize),
163 Old.CurPtr = Old.End =
nullptr;
164 Old.BytesAllocated = 0;
166 Old.CustomSizedSlabs.clear();
170 DeallocateSlabs(Slabs.begin(), Slabs.end());
171 DeallocateCustomSizedSlabs();
175 DeallocateSlabs(Slabs.begin(), Slabs.end());
176 DeallocateCustomSizedSlabs();
180 BytesAllocated = RHS.BytesAllocated;
181 RedZoneSize = RHS.RedZoneSize;
182 Slabs = std::move(RHS.Slabs);
183 CustomSizedSlabs = std::move(RHS.CustomSizedSlabs);
186 RHS.CurPtr = RHS.End =
nullptr;
187 RHS.BytesAllocated = 0;
189 RHS.CustomSizedSlabs.clear();
197 DeallocateCustomSizedSlabs();
198 CustomSizedSlabs.clear();
205 CurPtr = (
char *)Slabs.front();
206 End = CurPtr + SlabSize;
209 DeallocateSlabs(std::next(Slabs.begin()), Slabs.end());
210 Slabs.erase(std::next(Slabs.begin()), Slabs.end());
217 BytesAllocated +=
Size;
220 assert(Adjustment + Size >= Size &&
"Adjustment + Size must not overflow");
222 size_t SizeToAllocate =
Size;
223 #if LLVM_ADDRESS_SANITIZER_BUILD 225 SizeToAllocate += RedZoneSize;
229 if (Adjustment + SizeToAllocate <=
size_t(End - CurPtr)) {
230 char *AlignedPtr = CurPtr + Adjustment;
231 CurPtr = AlignedPtr + SizeToAllocate;
242 size_t PaddedSize = SizeToAllocate + Alignment.
value() - 1;
243 if (PaddedSize > SizeThreshold) {
244 void *NewSlab =
Allocator.Allocate(PaddedSize, 0);
248 CustomSizedSlabs.push_back(std::make_pair(NewSlab, PaddedSize));
250 uintptr_t AlignedAddr =
alignAddr(NewSlab, Alignment);
251 assert(AlignedAddr + Size <= (uintptr_t)NewSlab + PaddedSize);
252 char *AlignedPtr = (
char*)AlignedAddr;
260 uintptr_t AlignedAddr =
alignAddr(CurPtr, Alignment);
261 assert(AlignedAddr + SizeToAllocate <= (uintptr_t)End &&
262 "Unable to allocate memory!");
263 char *AlignedPtr = (
char*)AlignedAddr;
264 CurPtr = AlignedPtr + SizeToAllocate;
272 assert(Alignment > 0 &&
"0-byte alignment is not allowed. Use 1 instead.");
289 size_t GetNumSlabs()
const {
return Slabs.size() + CustomSizedSlabs.size(); }
297 const char *
P =
static_cast<const char *
>(Ptr);
298 int64_t InSlabIdx = 0;
299 for (
size_t Idx = 0,
E = Slabs.size(); Idx <
E; Idx++) {
300 const char *S =
static_cast<const char *
>(Slabs[Idx]);
301 if (P >= S && P < S + computeSlabSize(Idx))
302 return InSlabIdx + static_cast<int64_t>(P - S);
303 InSlabIdx +=
static_cast<int64_t
>(computeSlabSize(Idx));
307 int64_t InCustomSizedSlabIdx = -1;
308 for (
size_t Idx = 0, E = CustomSizedSlabs.size(); Idx <
E; Idx++) {
309 const char *S =
static_cast<const char *
>(CustomSizedSlabs[Idx].first);
310 size_t Size = CustomSizedSlabs[Idx].second;
311 if (P >= S && P < S + Size)
312 return InCustomSizedSlabIdx -
static_cast<int64_t
>(P - S);
313 InCustomSizedSlabIdx -=
static_cast<int64_t
>(
Size);
324 assert(Out &&
"Wrong allocator used");
338 template <
typename T>
340 int64_t Out = identifyKnownObject(Ptr);
341 assert(Out %
alignof(
T) == 0 &&
"Wrong alignment information");
342 return Out /
alignof(
T);
346 size_t TotalMemory = 0;
347 for (
auto I = Slabs.begin(),
E = Slabs.end();
I !=
E; ++
I)
348 TotalMemory += computeSlabSize(std::distance(Slabs.begin(),
I));
349 for (
auto &PtrAndSize : CustomSizedSlabs)
350 TotalMemory += PtrAndSize.second;
357 RedZoneSize = NewSize;
369 char *CurPtr =
nullptr;
383 size_t BytesAllocated = 0;
387 size_t RedZoneSize = 1;
392 static size_t computeSlabSize(
unsigned SlabIdx) {
397 return SlabSize * ((
size_t)1 << std::min<size_t>(30, SlabIdx / 128));
402 void StartNewSlab() {
403 size_t AllocatedSlabSize = computeSlabSize(Slabs.
size());
405 void *NewSlab = Allocator.Allocate(AllocatedSlabSize, 0);
411 CurPtr = (
char *)(NewSlab);
412 End = ((
char *)NewSlab) + AllocatedSlabSize;
418 for (; I !=
E; ++
I) {
419 size_t AllocatedSlabSize =
420 computeSlabSize(std::distance(Slabs.
begin(),
I));
421 Allocator.Deallocate(*I, AllocatedSlabSize);
426 void DeallocateCustomSizedSlabs() {
427 for (
auto &PtrAndSize : CustomSizedSlabs) {
428 void *Ptr = PtrAndSize.first;
429 size_t Size = PtrAndSize.second;
430 Allocator.Deallocate(Ptr, Size);
456 : Allocator(
std::move(Old.Allocator)) {}
460 Allocator = std::move(RHS.Allocator);
468 auto DestroyElements = [](
char *Begin,
char *End) {
470 for (
char *Ptr = Begin; Ptr +
sizeof(
T) <= End; Ptr +=
sizeof(
T))
471 reinterpret_cast<T *
>(Ptr)->~
T();
474 for (
auto I = Allocator.Slabs.
begin(),
E = Allocator.Slabs.
end();
I !=
E;
476 size_t AllocatedSlabSize = BumpPtrAllocator::computeSlabSize(
477 std::distance(Allocator.Slabs.
begin(),
I));
478 char *Begin = (
char *)
alignAddr(*
I, Align::Of<T>());
479 char *End = *
I == Allocator.Slabs.
back() ? Allocator.CurPtr
480 : (
char *)*
I + AllocatedSlabSize;
482 DestroyElements(Begin, End);
485 for (
auto &PtrAndSize : Allocator.CustomSizedSlabs) {
486 void *Ptr = PtrAndSize.first;
487 size_t Size = PtrAndSize.second;
488 DestroyElements((
char *)
alignAddr(Ptr, Align::Of<T>()),
501 template <
typename AllocatorT,
size_t SlabSize,
size_t SizeThreshold>
502 void *
operator new(
size_t Size,
518 template <
typename AllocatorT,
size_t SlabSize,
size_t SizeThreshold>
519 void operator delete(
523 #endif // LLVM_SUPPORT_ALLOCATOR_H
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
#define __msan_allocated_memory(p, size)
This class represents lattice values for constants.
uint64_t offsetToAlignedAddr(const void *Addr, Align Alignment)
Returns the necessary adjustment for aligning Addr to Alignment bytes, rounding up.
void Deallocate(const void *Ptr, size_t Size)
Deallocate Ptr to Size bytes of memory allocated by this allocator.
void push_back(const T &Elt)
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.
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'...
uintptr_t alignAddr(const void *Addr, Align Alignment)
Aligns Addr to Alignment bytes, rounding up.
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...
uint64_t value() const
This is a hole in the type system and should not be abused.
size_t getBytesAllocated() const
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.
llvm::Optional< int64_t > identifyObject(const void *Ptr)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void * Allocate(size_t Size, size_t Alignment)
#define offsetof(TYPE, MEMBER)
int64_t identifyKnownObject(const void *Ptr)
A wrapper around identifyObject that additionally asserts that the object is indeed within the alloca...
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.
SpecificBumpPtrAllocator & operator=(SpecificBumpPtrAllocator &&RHS)
This struct is a compact representation of a valid (non-zero power of two) alignment.
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
LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void * Allocate(size_t Size, Align Alignment)
Allocate space at the specified alignment.
#define LLVM_ATTRIBUTE_RETURNS_NOALIAS
LLVM_ATTRIBUTE_RETURNS_NOALIAS Used to mark a function as returning a pointer that does not alias any...
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.
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())
#define __asan_poison_memory_region(p, size)
BumpPtrAllocatorImpl(T &&Allocator)
BumpPtrAllocatorImpl(BumpPtrAllocatorImpl &&Old)
int64_t identifyKnownAlignedObject(const void *Ptr)
A wrapper around identifyKnownObject.
CRTP base class providing obvious overloads for the core Allocate() methods of LLVM-style allocators...