blob: 5961afe04c7ffd47b046a961e5ca81b5bc3ae4a6 [file] [log] [blame]
Chris Lattner9f617d62006-10-29 22:08:03 +00001//===--- Allocator.cpp - Simple memory allocation abstraction -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner9f617d62006-10-29 22:08:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the BumpPtrAllocator interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Support/Allocator.h"
John Criswell72a780f2006-11-08 15:04:35 +000015#include "llvm/Support/DataTypes.h"
Bill Wendlingfe6b1462006-11-26 10:52:51 +000016#include "llvm/Support/Streams.h"
Bill Wendling1a097e32006-12-07 23:41:45 +000017#include <ostream>
Chris Lattner9f617d62006-10-29 22:08:03 +000018using namespace llvm;
19
20//===----------------------------------------------------------------------===//
21// MemRegion class implementation
22//===----------------------------------------------------------------------===//
23
24namespace {
25/// MemRegion - This is one chunk of the BumpPtrAllocator.
26class MemRegion {
27 unsigned RegionSize;
28 MemRegion *Next;
29 char *NextPtr;
30public:
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) {
49 // Round size up to an even multiple of the alignment.
50 AllocSize = (AllocSize+Alignment-1) & ~(Alignment-1);
51
52 // If there is space in this region, return it.
53 if (unsigned(NextPtr+AllocSize-(char*)this) <= RegionSize) {
54 void *Result = NextPtr;
55 NextPtr += AllocSize;
56 return Result;
57 }
58
59 // Otherwise, we have to allocate a new chunk. Create one twice as big as
60 // this one.
61 MemRegion *NewRegion = (MemRegion *)malloc(RegionSize*2);
62 NewRegion->Init(RegionSize*2, Alignment, this);
63
64 // Update the current "first region" pointer to point to the new region.
65 *RegPtr = NewRegion;
66
67 // Try allocating from it now.
68 return NewRegion->Allocate(AllocSize, Alignment, RegPtr);
69 }
70
Evan Cheng7dfda9e2007-09-08 00:02:17 +000071 /// Deallocate - Recursively release all memory for this and its next regions
72 /// to the system.
Chris Lattner9f617d62006-10-29 22:08:03 +000073 void Deallocate() {
74 MemRegion *next = Next;
75 free(this);
76 if (next)
77 next->Deallocate();
78 }
Evan Cheng7dfda9e2007-09-08 00:02:17 +000079
80 /// DeallocateAllButLast - Recursively release all memory for this and its
81 /// next regions to the system stopping at the last region in the list.
82 /// Returns the pointer to the last region.
83 MemRegion *DeallocateAllButLast() {
84 MemRegion *next = Next;
85 if (!next)
86 return this;
87 free(this);
88 return next->DeallocateAllButLast();
89 }
Chris Lattner9f617d62006-10-29 22:08:03 +000090};
91}
92
93//===----------------------------------------------------------------------===//
94// BumpPtrAllocator class implementation
95//===----------------------------------------------------------------------===//
96
97BumpPtrAllocator::BumpPtrAllocator() {
98 TheMemory = malloc(4096);
99 ((MemRegion*)TheMemory)->Init(4096, 1, 0);
100}
101
102BumpPtrAllocator::~BumpPtrAllocator() {
103 ((MemRegion*)TheMemory)->Deallocate();
104}
105
Evan Cheng188b5222007-09-05 21:41:34 +0000106void BumpPtrAllocator::Reset() {
Evan Cheng7dfda9e2007-09-08 00:02:17 +0000107 MemRegion *MRP = (MemRegion*)TheMemory;
108 MRP = MRP->DeallocateAllButLast();
109 MRP->Init(4096, 1, 0);
110 TheMemory = MRP;
Evan Cheng188b5222007-09-05 21:41:34 +0000111}
112
Chris Lattner9f617d62006-10-29 22:08:03 +0000113void *BumpPtrAllocator::Allocate(unsigned Size, unsigned Align) {
Chris Lattnerd675b832007-02-23 22:31:24 +0000114 MemRegion *MRP = (MemRegion*)TheMemory;
115 void *Ptr = MRP->Allocate(Size, Align, &MRP);
116 TheMemory = MRP;
117 return Ptr;
Chris Lattner9f617d62006-10-29 22:08:03 +0000118}
119
120void BumpPtrAllocator::PrintStats() const {
121 unsigned BytesUsed = 0;
122 unsigned NumRegions = 0;
123 const MemRegion *R = (MemRegion*)TheMemory;
124 for (; R; R = R->getNext(), ++NumRegions)
125 BytesUsed += R->getNumBytesAllocated();
126
Bill Wendlinge8156192006-12-07 01:30:32 +0000127 cerr << "\nNumber of memory regions: " << NumRegions << "\n";
128 cerr << "Bytes allocated: " << BytesUsed << "\n";
Chris Lattner9f617d62006-10-29 22:08:03 +0000129}