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