blob: ef40e47d9851ae57fb9f000f25a985e3c50ff62f [file] [log] [blame]
Chris Lattner9f617d62006-10-29 22:08:03 +00001//===--- Allocator.cpp - Simple memory allocation abstraction -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner9f617d62006-10-29 22:08:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the BumpPtrAllocator interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Support/Allocator.h"
Chandler Carruth8b67f772009-10-26 01:35:46 +000015#include "llvm/System/DataTypes.h"
Reid Kleckner8f51a622009-07-23 18:34:13 +000016#include "llvm/Support/Recycler.h"
Daniel Dunbar7da95592009-07-24 04:01:01 +000017#include "llvm/Support/raw_ostream.h"
Evan Chengc48edbb2009-09-09 01:45:24 +000018#include "llvm/System/Memory.h"
Reid Kleckner8f51a622009-07-23 18:34:13 +000019#include <cstring>
Chris Lattner9f617d62006-10-29 22:08:03 +000020
Reid Kleckner8f51a622009-07-23 18:34:13 +000021namespace llvm {
Chris Lattner9f617d62006-10-29 22:08:03 +000022
Reid Kleckner8f51a622009-07-23 18:34:13 +000023BumpPtrAllocator::BumpPtrAllocator(size_t size, size_t threshold,
24 SlabAllocator &allocator)
25 : SlabSize(size), SizeThreshold(threshold), Allocator(allocator),
Benjamin Kramer5e6a7052010-04-13 14:41:51 +000026 CurSlab(0), BytesAllocated(0) { }
Chris Lattner9f617d62006-10-29 22:08:03 +000027
28BumpPtrAllocator::~BumpPtrAllocator() {
Reid Kleckner8f51a622009-07-23 18:34:13 +000029 DeallocateSlabs(CurSlab);
Chris Lattner9f617d62006-10-29 22:08:03 +000030}
31
Reid Kleckner8f51a622009-07-23 18:34:13 +000032/// 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.
35char *BumpPtrAllocator::AlignPtr(char *Ptr, size_t Alignment) {
36 assert(Alignment && (Alignment & (Alignment - 1)) == 0 &&
37 "Alignment is not a power of two!");
38
39 // Do the alignment.
40 return (char*)(((uintptr_t)Ptr + Alignment - 1) &
41 ~(uintptr_t)(Alignment - 1));
42}
43
44/// StartNewSlab - Allocate a new slab and move the bump pointers over into
45/// the new slab. Modifies CurPtr and End.
46void BumpPtrAllocator::StartNewSlab() {
47 MemSlab *NewSlab = Allocator.Allocate(SlabSize);
48 NewSlab->NextPtr = CurSlab;
49 CurSlab = NewSlab;
50 CurPtr = (char*)(CurSlab + 1);
51 End = ((char*)CurSlab) + CurSlab->Size;
52}
53
54/// DeallocateSlabs - Deallocate all memory slabs after and including this
55/// one.
56void BumpPtrAllocator::DeallocateSlabs(MemSlab *Slab) {
57 while (Slab) {
58 MemSlab *NextSlab = Slab->NextPtr;
59#ifndef NDEBUG
60 // Poison the memory so stale pointers crash sooner. Note we must
61 // preserve the Size and NextPtr fields at the beginning.
Evan Chengc48edbb2009-09-09 01:45:24 +000062 sys::Memory::setRangeWritable(Slab + 1, Slab->Size - sizeof(MemSlab));
Reid Kleckner8f51a622009-07-23 18:34:13 +000063 memset(Slab + 1, 0xCD, Slab->Size - sizeof(MemSlab));
64#endif
65 Allocator.Deallocate(Slab);
66 Slab = NextSlab;
67 }
68}
69
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.
Evan Cheng188b5222007-09-05 21:41:34 +000072void BumpPtrAllocator::Reset() {
Benjamin Kramer5e6a7052010-04-13 14:41:51 +000073 if (!CurSlab) // Start a new slab if we didn't allocate one already.
74 StartNewSlab();
Reid Kleckner8f51a622009-07-23 18:34:13 +000075 DeallocateSlabs(CurSlab->NextPtr);
76 CurSlab->NextPtr = 0;
77 CurPtr = (char*)(CurSlab + 1);
78 End = ((char*)CurSlab) + CurSlab->Size;
Evan Cheng188b5222007-09-05 21:41:34 +000079}
80
Reid Kleckner8f51a622009-07-23 18:34:13 +000081/// Allocate - Allocate space at the specified alignment.
82///
83void *BumpPtrAllocator::Allocate(size_t Size, size_t Alignment) {
Benjamin Kramer5e6a7052010-04-13 14:41:51 +000084 if (!CurSlab) // Start a new slab if we haven't allocated one already.
85 StartNewSlab();
86
Reid Kleckner8f51a622009-07-23 18:34:13 +000087 // Keep track of how many bytes we've allocated.
88 BytesAllocated += Size;
89
90 // 0-byte alignment means 1-byte alignment.
91 if (Alignment == 0) Alignment = 1;
92
93 // Allocate the aligned space, going forwards from CurPtr.
94 char *Ptr = AlignPtr(CurPtr, Alignment);
95
96 // Check if we can hold it.
97 if (Ptr + Size <= End) {
98 CurPtr = Ptr + Size;
99 return Ptr;
100 }
101
102 // If Size is really big, allocate a separate slab for it.
Reid Kleckner7d509132009-07-25 21:26:02 +0000103 size_t PaddedSize = Size + sizeof(MemSlab) + Alignment - 1;
104 if (PaddedSize > SizeThreshold) {
Reid Kleckner8f51a622009-07-23 18:34:13 +0000105 MemSlab *NewSlab = Allocator.Allocate(PaddedSize);
106
107 // Put the new slab after the current slab, since we are not allocating
108 // into it.
109 NewSlab->NextPtr = CurSlab->NextPtr;
110 CurSlab->NextPtr = NewSlab;
111
112 Ptr = AlignPtr((char*)(NewSlab + 1), Alignment);
113 assert((uintptr_t)Ptr + Size <= (uintptr_t)NewSlab + NewSlab->Size);
114 return Ptr;
115 }
116
117 // Otherwise, start a new slab and try again.
118 StartNewSlab();
119 Ptr = AlignPtr(CurPtr, Alignment);
120 CurPtr = Ptr + Size;
121 assert(CurPtr <= End && "Unable to allocate memory!");
Chris Lattnerd675b832007-02-23 22:31:24 +0000122 return Ptr;
Chris Lattner9f617d62006-10-29 22:08:03 +0000123}
124
Reid Kleckner8f51a622009-07-23 18:34:13 +0000125unsigned BumpPtrAllocator::GetNumSlabs() const {
126 unsigned NumSlabs = 0;
127 for (MemSlab *Slab = CurSlab; Slab != 0; Slab = Slab->NextPtr) {
128 ++NumSlabs;
129 }
130 return NumSlabs;
Reid Kleckner95eb3ad2009-07-23 00:30:41 +0000131}
132
Reid Kleckner8f51a622009-07-23 18:34:13 +0000133void 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;
138 ++NumSlabs;
139 }
140
Daniel Dunbar7da95592009-07-24 04:01:01 +0000141 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";
Reid Kleckner8f51a622009-07-23 18:34:13 +0000146}
147
Bill Wendlingc5b7b192010-01-16 01:06:58 +0000148MallocSlabAllocator BumpPtrAllocator::DefaultSlabAllocator =
149 MallocSlabAllocator();
Reid Kleckner8f51a622009-07-23 18:34:13 +0000150
151SlabAllocator::~SlabAllocator() { }
152
153MallocSlabAllocator::~MallocSlabAllocator() { }
154
155MemSlab *MallocSlabAllocator::Allocate(size_t Size) {
156 MemSlab *Slab = (MemSlab*)Allocator.Allocate(Size, 0);
157 Slab->Size = Size;
158 Slab->NextPtr = 0;
159 return Slab;
160}
161
162void MallocSlabAllocator::Deallocate(MemSlab *Slab) {
163 Allocator.Deallocate(Slab);
164}
165
166void PrintRecyclerStats(size_t Size,
167 size_t Align,
168 size_t FreeListSize) {
Daniel Dunbar7da95592009-07-24 04:01:01 +0000169 errs() << "Recycler element size: " << Size << '\n'
170 << "Recycler element alignment: " << Align << '\n'
171 << "Number of elements free for recycling: " << FreeListSize << '\n';
Reid Kleckner8f51a622009-07-23 18:34:13 +0000172}
173
Dan Gohmane14d81d2008-07-07 22:58:06 +0000174}