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" |
Bill Wendling | 1a097e3 | 2006-12-07 23:41:45 +0000 | [diff] [blame] | 18 | #include <ostream> |
Chris Lattner | 9f617d6 | 2006-10-29 22:08:03 +0000 | [diff] [blame] | 19 | using namespace llvm; |
| 20 | |
| 21 | //===----------------------------------------------------------------------===// |
| 22 | // MemRegion class implementation |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | |
| 25 | namespace { |
| 26 | /// MemRegion - This is one chunk of the BumpPtrAllocator. |
| 27 | class MemRegion { |
| 28 | unsigned RegionSize; |
| 29 | MemRegion *Next; |
| 30 | char *NextPtr; |
| 31 | public: |
| 32 | void Init(unsigned size, unsigned Alignment, MemRegion *next) { |
| 33 | RegionSize = size; |
| 34 | Next = next; |
| 35 | NextPtr = (char*)(this+1); |
| 36 | |
| 37 | // Align NextPtr. |
| 38 | NextPtr = (char*)((intptr_t)(NextPtr+Alignment-1) & |
| 39 | ~(intptr_t)(Alignment-1)); |
| 40 | } |
| 41 | |
| 42 | const MemRegion *getNext() const { return Next; } |
| 43 | unsigned getNumBytesAllocated() const { |
| 44 | return NextPtr-(const char*)this; |
| 45 | } |
| 46 | |
| 47 | /// Allocate - Allocate and return at least the specified number of bytes. |
| 48 | /// |
Evan Cheng | 34cd4a4 | 2008-05-05 18:30:58 +0000 | [diff] [blame] | 49 | void *Allocate(size_t AllocSize, size_t Alignment, MemRegion **RegPtr) { |
Chris Lattner | 9f617d6 | 2006-10-29 22:08:03 +0000 | [diff] [blame] | 50 | |
Ted Kremenek | c971562 | 2008-04-28 17:58:07 +0000 | [diff] [blame] | 51 | char* Result = (char*) (((uintptr_t) (NextPtr+Alignment-1)) |
Dan Gohman | 3e2dda6 | 2008-04-28 20:25:15 +0000 | [diff] [blame] | 52 | & ~((uintptr_t) Alignment-1)); |
Ted Kremenek | c971562 | 2008-04-28 17:58:07 +0000 | [diff] [blame] | 53 | |
| 54 | // Speculate the new value of NextPtr. |
| 55 | char* NextPtrTmp = Result + AllocSize; |
| 56 | |
| 57 | // If we are still within the current region, return Result. |
| 58 | if (unsigned (NextPtrTmp - (char*) this) <= RegionSize) { |
| 59 | NextPtr = NextPtrTmp; |
Chris Lattner | 9f617d6 | 2006-10-29 22:08:03 +0000 | [diff] [blame] | 60 | return Result; |
| 61 | } |
| 62 | |
| 63 | // Otherwise, we have to allocate a new chunk. Create one twice as big as |
| 64 | // this one. |
| 65 | MemRegion *NewRegion = (MemRegion *)malloc(RegionSize*2); |
| 66 | NewRegion->Init(RegionSize*2, Alignment, this); |
| 67 | |
| 68 | // Update the current "first region" pointer to point to the new region. |
| 69 | *RegPtr = NewRegion; |
| 70 | |
| 71 | // Try allocating from it now. |
| 72 | return NewRegion->Allocate(AllocSize, Alignment, RegPtr); |
| 73 | } |
| 74 | |
Evan Cheng | 7dfda9e | 2007-09-08 00:02:17 +0000 | [diff] [blame] | 75 | /// Deallocate - Recursively release all memory for this and its next regions |
| 76 | /// to the system. |
Chris Lattner | 9f617d6 | 2006-10-29 22:08:03 +0000 | [diff] [blame] | 77 | void Deallocate() { |
| 78 | MemRegion *next = Next; |
| 79 | free(this); |
| 80 | if (next) |
| 81 | next->Deallocate(); |
| 82 | } |
Evan Cheng | 7dfda9e | 2007-09-08 00:02:17 +0000 | [diff] [blame] | 83 | |
| 84 | /// DeallocateAllButLast - Recursively release all memory for this and its |
| 85 | /// next regions to the system stopping at the last region in the list. |
| 86 | /// Returns the pointer to the last region. |
| 87 | MemRegion *DeallocateAllButLast() { |
| 88 | MemRegion *next = Next; |
| 89 | if (!next) |
| 90 | return this; |
| 91 | free(this); |
| 92 | return next->DeallocateAllButLast(); |
| 93 | } |
Chris Lattner | 9f617d6 | 2006-10-29 22:08:03 +0000 | [diff] [blame] | 94 | }; |
| 95 | } |
| 96 | |
| 97 | //===----------------------------------------------------------------------===// |
| 98 | // BumpPtrAllocator class implementation |
| 99 | //===----------------------------------------------------------------------===// |
| 100 | |
| 101 | BumpPtrAllocator::BumpPtrAllocator() { |
| 102 | TheMemory = malloc(4096); |
| 103 | ((MemRegion*)TheMemory)->Init(4096, 1, 0); |
| 104 | } |
| 105 | |
| 106 | BumpPtrAllocator::~BumpPtrAllocator() { |
| 107 | ((MemRegion*)TheMemory)->Deallocate(); |
| 108 | } |
| 109 | |
Evan Cheng | 188b522 | 2007-09-05 21:41:34 +0000 | [diff] [blame] | 110 | void BumpPtrAllocator::Reset() { |
Evan Cheng | 7dfda9e | 2007-09-08 00:02:17 +0000 | [diff] [blame] | 111 | MemRegion *MRP = (MemRegion*)TheMemory; |
| 112 | MRP = MRP->DeallocateAllButLast(); |
| 113 | MRP->Init(4096, 1, 0); |
| 114 | TheMemory = MRP; |
Evan Cheng | 188b522 | 2007-09-05 21:41:34 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Evan Cheng | 34cd4a4 | 2008-05-05 18:30:58 +0000 | [diff] [blame] | 117 | void *BumpPtrAllocator::Allocate(size_t Size, size_t Align) { |
Chris Lattner | d675b83 | 2007-02-23 22:31:24 +0000 | [diff] [blame] | 118 | MemRegion *MRP = (MemRegion*)TheMemory; |
| 119 | void *Ptr = MRP->Allocate(Size, Align, &MRP); |
| 120 | TheMemory = MRP; |
| 121 | return Ptr; |
Chris Lattner | 9f617d6 | 2006-10-29 22:08:03 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void BumpPtrAllocator::PrintStats() const { |
| 125 | unsigned BytesUsed = 0; |
| 126 | unsigned NumRegions = 0; |
| 127 | const MemRegion *R = (MemRegion*)TheMemory; |
| 128 | for (; R; R = R->getNext(), ++NumRegions) |
| 129 | BytesUsed += R->getNumBytesAllocated(); |
| 130 | |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 131 | cerr << "\nNumber of memory regions: " << NumRegions << "\n"; |
| 132 | cerr << "Bytes allocated: " << BytesUsed << "\n"; |
Chris Lattner | 9f617d6 | 2006-10-29 22:08:03 +0000 | [diff] [blame] | 133 | } |
Dan Gohman | e14d81d | 2008-07-07 22:58:06 +0000 | [diff] [blame] | 134 | |
Dan Gohman | fed90b6 | 2008-07-28 21:51:04 +0000 | [diff] [blame] | 135 | void llvm::PrintRecyclerStats(size_t Size, |
| 136 | size_t Align, |
Dan Gohman | e14d81d | 2008-07-07 22:58:06 +0000 | [diff] [blame] | 137 | size_t FreeListSize) { |
Dan Gohman | fed90b6 | 2008-07-28 21:51:04 +0000 | [diff] [blame] | 138 | cerr << "Recycler element size: " << Size << '\n'; |
| 139 | cerr << "Recycler element alignment: " << Align << '\n'; |
Dan Gohman | e14d81d | 2008-07-07 22:58:06 +0000 | [diff] [blame] | 140 | cerr << "Number of elements free for recycling: " << FreeListSize << '\n'; |
| 141 | } |