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