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