#ifndef LLVM_SUPPORT_ALLOCATOR_H
#define LLVM_SUPPORT_ALLOCATOR_H
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/Alignment.h"
#include "llvm/Support/AllocatorBase.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/MathExtras.h"
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <iterator>
#include <utility>
namespace llvm {
namespace detail {
void printBumpPtrAllocatorStats(unsigned NumSlabs, size_t BytesAllocated,
size_t TotalMemory);
}
template <typename AllocatorT = MallocAllocator, size_t SlabSize = 4096,
size_t SizeThreshold = SlabSize, size_t GrowthDelay = 128>
class BumpPtrAllocatorImpl
: public AllocatorBase<BumpPtrAllocatorImpl<AllocatorT, SlabSize,
SizeThreshold, GrowthDelay>>,
private detail::AllocatorHolder<AllocatorT> {
using AllocTy = detail::AllocatorHolder<AllocatorT>;
public:
static_assert(SizeThreshold <= SlabSize,
"The SizeThreshold must be at most the SlabSize to ensure "
"that objects larger than a slab go into their own memory "
"allocation.");
static_assert(GrowthDelay > 0,
"GrowthDelay must be at least 1 which already increases the"
"slab size after each allocated slab.");
BumpPtrAllocatorImpl() = default;
template <typename T>
BumpPtrAllocatorImpl(T &&Allocator)
: AllocTy(std::forward<T &&>(Allocator)) {}
BumpPtrAllocatorImpl(BumpPtrAllocatorImpl &&Old)
: AllocTy(std::move(Old.getAllocator())), CurPtr(Old.CurPtr),
End(Old.End), Slabs(std::move(Old.Slabs)),
CustomSizedSlabs(std::move(Old.CustomSizedSlabs)),
BytesAllocated(Old.BytesAllocated), RedZoneSize(Old.RedZoneSize) {
Old.CurPtr = Old.End = nullptr;
Old.BytesAllocated = 0;
Old.Slabs.clear();
Old.CustomSizedSlabs.clear();
}
~BumpPtrAllocatorImpl() {
DeallocateSlabs(Slabs.begin(), Slabs.end());
DeallocateCustomSizedSlabs();
}
BumpPtrAllocatorImpl &operator=(BumpPtrAllocatorImpl &&RHS) {
DeallocateSlabs(Slabs.begin(), Slabs.end());
DeallocateCustomSizedSlabs();
CurPtr = RHS.CurPtr;
End = RHS.End;
BytesAllocated = RHS.BytesAllocated;
RedZoneSize = RHS.RedZoneSize;
Slabs = std::move(RHS.Slabs);
CustomSizedSlabs = std::move(RHS.CustomSizedSlabs);
AllocTy::operator=(std::move(RHS.getAllocator()));
RHS.CurPtr = RHS.End = nullptr;
RHS.BytesAllocated = 0;
RHS.Slabs.clear();
RHS.CustomSizedSlabs.clear();
return *this;
}
void Reset() {
DeallocateCustomSizedSlabs();
CustomSizedSlabs.clear();
if (Slabs.empty())
return;
BytesAllocated = 0;
CurPtr = (char *)Slabs.front();
End = CurPtr + SlabSize;
__asan_poison_memory_region(*Slabs.begin(), computeSlabSize(0));
DeallocateSlabs(std::next(Slabs.begin()), Slabs.end());
Slabs.erase(std::next(Slabs.begin()), Slabs.end());
}
LLVM_ATTRIBUTE_RETURNS_NONNULL void *Allocate(size_t Size, Align Alignment) {
BytesAllocated += Size;
size_t Adjustment = offsetToAlignedAddr(CurPtr, Alignment);
assert(Adjustment + Size >= Size && "Adjustment + Size must not overflow");
size_t SizeToAllocate = Size;
#if LLVM_ADDRESS_SANITIZER_BUILD
SizeToAllocate += RedZoneSize;
#endif
if (Adjustment + SizeToAllocate <= size_t(End - CurPtr)
&& CurPtr != nullptr) {
char *AlignedPtr = CurPtr + Adjustment;
CurPtr = AlignedPtr + SizeToAllocate;
__msan_allocated_memory(AlignedPtr, Size);
__asan_unpoison_memory_region(AlignedPtr, Size);
return AlignedPtr;
}
size_t PaddedSize = SizeToAllocate + Alignment.value() - 1;
if (PaddedSize > SizeThreshold) {
void *NewSlab =
this->getAllocator().Allocate(PaddedSize, alignof(std::max_align_t));
__asan_poison_memory_region(NewSlab, PaddedSize);
CustomSizedSlabs.push_back(std::make_pair(NewSlab, PaddedSize));
uintptr_t AlignedAddr = alignAddr(NewSlab, Alignment);
assert(AlignedAddr + Size <= (uintptr_t)NewSlab + PaddedSize);
char *AlignedPtr = (char*)AlignedAddr;
__msan_allocated_memory(AlignedPtr, Size);
__asan_unpoison_memory_region(AlignedPtr, Size);
return AlignedPtr;
}
StartNewSlab();
uintptr_t AlignedAddr = alignAddr(CurPtr, Alignment);
assert(AlignedAddr + SizeToAllocate <= (uintptr_t)End &&
"Unable to allocate memory!");
char *AlignedPtr = (char*)AlignedAddr;
CurPtr = AlignedPtr + SizeToAllocate;
__msan_allocated_memory(AlignedPtr, Size);
__asan_unpoison_memory_region(AlignedPtr, Size);
return AlignedPtr;
}
inline LLVM_ATTRIBUTE_RETURNS_NONNULL void *
Allocate(size_t Size, size_t Alignment) {
assert(Alignment > 0 && "0-byte alignment is not allowed. Use 1 instead.");
return Allocate(Size, Align(Alignment));
}
using AllocatorBase<BumpPtrAllocatorImpl>::Allocate;
void Deallocate(const void *Ptr, size_t Size, size_t ) {
__asan_poison_memory_region(Ptr, Size);
}
using AllocatorBase<BumpPtrAllocatorImpl>::Deallocate;
size_t GetNumSlabs() const { return Slabs.size() + CustomSizedSlabs.size(); }
llvm::Optional<int64_t> identifyObject(const void *Ptr) {
const char *P = static_cast<const char *>(Ptr);
int64_t InSlabIdx = 0;
for (size_t Idx = 0, E = Slabs.size(); Idx < E; Idx++) {
const char *S = static_cast<const char *>(Slabs[Idx]);
if (P >= S && P < S + computeSlabSize(Idx))
return InSlabIdx + static_cast<int64_t>(P - S);
InSlabIdx += static_cast<int64_t>(computeSlabSize(Idx));
}
int64_t InCustomSizedSlabIdx = -1;
for (size_t Idx = 0, E = CustomSizedSlabs.size(); Idx < E; Idx++) {
const char *S = static_cast<const char *>(CustomSizedSlabs[Idx].first);
size_t Size = CustomSizedSlabs[Idx].second;
if (P >= S && P < S + Size)
return InCustomSizedSlabIdx - static_cast<int64_t>(P - S);
InCustomSizedSlabIdx -= static_cast<int64_t>(Size);
}
return None;
}
int64_t identifyKnownObject(const void *Ptr) {
Optional<int64_t> Out = identifyObject(Ptr);
assert(Out && "Wrong allocator used");
return *Out;
}
template <typename T>
int64_t identifyKnownAlignedObject(const void *Ptr) {
int64_t Out = identifyKnownObject(Ptr);
assert(Out % alignof(T) == 0 && "Wrong alignment information");
return Out / alignof(T);
}
size_t getTotalMemory() const {
size_t TotalMemory = 0;
for (auto I = Slabs.begin(), E = Slabs.end(); I != E; ++I)
TotalMemory += computeSlabSize(std::distance(Slabs.begin(), I));
for (const auto &PtrAndSize : CustomSizedSlabs)
TotalMemory += PtrAndSize.second;
return TotalMemory;
}
size_t getBytesAllocated() const { return BytesAllocated; }
void setRedZoneSize(size_t NewSize) {
RedZoneSize = NewSize;
}
void PrintStats() const {
detail::printBumpPtrAllocatorStats(Slabs.size(), BytesAllocated,
getTotalMemory());
}
private:
char *CurPtr = nullptr;
char *End = nullptr;
SmallVector<void *, 4> Slabs;
SmallVector<std::pair<void *, size_t>, 0> CustomSizedSlabs;
size_t BytesAllocated = 0;
size_t RedZoneSize = 1;
static size_t computeSlabSize(unsigned SlabIdx) {
return SlabSize *
((size_t)1 << std::min<size_t>(30, SlabIdx / GrowthDelay));
}
void StartNewSlab() {
size_t AllocatedSlabSize = computeSlabSize(Slabs.size());
void *NewSlab = this->getAllocator().Allocate(AllocatedSlabSize,
alignof(std::max_align_t));
__asan_poison_memory_region(NewSlab, AllocatedSlabSize);
Slabs.push_back(NewSlab);
CurPtr = (char *)(NewSlab);
End = ((char *)NewSlab) + AllocatedSlabSize;
}
void DeallocateSlabs(SmallVectorImpl<void *>::iterator I,
SmallVectorImpl<void *>::iterator E) {
for (; I != E; ++I) {
size_t AllocatedSlabSize =
computeSlabSize(std::distance(Slabs.begin(), I));
this->getAllocator().Deallocate(*I, AllocatedSlabSize,
alignof(std::max_align_t));
}
}
void DeallocateCustomSizedSlabs() {
for (auto &PtrAndSize : CustomSizedSlabs) {
void *Ptr = PtrAndSize.first;
size_t Size = PtrAndSize.second;
this->getAllocator().Deallocate(Ptr, Size, alignof(std::max_align_t));
}
}
template <typename T> friend class SpecificBumpPtrAllocator;
};
typedef BumpPtrAllocatorImpl<> BumpPtrAllocator;
template <typename T> class SpecificBumpPtrAllocator {
BumpPtrAllocator Allocator;
public:
SpecificBumpPtrAllocator() {
Allocator.setRedZoneSize(0);
}
SpecificBumpPtrAllocator(SpecificBumpPtrAllocator &&Old)
: Allocator(std::move(Old.Allocator)) {}
~SpecificBumpPtrAllocator() { DestroyAll(); }
SpecificBumpPtrAllocator &operator=(SpecificBumpPtrAllocator &&RHS) {
Allocator = std::move(RHS.Allocator);
return *this;
}
void DestroyAll() {
auto DestroyElements = [](char *Begin, char *End) {
assert(Begin == (char *)alignAddr(Begin, Align::Of<T>()));
for (char *Ptr = Begin; Ptr + sizeof(T) <= End; Ptr += sizeof(T))
reinterpret_cast<T *>(Ptr)->~T();
};
for (auto I = Allocator.Slabs.begin(), E = Allocator.Slabs.end(); I != E;
++I) {
size_t AllocatedSlabSize = BumpPtrAllocator::computeSlabSize(
std::distance(Allocator.Slabs.begin(), I));
char *Begin = (char *)alignAddr(*I, Align::Of<T>());
char *End = *I == Allocator.Slabs.back() ? Allocator.CurPtr
: (char *)*I + AllocatedSlabSize;
DestroyElements(Begin, End);
}
for (auto &PtrAndSize : Allocator.CustomSizedSlabs) {
void *Ptr = PtrAndSize.first;
size_t Size = PtrAndSize.second;
DestroyElements((char *)alignAddr(Ptr, Align::Of<T>()),
(char *)Ptr + Size);
}
Allocator.Reset();
}
T *Allocate(size_t num = 1) { return Allocator.Allocate<T>(num); }
};
}
template <typename AllocatorT, size_t SlabSize, size_t SizeThreshold,
size_t GrowthDelay>
void *
operator new(size_t Size,
llvm::BumpPtrAllocatorImpl<AllocatorT, SlabSize, SizeThreshold,
GrowthDelay> &Allocator) {
return Allocator.Allocate(Size, std::min((size_t)llvm::NextPowerOf2(Size),
alignof(std::max_align_t)));
}
template <typename AllocatorT, size_t SlabSize, size_t SizeThreshold,
size_t GrowthDelay>
void operator delete(void *,
llvm::BumpPtrAllocatorImpl<AllocatorT, SlabSize,
SizeThreshold, GrowthDelay> &) {
}
#endif