1 //===--- Allocator.cpp - Simple memory allocation abstraction -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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"
20 //===----------------------------------------------------------------------===//
21 // MemRegion class implementation
22 //===----------------------------------------------------------------------===//
25 /// MemRegion - This is one chunk of the BumpPtrAllocator.
31 void Init(unsigned size, unsigned Alignment, MemRegion *next) {
34 NextPtr = (char*)(this+1);
37 NextPtr = (char*)((intptr_t)(NextPtr+Alignment-1) &
38 ~(intptr_t)(Alignment-1));
41 const MemRegion *getNext() const { return Next; }
42 unsigned getNumBytesAllocated() const {
43 return NextPtr-(const char*)this;
46 /// Allocate - Allocate and return at least the specified number of bytes.
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);
52 // If there is space in this region, return it.
53 if (unsigned(NextPtr+AllocSize-(char*)this) <= RegionSize) {
54 void *Result = NextPtr;
59 // Otherwise, we have to allocate a new chunk. Create one twice as big as
61 MemRegion *NewRegion = (MemRegion *)malloc(RegionSize*2);
62 NewRegion->Init(RegionSize*2, Alignment, this);
64 // Update the current "first region" pointer to point to the new region.
67 // Try allocating from it now.
68 return NewRegion->Allocate(AllocSize, Alignment, RegPtr);
71 /// Deallocate - Recursively release all memory for this and its next regions
74 MemRegion *next = Next;
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;
88 return next->DeallocateAllButLast();
93 //===----------------------------------------------------------------------===//
94 // BumpPtrAllocator class implementation
95 //===----------------------------------------------------------------------===//
97 BumpPtrAllocator::BumpPtrAllocator() {
98 TheMemory = malloc(4096);
99 ((MemRegion*)TheMemory)->Init(4096, 1, 0);
102 BumpPtrAllocator::~BumpPtrAllocator() {
103 ((MemRegion*)TheMemory)->Deallocate();
106 void BumpPtrAllocator::Reset() {
107 MemRegion *MRP = (MemRegion*)TheMemory;
108 MRP = MRP->DeallocateAllButLast();
109 MRP->Init(4096, 1, 0);
113 void *BumpPtrAllocator::Allocate(unsigned Size, unsigned Align) {
114 MemRegion *MRP = (MemRegion*)TheMemory;
115 void *Ptr = MRP->Allocate(Size, Align, &MRP);
120 void 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();
127 cerr << "\nNumber of memory regions: " << NumRegions << "\n";
128 cerr << "Bytes allocated: " << BytesUsed << "\n";