0d0367a6f13e3a5102715cbd2bc4a622a7f87d96
[oota-llvm.git] / lib / Support / Allocator.cpp
1 //===--- 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"
15 #include <iostream>
16 using namespace llvm;
17
18 //===----------------------------------------------------------------------===//
19 // MemRegion class implementation
20 //===----------------------------------------------------------------------===//
21
22 namespace {
23 /// MemRegion - This is one chunk of the BumpPtrAllocator.
24 class MemRegion {
25   unsigned RegionSize;
26   MemRegion *Next;
27   char *NextPtr;
28 public:
29   void Init(unsigned size, unsigned Alignment, MemRegion *next) {
30     RegionSize = size;
31     Next = next;
32     NextPtr = (char*)(this+1);
33     
34     // Align NextPtr.
35     NextPtr = (char*)((intptr_t)(NextPtr+Alignment-1) &
36                       ~(intptr_t)(Alignment-1));
37   }
38   
39   const MemRegion *getNext() const { return Next; }
40   unsigned getNumBytesAllocated() const {
41     return NextPtr-(const char*)this;
42   }
43   
44   /// Allocate - Allocate and return at least the specified number of bytes.
45   ///
46   void *Allocate(unsigned AllocSize, unsigned Alignment, MemRegion **RegPtr) {
47     // Round size up to an even multiple of the alignment.
48     AllocSize = (AllocSize+Alignment-1) & ~(Alignment-1);
49     
50     // If there is space in this region, return it.
51     if (unsigned(NextPtr+AllocSize-(char*)this) <= RegionSize) {
52       void *Result = NextPtr;
53       NextPtr += AllocSize;
54       return Result;
55     }
56     
57     // Otherwise, we have to allocate a new chunk.  Create one twice as big as
58     // this one.
59     MemRegion *NewRegion = (MemRegion *)malloc(RegionSize*2);
60     NewRegion->Init(RegionSize*2, Alignment, this);
61
62     // Update the current "first region" pointer  to point to the new region.
63     *RegPtr = NewRegion;
64     
65     // Try allocating from it now.
66     return NewRegion->Allocate(AllocSize, Alignment, RegPtr);
67   }
68   
69   /// Deallocate - Release all memory for this region to the system.
70   ///
71   void Deallocate() {
72     MemRegion *next = Next;
73     free(this);
74     if (next)
75       next->Deallocate();
76   }
77 };
78 }
79
80 //===----------------------------------------------------------------------===//
81 // BumpPtrAllocator class implementation
82 //===----------------------------------------------------------------------===//
83
84 BumpPtrAllocator::BumpPtrAllocator() {
85   TheMemory = malloc(4096);
86   ((MemRegion*)TheMemory)->Init(4096, 1, 0);
87 }
88
89 BumpPtrAllocator::~BumpPtrAllocator() {
90   ((MemRegion*)TheMemory)->Deallocate();
91 }
92
93 void *BumpPtrAllocator::Allocate(unsigned Size, unsigned Align) {
94   return ((MemRegion*)TheMemory)->Allocate(Size, Align,(MemRegion**)&TheMemory);
95 }
96
97 void BumpPtrAllocator::PrintStats() const {
98   unsigned BytesUsed = 0;
99   unsigned NumRegions = 0;
100   const MemRegion *R = (MemRegion*)TheMemory;
101   for (; R; R = R->getNext(), ++NumRegions)
102     BytesUsed += R->getNumBytesAllocated();
103
104   std::cerr << "\nNumber of memory regions: " << NumRegions << "\n";
105   std::cerr << "Bytes allocated: " << BytesUsed << "\n";
106 }