1 //===--- Allocator.cpp - Simple memory allocation abstraction -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the BumpPtrAllocator interface.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/Allocator.h"
15 #include "llvm/Support/Compiler.h"
16 #include "llvm/Support/DataTypes.h"
17 #include "llvm/Support/Memory.h"
18 #include "llvm/Support/Recycler.h"
19 #include "llvm/Support/raw_ostream.h"
24 BumpPtrAllocator::BumpPtrAllocator(size_t size, size_t threshold,
25 SlabAllocator &allocator)
26 : SlabSize(size), SizeThreshold(std::min(size, threshold)),
27 Allocator(allocator), CurSlab(0), BytesAllocated(0) { }
29 BumpPtrAllocator::~BumpPtrAllocator() {
30 DeallocateSlabs(CurSlab);
33 /// AlignPtr - Align Ptr to Alignment bytes, rounding up. Alignment should
34 /// be a power of two. This method rounds up, so AlignPtr(7, 4) == 8 and
35 /// AlignPtr(8, 4) == 8.
36 char *BumpPtrAllocator::AlignPtr(char *Ptr, size_t Alignment) {
37 assert(Alignment && (Alignment & (Alignment - 1)) == 0 &&
38 "Alignment is not a power of two!");
41 return (char*)(((uintptr_t)Ptr + Alignment - 1) &
42 ~(uintptr_t)(Alignment - 1));
45 /// StartNewSlab - Allocate a new slab and move the bump pointers over into
46 /// the new slab. Modifies CurPtr and End.
47 void BumpPtrAllocator::StartNewSlab() {
48 // If we allocated a big number of slabs already it's likely that we're going
49 // to allocate more. Increase slab size to reduce mallocs and possibly memory
50 // overhead. The factors are chosen conservatively to avoid overallocation.
51 if (BytesAllocated >= SlabSize * 128)
54 MemSlab *NewSlab = Allocator.Allocate(SlabSize);
55 NewSlab->NextPtr = CurSlab;
57 CurPtr = (char*)(CurSlab + 1);
58 End = ((char*)CurSlab) + CurSlab->Size;
61 /// DeallocateSlabs - Deallocate all memory slabs after and including this
63 void BumpPtrAllocator::DeallocateSlabs(MemSlab *Slab) {
65 MemSlab *NextSlab = Slab->NextPtr;
67 // Poison the memory so stale pointers crash sooner. Note we must
68 // preserve the Size and NextPtr fields at the beginning.
69 sys::Memory::setRangeWritable(Slab + 1, Slab->Size - sizeof(MemSlab));
70 memset(Slab + 1, 0xCD, Slab->Size - sizeof(MemSlab));
72 Allocator.Deallocate(Slab);
77 /// Reset - Deallocate all but the current slab and reset the current pointer
78 /// to the beginning of it, freeing all memory allocated so far.
79 void BumpPtrAllocator::Reset() {
82 DeallocateSlabs(CurSlab->NextPtr);
84 CurPtr = (char*)(CurSlab + 1);
85 End = ((char*)CurSlab) + CurSlab->Size;
89 /// Allocate - Allocate space at the specified alignment.
91 void *BumpPtrAllocator::Allocate(size_t Size, size_t Alignment) {
92 if (!CurSlab) // Start a new slab if we haven't allocated one already.
95 // Keep track of how many bytes we've allocated.
96 BytesAllocated += Size;
98 // 0-byte alignment means 1-byte alignment.
99 if (Alignment == 0) Alignment = 1;
101 // Allocate the aligned space, going forwards from CurPtr.
102 char *Ptr = AlignPtr(CurPtr, Alignment);
104 // Check if we can hold it.
105 if (Ptr + Size <= End) {
107 // Update the allocation point of this memory block in MemorySanitizer.
108 // Without this, MemorySanitizer messages for values originated from here
109 // will point to the allocation of the entire slab.
110 __msan_allocated_memory(Ptr, Size);
114 // If Size is really big, allocate a separate slab for it.
115 size_t PaddedSize = Size + sizeof(MemSlab) + Alignment - 1;
116 if (PaddedSize > SizeThreshold) {
117 MemSlab *NewSlab = Allocator.Allocate(PaddedSize);
119 // Put the new slab after the current slab, since we are not allocating
121 NewSlab->NextPtr = CurSlab->NextPtr;
122 CurSlab->NextPtr = NewSlab;
124 Ptr = AlignPtr((char*)(NewSlab + 1), Alignment);
125 assert((uintptr_t)Ptr + Size <= (uintptr_t)NewSlab + NewSlab->Size);
126 __msan_allocated_memory(Ptr, Size);
130 // Otherwise, start a new slab and try again.
132 Ptr = AlignPtr(CurPtr, Alignment);
134 assert(CurPtr <= End && "Unable to allocate memory!");
135 __msan_allocated_memory(Ptr, Size);
139 unsigned BumpPtrAllocator::GetNumSlabs() const {
140 unsigned NumSlabs = 0;
141 for (MemSlab *Slab = CurSlab; Slab != 0; Slab = Slab->NextPtr) {
147 size_t BumpPtrAllocator::getTotalMemory() const {
148 size_t TotalMemory = 0;
149 for (MemSlab *Slab = CurSlab; Slab != 0; Slab = Slab->NextPtr) {
150 TotalMemory += Slab->Size;
155 void BumpPtrAllocator::PrintStats() const {
156 unsigned NumSlabs = 0;
157 size_t TotalMemory = 0;
158 for (MemSlab *Slab = CurSlab; Slab != 0; Slab = Slab->NextPtr) {
159 TotalMemory += Slab->Size;
163 errs() << "\nNumber of memory regions: " << NumSlabs << '\n'
164 << "Bytes used: " << BytesAllocated << '\n'
165 << "Bytes allocated: " << TotalMemory << '\n'
166 << "Bytes wasted: " << (TotalMemory - BytesAllocated)
167 << " (includes alignment, etc)\n";
170 MallocSlabAllocator BumpPtrAllocator::DefaultSlabAllocator =
171 MallocSlabAllocator();
173 SlabAllocator::~SlabAllocator() { }
175 MallocSlabAllocator::~MallocSlabAllocator() { }
177 MemSlab *MallocSlabAllocator::Allocate(size_t Size) {
178 MemSlab *Slab = (MemSlab*)Allocator.Allocate(Size, 0);
184 void MallocSlabAllocator::Deallocate(MemSlab *Slab) {
185 Allocator.Deallocate(Slab);
188 void PrintRecyclerStats(size_t Size,
190 size_t FreeListSize) {
191 errs() << "Recycler element size: " << Size << '\n'
192 << "Recycler element alignment: " << Align << '\n'
193 << "Number of elements free for recycling: " << FreeListSize << '\n';