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