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(size_t AllocSize, size_t Alignment, MemRegion **RegPtr) {
50 char* Result = (char*) (((uintptr_t) (NextPtr+Alignment-1))
51 & ~((uintptr_t) Alignment-1));
53 // Speculate the new value of NextPtr.
54 char* NextPtrTmp = Result + AllocSize;
56 // If we are still within the current region, return Result.
57 if (unsigned (NextPtrTmp - (char*) this) <= RegionSize) {
62 // Otherwise, we have to allocate a new chunk. Create one twice as big as
64 MemRegion *NewRegion = (MemRegion *)malloc(RegionSize*2);
65 NewRegion->Init(RegionSize*2, Alignment, this);
67 // Update the current "first region" pointer to point to the new region.
70 // Try allocating from it now.
71 return NewRegion->Allocate(AllocSize, Alignment, RegPtr);
74 /// Deallocate - Recursively release all memory for this and its next regions
77 MemRegion *next = Next;
83 /// DeallocateAllButLast - Recursively release all memory for this and its
84 /// next regions to the system stopping at the last region in the list.
85 /// Returns the pointer to the last region.
86 MemRegion *DeallocateAllButLast() {
87 MemRegion *next = Next;
91 return next->DeallocateAllButLast();
96 //===----------------------------------------------------------------------===//
97 // BumpPtrAllocator class implementation
98 //===----------------------------------------------------------------------===//
100 BumpPtrAllocator::BumpPtrAllocator() {
101 TheMemory = malloc(4096);
102 ((MemRegion*)TheMemory)->Init(4096, 1, 0);
105 BumpPtrAllocator::~BumpPtrAllocator() {
106 ((MemRegion*)TheMemory)->Deallocate();
109 void BumpPtrAllocator::Reset() {
110 MemRegion *MRP = (MemRegion*)TheMemory;
111 MRP = MRP->DeallocateAllButLast();
112 MRP->Init(4096, 1, 0);
116 void *BumpPtrAllocator::Allocate(size_t Size, size_t Align) {
117 MemRegion *MRP = (MemRegion*)TheMemory;
118 void *Ptr = MRP->Allocate(Size, Align, &MRP);
123 void BumpPtrAllocator::PrintStats() const {
124 unsigned BytesUsed = 0;
125 unsigned NumRegions = 0;
126 const MemRegion *R = (MemRegion*)TheMemory;
127 for (; R; R = R->getNext(), ++NumRegions)
128 BytesUsed += R->getNumBytesAllocated();
130 cerr << "\nNumber of memory regions: " << NumRegions << "\n";
131 cerr << "Bytes allocated: " << BytesUsed << "\n";