blob: 96f0743f3d7854b7bb5de2325071905eff6cd60e [file] [log] [blame]
Chris Lattnerb9de9032006-10-29 22:08:03 +00001//===--- Allocator.cpp - Simple memory allocation abstraction -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the BumpPtrAllocator interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Support/Allocator.h"
John Criswell8ea2e682006-11-08 15:04:35 +000015#include "llvm/Support/DataTypes.h"
Bill Wendling3750ae22006-11-26 10:52:51 +000016#include "llvm/Support/Streams.h"
Chris Lattnerb9de9032006-10-29 22:08:03 +000017using namespace llvm;
18
19//===----------------------------------------------------------------------===//
20// MemRegion class implementation
21//===----------------------------------------------------------------------===//
22
23namespace {
24/// MemRegion - This is one chunk of the BumpPtrAllocator.
25class MemRegion {
26 unsigned RegionSize;
27 MemRegion *Next;
28 char *NextPtr;
29public:
30 void Init(unsigned size, unsigned Alignment, MemRegion *next) {
31 RegionSize = size;
32 Next = next;
33 NextPtr = (char*)(this+1);
34
35 // Align NextPtr.
36 NextPtr = (char*)((intptr_t)(NextPtr+Alignment-1) &
37 ~(intptr_t)(Alignment-1));
38 }
39
40 const MemRegion *getNext() const { return Next; }
41 unsigned getNumBytesAllocated() const {
42 return NextPtr-(const char*)this;
43 }
44
45 /// Allocate - Allocate and return at least the specified number of bytes.
46 ///
47 void *Allocate(unsigned AllocSize, unsigned Alignment, MemRegion **RegPtr) {
48 // Round size up to an even multiple of the alignment.
49 AllocSize = (AllocSize+Alignment-1) & ~(Alignment-1);
50
51 // If there is space in this region, return it.
52 if (unsigned(NextPtr+AllocSize-(char*)this) <= RegionSize) {
53 void *Result = NextPtr;
54 NextPtr += AllocSize;
55 return Result;
56 }
57
58 // Otherwise, we have to allocate a new chunk. Create one twice as big as
59 // this one.
60 MemRegion *NewRegion = (MemRegion *)malloc(RegionSize*2);
61 NewRegion->Init(RegionSize*2, Alignment, this);
62
63 // Update the current "first region" pointer to point to the new region.
64 *RegPtr = NewRegion;
65
66 // Try allocating from it now.
67 return NewRegion->Allocate(AllocSize, Alignment, RegPtr);
68 }
69
70 /// Deallocate - Release all memory for this region to the system.
71 ///
72 void Deallocate() {
73 MemRegion *next = Next;
74 free(this);
75 if (next)
76 next->Deallocate();
77 }
78};
79}
80
81//===----------------------------------------------------------------------===//
82// BumpPtrAllocator class implementation
83//===----------------------------------------------------------------------===//
84
85BumpPtrAllocator::BumpPtrAllocator() {
86 TheMemory = malloc(4096);
87 ((MemRegion*)TheMemory)->Init(4096, 1, 0);
88}
89
90BumpPtrAllocator::~BumpPtrAllocator() {
91 ((MemRegion*)TheMemory)->Deallocate();
92}
93
94void *BumpPtrAllocator::Allocate(unsigned Size, unsigned Align) {
95 return ((MemRegion*)TheMemory)->Allocate(Size, Align,(MemRegion**)&TheMemory);
96}
97
98void BumpPtrAllocator::PrintStats() const {
99 unsigned BytesUsed = 0;
100 unsigned NumRegions = 0;
101 const MemRegion *R = (MemRegion*)TheMemory;
102 for (; R; R = R->getNext(), ++NumRegions)
103 BytesUsed += R->getNumBytesAllocated();
104
Bill Wendling3750ae22006-11-26 10:52:51 +0000105 llvm_cerr << "\nNumber of memory regions: " << NumRegions << "\n";
106 llvm_cerr << "Bytes allocated: " << BytesUsed << "\n";
Chris Lattnerb9de9032006-10-29 22:08:03 +0000107}