b4fdc1e1bc1e02f98b9afc3917cc3ea75d54c2ce
[oota-llvm.git] / lib / Support / Allocator.cpp
1 //===--- Allocator.cpp - Simple memory allocation abstraction -------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the BumpPtrAllocator interface.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Support/Allocator.h"
15 #include "llvm/Support/Compiler.h"
16 #include "llvm/Support/DataTypes.h"
17 #include "llvm/Support/Memory.h"
18 #include "llvm/Support/Recycler.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include <cstring>
21
22 namespace llvm {
23
24 BumpPtrAllocator::BumpPtrAllocator(size_t size, size_t threshold,
25                                    SlabAllocator &allocator)
26     : SlabSize(size), SizeThreshold(std::min(size, threshold)),
27       Allocator(allocator), CurSlab(0), BytesAllocated(0) { }
28
29 BumpPtrAllocator::~BumpPtrAllocator() {
30   DeallocateSlabs(CurSlab);
31 }
32
33 /// 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.
36 char *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.
47 void BumpPtrAllocator::StartNewSlab() {
48   // 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
54   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.
63 void 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.
69     sys::Memory::setRangeWritable(Slab + 1, Slab->Size - sizeof(MemSlab));
70     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.
79 void BumpPtrAllocator::Reset() {
80   if (!CurSlab)
81     return;
82   DeallocateSlabs(CurSlab->NextPtr);
83   CurSlab->NextPtr = 0;
84   CurPtr = (char*)(CurSlab + 1);
85   End = ((char*)CurSlab) + CurSlab->Size;
86 }
87
88 /// Allocate - Allocate space at the specified alignment.
89 ///
90 void *BumpPtrAllocator::Allocate(size_t Size, size_t Alignment) {
91   if (!CurSlab) // Start a new slab if we haven't allocated one already.
92     StartNewSlab();
93
94   // Keep track of how many bytes we've allocated.
95   BytesAllocated += Size;
96
97   // 0-byte alignment means 1-byte alignment.
98   if (Alignment == 0) Alignment = 1;
99
100   // 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;
106     // 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);
110     return Ptr;
111   }
112
113   // If Size is really big, allocate a separate slab for it.
114   size_t PaddedSize = Size + sizeof(MemSlab) + Alignment - 1;
115   if (PaddedSize > SizeThreshold) {
116     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);
125     __msan_allocated_memory(Ptr, Size);
126     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!");
134   __msan_allocated_memory(Ptr, Size);
135   return Ptr;
136 }
137
138 unsigned BumpPtrAllocator::GetNumSlabs() const {
139   unsigned NumSlabs = 0;
140   for (MemSlab *Slab = CurSlab; Slab != 0; Slab = Slab->NextPtr) {
141     ++NumSlabs;
142   }
143   return NumSlabs;
144 }
145
146 size_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   
154 void 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
162   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";
167 }
168
169 MallocSlabAllocator BumpPtrAllocator::DefaultSlabAllocator =
170   MallocSlabAllocator();
171
172 SlabAllocator::~SlabAllocator() { }
173
174 MallocSlabAllocator::~MallocSlabAllocator() { }
175
176 MemSlab *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
183 void MallocSlabAllocator::Deallocate(MemSlab *Slab) {
184   Allocator.Deallocate(Slab);
185 }
186
187 void PrintRecyclerStats(size_t Size,
188                         size_t Align,
189                         size_t FreeListSize) {
190   errs() << "Recycler element size: " << Size << '\n'
191          << "Recycler element alignment: " << Align << '\n'
192          << "Number of elements free for recycling: " << FreeListSize << '\n';
193 }
194
195 }