1 //===- Allocator.h - Simple memory allocation abstraction -------*- C++ -*-===//
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 /// \file
10 ///
11 /// This file defines the MallocAllocator and BumpPtrAllocator interfaces. Both
12 /// of these conform to an LLVM "Allocator" concept which consists of an
13 /// Allocate method accepting a size and alignment, and a Deallocate accepting
14 /// a pointer and size. Further, the LLVM "Allocator" concept has overloads of
15 /// Allocate and Deallocate for setting size and alignment based on the final
16 /// type. These overloads are typically provided by a base class template \c
17 /// AllocatorBase.
18 ///
19 //===----------------------------------------------------------------------===//
20 
21 #ifndef LLVM_SUPPORT_ALLOCATOR_H
22 #define LLVM_SUPPORT_ALLOCATOR_H
23 
24 #include "llvm/ADT/Optional.h"
25 #include "llvm/ADT/SmallVector.h"
26 #include "llvm/Support/Compiler.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/MathExtras.h"
29 #include "llvm/Support/MemAlloc.h"
30 #include <algorithm>
31 #include <cassert>
32 #include <cstddef>
33 #include <cstdint>
34 #include <cstdlib>
35 #include <iterator>
36 #include <type_traits>
37 #include <utility>
38 
39 namespace llvm {
40 
41 /// CRTP base class providing obvious overloads for the core \c
42 /// Allocate() methods of LLVM-style allocators.
43 ///
44 /// This base class both documents the full public interface exposed by all
45 /// LLVM-style allocators, and redirects all of the overloads to a single core
46 /// set of methods which the derived class must define.
47 template <typename DerivedT> class AllocatorBase {
48 public:
49   /// Allocate \a Size bytes of \a Alignment aligned memory. This method
50   /// must be implemented by \c DerivedT.
Allocate(size_t Size,size_t Alignment)51   void *Allocate(size_t Size, size_t Alignment) {
52 #ifdef __clang__
53     static_assert(static_cast<void *(AllocatorBase::*)(size_t, size_t)>(
54                       &AllocatorBase::Allocate) !=
55                       static_cast<void *(DerivedT::*)(size_t, size_t)>(
56                           &DerivedT::Allocate),
57                   "Class derives from AllocatorBase without implementing the "
58                   "core Allocate(size_t, size_t) overload!");
59 #endif
60     return static_cast<DerivedT *>(this)->Allocate(Size, Alignment);
61   }
62 
63   /// Deallocate \a Ptr to \a Size bytes of memory allocated by this
64   /// allocator.
Deallocate(const void * Ptr,size_t Size)65   void Deallocate(const void *Ptr, size_t Size) {
66 #ifdef __clang__
67     static_assert(static_cast<void (AllocatorBase::*)(const void *, size_t)>(
68                       &AllocatorBase::Deallocate) !=
69                       static_cast<void (DerivedT::*)(const void *, size_t)>(
70                           &DerivedT::Deallocate),
71                   "Class derives from AllocatorBase without implementing the "
72                   "core Deallocate(void *) overload!");
73 #endif
74     return static_cast<DerivedT *>(this)->Deallocate(Ptr, Size);
75   }
76 
77   // The rest of these methods are helpers that redirect to one of the above
78   // core methods.
79 
80   /// Allocate space for a sequence of objects without constructing them.
81   template <typename T> T *Allocate(size_t Num = 1) {
82     return static_cast<T *>(Allocate(Num * sizeof(T), alignof(T)));
83   }
84 
85   /// Deallocate space for a sequence of objects without constructing them.
86   template <typename T>
87   typename std::enable_if<
88       !std::is_same<typename std::remove_cv<T>::type, void>::value, void>::type
89   Deallocate(T *Ptr, size_t Num = 1) {
90     Deallocate(static_cast<const void *>(Ptr), Num * sizeof(T));
91   }
92 };
93 
94 class MallocAllocator : public AllocatorBase<MallocAllocator> {
95 public:
Reset()96   void Reset() {}
97 
Allocate(size_t Size,size_t)98   LLVM_ATTRIBUTE_RETURNS_NONNULL void *Allocate(size_t Size,
99                                                 size_t /*Alignment*/) {
100     return safe_malloc(Size);
101   }
102 
103   // Pull in base class overloads.
104   using AllocatorBase<MallocAllocator>::Allocate;
105 
Deallocate(const void * Ptr,size_t)106   void Deallocate(const void *Ptr, size_t /*Size*/) {
107     free(const_cast<void *>(Ptr));
108   }
109 
110   // Pull in base class overloads.
111   using AllocatorBase<MallocAllocator>::Deallocate;
112 
PrintStats()113   void PrintStats() const {}
114 };
115 
116 namespace detail {
117 
118 // We call out to an external function to actually print the message as the
119 // printing code uses Allocator.h in its implementation.
120 void printBumpPtrAllocatorStats(unsigned NumSlabs, size_t BytesAllocated,
121                                 size_t TotalMemory);
122 
123 } // end namespace detail
124 
125 /// Allocate memory in an ever growing pool, as if by bump-pointer.
126 ///
127 /// This isn't strictly a bump-pointer allocator as it uses backing slabs of
128 /// memory rather than relying on a boundless contiguous heap. However, it has
129 /// bump-pointer semantics in that it is a monotonically growing pool of memory
130 /// where every allocation is found by merely allocating the next N bytes in
131 /// the slab, or the next N bytes in the next slab.
132 ///
133 /// Note that this also has a threshold for forcing allocations above a certain
134 /// size into their own slab.
135 ///
136 /// The BumpPtrAllocatorImpl template defaults to using a MallocAllocator
137 /// object, which wraps malloc, to allocate memory, but it can be changed to
138 /// use a custom allocator.
139 template <typename AllocatorT = MallocAllocator, size_t SlabSize = 4096,
140           size_t SizeThreshold = SlabSize>
141 class BumpPtrAllocatorImpl
142     : public AllocatorBase<
143           BumpPtrAllocatorImpl<AllocatorT, SlabSize, SizeThreshold>> {
144 public:
145   static_assert(SizeThreshold <= SlabSize,
146                 "The SizeThreshold must be at most the SlabSize to ensure "
147                 "that objects larger than a slab go into their own memory "
148                 "allocation.");
149 
150   BumpPtrAllocatorImpl() = default;
151 
152   template <typename T>
BumpPtrAllocatorImpl(T && Allocator)153   BumpPtrAllocatorImpl(T &&Allocator)
154       : Allocator(std::forward<T &&>(Allocator)) {}
155 
156   // Manually implement a move constructor as we must clear the old allocator's
157   // slabs as a matter of correctness.
BumpPtrAllocatorImpl(BumpPtrAllocatorImpl && Old)158   BumpPtrAllocatorImpl(BumpPtrAllocatorImpl &&Old)
159       : CurPtr(Old.CurPtr), End(Old.End), Slabs(std::move(Old.Slabs)),
160         CustomSizedSlabs(std::move(Old.CustomSizedSlabs)),
161         BytesAllocated(Old.BytesAllocated), RedZoneSize(Old.RedZoneSize),
162         Allocator(std::move(Old.Allocator)) {
163     Old.CurPtr = Old.End = nullptr;
164     Old.BytesAllocated = 0;
165     Old.Slabs.clear();
166     Old.CustomSizedSlabs.clear();
167   }
168 
~BumpPtrAllocatorImpl()169   ~BumpPtrAllocatorImpl() {
170     DeallocateSlabs(Slabs.begin(), Slabs.end());
171     DeallocateCustomSizedSlabs();
172   }
173 
174   BumpPtrAllocatorImpl &operator=(BumpPtrAllocatorImpl &&RHS) {
175     DeallocateSlabs(Slabs.begin(), Slabs.end());
176     DeallocateCustomSizedSlabs();
177 
178     CurPtr = RHS.CurPtr;
179     End = RHS.End;
180     BytesAllocated = RHS.BytesAllocated;
181     RedZoneSize = RHS.RedZoneSize;
182     Slabs = std::move(RHS.Slabs);
183     CustomSizedSlabs = std::move(RHS.CustomSizedSlabs);
184     Allocator = std::move(RHS.Allocator);
185 
186     RHS.CurPtr = RHS.End = nullptr;
187     RHS.BytesAllocated = 0;
188     RHS.Slabs.clear();
189     RHS.CustomSizedSlabs.clear();
190     return *this;
191   }
192 
193   /// Deallocate all but the current slab and reset the current pointer
194   /// to the beginning of it, freeing all memory allocated so far.
Reset()195   void Reset() {
196     // Deallocate all but the first slab, and deallocate all custom-sized slabs.
197     DeallocateCustomSizedSlabs();
198     CustomSizedSlabs.clear();
199 
200     if (Slabs.empty())
201       return;
202 
203     // Reset the state.
204     BytesAllocated = 0;
205     CurPtr = (char *)Slabs.front();
206     End = CurPtr + SlabSize;
207 
208     __asan_poison_memory_region(*Slabs.begin(), computeSlabSize(0));
209     DeallocateSlabs(std::next(Slabs.begin()), Slabs.end());
210     Slabs.erase(std::next(Slabs.begin()), Slabs.end());
211   }
212 
213   /// Allocate space at the specified alignment.
214   LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void *
Allocate(size_t Size,size_t Alignment)215   Allocate(size_t Size, size_t Alignment) {
216     assert(Alignment > 0 && "0-byte alignnment is not allowed. Use 1 instead.");
217 
218     // Keep track of how many bytes we've allocated.
219     BytesAllocated += Size;
220 
221     size_t Adjustment = alignmentAdjustment(CurPtr, Alignment);
222     assert(Adjustment + Size >= Size && "Adjustment + Size must not overflow");
223 
224     size_t SizeToAllocate = Size;
225 #if LLVM_ADDRESS_SANITIZER_BUILD
226     // Add trailing bytes as a "red zone" under ASan.
227     SizeToAllocate += RedZoneSize;
228 #endif
229 
230     // Check if we have enough space.
231     if (Adjustment + SizeToAllocate <= size_t(End - CurPtr)) {
232       char *AlignedPtr = CurPtr + Adjustment;
233       CurPtr = AlignedPtr + SizeToAllocate;
234       // Update the allocation point of this memory block in MemorySanitizer.
235       // Without this, MemorySanitizer messages for values originated from here
236       // will point to the allocation of the entire slab.
237       __msan_allocated_memory(AlignedPtr, Size);
238       // Similarly, tell ASan about this space.
239       __asan_unpoison_memory_region(AlignedPtr, Size);
240       return AlignedPtr;
241     }
242 
243     // If Size is really big, allocate a separate slab for it.
244     size_t PaddedSize = SizeToAllocate + Alignment - 1;
245     if (PaddedSize > SizeThreshold) {
246       void *NewSlab = Allocator.Allocate(PaddedSize, 0);
247       // We own the new slab and don't want anyone reading anyting other than
248       // pieces returned from this method.  So poison the whole slab.
249       __asan_poison_memory_region(NewSlab, PaddedSize);
250       CustomSizedSlabs.push_back(std::make_pair(NewSlab, PaddedSize));
251 
252       uintptr_t AlignedAddr = alignAddr(NewSlab, Alignment);
253       assert(AlignedAddr + Size <= (uintptr_t)NewSlab + PaddedSize);
254       char *AlignedPtr = (char*)AlignedAddr;
255       __msan_allocated_memory(AlignedPtr, Size);
256       __asan_unpoison_memory_region(AlignedPtr, Size);
257       return AlignedPtr;
258     }
259 
260     // Otherwise, start a new slab and try again.
261     StartNewSlab();
262     uintptr_t AlignedAddr = alignAddr(CurPtr, Alignment);
263     assert(AlignedAddr + SizeToAllocate <= (uintptr_t)End &&
264            "Unable to allocate memory!");
265     char *AlignedPtr = (char*)AlignedAddr;
266     CurPtr = AlignedPtr + SizeToAllocate;
267     __msan_allocated_memory(AlignedPtr, Size);
268     __asan_unpoison_memory_region(AlignedPtr, Size);
269     return AlignedPtr;
270   }
271 
272   // Pull in base class overloads.
273   using AllocatorBase<BumpPtrAllocatorImpl>::Allocate;
274 
275   // Bump pointer allocators are expected to never free their storage; and
276   // clients expect pointers to remain valid for non-dereferencing uses even
277   // after deallocation.
Deallocate(const void * Ptr,size_t Size)278   void Deallocate(const void *Ptr, size_t Size) {
279     __asan_poison_memory_region(Ptr, Size);
280   }
281 
282   // Pull in base class overloads.
283   using AllocatorBase<BumpPtrAllocatorImpl>::Deallocate;
284 
GetNumSlabs()285   size_t GetNumSlabs() const { return Slabs.size() + CustomSizedSlabs.size(); }
286 
287   /// \return An index uniquely and reproducibly identifying
288   /// an input pointer \p Ptr in the given allocator.
289   /// The returned value is negative iff the object is inside a custom-size
290   /// slab.
291   /// Returns an empty optional if the pointer is not found in the allocator.
identifyObject(const void * Ptr)292   llvm::Optional<int64_t> identifyObject(const void *Ptr) {
293     const char *P = static_cast<const char *>(Ptr);
294     int64_t InSlabIdx = 0;
295     for (size_t Idx = 0, E = Slabs.size(); Idx < E; Idx++) {
296       const char *S = static_cast<const char *>(Slabs[Idx]);
297       if (P >= S && P < S + computeSlabSize(Idx))
298         return InSlabIdx + static_cast<int64_t>(P - S);
299       InSlabIdx += static_cast<int64_t>(computeSlabSize(Idx));
300     }
301 
302     // Use negative index to denote custom sized slabs.
303     int64_t InCustomSizedSlabIdx = -1;
304     for (size_t Idx = 0, E = CustomSizedSlabs.size(); Idx < E; Idx++) {
305       const char *S = static_cast<const char *>(CustomSizedSlabs[Idx].first);
306       size_t Size = CustomSizedSlabs[Idx].second;
307       if (P >= S && P < S + Size)
308         return InCustomSizedSlabIdx - static_cast<int64_t>(P - S);
309       InCustomSizedSlabIdx -= static_cast<int64_t>(Size);
310     }
311     return None;
312   }
313 
314   /// A wrapper around identifyObject that additionally asserts that
315   /// the object is indeed within the allocator.
316   /// \return An index uniquely and reproducibly identifying
317   /// an input pointer \p Ptr in the given allocator.
identifyKnownObject(const void * Ptr)318   int64_t identifyKnownObject(const void *Ptr) {
319     Optional<int64_t> Out = identifyObject(Ptr);
320     assert(Out && "Wrong allocator used");
321     return *Out;
322   }
323 
324   /// A wrapper around identifyKnownObject. Accepts type information
325   /// about the object and produces a smaller identifier by relying on
326   /// the alignment information. Note that sub-classes may have different
327   /// alignment, so the most base class should be passed as template parameter
328   /// in order to obtain correct results. For that reason automatic template
329   /// parameter deduction is disabled.
330   /// \return An index uniquely and reproducibly identifying
331   /// an input pointer \p Ptr in the given allocator. This identifier is
332   /// different from the ones produced by identifyObject and
333   /// identifyAlignedObject.
334   template <typename T>
identifyKnownAlignedObject(const void * Ptr)335   int64_t identifyKnownAlignedObject(const void *Ptr) {
336     int64_t Out = identifyKnownObject(Ptr);
337     assert(Out % alignof(T) == 0 && "Wrong alignment information");
338     return Out / alignof(T);
339   }
340 
getTotalMemory()341   size_t getTotalMemory() const {
342     size_t TotalMemory = 0;
343     for (auto I = Slabs.begin(), E = Slabs.end(); I != E; ++I)
344       TotalMemory += computeSlabSize(std::distance(Slabs.begin(), I));
345     for (auto &PtrAndSize : CustomSizedSlabs)
346       TotalMemory += PtrAndSize.second;
347     return TotalMemory;
348   }
349 
getBytesAllocated()350   size_t getBytesAllocated() const { return BytesAllocated; }
351 
setRedZoneSize(size_t NewSize)352   void setRedZoneSize(size_t NewSize) {
353     RedZoneSize = NewSize;
354   }
355 
PrintStats()356   void PrintStats() const {
357     detail::printBumpPtrAllocatorStats(Slabs.size(), BytesAllocated,
358                                        getTotalMemory());
359   }
360 
361 private:
362   /// The current pointer into the current slab.
363   ///
364   /// This points to the next free byte in the slab.
365   char *CurPtr = nullptr;
366 
367   /// The end of the current slab.
368   char *End = nullptr;
369 
370   /// The slabs allocated so far.
371   SmallVector<void *, 4> Slabs;
372 
373   /// Custom-sized slabs allocated for too-large allocation requests.
374   SmallVector<std::pair<void *, size_t>, 0> CustomSizedSlabs;
375 
376   /// How many bytes we've allocated.
377   ///
378   /// Used so that we can compute how much space was wasted.
379   size_t BytesAllocated = 0;
380 
381   /// The number of bytes to put between allocations when running under
382   /// a sanitizer.
383   size_t RedZoneSize = 1;
384 
385   /// The allocator instance we use to get slabs of memory.
386   AllocatorT Allocator;
387 
computeSlabSize(unsigned SlabIdx)388   static size_t computeSlabSize(unsigned SlabIdx) {
389     // Scale the actual allocated slab size based on the number of slabs
390     // allocated. Every 128 slabs allocated, we double the allocated size to
391     // reduce allocation frequency, but saturate at multiplying the slab size by
392     // 2^30.
393     return SlabSize * ((size_t)1 << std::min<size_t>(30, SlabIdx / 128));
394   }
395 
396   /// Allocate a new slab and move the bump pointers over into the new
397   /// slab, modifying CurPtr and End.
StartNewSlab()398   void StartNewSlab() {
399     size_t AllocatedSlabSize = computeSlabSize(Slabs.size());
400 
401     void *NewSlab = Allocator.Allocate(AllocatedSlabSize, 0);
402     // We own the new slab and don't want anyone reading anything other than
403     // pieces returned from this method.  So poison the whole slab.
404     __asan_poison_memory_region(NewSlab, AllocatedSlabSize);
405 
406     Slabs.push_back(NewSlab);
407     CurPtr = (char *)(NewSlab);
408     End = ((char *)NewSlab) + AllocatedSlabSize;
409   }
410 
411   /// Deallocate a sequence of slabs.
DeallocateSlabs(SmallVectorImpl<void * >::iterator I,SmallVectorImpl<void * >::iterator E)412   void DeallocateSlabs(SmallVectorImpl<void *>::iterator I,
413                        SmallVectorImpl<void *>::iterator E) {
414     for (; I != E; ++I) {
415       size_t AllocatedSlabSize =
416           computeSlabSize(std::distance(Slabs.begin(), I));
417       Allocator.Deallocate(*I, AllocatedSlabSize);
418     }
419   }
420 
421   /// Deallocate all memory for custom sized slabs.
DeallocateCustomSizedSlabs()422   void DeallocateCustomSizedSlabs() {
423     for (auto &PtrAndSize : CustomSizedSlabs) {
424       void *Ptr = PtrAndSize.first;
425       size_t Size = PtrAndSize.second;
426       Allocator.Deallocate(Ptr, Size);
427     }
428   }
429 
430   template <typename T> friend class SpecificBumpPtrAllocator;
431 };
432 
433 /// The standard BumpPtrAllocator which just uses the default template
434 /// parameters.
435 typedef BumpPtrAllocatorImpl<> BumpPtrAllocator;
436 
437 /// A BumpPtrAllocator that allows only elements of a specific type to be
438 /// allocated.
439 ///
440 /// This allows calling the destructor in DestroyAll() and when the allocator is
441 /// destroyed.
442 template <typename T> class SpecificBumpPtrAllocator {
443   BumpPtrAllocator Allocator;
444 
445 public:
SpecificBumpPtrAllocator()446   SpecificBumpPtrAllocator() {
447     // Because SpecificBumpPtrAllocator walks the memory to call destructors,
448     // it can't have red zones between allocations.
449     Allocator.setRedZoneSize(0);
450   }
SpecificBumpPtrAllocator(SpecificBumpPtrAllocator && Old)451   SpecificBumpPtrAllocator(SpecificBumpPtrAllocator &&Old)
452       : Allocator(std::move(Old.Allocator)) {}
~SpecificBumpPtrAllocator()453   ~SpecificBumpPtrAllocator() { DestroyAll(); }
454 
455   SpecificBumpPtrAllocator &operator=(SpecificBumpPtrAllocator &&RHS) {
456     Allocator = std::move(RHS.Allocator);
457     return *this;
458   }
459 
460   /// Call the destructor of each allocated object and deallocate all but the
461   /// current slab and reset the current pointer to the beginning of it, freeing
462   /// all memory allocated so far.
DestroyAll()463   void DestroyAll() {
464     auto DestroyElements = [](char *Begin, char *End) {
465       assert(Begin == (char *)alignAddr(Begin, alignof(T)));
466       for (char *Ptr = Begin; Ptr + sizeof(T) <= End; Ptr += sizeof(T))
467         reinterpret_cast<T *>(Ptr)->~T();
468     };
469 
470     for (auto I = Allocator.Slabs.begin(), E = Allocator.Slabs.end(); I != E;
471          ++I) {
472       size_t AllocatedSlabSize = BumpPtrAllocator::computeSlabSize(
473           std::distance(Allocator.Slabs.begin(), I));
474       char *Begin = (char *)alignAddr(*I, alignof(T));
475       char *End = *I == Allocator.Slabs.back() ? Allocator.CurPtr
476                                                : (char *)*I + AllocatedSlabSize;
477 
478       DestroyElements(Begin, End);
479     }
480 
481     for (auto &PtrAndSize : Allocator.CustomSizedSlabs) {
482       void *Ptr = PtrAndSize.first;
483       size_t Size = PtrAndSize.second;
484       DestroyElements((char *)alignAddr(Ptr, alignof(T)), (char *)Ptr + Size);
485     }
486 
487     Allocator.Reset();
488   }
489 
490   /// Allocate space for an array of objects without constructing them.
491   T *Allocate(size_t num = 1) { return Allocator.Allocate<T>(num); }
492 };
493 
494 } // end namespace llvm
495 
496 template <typename AllocatorT, size_t SlabSize, size_t SizeThreshold>
new(size_t Size,llvm::BumpPtrAllocatorImpl<AllocatorT,SlabSize,SizeThreshold> & Allocator)497 void *operator new(size_t Size,
498                    llvm::BumpPtrAllocatorImpl<AllocatorT, SlabSize,
499                                               SizeThreshold> &Allocator) {
500   struct S {
501     char c;
502     union {
503       double D;
504       long double LD;
505       long long L;
506       void *P;
507     } x;
508   };
509   return Allocator.Allocate(
510       Size, std::min((size_t)llvm::NextPowerOf2(Size), offsetof(S, x)));
511 }
512 
513 template <typename AllocatorT, size_t SlabSize, size_t SizeThreshold>
delete(void *,llvm::BumpPtrAllocatorImpl<AllocatorT,SlabSize,SizeThreshold> &)514 void operator delete(
515     void *, llvm::BumpPtrAllocatorImpl<AllocatorT, SlabSize, SizeThreshold> &) {
516 }
517 
518 #endif // LLVM_SUPPORT_ALLOCATOR_H
519