1 //===--- Allocator.cpp - Simple memory allocation abstraction -------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the BumpPtrAllocator interface.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/Allocator.h"
15 #include "llvm/Support/DataTypes.h"
16 #include "llvm/Support/Streams.h"
19 //===----------------------------------------------------------------------===//
20 // MemRegion class implementation
21 //===----------------------------------------------------------------------===//
24 /// MemRegion - This is one chunk of the BumpPtrAllocator.
30 void Init(unsigned size, unsigned Alignment, MemRegion *next) {
33 NextPtr = (char*)(this+1);
36 NextPtr = (char*)((intptr_t)(NextPtr+Alignment-1) &
37 ~(intptr_t)(Alignment-1));
40 const MemRegion *getNext() const { return Next; }
41 unsigned getNumBytesAllocated() const {
42 return NextPtr-(const char*)this;
45 /// Allocate - Allocate and return at least the specified number of bytes.
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);
51 // If there is space in this region, return it.
52 if (unsigned(NextPtr+AllocSize-(char*)this) <= RegionSize) {
53 void *Result = NextPtr;
58 // Otherwise, we have to allocate a new chunk. Create one twice as big as
60 MemRegion *NewRegion = (MemRegion *)malloc(RegionSize*2);
61 NewRegion->Init(RegionSize*2, Alignment, this);
63 // Update the current "first region" pointer to point to the new region.
66 // Try allocating from it now.
67 return NewRegion->Allocate(AllocSize, Alignment, RegPtr);
70 /// Deallocate - Release all memory for this region to the system.
73 MemRegion *next = Next;
81 //===----------------------------------------------------------------------===//
82 // BumpPtrAllocator class implementation
83 //===----------------------------------------------------------------------===//
85 BumpPtrAllocator::BumpPtrAllocator() {
86 TheMemory = malloc(4096);
87 ((MemRegion*)TheMemory)->Init(4096, 1, 0);
90 BumpPtrAllocator::~BumpPtrAllocator() {
91 ((MemRegion*)TheMemory)->Deallocate();
94 void *BumpPtrAllocator::Allocate(unsigned Size, unsigned Align) {
95 return ((MemRegion*)TheMemory)->Allocate(Size, Align,(MemRegion**)&TheMemory);
98 void 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();
105 llvm_cerr << "\nNumber of memory regions: " << NumRegions << "\n";
106 llvm_cerr << "Bytes allocated: " << BytesUsed << "\n";