Chris Lattner | 9f617d6 | 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 | 4ee451d | 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 | 9f617d6 | 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" |
Chandler Carruth | 8b67f77 | 2009-10-26 01:35:46 +0000 | [diff] [blame] | 15 | #include "llvm/System/DataTypes.h" |
Reid Kleckner | 8f51a62 | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Recycler.h" |
Daniel Dunbar | 7da9559 | 2009-07-24 04:01:01 +0000 | [diff] [blame] | 17 | #include "llvm/Support/raw_ostream.h" |
Evan Cheng | c48edbb | 2009-09-09 01:45:24 +0000 | [diff] [blame] | 18 | #include "llvm/System/Memory.h" |
Reid Kleckner | 8f51a62 | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 19 | #include <cstring> |
Chris Lattner | 9f617d6 | 2006-10-29 22:08:03 +0000 | [diff] [blame] | 20 | |
Reid Kleckner | 8f51a62 | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 21 | namespace llvm { |
Chris Lattner | 9f617d6 | 2006-10-29 22:08:03 +0000 | [diff] [blame] | 22 | |
Reid Kleckner | 8f51a62 | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 23 | BumpPtrAllocator::BumpPtrAllocator(size_t size, size_t threshold, |
| 24 | SlabAllocator &allocator) |
| 25 | : SlabSize(size), SizeThreshold(threshold), Allocator(allocator), |
Benjamin Kramer | 5e6a705 | 2010-04-13 14:41:51 +0000 | [diff] [blame] | 26 | CurSlab(0), BytesAllocated(0) { } |
Chris Lattner | 9f617d6 | 2006-10-29 22:08:03 +0000 | [diff] [blame] | 27 | |
| 28 | BumpPtrAllocator::~BumpPtrAllocator() { |
Reid Kleckner | 8f51a62 | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 29 | DeallocateSlabs(CurSlab); |
Chris Lattner | 9f617d6 | 2006-10-29 22:08:03 +0000 | [diff] [blame] | 30 | } |
| 31 | |
Reid Kleckner | 8f51a62 | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 32 | /// AlignPtr - Align Ptr to Alignment bytes, rounding up. Alignment should |
| 33 | /// be a power of two. This method rounds up, so AlignPtr(7, 4) == 8 and |
| 34 | /// AlignPtr(8, 4) == 8. |
| 35 | char *BumpPtrAllocator::AlignPtr(char *Ptr, size_t Alignment) { |
| 36 | assert(Alignment && (Alignment & (Alignment - 1)) == 0 && |
| 37 | "Alignment is not a power of two!"); |
| 38 | |
| 39 | // Do the alignment. |
| 40 | return (char*)(((uintptr_t)Ptr + Alignment - 1) & |
| 41 | ~(uintptr_t)(Alignment - 1)); |
| 42 | } |
| 43 | |
| 44 | /// StartNewSlab - Allocate a new slab and move the bump pointers over into |
| 45 | /// the new slab. Modifies CurPtr and End. |
| 46 | void BumpPtrAllocator::StartNewSlab() { |
Benjamin Kramer | 82d96cc | 2010-09-30 16:18:28 +0000 | [diff] [blame] | 47 | // If we allocated a big number of slabs already it's likely that we're going |
| 48 | // to allocate more. Increase slab size to reduce mallocs and possibly memory |
| 49 | // overhead. The factors are chosen conservatively to avoid overallocation. |
| 50 | if (BytesAllocated >= SlabSize * 128) |
| 51 | SlabSize *= 2; |
| 52 | |
Reid Kleckner | 8f51a62 | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 53 | MemSlab *NewSlab = Allocator.Allocate(SlabSize); |
| 54 | NewSlab->NextPtr = CurSlab; |
| 55 | CurSlab = NewSlab; |
| 56 | CurPtr = (char*)(CurSlab + 1); |
| 57 | End = ((char*)CurSlab) + CurSlab->Size; |
| 58 | } |
| 59 | |
| 60 | /// DeallocateSlabs - Deallocate all memory slabs after and including this |
| 61 | /// one. |
| 62 | void BumpPtrAllocator::DeallocateSlabs(MemSlab *Slab) { |
| 63 | while (Slab) { |
| 64 | MemSlab *NextSlab = Slab->NextPtr; |
| 65 | #ifndef NDEBUG |
| 66 | // Poison the memory so stale pointers crash sooner. Note we must |
| 67 | // preserve the Size and NextPtr fields at the beginning. |
Evan Cheng | c48edbb | 2009-09-09 01:45:24 +0000 | [diff] [blame] | 68 | sys::Memory::setRangeWritable(Slab + 1, Slab->Size - sizeof(MemSlab)); |
Reid Kleckner | 8f51a62 | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 69 | memset(Slab + 1, 0xCD, Slab->Size - sizeof(MemSlab)); |
| 70 | #endif |
| 71 | Allocator.Deallocate(Slab); |
| 72 | Slab = NextSlab; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /// Reset - Deallocate all but the current slab and reset the current pointer |
| 77 | /// to the beginning of it, freeing all memory allocated so far. |
Evan Cheng | 188b522 | 2007-09-05 21:41:34 +0000 | [diff] [blame] | 78 | void BumpPtrAllocator::Reset() { |
Benjamin Kramer | b0322e6 | 2010-04-13 16:38:06 +0000 | [diff] [blame] | 79 | if (!CurSlab) |
| 80 | return; |
Reid Kleckner | 8f51a62 | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 81 | DeallocateSlabs(CurSlab->NextPtr); |
| 82 | CurSlab->NextPtr = 0; |
| 83 | CurPtr = (char*)(CurSlab + 1); |
| 84 | End = ((char*)CurSlab) + CurSlab->Size; |
Evan Cheng | 188b522 | 2007-09-05 21:41:34 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Reid Kleckner | 8f51a62 | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 87 | /// Allocate - Allocate space at the specified alignment. |
| 88 | /// |
| 89 | void *BumpPtrAllocator::Allocate(size_t Size, size_t Alignment) { |
Benjamin Kramer | 5e6a705 | 2010-04-13 14:41:51 +0000 | [diff] [blame] | 90 | if (!CurSlab) // Start a new slab if we haven't allocated one already. |
| 91 | StartNewSlab(); |
| 92 | |
Reid Kleckner | 8f51a62 | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 93 | // Keep track of how many bytes we've allocated. |
| 94 | BytesAllocated += Size; |
| 95 | |
| 96 | // 0-byte alignment means 1-byte alignment. |
| 97 | if (Alignment == 0) Alignment = 1; |
| 98 | |
| 99 | // Allocate the aligned space, going forwards from CurPtr. |
| 100 | char *Ptr = AlignPtr(CurPtr, Alignment); |
| 101 | |
| 102 | // Check if we can hold it. |
| 103 | if (Ptr + Size <= End) { |
| 104 | CurPtr = Ptr + Size; |
| 105 | return Ptr; |
| 106 | } |
| 107 | |
| 108 | // If Size is really big, allocate a separate slab for it. |
Reid Kleckner | 7d50913 | 2009-07-25 21:26:02 +0000 | [diff] [blame] | 109 | size_t PaddedSize = Size + sizeof(MemSlab) + Alignment - 1; |
| 110 | if (PaddedSize > SizeThreshold) { |
Reid Kleckner | 8f51a62 | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 111 | MemSlab *NewSlab = Allocator.Allocate(PaddedSize); |
| 112 | |
| 113 | // Put the new slab after the current slab, since we are not allocating |
| 114 | // into it. |
| 115 | NewSlab->NextPtr = CurSlab->NextPtr; |
| 116 | CurSlab->NextPtr = NewSlab; |
| 117 | |
| 118 | Ptr = AlignPtr((char*)(NewSlab + 1), Alignment); |
| 119 | assert((uintptr_t)Ptr + Size <= (uintptr_t)NewSlab + NewSlab->Size); |
| 120 | return Ptr; |
| 121 | } |
| 122 | |
| 123 | // Otherwise, start a new slab and try again. |
| 124 | StartNewSlab(); |
| 125 | Ptr = AlignPtr(CurPtr, Alignment); |
| 126 | CurPtr = Ptr + Size; |
| 127 | assert(CurPtr <= End && "Unable to allocate memory!"); |
Chris Lattner | d675b83 | 2007-02-23 22:31:24 +0000 | [diff] [blame] | 128 | return Ptr; |
Chris Lattner | 9f617d6 | 2006-10-29 22:08:03 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Reid Kleckner | 8f51a62 | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 131 | unsigned BumpPtrAllocator::GetNumSlabs() const { |
| 132 | unsigned NumSlabs = 0; |
| 133 | for (MemSlab *Slab = CurSlab; Slab != 0; Slab = Slab->NextPtr) { |
| 134 | ++NumSlabs; |
| 135 | } |
| 136 | return NumSlabs; |
Reid Kleckner | 95eb3ad | 2009-07-23 00:30:41 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Reid Kleckner | 8f51a62 | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 139 | void BumpPtrAllocator::PrintStats() const { |
| 140 | unsigned NumSlabs = 0; |
| 141 | size_t TotalMemory = 0; |
| 142 | for (MemSlab *Slab = CurSlab; Slab != 0; Slab = Slab->NextPtr) { |
| 143 | TotalMemory += Slab->Size; |
| 144 | ++NumSlabs; |
| 145 | } |
| 146 | |
Daniel Dunbar | 7da9559 | 2009-07-24 04:01:01 +0000 | [diff] [blame] | 147 | errs() << "\nNumber of memory regions: " << NumSlabs << '\n' |
| 148 | << "Bytes used: " << BytesAllocated << '\n' |
| 149 | << "Bytes allocated: " << TotalMemory << '\n' |
| 150 | << "Bytes wasted: " << (TotalMemory - BytesAllocated) |
| 151 | << " (includes alignment, etc)\n"; |
Reid Kleckner | 8f51a62 | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Bill Wendling | c5b7b19 | 2010-01-16 01:06:58 +0000 | [diff] [blame] | 154 | MallocSlabAllocator BumpPtrAllocator::DefaultSlabAllocator = |
| 155 | MallocSlabAllocator(); |
Reid Kleckner | 8f51a62 | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 156 | |
| 157 | SlabAllocator::~SlabAllocator() { } |
| 158 | |
| 159 | MallocSlabAllocator::~MallocSlabAllocator() { } |
| 160 | |
| 161 | MemSlab *MallocSlabAllocator::Allocate(size_t Size) { |
| 162 | MemSlab *Slab = (MemSlab*)Allocator.Allocate(Size, 0); |
| 163 | Slab->Size = Size; |
| 164 | Slab->NextPtr = 0; |
| 165 | return Slab; |
| 166 | } |
| 167 | |
| 168 | void MallocSlabAllocator::Deallocate(MemSlab *Slab) { |
| 169 | Allocator.Deallocate(Slab); |
| 170 | } |
| 171 | |
| 172 | void PrintRecyclerStats(size_t Size, |
| 173 | size_t Align, |
| 174 | size_t FreeListSize) { |
Daniel Dunbar | 7da9559 | 2009-07-24 04:01:01 +0000 | [diff] [blame] | 175 | errs() << "Recycler element size: " << Size << '\n' |
| 176 | << "Recycler element alignment: " << Align << '\n' |
| 177 | << "Number of elements free for recycling: " << FreeListSize << '\n'; |
Reid Kleckner | 8f51a62 | 2009-07-23 18:34:13 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Dan Gohman | e14d81d | 2008-07-07 22:58:06 +0000 | [diff] [blame] | 180 | } |