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/System/DataTypes.h"
16 #include "llvm/Support/Recycler.h"
17 #include "llvm/Support/raw_ostream.h"
18 #include "llvm/System/Memory.h"
23 BumpPtrAllocator::BumpPtrAllocator(size_t size, size_t threshold,
24 SlabAllocator &allocator)
25 : SlabSize(size), SizeThreshold(threshold), Allocator(allocator),
26 CurSlab(0), BytesAllocated(0) { }
28 BumpPtrAllocator::~BumpPtrAllocator() {
29 DeallocateSlabs(CurSlab);
32 /// AlignPtr - Align Ptr to Alignment bytes, rounding up. Alignment should
33 /// be a power of two. This method rounds up, so AlignPtr(7, 4) == 8 and
34 /// AlignPtr(8, 4) == 8.
35 char *BumpPtrAllocator::AlignPtr(char *Ptr, size_t Alignment) {
36 assert(Alignment && (Alignment & (Alignment - 1)) == 0 &&
37 "Alignment is not a power of two!");
40 return (char*)(((uintptr_t)Ptr + Alignment - 1) &
41 ~(uintptr_t)(Alignment - 1));
44 /// StartNewSlab - Allocate a new slab and move the bump pointers over into
45 /// the new slab. Modifies CurPtr and End.
46 void BumpPtrAllocator::StartNewSlab() {
47 MemSlab *NewSlab = Allocator.Allocate(SlabSize);
48 NewSlab->NextPtr = CurSlab;
50 CurPtr = (char*)(CurSlab + 1);
51 End = ((char*)CurSlab) + CurSlab->Size;
54 /// DeallocateSlabs - Deallocate all memory slabs after and including this
56 void BumpPtrAllocator::DeallocateSlabs(MemSlab *Slab) {
58 MemSlab *NextSlab = Slab->NextPtr;
60 // Poison the memory so stale pointers crash sooner. Note we must
61 // preserve the Size and NextPtr fields at the beginning.
62 sys::Memory::setRangeWritable(Slab + 1, Slab->Size - sizeof(MemSlab));
63 memset(Slab + 1, 0xCD, Slab->Size - sizeof(MemSlab));
65 Allocator.Deallocate(Slab);
70 /// Reset - Deallocate all but the current slab and reset the current pointer
71 /// to the beginning of it, freeing all memory allocated so far.
72 void BumpPtrAllocator::Reset() {
75 DeallocateSlabs(CurSlab->NextPtr);
77 CurPtr = (char*)(CurSlab + 1);
78 End = ((char*)CurSlab) + CurSlab->Size;
81 /// Allocate - Allocate space at the specified alignment.
83 void *BumpPtrAllocator::Allocate(size_t Size, size_t Alignment) {
84 if (!CurSlab) // Start a new slab if we haven't allocated one already.
87 // Keep track of how many bytes we've allocated.
88 BytesAllocated += Size;
90 // 0-byte alignment means 1-byte alignment.
91 if (Alignment == 0) Alignment = 1;
93 // Allocate the aligned space, going forwards from CurPtr.
94 char *Ptr = AlignPtr(CurPtr, Alignment);
96 // Check if we can hold it.
97 if (Ptr + Size <= End) {
102 // If Size is really big, allocate a separate slab for it.
103 size_t PaddedSize = Size + sizeof(MemSlab) + Alignment - 1;
104 if (PaddedSize > SizeThreshold) {
105 MemSlab *NewSlab = Allocator.Allocate(PaddedSize);
107 // Put the new slab after the current slab, since we are not allocating
109 NewSlab->NextPtr = CurSlab->NextPtr;
110 CurSlab->NextPtr = NewSlab;
112 Ptr = AlignPtr((char*)(NewSlab + 1), Alignment);
113 assert((uintptr_t)Ptr + Size <= (uintptr_t)NewSlab + NewSlab->Size);
117 // Otherwise, start a new slab and try again.
119 Ptr = AlignPtr(CurPtr, Alignment);
121 assert(CurPtr <= End && "Unable to allocate memory!");
125 unsigned BumpPtrAllocator::GetNumSlabs() const {
126 unsigned NumSlabs = 0;
127 for (MemSlab *Slab = CurSlab; Slab != 0; Slab = Slab->NextPtr) {
133 void BumpPtrAllocator::PrintStats() const {
134 unsigned NumSlabs = 0;
135 size_t TotalMemory = 0;
136 for (MemSlab *Slab = CurSlab; Slab != 0; Slab = Slab->NextPtr) {
137 TotalMemory += Slab->Size;
141 errs() << "\nNumber of memory regions: " << NumSlabs << '\n'
142 << "Bytes used: " << BytesAllocated << '\n'
143 << "Bytes allocated: " << TotalMemory << '\n'
144 << "Bytes wasted: " << (TotalMemory - BytesAllocated)
145 << " (includes alignment, etc)\n";
148 MallocSlabAllocator BumpPtrAllocator::DefaultSlabAllocator =
149 MallocSlabAllocator();
151 SlabAllocator::~SlabAllocator() { }
153 MallocSlabAllocator::~MallocSlabAllocator() { }
155 MemSlab *MallocSlabAllocator::Allocate(size_t Size) {
156 MemSlab *Slab = (MemSlab*)Allocator.Allocate(Size, 0);
162 void MallocSlabAllocator::Deallocate(MemSlab *Slab) {
163 Allocator.Deallocate(Slab);
166 void PrintRecyclerStats(size_t Size,
168 size_t FreeListSize) {
169 errs() << "Recycler element size: " << Size << '\n'
170 << "Recycler element alignment: " << Align << '\n'
171 << "Number of elements free for recycling: " << FreeListSize << '\n';