2006-10-29 22:08:03 +00:00
|
|
|
//===--- Allocator.cpp - Simple memory allocation abstraction -------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-10-29 22:08:03 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the BumpPtrAllocator interface.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Support/Allocator.h"
|
2006-11-08 15:04:35 +00:00
|
|
|
#include "llvm/Support/DataTypes.h"
|
2006-11-26 10:52:51 +00:00
|
|
|
#include "llvm/Support/Streams.h"
|
2006-12-07 23:41:45 +00:00
|
|
|
#include <ostream>
|
2006-10-29 22:08:03 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// MemRegion class implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
/// MemRegion - This is one chunk of the BumpPtrAllocator.
|
|
|
|
class MemRegion {
|
|
|
|
unsigned RegionSize;
|
|
|
|
MemRegion *Next;
|
|
|
|
char *NextPtr;
|
|
|
|
public:
|
|
|
|
void Init(unsigned size, unsigned Alignment, MemRegion *next) {
|
|
|
|
RegionSize = size;
|
|
|
|
Next = next;
|
|
|
|
NextPtr = (char*)(this+1);
|
|
|
|
|
|
|
|
// Align NextPtr.
|
|
|
|
NextPtr = (char*)((intptr_t)(NextPtr+Alignment-1) &
|
|
|
|
~(intptr_t)(Alignment-1));
|
|
|
|
}
|
|
|
|
|
|
|
|
const MemRegion *getNext() const { return Next; }
|
|
|
|
unsigned getNumBytesAllocated() const {
|
|
|
|
return NextPtr-(const char*)this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Allocate - Allocate and return at least the specified number of bytes.
|
|
|
|
///
|
|
|
|
void *Allocate(unsigned AllocSize, unsigned Alignment, MemRegion **RegPtr) {
|
|
|
|
// Round size up to an even multiple of the alignment.
|
|
|
|
AllocSize = (AllocSize+Alignment-1) & ~(Alignment-1);
|
|
|
|
|
|
|
|
// If there is space in this region, return it.
|
|
|
|
if (unsigned(NextPtr+AllocSize-(char*)this) <= RegionSize) {
|
|
|
|
void *Result = NextPtr;
|
|
|
|
NextPtr += AllocSize;
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we have to allocate a new chunk. Create one twice as big as
|
|
|
|
// this one.
|
|
|
|
MemRegion *NewRegion = (MemRegion *)malloc(RegionSize*2);
|
|
|
|
NewRegion->Init(RegionSize*2, Alignment, this);
|
|
|
|
|
|
|
|
// Update the current "first region" pointer to point to the new region.
|
|
|
|
*RegPtr = NewRegion;
|
|
|
|
|
|
|
|
// Try allocating from it now.
|
|
|
|
return NewRegion->Allocate(AllocSize, Alignment, RegPtr);
|
|
|
|
}
|
|
|
|
|
2007-09-08 00:02:17 +00:00
|
|
|
/// Deallocate - Recursively release all memory for this and its next regions
|
|
|
|
/// to the system.
|
2006-10-29 22:08:03 +00:00
|
|
|
void Deallocate() {
|
|
|
|
MemRegion *next = Next;
|
|
|
|
free(this);
|
|
|
|
if (next)
|
|
|
|
next->Deallocate();
|
|
|
|
}
|
2007-09-08 00:02:17 +00:00
|
|
|
|
|
|
|
/// DeallocateAllButLast - Recursively release all memory for this and its
|
|
|
|
/// next regions to the system stopping at the last region in the list.
|
|
|
|
/// Returns the pointer to the last region.
|
|
|
|
MemRegion *DeallocateAllButLast() {
|
|
|
|
MemRegion *next = Next;
|
|
|
|
if (!next)
|
|
|
|
return this;
|
|
|
|
free(this);
|
|
|
|
return next->DeallocateAllButLast();
|
|
|
|
}
|
2006-10-29 22:08:03 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// BumpPtrAllocator class implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
BumpPtrAllocator::BumpPtrAllocator() {
|
|
|
|
TheMemory = malloc(4096);
|
|
|
|
((MemRegion*)TheMemory)->Init(4096, 1, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
BumpPtrAllocator::~BumpPtrAllocator() {
|
|
|
|
((MemRegion*)TheMemory)->Deallocate();
|
|
|
|
}
|
|
|
|
|
2007-09-05 21:41:34 +00:00
|
|
|
void BumpPtrAllocator::Reset() {
|
2007-09-08 00:02:17 +00:00
|
|
|
MemRegion *MRP = (MemRegion*)TheMemory;
|
|
|
|
MRP = MRP->DeallocateAllButLast();
|
|
|
|
MRP->Init(4096, 1, 0);
|
|
|
|
TheMemory = MRP;
|
2007-09-05 21:41:34 +00:00
|
|
|
}
|
|
|
|
|
2006-10-29 22:08:03 +00:00
|
|
|
void *BumpPtrAllocator::Allocate(unsigned Size, unsigned Align) {
|
2007-02-23 22:31:24 +00:00
|
|
|
MemRegion *MRP = (MemRegion*)TheMemory;
|
|
|
|
void *Ptr = MRP->Allocate(Size, Align, &MRP);
|
|
|
|
TheMemory = MRP;
|
|
|
|
return Ptr;
|
2006-10-29 22:08:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BumpPtrAllocator::PrintStats() const {
|
|
|
|
unsigned BytesUsed = 0;
|
|
|
|
unsigned NumRegions = 0;
|
|
|
|
const MemRegion *R = (MemRegion*)TheMemory;
|
|
|
|
for (; R; R = R->getNext(), ++NumRegions)
|
|
|
|
BytesUsed += R->getNumBytesAllocated();
|
|
|
|
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << "\nNumber of memory regions: " << NumRegions << "\n";
|
|
|
|
cerr << "Bytes allocated: " << BytesUsed << "\n";
|
2006-10-29 22:08:03 +00:00
|
|
|
}
|