blob: b4fdc1e1bc1e02f98b9afc3917cc3ea75d54c2ce [file] [log] [blame]
Chris Lattnerb9de9032006-10-29 22:08:03 +00001//===--- Allocator.cpp - Simple memory allocation abstraction -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Lattnerb9de9032006-10-29 22:08:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the BumpPtrAllocator interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Support/Allocator.h"
Evgeniy Stepanov130fdcd2013-01-31 09:58:59 +000015#include "llvm/Support/Compiler.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000016#include "llvm/Support/DataTypes.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/Support/Memory.h"
Reid Klecknerc2d882d2009-07-23 18:34:13 +000018#include "llvm/Support/Recycler.h"
Daniel Dunbara3d677b2009-07-24 04:01:01 +000019#include "llvm/Support/raw_ostream.h"
Reid Klecknerc2d882d2009-07-23 18:34:13 +000020#include <cstring>
Chris Lattnerb9de9032006-10-29 22:08:03 +000021
Reid Klecknerc2d882d2009-07-23 18:34:13 +000022namespace llvm {
Chris Lattnerb9de9032006-10-29 22:08:03 +000023
Reid Klecknerc2d882d2009-07-23 18:34:13 +000024BumpPtrAllocator::BumpPtrAllocator(size_t size, size_t threshold,
25 SlabAllocator &allocator)
Benjamin Kramerf7e02a02012-03-01 22:10:16 +000026 : SlabSize(size), SizeThreshold(std::min(size, threshold)),
27 Allocator(allocator), CurSlab(0), BytesAllocated(0) { }
Chris Lattnerb9de9032006-10-29 22:08:03 +000028
29BumpPtrAllocator::~BumpPtrAllocator() {
Reid Klecknerc2d882d2009-07-23 18:34:13 +000030 DeallocateSlabs(CurSlab);
Chris Lattnerb9de9032006-10-29 22:08:03 +000031}
32
Reid Klecknerc2d882d2009-07-23 18:34:13 +000033/// AlignPtr - Align Ptr to Alignment bytes, rounding up. Alignment should
34/// be a power of two. This method rounds up, so AlignPtr(7, 4) == 8 and
35/// AlignPtr(8, 4) == 8.
36char *BumpPtrAllocator::AlignPtr(char *Ptr, size_t Alignment) {
37 assert(Alignment && (Alignment & (Alignment - 1)) == 0 &&
38 "Alignment is not a power of two!");
39
40 // Do the alignment.
41 return (char*)(((uintptr_t)Ptr + Alignment - 1) &
42 ~(uintptr_t)(Alignment - 1));
43}
44
45/// StartNewSlab - Allocate a new slab and move the bump pointers over into
46/// the new slab. Modifies CurPtr and End.
47void BumpPtrAllocator::StartNewSlab() {
Benjamin Kramer90086ab2010-09-30 16:18:28 +000048 // If we allocated a big number of slabs already it's likely that we're going
49 // to allocate more. Increase slab size to reduce mallocs and possibly memory
50 // overhead. The factors are chosen conservatively to avoid overallocation.
51 if (BytesAllocated >= SlabSize * 128)
52 SlabSize *= 2;
53
Reid Klecknerc2d882d2009-07-23 18:34:13 +000054 MemSlab *NewSlab = Allocator.Allocate(SlabSize);
55 NewSlab->NextPtr = CurSlab;
56 CurSlab = NewSlab;
57 CurPtr = (char*)(CurSlab + 1);
58 End = ((char*)CurSlab) + CurSlab->Size;
59}
60
61/// DeallocateSlabs - Deallocate all memory slabs after and including this
62/// one.
63void BumpPtrAllocator::DeallocateSlabs(MemSlab *Slab) {
64 while (Slab) {
65 MemSlab *NextSlab = Slab->NextPtr;
66#ifndef NDEBUG
67 // Poison the memory so stale pointers crash sooner. Note we must
68 // preserve the Size and NextPtr fields at the beginning.
Evan Cheng49adbf42009-09-09 01:45:24 +000069 sys::Memory::setRangeWritable(Slab + 1, Slab->Size - sizeof(MemSlab));
Reid Klecknerc2d882d2009-07-23 18:34:13 +000070 memset(Slab + 1, 0xCD, Slab->Size - sizeof(MemSlab));
71#endif
72 Allocator.Deallocate(Slab);
73 Slab = NextSlab;
74 }
75}
76
77/// Reset - Deallocate all but the current slab and reset the current pointer
78/// to the beginning of it, freeing all memory allocated so far.
Evan Cheng3dba41b2007-09-05 21:41:34 +000079void BumpPtrAllocator::Reset() {
Benjamin Kramer55cfaa32010-04-13 16:38:06 +000080 if (!CurSlab)
81 return;
Reid Klecknerc2d882d2009-07-23 18:34:13 +000082 DeallocateSlabs(CurSlab->NextPtr);
83 CurSlab->NextPtr = 0;
84 CurPtr = (char*)(CurSlab + 1);
85 End = ((char*)CurSlab) + CurSlab->Size;
Evan Cheng3dba41b2007-09-05 21:41:34 +000086}
87
Reid Klecknerc2d882d2009-07-23 18:34:13 +000088/// Allocate - Allocate space at the specified alignment.
89///
90void *BumpPtrAllocator::Allocate(size_t Size, size_t Alignment) {
Benjamin Kramer5b0650b2010-04-13 14:41:51 +000091 if (!CurSlab) // Start a new slab if we haven't allocated one already.
92 StartNewSlab();
93
Reid Klecknerc2d882d2009-07-23 18:34:13 +000094 // Keep track of how many bytes we've allocated.
95 BytesAllocated += Size;
96
Benjamin Kramerf7e02a02012-03-01 22:10:16 +000097 // 0-byte alignment means 1-byte alignment.
98 if (Alignment == 0) Alignment = 1;
99
Reid Klecknerc2d882d2009-07-23 18:34:13 +0000100 // Allocate the aligned space, going forwards from CurPtr.
101 char *Ptr = AlignPtr(CurPtr, Alignment);
102
103 // Check if we can hold it.
104 if (Ptr + Size <= End) {
105 CurPtr = Ptr + Size;
Evgeniy Stepanov130fdcd2013-01-31 09:58:59 +0000106 // Update the allocation point of this memory block in MemorySanitizer.
107 // Without this, MemorySanitizer reports for values originating from it will
108 // point to the allocation point of the entire slab.
109 __msan_allocated_memory(Ptr, Size);
Reid Klecknerc2d882d2009-07-23 18:34:13 +0000110 return Ptr;
111 }
112
113 // If Size is really big, allocate a separate slab for it.
Benjamin Kramerf7e02a02012-03-01 22:10:16 +0000114 size_t PaddedSize = Size + sizeof(MemSlab) + Alignment - 1;
Reid Kleckner4b1f2f42009-07-25 21:26:02 +0000115 if (PaddedSize > SizeThreshold) {
Reid Klecknerc2d882d2009-07-23 18:34:13 +0000116 MemSlab *NewSlab = Allocator.Allocate(PaddedSize);
117
118 // Put the new slab after the current slab, since we are not allocating
119 // into it.
120 NewSlab->NextPtr = CurSlab->NextPtr;
121 CurSlab->NextPtr = NewSlab;
122
123 Ptr = AlignPtr((char*)(NewSlab + 1), Alignment);
124 assert((uintptr_t)Ptr + Size <= (uintptr_t)NewSlab + NewSlab->Size);
Evgeniy Stepanov130fdcd2013-01-31 09:58:59 +0000125 __msan_allocated_memory(Ptr, Size);
Reid Klecknerc2d882d2009-07-23 18:34:13 +0000126 return Ptr;
127 }
128
129 // Otherwise, start a new slab and try again.
130 StartNewSlab();
131 Ptr = AlignPtr(CurPtr, Alignment);
132 CurPtr = Ptr + Size;
133 assert(CurPtr <= End && "Unable to allocate memory!");
Evgeniy Stepanov130fdcd2013-01-31 09:58:59 +0000134 __msan_allocated_memory(Ptr, Size);
Chris Lattner66330fd2007-02-23 22:31:24 +0000135 return Ptr;
Chris Lattnerb9de9032006-10-29 22:08:03 +0000136}
137
Reid Klecknerc2d882d2009-07-23 18:34:13 +0000138unsigned BumpPtrAllocator::GetNumSlabs() const {
139 unsigned NumSlabs = 0;
140 for (MemSlab *Slab = CurSlab; Slab != 0; Slab = Slab->NextPtr) {
141 ++NumSlabs;
142 }
143 return NumSlabs;
Reid Kleckner5bd61052009-07-23 00:30:41 +0000144}
145
Ted Kremenek28af26d2011-04-18 22:44:46 +0000146size_t BumpPtrAllocator::getTotalMemory() const {
147 size_t TotalMemory = 0;
148 for (MemSlab *Slab = CurSlab; Slab != 0; Slab = Slab->NextPtr) {
149 TotalMemory += Slab->Size;
150 }
151 return TotalMemory;
152}
153
Reid Klecknerc2d882d2009-07-23 18:34:13 +0000154void BumpPtrAllocator::PrintStats() const {
155 unsigned NumSlabs = 0;
156 size_t TotalMemory = 0;
157 for (MemSlab *Slab = CurSlab; Slab != 0; Slab = Slab->NextPtr) {
158 TotalMemory += Slab->Size;
159 ++NumSlabs;
160 }
161
Daniel Dunbara3d677b2009-07-24 04:01:01 +0000162 errs() << "\nNumber of memory regions: " << NumSlabs << '\n'
163 << "Bytes used: " << BytesAllocated << '\n'
164 << "Bytes allocated: " << TotalMemory << '\n'
165 << "Bytes wasted: " << (TotalMemory - BytesAllocated)
166 << " (includes alignment, etc)\n";
Reid Klecknerc2d882d2009-07-23 18:34:13 +0000167}
168
Bill Wendlingefda1e42010-01-16 01:06:58 +0000169MallocSlabAllocator BumpPtrAllocator::DefaultSlabAllocator =
170 MallocSlabAllocator();
Reid Klecknerc2d882d2009-07-23 18:34:13 +0000171
172SlabAllocator::~SlabAllocator() { }
173
174MallocSlabAllocator::~MallocSlabAllocator() { }
175
176MemSlab *MallocSlabAllocator::Allocate(size_t Size) {
177 MemSlab *Slab = (MemSlab*)Allocator.Allocate(Size, 0);
178 Slab->Size = Size;
179 Slab->NextPtr = 0;
180 return Slab;
181}
182
183void MallocSlabAllocator::Deallocate(MemSlab *Slab) {
184 Allocator.Deallocate(Slab);
185}
186
187void PrintRecyclerStats(size_t Size,
188 size_t Align,
189 size_t FreeListSize) {
Daniel Dunbara3d677b2009-07-24 04:01:01 +0000190 errs() << "Recycler element size: " << Size << '\n'
191 << "Recycler element alignment: " << Align << '\n'
192 << "Number of elements free for recycling: " << FreeListSize << '\n';
Reid Klecknerc2d882d2009-07-23 18:34:13 +0000193}
194
Dan Gohmane5932e52008-07-07 22:58:06 +0000195}