Chris Lattner | b9de903 | 2006-10-29 22:08:03 +0000 | [diff] [blame] | 1 | //===--- Allocator.cpp - Simple memory allocation abstraction -------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | b9de903 | 2006-10-29 22:08:03 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the BumpPtrAllocator interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Support/Allocator.h" |
Evgeniy Stepanov | 130fdcd | 2013-01-31 09:58:59 +0000 | [diff] [blame] | 15 | #include "llvm/Support/Compiler.h" |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 16 | #include "llvm/Support/DataTypes.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Memory.h" |
Reid Kleckner | c2d882d | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Recycler.h" |
Daniel Dunbar | a3d677b | 2009-07-24 04:01:01 +0000 | [diff] [blame] | 19 | #include "llvm/Support/raw_ostream.h" |
Reid Kleckner | c2d882d | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 20 | #include <cstring> |
Chris Lattner | b9de903 | 2006-10-29 22:08:03 +0000 | [diff] [blame] | 21 | |
Reid Kleckner | c2d882d | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 22 | namespace llvm { |
Chris Lattner | b9de903 | 2006-10-29 22:08:03 +0000 | [diff] [blame] | 23 | |
Reid Kleckner | c2d882d | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 24 | BumpPtrAllocator::BumpPtrAllocator(size_t size, size_t threshold, |
| 25 | SlabAllocator &allocator) |
Benjamin Kramer | f7e02a0 | 2012-03-01 22:10:16 +0000 | [diff] [blame] | 26 | : SlabSize(size), SizeThreshold(std::min(size, threshold)), |
| 27 | Allocator(allocator), CurSlab(0), BytesAllocated(0) { } |
Chris Lattner | b9de903 | 2006-10-29 22:08:03 +0000 | [diff] [blame] | 28 | |
Argyrios Kyrtzidis | aae63a0 | 2013-08-28 01:02:21 +0000 | [diff] [blame] | 29 | BumpPtrAllocator::BumpPtrAllocator(size_t size, size_t threshold) |
| 30 | : SlabSize(size), SizeThreshold(std::min(size, threshold)), |
| 31 | Allocator(DefaultSlabAllocator), CurSlab(0), BytesAllocated(0) { } |
| 32 | |
Chris Lattner | b9de903 | 2006-10-29 22:08:03 +0000 | [diff] [blame] | 33 | BumpPtrAllocator::~BumpPtrAllocator() { |
Reid Kleckner | c2d882d | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 34 | DeallocateSlabs(CurSlab); |
Chris Lattner | b9de903 | 2006-10-29 22:08:03 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Reid Kleckner | c2d882d | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 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!"); |
| 43 | |
| 44 | // Do the alignment. |
| 45 | return (char*)(((uintptr_t)Ptr + Alignment - 1) & |
| 46 | ~(uintptr_t)(Alignment - 1)); |
| 47 | } |
| 48 | |
| 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() { |
Benjamin Kramer | 90086ab | 2010-09-30 16:18:28 +0000 | [diff] [blame] | 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) |
| 56 | SlabSize *= 2; |
| 57 | |
Reid Kleckner | c2d882d | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 58 | MemSlab *NewSlab = Allocator.Allocate(SlabSize); |
| 59 | NewSlab->NextPtr = CurSlab; |
| 60 | CurSlab = NewSlab; |
| 61 | CurPtr = (char*)(CurSlab + 1); |
| 62 | End = ((char*)CurSlab) + CurSlab->Size; |
| 63 | } |
| 64 | |
| 65 | /// DeallocateSlabs - Deallocate all memory slabs after and including this |
| 66 | /// one. |
| 67 | void BumpPtrAllocator::DeallocateSlabs(MemSlab *Slab) { |
| 68 | while (Slab) { |
| 69 | MemSlab *NextSlab = Slab->NextPtr; |
| 70 | #ifndef NDEBUG |
| 71 | // Poison the memory so stale pointers crash sooner. Note we must |
| 72 | // preserve the Size and NextPtr fields at the beginning. |
Evan Cheng | 49adbf4 | 2009-09-09 01:45:24 +0000 | [diff] [blame] | 73 | sys::Memory::setRangeWritable(Slab + 1, Slab->Size - sizeof(MemSlab)); |
Reid Kleckner | c2d882d | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 74 | memset(Slab + 1, 0xCD, Slab->Size - sizeof(MemSlab)); |
| 75 | #endif |
| 76 | Allocator.Deallocate(Slab); |
| 77 | Slab = NextSlab; |
| 78 | } |
| 79 | } |
| 80 | |
| 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. |
Evan Cheng | 3dba41b | 2007-09-05 21:41:34 +0000 | [diff] [blame] | 83 | void BumpPtrAllocator::Reset() { |
Benjamin Kramer | 55cfaa3 | 2010-04-13 16:38:06 +0000 | [diff] [blame] | 84 | if (!CurSlab) |
| 85 | return; |
Reid Kleckner | c2d882d | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 86 | DeallocateSlabs(CurSlab->NextPtr); |
| 87 | CurSlab->NextPtr = 0; |
| 88 | CurPtr = (char*)(CurSlab + 1); |
| 89 | End = ((char*)CurSlab) + CurSlab->Size; |
Pedro Artigas | 0c09481 | 2013-02-20 23:30:56 +0000 | [diff] [blame] | 90 | BytesAllocated = 0; |
Evan Cheng | 3dba41b | 2007-09-05 21:41:34 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Reid Kleckner | c2d882d | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 93 | /// Allocate - Allocate space at the specified alignment. |
| 94 | /// |
| 95 | void *BumpPtrAllocator::Allocate(size_t Size, size_t Alignment) { |
Benjamin Kramer | 5b0650b | 2010-04-13 14:41:51 +0000 | [diff] [blame] | 96 | if (!CurSlab) // Start a new slab if we haven't allocated one already. |
| 97 | StartNewSlab(); |
| 98 | |
Reid Kleckner | c2d882d | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 99 | // Keep track of how many bytes we've allocated. |
| 100 | BytesAllocated += Size; |
| 101 | |
Benjamin Kramer | f7e02a0 | 2012-03-01 22:10:16 +0000 | [diff] [blame] | 102 | // 0-byte alignment means 1-byte alignment. |
| 103 | if (Alignment == 0) Alignment = 1; |
| 104 | |
Reid Kleckner | c2d882d | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 105 | // Allocate the aligned space, going forwards from CurPtr. |
| 106 | char *Ptr = AlignPtr(CurPtr, Alignment); |
| 107 | |
| 108 | // Check if we can hold it. |
| 109 | if (Ptr + Size <= End) { |
| 110 | CurPtr = Ptr + Size; |
Evgeniy Stepanov | 130fdcd | 2013-01-31 09:58:59 +0000 | [diff] [blame] | 111 | // Update the allocation point of this memory block in MemorySanitizer. |
Evgeniy Stepanov | 1f5a714 | 2013-02-04 07:03:24 +0000 | [diff] [blame] | 112 | // Without this, MemorySanitizer messages for values originated from here |
| 113 | // will point to the allocation of the entire slab. |
Evgeniy Stepanov | 130fdcd | 2013-01-31 09:58:59 +0000 | [diff] [blame] | 114 | __msan_allocated_memory(Ptr, Size); |
Reid Kleckner | c2d882d | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 115 | return Ptr; |
| 116 | } |
| 117 | |
| 118 | // If Size is really big, allocate a separate slab for it. |
Benjamin Kramer | f7e02a0 | 2012-03-01 22:10:16 +0000 | [diff] [blame] | 119 | size_t PaddedSize = Size + sizeof(MemSlab) + Alignment - 1; |
Reid Kleckner | 4b1f2f4 | 2009-07-25 21:26:02 +0000 | [diff] [blame] | 120 | if (PaddedSize > SizeThreshold) { |
Reid Kleckner | c2d882d | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 121 | MemSlab *NewSlab = Allocator.Allocate(PaddedSize); |
| 122 | |
| 123 | // Put the new slab after the current slab, since we are not allocating |
| 124 | // into it. |
| 125 | NewSlab->NextPtr = CurSlab->NextPtr; |
| 126 | CurSlab->NextPtr = NewSlab; |
| 127 | |
| 128 | Ptr = AlignPtr((char*)(NewSlab + 1), Alignment); |
| 129 | assert((uintptr_t)Ptr + Size <= (uintptr_t)NewSlab + NewSlab->Size); |
Evgeniy Stepanov | 130fdcd | 2013-01-31 09:58:59 +0000 | [diff] [blame] | 130 | __msan_allocated_memory(Ptr, Size); |
Reid Kleckner | c2d882d | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 131 | return Ptr; |
| 132 | } |
| 133 | |
| 134 | // Otherwise, start a new slab and try again. |
| 135 | StartNewSlab(); |
| 136 | Ptr = AlignPtr(CurPtr, Alignment); |
| 137 | CurPtr = Ptr + Size; |
| 138 | assert(CurPtr <= End && "Unable to allocate memory!"); |
Evgeniy Stepanov | 130fdcd | 2013-01-31 09:58:59 +0000 | [diff] [blame] | 139 | __msan_allocated_memory(Ptr, Size); |
Chris Lattner | 66330fd | 2007-02-23 22:31:24 +0000 | [diff] [blame] | 140 | return Ptr; |
Chris Lattner | b9de903 | 2006-10-29 22:08:03 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Reid Kleckner | c2d882d | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 143 | unsigned BumpPtrAllocator::GetNumSlabs() const { |
| 144 | unsigned NumSlabs = 0; |
| 145 | for (MemSlab *Slab = CurSlab; Slab != 0; Slab = Slab->NextPtr) { |
| 146 | ++NumSlabs; |
| 147 | } |
| 148 | return NumSlabs; |
Reid Kleckner | 5bd6105 | 2009-07-23 00:30:41 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Ted Kremenek | 28af26d | 2011-04-18 22:44:46 +0000 | [diff] [blame] | 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; |
| 155 | } |
| 156 | return TotalMemory; |
| 157 | } |
| 158 | |
Reid Kleckner | c2d882d | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 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; |
| 164 | ++NumSlabs; |
| 165 | } |
| 166 | |
Daniel Dunbar | a3d677b | 2009-07-24 04:01:01 +0000 | [diff] [blame] | 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"; |
Reid Kleckner | c2d882d | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Reid Kleckner | c2d882d | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 174 | SlabAllocator::~SlabAllocator() { } |
| 175 | |
| 176 | MallocSlabAllocator::~MallocSlabAllocator() { } |
| 177 | |
| 178 | MemSlab *MallocSlabAllocator::Allocate(size_t Size) { |
| 179 | MemSlab *Slab = (MemSlab*)Allocator.Allocate(Size, 0); |
| 180 | Slab->Size = Size; |
| 181 | Slab->NextPtr = 0; |
| 182 | return Slab; |
| 183 | } |
| 184 | |
| 185 | void MallocSlabAllocator::Deallocate(MemSlab *Slab) { |
| 186 | Allocator.Deallocate(Slab); |
| 187 | } |
| 188 | |
| 189 | void PrintRecyclerStats(size_t Size, |
| 190 | size_t Align, |
| 191 | size_t FreeListSize) { |
Daniel Dunbar | a3d677b | 2009-07-24 04:01:01 +0000 | [diff] [blame] | 192 | errs() << "Recycler element size: " << Size << '\n' |
| 193 | << "Recycler element alignment: " << Align << '\n' |
| 194 | << "Number of elements free for recycling: " << FreeListSize << '\n'; |
Reid Kleckner | c2d882d | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Dan Gohman | e5932e5 | 2008-07-07 22:58:06 +0000 | [diff] [blame] | 197 | } |