16 #include "llvm/Support/DataTypes.h"
24 BumpPtrAllocator::BumpPtrAllocator(
size_t size,
size_t threshold,
26 : SlabSize(size), SizeThreshold(std::min(size, threshold)),
27 Allocator(allocator), CurSlab(0), BytesAllocated(0) { }
29 BumpPtrAllocator::BumpPtrAllocator(
size_t size,
size_t threshold)
30 : SlabSize(size), SizeThreshold(std::min(size, threshold)),
31 Allocator(DefaultSlabAllocator), CurSlab(0), BytesAllocated(0) { }
34 DeallocateSlabs(CurSlab);
40 char *BumpPtrAllocator::AlignPtr(
char *Ptr,
size_t Alignment) {
41 assert(Alignment && (Alignment & (Alignment - 1)) == 0 &&
42 "Alignment is not a power of two!");
45 return (
char*)(((uintptr_t)Ptr + Alignment - 1) &
46 ~(uintptr_t)(Alignment - 1));
51 void BumpPtrAllocator::StartNewSlab() {
55 if (BytesAllocated >= SlabSize * 128)
58 MemSlab *NewSlab = Allocator.
Allocate(SlabSize);
61 CurPtr = (
char*)(CurSlab + 1);
62 End = ((
char*)CurSlab) + CurSlab->
Size;
67 void BumpPtrAllocator::DeallocateSlabs(MemSlab *Slab) {
69 MemSlab *NextSlab = Slab->NextPtr;
74 memset(Slab + 1, 0xCD, Slab->Size -
sizeof(MemSlab));
86 DeallocateSlabs(CurSlab->
NextPtr);
88 CurPtr = (
char*)(CurSlab + 1);
89 End = ((
char*)CurSlab) + CurSlab->
Size;
100 BytesAllocated += Size;
103 if (Alignment == 0) Alignment = 1;
106 char *Ptr = AlignPtr(CurPtr, Alignment);
109 if (Ptr + Size <= End) {
119 size_t PaddedSize = Size +
sizeof(
MemSlab) + Alignment - 1;
120 if (PaddedSize > SizeThreshold) {
128 Ptr = AlignPtr((
char*)(NewSlab + 1), Alignment);
129 assert((uintptr_t)Ptr + Size <= (uintptr_t)NewSlab + NewSlab->
Size);
136 Ptr = AlignPtr(CurPtr, Alignment);
138 assert(CurPtr <= End &&
"Unable to allocate memory!");
144 unsigned NumSlabs = 0;
152 size_t TotalMemory = 0;
154 TotalMemory += Slab->Size;
160 unsigned NumSlabs = 0;
161 size_t TotalMemory = 0;
163 TotalMemory += Slab->Size;
167 errs() <<
"\nNumber of memory regions: " << NumSlabs <<
'\n'
168 <<
"Bytes used: " << BytesAllocated <<
'\n'
169 <<
"Bytes allocated: " << TotalMemory <<
'\n'
170 <<
"Bytes wasted: " << (TotalMemory - BytesAllocated)
171 <<
" (includes alignment, etc)\n";
191 size_t FreeListSize) {
192 errs() <<
"Recycler element size: " << Size <<
'\n'
193 <<
"Recycler element alignment: " << Align <<
'\n'
194 <<
"Number of elements free for recycling: " << FreeListSize <<
'\n';
#define __msan_allocated_memory(p, size)
static bool setRangeWritable(const void *Addr, size_t Size)
void PrintRecyclerStats(size_t Size, size_t Align, size_t FreeListSize)
virtual void Deallocate(MemSlab *Slab)=0
void * Allocate(size_t Size, size_t)
virtual MemSlab * Allocate(size_t Size) LLVM_OVERRIDE
void Deallocate(const void *Ptr)
virtual MemSlab * Allocate(size_t Size)=0
unsigned GetNumSlabs() const
static cl::opt< AlignMode > Align(cl::desc("Load/store alignment support"), cl::Hidden, cl::init(DefaultAlign), cl::values(clEnumValN(DefaultAlign,"arm-default-align","Generate unaligned accesses only on hardware/OS ""combinations that are known to support them"), clEnumValN(StrictAlign,"arm-strict-align","Disallow all unaligned memory accesses"), clEnumValN(NoStrictAlign,"arm-no-strict-align","Allow unaligned memory accesses"), clEnumValEnd))
virtual ~MallocSlabAllocator()
size_t getTotalMemory() const
Compute the total physical memory allocated by this allocator.
virtual void Deallocate(MemSlab *Slab) LLVM_OVERRIDE