blob: a31b80fcf355e698b55d98092e8693b3a1747213 [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"
Bill Wendling30c0f332006-12-07 23:41:45 +000017#include <ostream>
Chris Lattnerb9de9032006-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
71 /// Deallocate - Release all memory for this region to the system.
72 ///
73 void Deallocate() {
74 MemRegion *next = Next;
75 free(this);
76 if (next)
77 next->Deallocate();
78 }
79};
80}
81
82//===----------------------------------------------------------------------===//
83// BumpPtrAllocator class implementation
84//===----------------------------------------------------------------------===//
85
86BumpPtrAllocator::BumpPtrAllocator() {
87 TheMemory = malloc(4096);
88 ((MemRegion*)TheMemory)->Init(4096, 1, 0);
89}
90
91BumpPtrAllocator::~BumpPtrAllocator() {
92 ((MemRegion*)TheMemory)->Deallocate();
93}
94
95void *BumpPtrAllocator::Allocate(unsigned Size, unsigned Align) {
96 return ((MemRegion*)TheMemory)->Allocate(Size, Align,(MemRegion**)&TheMemory);
97}
98
99void BumpPtrAllocator::PrintStats() const {
100 unsigned BytesUsed = 0;
101 unsigned NumRegions = 0;
102 const MemRegion *R = (MemRegion*)TheMemory;
103 for (; R; R = R->getNext(), ++NumRegions)
104 BytesUsed += R->getNumBytesAllocated();
105
Bill Wendlingf3baad32006-12-07 01:30:32 +0000106 cerr << "\nNumber of memory regions: " << NumRegions << "\n";
107 cerr << "Bytes allocated: " << BytesUsed << "\n";
Chris Lattnerb9de9032006-10-29 22:08:03 +0000108}