blob: 6e7a541dd4bd57ee78ce947b12fe9a3ddcde81b7 [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
Argyrios Kyrtzidisaae63a02013-08-28 01:02:21 +000029BumpPtrAllocator::BumpPtrAllocator(size_t size, size_t threshold)
30 : SlabSize(size), SizeThreshold(std::min(size, threshold)),
31 Allocator(DefaultSlabAllocator), CurSlab(0), BytesAllocated(0) { }
32
Chris Lattnerb9de9032006-10-29 22:08:03 +000033BumpPtrAllocator::~BumpPtrAllocator() {
Reid Klecknerc2d882d2009-07-23 18:34:13 +000034 DeallocateSlabs(CurSlab);
Chris Lattnerb9de9032006-10-29 22:08:03 +000035}
36
Reid Klecknerc2d882d2009-07-23 18:34:13 +000037/// AlignPtr - Align Ptr to Alignment bytes, rounding up. Alignment should
38/// be a power of two. This method rounds up, so AlignPtr(7, 4) == 8 and
39/// AlignPtr(8, 4) == 8.
40char *BumpPtrAllocator::AlignPtr(char *Ptr, size_t Alignment) {
41 assert(Alignment && (Alignment & (Alignment - 1)) == 0 &&
42 "Alignment is not a power of two!");
43
44 // Do the alignment.
45 return (char*)(((uintptr_t)Ptr + Alignment - 1) &
46 ~(uintptr_t)(Alignment - 1));
47}
48
49/// StartNewSlab - Allocate a new slab and move the bump pointers over into
50/// the new slab. Modifies CurPtr and End.
51void BumpPtrAllocator::StartNewSlab() {
Benjamin Kramer90086ab2010-09-30 16:18:28 +000052 // If we allocated a big number of slabs already it's likely that we're going
53 // to allocate more. Increase slab size to reduce mallocs and possibly memory
54 // overhead. The factors are chosen conservatively to avoid overallocation.
55 if (BytesAllocated >= SlabSize * 128)
56 SlabSize *= 2;
57
Reid Klecknerc2d882d2009-07-23 18:34:13 +000058 MemSlab *NewSlab = Allocator.Allocate(SlabSize);
59 NewSlab->NextPtr = CurSlab;
60 CurSlab = NewSlab;
61 CurPtr = (char*)(CurSlab + 1);
62 End = ((char*)CurSlab) + CurSlab->Size;
63}
64
65/// DeallocateSlabs - Deallocate all memory slabs after and including this
66/// one.
67void BumpPtrAllocator::DeallocateSlabs(MemSlab *Slab) {
68 while (Slab) {
69 MemSlab *NextSlab = Slab->NextPtr;
70#ifndef NDEBUG
71 // Poison the memory so stale pointers crash sooner. Note we must
72 // preserve the Size and NextPtr fields at the beginning.
Evan Cheng49adbf42009-09-09 01:45:24 +000073 sys::Memory::setRangeWritable(Slab + 1, Slab->Size - sizeof(MemSlab));
Reid Klecknerc2d882d2009-07-23 18:34:13 +000074 memset(Slab + 1, 0xCD, Slab->Size - sizeof(MemSlab));
75#endif
76 Allocator.Deallocate(Slab);
77 Slab = NextSlab;
78 }
79}
80
81/// Reset - Deallocate all but the current slab and reset the current pointer
82/// to the beginning of it, freeing all memory allocated so far.
Evan Cheng3dba41b2007-09-05 21:41:34 +000083void BumpPtrAllocator::Reset() {
Benjamin Kramer55cfaa32010-04-13 16:38:06 +000084 if (!CurSlab)
85 return;
Reid Klecknerc2d882d2009-07-23 18:34:13 +000086 DeallocateSlabs(CurSlab->NextPtr);
87 CurSlab->NextPtr = 0;
88 CurPtr = (char*)(CurSlab + 1);
89 End = ((char*)CurSlab) + CurSlab->Size;
Pedro Artigas0c094812013-02-20 23:30:56 +000090 BytesAllocated = 0;
Evan Cheng3dba41b2007-09-05 21:41:34 +000091}
92
Reid Klecknerc2d882d2009-07-23 18:34:13 +000093/// Allocate - Allocate space at the specified alignment.
94///
95void *BumpPtrAllocator::Allocate(size_t Size, size_t Alignment) {
Benjamin Kramer5b0650b2010-04-13 14:41:51 +000096 if (!CurSlab) // Start a new slab if we haven't allocated one already.
97 StartNewSlab();
98
Reid Klecknerc2d882d2009-07-23 18:34:13 +000099 // Keep track of how many bytes we've allocated.
100 BytesAllocated += Size;
101
Benjamin Kramerf7e02a02012-03-01 22:10:16 +0000102 // 0-byte alignment means 1-byte alignment.
103 if (Alignment == 0) Alignment = 1;
104
Reid Klecknerc2d882d2009-07-23 18:34:13 +0000105 // Allocate the aligned space, going forwards from CurPtr.
106 char *Ptr = AlignPtr(CurPtr, Alignment);
107
108 // Check if we can hold it.
109 if (Ptr + Size <= End) {
110 CurPtr = Ptr + Size;
Evgeniy Stepanov130fdcd2013-01-31 09:58:59 +0000111 // Update the allocation point of this memory block in MemorySanitizer.
Evgeniy Stepanov1f5a7142013-02-04 07:03:24 +0000112 // Without this, MemorySanitizer messages for values originated from here
113 // will point to the allocation of the entire slab.
Evgeniy Stepanov130fdcd2013-01-31 09:58:59 +0000114 __msan_allocated_memory(Ptr, Size);
Reid Klecknerc2d882d2009-07-23 18:34:13 +0000115 return Ptr;
116 }
117
118 // If Size is really big, allocate a separate slab for it.
Benjamin Kramerf7e02a02012-03-01 22:10:16 +0000119 size_t PaddedSize = Size + sizeof(MemSlab) + Alignment - 1;
Reid Kleckner4b1f2f42009-07-25 21:26:02 +0000120 if (PaddedSize > SizeThreshold) {
Reid Klecknerc2d882d2009-07-23 18:34:13 +0000121 MemSlab *NewSlab = Allocator.Allocate(PaddedSize);
122
123 // Put the new slab after the current slab, since we are not allocating
124 // into it.
125 NewSlab->NextPtr = CurSlab->NextPtr;
126 CurSlab->NextPtr = NewSlab;
127
128 Ptr = AlignPtr((char*)(NewSlab + 1), Alignment);
129 assert((uintptr_t)Ptr + Size <= (uintptr_t)NewSlab + NewSlab->Size);
Evgeniy Stepanov130fdcd2013-01-31 09:58:59 +0000130 __msan_allocated_memory(Ptr, Size);
Reid Klecknerc2d882d2009-07-23 18:34:13 +0000131 return Ptr;
132 }
133
134 // Otherwise, start a new slab and try again.
135 StartNewSlab();
136 Ptr = AlignPtr(CurPtr, Alignment);
137 CurPtr = Ptr + Size;
138 assert(CurPtr <= End && "Unable to allocate memory!");
Evgeniy Stepanov130fdcd2013-01-31 09:58:59 +0000139 __msan_allocated_memory(Ptr, Size);
Chris Lattner66330fd2007-02-23 22:31:24 +0000140 return Ptr;
Chris Lattnerb9de9032006-10-29 22:08:03 +0000141}
142
Reid Klecknerc2d882d2009-07-23 18:34:13 +0000143unsigned BumpPtrAllocator::GetNumSlabs() const {
144 unsigned NumSlabs = 0;
145 for (MemSlab *Slab = CurSlab; Slab != 0; Slab = Slab->NextPtr) {
146 ++NumSlabs;
147 }
148 return NumSlabs;
Reid Kleckner5bd61052009-07-23 00:30:41 +0000149}
150
Ted Kremenek28af26d2011-04-18 22:44:46 +0000151size_t BumpPtrAllocator::getTotalMemory() const {
152 size_t TotalMemory = 0;
153 for (MemSlab *Slab = CurSlab; Slab != 0; Slab = Slab->NextPtr) {
154 TotalMemory += Slab->Size;
155 }
156 return TotalMemory;
157}
158
Reid Klecknerc2d882d2009-07-23 18:34:13 +0000159void BumpPtrAllocator::PrintStats() const {
160 unsigned NumSlabs = 0;
161 size_t TotalMemory = 0;
162 for (MemSlab *Slab = CurSlab; Slab != 0; Slab = Slab->NextPtr) {
163 TotalMemory += Slab->Size;
164 ++NumSlabs;
165 }
166
Daniel Dunbara3d677b2009-07-24 04:01:01 +0000167 errs() << "\nNumber of memory regions: " << NumSlabs << '\n'
168 << "Bytes used: " << BytesAllocated << '\n'
169 << "Bytes allocated: " << TotalMemory << '\n'
170 << "Bytes wasted: " << (TotalMemory - BytesAllocated)
171 << " (includes alignment, etc)\n";
Reid Klecknerc2d882d2009-07-23 18:34:13 +0000172}
173
Reid Klecknerc2d882d2009-07-23 18:34:13 +0000174SlabAllocator::~SlabAllocator() { }
175
176MallocSlabAllocator::~MallocSlabAllocator() { }
177
178MemSlab *MallocSlabAllocator::Allocate(size_t Size) {
179 MemSlab *Slab = (MemSlab*)Allocator.Allocate(Size, 0);
180 Slab->Size = Size;
181 Slab->NextPtr = 0;
182 return Slab;
183}
184
185void MallocSlabAllocator::Deallocate(MemSlab *Slab) {
186 Allocator.Deallocate(Slab);
187}
188
189void PrintRecyclerStats(size_t Size,
190 size_t Align,
191 size_t FreeListSize) {
Daniel Dunbara3d677b2009-07-24 04:01:01 +0000192 errs() << "Recycler element size: " << Size << '\n'
193 << "Recycler element alignment: " << Align << '\n'
194 << "Number of elements free for recycling: " << FreeListSize << '\n';
Reid Klecknerc2d882d2009-07-23 18:34:13 +0000195}
196
Dan Gohmane5932e52008-07-07 22:58:06 +0000197}