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(size_t size, size_t threshold)
30 : SlabSize(size), SizeThreshold(std::min(size, threshold)),
31 Allocator(DefaultSlabAllocator), CurSlab(0), BytesAllocated(0) { }
33 BumpPtrAllocator::~BumpPtrAllocator() {
34 DeallocateSlabs(CurSlab);
37 /// AlignPtr - Align Ptr to Alignment bytes, rounding up. Alignment should
38 /// be a power of two. This method rounds up, so AlignPtr(7, 4) == 8 and
39 /// AlignPtr(8, 4) == 8.
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));
49 /// StartNewSlab - Allocate a new slab and move the bump pointers over into
50 /// the new slab. Modifies CurPtr and End.
51 void BumpPtrAllocator::StartNewSlab() {
52 // If we allocated a big number of slabs already it's likely that we're going
53 // to allocate more. Increase slab size to reduce mallocs and possibly memory
54 // overhead. The factors are chosen conservatively to avoid overallocation.
55 if (BytesAllocated >= SlabSize * 128)
58 MemSlab *NewSlab = Allocator.Allocate(SlabSize);
59 NewSlab->NextPtr = CurSlab;
61 CurPtr = (char*)(CurSlab + 1);
62 End = ((char*)CurSlab) + CurSlab->Size;
65 /// DeallocateSlabs - Deallocate all memory slabs after and including this
67 void BumpPtrAllocator::DeallocateSlabs(MemSlab *Slab) {
69 MemSlab *NextSlab = Slab->NextPtr;
71 // Poison the memory so stale pointers crash sooner. Note we must
72 // preserve the Size and NextPtr fields at the beginning.
73 sys::Memory::setRangeWritable(Slab + 1, Slab->Size - sizeof(MemSlab));
74 memset(Slab + 1, 0xCD, Slab->Size - sizeof(MemSlab));
76 Allocator.Deallocate(Slab);
81 /// Reset - Deallocate all but the current slab and reset the current pointer
82 /// to the beginning of it, freeing all memory allocated so far.
83 void BumpPtrAllocator::Reset() {
86 DeallocateSlabs(CurSlab->NextPtr);
88 CurPtr = (char*)(CurSlab + 1);
89 End = ((char*)CurSlab) + CurSlab->Size;
93 /// Allocate - Allocate space at the specified alignment.
95 void *BumpPtrAllocator::Allocate(size_t Size, size_t Alignment) {
96 if (!CurSlab) // Start a new slab if we haven't allocated one already.
99 // Keep track of how many bytes we've allocated.
100 BytesAllocated += Size;
102 // 0-byte alignment means 1-byte alignment.
103 if (Alignment == 0) Alignment = 1;
105 // Allocate the aligned space, going forwards from CurPtr.
106 char *Ptr = AlignPtr(CurPtr, Alignment);
108 // Check if we can hold it.
109 if (Ptr + Size <= End) {
111 // Update the allocation point of this memory block in MemorySanitizer.
112 // Without this, MemorySanitizer messages for values originated from here
113 // will point to the allocation of the entire slab.
114 __msan_allocated_memory(Ptr, Size);
118 // If Size is really big, allocate a separate slab for it.
119 size_t PaddedSize = Size + sizeof(MemSlab) + Alignment - 1;
120 if (PaddedSize > SizeThreshold) {
121 MemSlab *NewSlab = Allocator.Allocate(PaddedSize);
123 // Put the new slab after the current slab, since we are not allocating
125 NewSlab->NextPtr = CurSlab->NextPtr;
126 CurSlab->NextPtr = NewSlab;
128 Ptr = AlignPtr((char*)(NewSlab + 1), Alignment);
129 assert((uintptr_t)Ptr + Size <= (uintptr_t)NewSlab + NewSlab->Size);
130 __msan_allocated_memory(Ptr, Size);
134 // Otherwise, start a new slab and try again.
136 Ptr = AlignPtr(CurPtr, Alignment);
138 assert(CurPtr <= End && "Unable to allocate memory!");
139 __msan_allocated_memory(Ptr, Size);
143 unsigned BumpPtrAllocator::GetNumSlabs() const {
144 unsigned NumSlabs = 0;
145 for (MemSlab *Slab = CurSlab; Slab != 0; Slab = Slab->NextPtr) {
151 size_t BumpPtrAllocator::getTotalMemory() const {
152 size_t TotalMemory = 0;
153 for (MemSlab *Slab = CurSlab; Slab != 0; Slab = Slab->NextPtr) {
154 TotalMemory += Slab->Size;
159 void BumpPtrAllocator::PrintStats() const {
160 unsigned NumSlabs = 0;
161 size_t TotalMemory = 0;
162 for (MemSlab *Slab = CurSlab; Slab != 0; Slab = Slab->NextPtr) {
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";
174 SlabAllocator::~SlabAllocator() { }
176 MallocSlabAllocator::~MallocSlabAllocator() { }
178 MemSlab *MallocSlabAllocator::Allocate(size_t Size) {
179 MemSlab *Slab = (MemSlab*)Allocator.Allocate(Size, 0);
185 void MallocSlabAllocator::Deallocate(MemSlab *Slab) {
186 Allocator.Deallocate(Slab);
189 void PrintRecyclerStats(size_t Size,
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';