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;
88 /// Allocate - Allocate space at the specified alignment.
90 void *BumpPtrAllocator::Allocate(size_t Size, size_t Alignment) {
91 if (!CurSlab) // Start a new slab if we haven't allocated one already.
94 // Keep track of how many bytes we've allocated.
95 BytesAllocated += Size;
97 // 0-byte alignment means 1-byte alignment.
98 if (Alignment == 0) Alignment = 1;
100 // Allocate the aligned space, going forwards from CurPtr.
101 char *Ptr = AlignPtr(CurPtr, Alignment);
103 // Check if we can hold it.
104 if (Ptr + Size <= End) {
106 // Update the allocation point of this memory block in MemorySanitizer.
107 // Without this, MemorySanitizer reports for values originating from it will
108 // point to the allocation point of the entire slab.
109 __msan_allocated_memory(Ptr, Size);
113 // If Size is really big, allocate a separate slab for it.
114 size_t PaddedSize = Size + sizeof(MemSlab) + Alignment - 1;
115 if (PaddedSize > SizeThreshold) {
116 MemSlab *NewSlab = Allocator.Allocate(PaddedSize);
118 // Put the new slab after the current slab, since we are not allocating
120 NewSlab->NextPtr = CurSlab->NextPtr;
121 CurSlab->NextPtr = NewSlab;
123 Ptr = AlignPtr((char*)(NewSlab + 1), Alignment);
124 assert((uintptr_t)Ptr + Size <= (uintptr_t)NewSlab + NewSlab->Size);
125 __msan_allocated_memory(Ptr, Size);
129 // Otherwise, start a new slab and try again.
131 Ptr = AlignPtr(CurPtr, Alignment);
133 assert(CurPtr <= End && "Unable to allocate memory!");
134 __msan_allocated_memory(Ptr, Size);
138 unsigned BumpPtrAllocator::GetNumSlabs() const {
139 unsigned NumSlabs = 0;
140 for (MemSlab *Slab = CurSlab; Slab != 0; Slab = Slab->NextPtr) {
146 size_t BumpPtrAllocator::getTotalMemory() const {
147 size_t TotalMemory = 0;
148 for (MemSlab *Slab = CurSlab; Slab != 0; Slab = Slab->NextPtr) {
149 TotalMemory += Slab->Size;
154 void BumpPtrAllocator::PrintStats() const {
155 unsigned NumSlabs = 0;
156 size_t TotalMemory = 0;
157 for (MemSlab *Slab = CurSlab; Slab != 0; Slab = Slab->NextPtr) {
158 TotalMemory += Slab->Size;
162 errs() << "\nNumber of memory regions: " << NumSlabs << '\n'
163 << "Bytes used: " << BytesAllocated << '\n'
164 << "Bytes allocated: " << TotalMemory << '\n'
165 << "Bytes wasted: " << (TotalMemory - BytesAllocated)
166 << " (includes alignment, etc)\n";
169 MallocSlabAllocator BumpPtrAllocator::DefaultSlabAllocator =
170 MallocSlabAllocator();
172 SlabAllocator::~SlabAllocator() { }
174 MallocSlabAllocator::~MallocSlabAllocator() { }
176 MemSlab *MallocSlabAllocator::Allocate(size_t Size) {
177 MemSlab *Slab = (MemSlab*)Allocator.Allocate(Size, 0);
183 void MallocSlabAllocator::Deallocate(MemSlab *Slab) {
184 Allocator.Deallocate(Slab);
187 void PrintRecyclerStats(size_t Size,
189 size_t FreeListSize) {
190 errs() << "Recycler element size: " << Size << '\n'
191 << "Recycler element alignment: " << Align << '\n'
192 << "Number of elements free for recycling: " << FreeListSize << '\n';