blob: 3c4191b805a368eecb820cdecb11dad839ae497a [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;
Pedro Artigas0c094812013-02-20 23:30:56 +000086 BytesAllocated = 0;
Evan Cheng3dba41b2007-09-05 21:41:34 +000087}
88
Reid Klecknerc2d882d2009-07-23 18:34:13 +000089/// Allocate - Allocate space at the specified alignment.
90///
91void *BumpPtrAllocator::Allocate(size_t Size, size_t Alignment) {
Benjamin Kramer5b0650b2010-04-13 14:41:51 +000092 if (!CurSlab) // Start a new slab if we haven't allocated one already.
93 StartNewSlab();
94
Reid Klecknerc2d882d2009-07-23 18:34:13 +000095 // Keep track of how many bytes we've allocated.
96 BytesAllocated += Size;
97
Benjamin Kramerf7e02a02012-03-01 22:10:16 +000098 // 0-byte alignment means 1-byte alignment.
99 if (Alignment == 0) Alignment = 1;
100
Reid Klecknerc2d882d2009-07-23 18:34:13 +0000101 // Allocate the aligned space, going forwards from CurPtr.
102 char *Ptr = AlignPtr(CurPtr, Alignment);
103
104 // Check if we can hold it.
105 if (Ptr + Size <= End) {
106 CurPtr = Ptr + Size;
Evgeniy Stepanov130fdcd2013-01-31 09:58:59 +0000107 // Update the allocation point of this memory block in MemorySanitizer.
Evgeniy Stepanov1f5a7142013-02-04 07:03:24 +0000108 // Without this, MemorySanitizer messages for values originated from here
109 // will point to the allocation of the entire slab.
Evgeniy Stepanov130fdcd2013-01-31 09:58:59 +0000110 __msan_allocated_memory(Ptr, Size);
Reid Klecknerc2d882d2009-07-23 18:34:13 +0000111 return Ptr;
112 }
113
114 // If Size is really big, allocate a separate slab for it.
Benjamin Kramerf7e02a02012-03-01 22:10:16 +0000115 size_t PaddedSize = Size + sizeof(MemSlab) + Alignment - 1;
Reid Kleckner4b1f2f42009-07-25 21:26:02 +0000116 if (PaddedSize > SizeThreshold) {
Reid Klecknerc2d882d2009-07-23 18:34:13 +0000117 MemSlab *NewSlab = Allocator.Allocate(PaddedSize);
118
119 // Put the new slab after the current slab, since we are not allocating
120 // into it.
121 NewSlab->NextPtr = CurSlab->NextPtr;
122 CurSlab->NextPtr = NewSlab;
123
124 Ptr = AlignPtr((char*)(NewSlab + 1), Alignment);
125 assert((uintptr_t)Ptr + Size <= (uintptr_t)NewSlab + NewSlab->Size);
Evgeniy Stepanov130fdcd2013-01-31 09:58:59 +0000126 __msan_allocated_memory(Ptr, Size);
Reid Klecknerc2d882d2009-07-23 18:34:13 +0000127 return Ptr;
128 }
129
130 // Otherwise, start a new slab and try again.
131 StartNewSlab();
132 Ptr = AlignPtr(CurPtr, Alignment);
133 CurPtr = Ptr + Size;
134 assert(CurPtr <= End && "Unable to allocate memory!");
Evgeniy Stepanov130fdcd2013-01-31 09:58:59 +0000135 __msan_allocated_memory(Ptr, Size);
Chris Lattner66330fd2007-02-23 22:31:24 +0000136 return Ptr;
Chris Lattnerb9de9032006-10-29 22:08:03 +0000137}
138
Reid Klecknerc2d882d2009-07-23 18:34:13 +0000139unsigned BumpPtrAllocator::GetNumSlabs() const {
140 unsigned NumSlabs = 0;
141 for (MemSlab *Slab = CurSlab; Slab != 0; Slab = Slab->NextPtr) {
142 ++NumSlabs;
143 }
144 return NumSlabs;
Reid Kleckner5bd61052009-07-23 00:30:41 +0000145}
146
Ted Kremenek28af26d2011-04-18 22:44:46 +0000147size_t BumpPtrAllocator::getTotalMemory() const {
148 size_t TotalMemory = 0;
149 for (MemSlab *Slab = CurSlab; Slab != 0; Slab = Slab->NextPtr) {
150 TotalMemory += Slab->Size;
151 }
152 return TotalMemory;
153}
154
Reid Klecknerc2d882d2009-07-23 18:34:13 +0000155void BumpPtrAllocator::PrintStats() const {
156 unsigned NumSlabs = 0;
157 size_t TotalMemory = 0;
158 for (MemSlab *Slab = CurSlab; Slab != 0; Slab = Slab->NextPtr) {
159 TotalMemory += Slab->Size;
160 ++NumSlabs;
161 }
162
Daniel Dunbara3d677b2009-07-24 04:01:01 +0000163 errs() << "\nNumber of memory regions: " << NumSlabs << '\n'
164 << "Bytes used: " << BytesAllocated << '\n'
165 << "Bytes allocated: " << TotalMemory << '\n'
166 << "Bytes wasted: " << (TotalMemory - BytesAllocated)
167 << " (includes alignment, etc)\n";
Reid Klecknerc2d882d2009-07-23 18:34:13 +0000168}
169
Bill Wendlingefda1e42010-01-16 01:06:58 +0000170MallocSlabAllocator BumpPtrAllocator::DefaultSlabAllocator =
171 MallocSlabAllocator();
Reid Klecknerc2d882d2009-07-23 18:34:13 +0000172
173SlabAllocator::~SlabAllocator() { }
174
175MallocSlabAllocator::~MallocSlabAllocator() { }
176
177MemSlab *MallocSlabAllocator::Allocate(size_t Size) {
178 MemSlab *Slab = (MemSlab*)Allocator.Allocate(Size, 0);
179 Slab->Size = Size;
180 Slab->NextPtr = 0;
181 return Slab;
182}
183
184void MallocSlabAllocator::Deallocate(MemSlab *Slab) {
185 Allocator.Deallocate(Slab);
186}
187
188void PrintRecyclerStats(size_t Size,
189 size_t Align,
190 size_t FreeListSize) {
Daniel Dunbara3d677b2009-07-24 04:01:01 +0000191 errs() << "Recycler element size: " << Size << '\n'
192 << "Recycler element alignment: " << Align << '\n'
193 << "Number of elements free for recycling: " << FreeListSize << '\n';
Reid Klecknerc2d882d2009-07-23 18:34:13 +0000194}
195
Dan Gohmane5932e52008-07-07 22:58:06 +0000196}