1 //===- Allocator.h - Simple memory allocation abstraction -------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 /// \file
9 ///
10 /// This file defines the MallocAllocator and BumpPtrAllocator interfaces. Both
11 /// of these conform to an LLVM "Allocator" concept which consists of an
12 /// Allocate method accepting a size and alignment, and a Deallocate accepting
13 /// a pointer and size. Further, the LLVM "Allocator" concept has overloads of
14 /// Allocate and Deallocate for setting size and alignment based on the final
15 /// type. These overloads are typically provided by a base class template \c
16 /// AllocatorBase.
17 ///
18 //===----------------------------------------------------------------------===//
19 
20 #ifndef LLVM_SUPPORT_ALLOCATOR_H
21 #define LLVM_SUPPORT_ALLOCATOR_H
22 
23 #include "llvm/ADT/Optional.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/Support/Alignment.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.
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.
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:
96   void Reset() {}
97 
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 
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 
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>
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.
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 
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.
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 *
215   Allocate(size_t Size, Align Alignment) {
216     // Keep track of how many bytes we've allocated.
217     BytesAllocated += Size;
218 
219     size_t Adjustment = offsetToAlignedAddr(CurPtr, Alignment);
220     assert(Adjustment + Size >= Size && "Adjustment + Size must not overflow");
221 
222     size_t SizeToAllocate = Size;
223 #if LLVM_ADDRESS_SANITIZER_BUILD
224     // Add trailing bytes as a "red zone" under ASan.
225     SizeToAllocate += RedZoneSize;
226 #endif
227 
228     // Check if we have enough space.
229     if (Adjustment + SizeToAllocate <= size_t(End - CurPtr)) {
230       char *AlignedPtr = CurPtr + Adjustment;
231       CurPtr = AlignedPtr + SizeToAllocate;
232       // Update the allocation point of this memory block in MemorySanitizer.
233       // Without this, MemorySanitizer messages for values originated from here
234       // will point to the allocation of the entire slab.
235       __msan_allocated_memory(AlignedPtr, Size);
236       // Similarly, tell ASan about this space.
237       __asan_unpoison_memory_region(AlignedPtr, Size);
238       return AlignedPtr;
239     }
240 
241     // If Size is really big, allocate a separate slab for it.
242     size_t PaddedSize = SizeToAllocate + Alignment.value() - 1;
243     if (PaddedSize > SizeThreshold) {
244       void *NewSlab = Allocator.Allocate(PaddedSize, 0);
245       // We own the new slab and don't want anyone reading anyting other than
246       // pieces returned from this method.  So poison the whole slab.
247       __asan_poison_memory_region(NewSlab, PaddedSize);
248       CustomSizedSlabs.push_back(std::make_pair(NewSlab, PaddedSize));
249 
250       uintptr_t AlignedAddr = alignAddr(NewSlab, Alignment);
251       assert(AlignedAddr + Size <= (uintptr_t)NewSlab + PaddedSize);
252       char *AlignedPtr = (char*)AlignedAddr;
253       __msan_allocated_memory(AlignedPtr, Size);
254       __asan_unpoison_memory_region(AlignedPtr, Size);
255       return AlignedPtr;
256     }
257 
258     // Otherwise, start a new slab and try again.
259     StartNewSlab();
260     uintptr_t AlignedAddr = alignAddr(CurPtr, Alignment);
261     assert(AlignedAddr + SizeToAllocate <= (uintptr_t)End &&
262            "Unable to allocate memory!");
263     char *AlignedPtr = (char*)AlignedAddr;
264     CurPtr = AlignedPtr + SizeToAllocate;
265     __msan_allocated_memory(AlignedPtr, Size);
266     __asan_unpoison_memory_region(AlignedPtr, Size);
267     return AlignedPtr;
268   }
269 
270   inline LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void *
271   Allocate(size_t Size, size_t Alignment) {
272     assert(Alignment > 0 && "0-byte alignment is not allowed. Use 1 instead.");
273     return Allocate(Size, Align(Alignment));
274   }
275 
276   // Pull in base class overloads.
277   using AllocatorBase<BumpPtrAllocatorImpl>::Allocate;
278 
279   // Bump pointer allocators are expected to never free their storage; and
280   // clients expect pointers to remain valid for non-dereferencing uses even
281   // after deallocation.
282   void Deallocate(const void *Ptr, size_t Size) {
283     __asan_poison_memory_region(Ptr, Size);
284   }
285 
286   // Pull in base class overloads.
287   using AllocatorBase<BumpPtrAllocatorImpl>::Deallocate;
288 
289   size_t GetNumSlabs() const { return Slabs.size() + CustomSizedSlabs.size(); }
290 
291   /// \return An index uniquely and reproducibly identifying
292   /// an input pointer \p Ptr in the given allocator.
293   /// The returned value is negative iff the object is inside a custom-size
294   /// slab.
295   /// Returns an empty optional if the pointer is not found in the allocator.
296   llvm::Optional<int64_t> identifyObject(const void *Ptr) {
297     const char *P = static_cast<const char *>(Ptr);
298     int64_t InSlabIdx = 0;
299     for (size_t Idx = 0, E = Slabs.size(); Idx < E; Idx++) {
300       const char *S = static_cast<const char *>(Slabs[Idx]);
301       if (P >= S && P < S + computeSlabSize(Idx))
302         return InSlabIdx + static_cast<int64_t>(P - S);
303       InSlabIdx += static_cast<int64_t>(computeSlabSize(Idx));
304     }
305 
306     // Use negative index to denote custom sized slabs.
307     int64_t InCustomSizedSlabIdx = -1;
308     for (size_t Idx = 0, E = CustomSizedSlabs.size(); Idx < E; Idx++) {
309       const char *S = static_cast<const char *>(CustomSizedSlabs[Idx].first);
310       size_t Size = CustomSizedSlabs[Idx].second;
311       if (P >= S && P < S + Size)
312         return InCustomSizedSlabIdx - static_cast<int64_t>(P - S);
313       InCustomSizedSlabIdx -= static_cast<int64_t>(Size);
314     }
315     return None;
316   }
317 
318   /// A wrapper around identifyObject that additionally asserts that
319   /// the object is indeed within the allocator.
320   /// \return An index uniquely and reproducibly identifying
321   /// an input pointer \p Ptr in the given allocator.
322   int64_t identifyKnownObject(const void *Ptr) {
323     Optional<int64_t> Out = identifyObject(Ptr);
324     assert(Out && "Wrong allocator used");
325     return *Out;
326   }
327 
328   /// A wrapper around identifyKnownObject. Accepts type information
329   /// about the object and produces a smaller identifier by relying on
330   /// the alignment information. Note that sub-classes may have different
331   /// alignment, so the most base class should be passed as template parameter
332   /// in order to obtain correct results. For that reason automatic template
333   /// parameter deduction is disabled.
334   /// \return An index uniquely and reproducibly identifying
335   /// an input pointer \p Ptr in the given allocator. This identifier is
336   /// different from the ones produced by identifyObject and
337   /// identifyAlignedObject.
338   template <typename T>
339   int64_t identifyKnownAlignedObject(const void *Ptr) {
340     int64_t Out = identifyKnownObject(Ptr);
341     assert(Out % alignof(T) == 0 && "Wrong alignment information");
342     return Out / alignof(T);
343   }
344 
345   size_t getTotalMemory() const {
346     size_t TotalMemory = 0;
347     for (auto I = Slabs.begin(), E = Slabs.end(); I != E; ++I)
348       TotalMemory += computeSlabSize(std::distance(Slabs.begin(), I));
349     for (auto &PtrAndSize : CustomSizedSlabs)
350       TotalMemory += PtrAndSize.second;
351     return TotalMemory;
352   }
353 
354   size_t getBytesAllocated() const { return BytesAllocated; }
355 
356   void setRedZoneSize(size_t NewSize) {
357     RedZoneSize = NewSize;
358   }
359 
360   void PrintStats() const {
361     detail::printBumpPtrAllocatorStats(Slabs.size(), BytesAllocated,
362                                        getTotalMemory());
363   }
364 
365 private:
366   /// The current pointer into the current slab.
367   ///
368   /// This points to the next free byte in the slab.
369   char *CurPtr = nullptr;
370 
371   /// The end of the current slab.
372   char *End = nullptr;
373 
374   /// The slabs allocated so far.
375   SmallVector<void *, 4> Slabs;
376 
377   /// Custom-sized slabs allocated for too-large allocation requests.
378   SmallVector<std::pair<void *, size_t>, 0> CustomSizedSlabs;
379 
380   /// How many bytes we've allocated.
381   ///
382   /// Used so that we can compute how much space was wasted.
383   size_t BytesAllocated = 0;
384 
385   /// The number of bytes to put between allocations when running under
386   /// a sanitizer.
387   size_t RedZoneSize = 1;
388 
389   /// The allocator instance we use to get slabs of memory.
390   AllocatorT Allocator;
391 
392   static size_t computeSlabSize(unsigned SlabIdx) {
393     // Scale the actual allocated slab size based on the number of slabs
394     // allocated. Every 128 slabs allocated, we double the allocated size to
395     // reduce allocation frequency, but saturate at multiplying the slab size by
396     // 2^30.
397     return SlabSize * ((size_t)1 << std::min<size_t>(30, SlabIdx / 128));
398   }
399 
400   /// Allocate a new slab and move the bump pointers over into the new
401   /// slab, modifying CurPtr and End.
402   void StartNewSlab() {
403     size_t AllocatedSlabSize = computeSlabSize(Slabs.size());
404 
405     void *NewSlab = Allocator.Allocate(AllocatedSlabSize, 0);
406     // We own the new slab and don't want anyone reading anything other than
407     // pieces returned from this method.  So poison the whole slab.
408     __asan_poison_memory_region(NewSlab, AllocatedSlabSize);
409 
410     Slabs.push_back(NewSlab);
411     CurPtr = (char *)(NewSlab);
412     End = ((char *)NewSlab) + AllocatedSlabSize;
413   }
414 
415   /// Deallocate a sequence of slabs.
416   void DeallocateSlabs(SmallVectorImpl<void *>::iterator I,
417                        SmallVectorImpl<void *>::iterator E) {
418     for (; I != E; ++I) {
419       size_t AllocatedSlabSize =
420           computeSlabSize(std::distance(Slabs.begin(), I));
421       Allocator.Deallocate(*I, AllocatedSlabSize);
422     }
423   }
424 
425   /// Deallocate all memory for custom sized slabs.
426   void DeallocateCustomSizedSlabs() {
427     for (auto &PtrAndSize : CustomSizedSlabs) {
428       void *Ptr = PtrAndSize.first;
429       size_t Size = PtrAndSize.second;
430       Allocator.Deallocate(Ptr, Size);
431     }
432   }
433 
434   template <typename T> friend class SpecificBumpPtrAllocator;
435 };
436 
437 /// The standard BumpPtrAllocator which just uses the default template
438 /// parameters.
439 typedef BumpPtrAllocatorImpl<> BumpPtrAllocator;
440 
441 /// A BumpPtrAllocator that allows only elements of a specific type to be
442 /// allocated.
443 ///
444 /// This allows calling the destructor in DestroyAll() and when the allocator is
445 /// destroyed.
446 template <typename T> class SpecificBumpPtrAllocator {
447   BumpPtrAllocator Allocator;
448 
449 public:
450   SpecificBumpPtrAllocator() {
451     // Because SpecificBumpPtrAllocator walks the memory to call destructors,
452     // it can't have red zones between allocations.
453     Allocator.setRedZoneSize(0);
454   }
455   SpecificBumpPtrAllocator(SpecificBumpPtrAllocator &&Old)
456       : Allocator(std::move(Old.Allocator)) {}
457   ~SpecificBumpPtrAllocator() { DestroyAll(); }
458 
459   SpecificBumpPtrAllocator &operator=(SpecificBumpPtrAllocator &&RHS) {
460     Allocator = std::move(RHS.Allocator);
461     return *this;
462   }
463 
464   /// Call the destructor of each allocated object and deallocate all but the
465   /// current slab and reset the current pointer to the beginning of it, freeing
466   /// all memory allocated so far.
467   void DestroyAll() {
468     auto DestroyElements = [](char *Begin, char *End) {
469       assert(Begin == (char *)alignAddr(Begin, Align::Of<T>()));
470       for (char *Ptr = Begin; Ptr + sizeof(T) <= End; Ptr += sizeof(T))
471         reinterpret_cast<T *>(Ptr)->~T();
472     };
473 
474     for (auto I = Allocator.Slabs.begin(), E = Allocator.Slabs.end(); I != E;
475          ++I) {
476       size_t AllocatedSlabSize = BumpPtrAllocator::computeSlabSize(
477           std::distance(Allocator.Slabs.begin(), I));
478       char *Begin = (char *)alignAddr(*I, Align::Of<T>());
479       char *End = *I == Allocator.Slabs.back() ? Allocator.CurPtr
480                                                : (char *)*I + AllocatedSlabSize;
481 
482       DestroyElements(Begin, End);
483     }
484 
485     for (auto &PtrAndSize : Allocator.CustomSizedSlabs) {
486       void *Ptr = PtrAndSize.first;
487       size_t Size = PtrAndSize.second;
488       DestroyElements((char *)alignAddr(Ptr, Align::Of<T>()),
489                       (char *)Ptr + Size);
490     }
491 
492     Allocator.Reset();
493   }
494 
495   /// Allocate space for an array of objects without constructing them.
496   T *Allocate(size_t num = 1) { return Allocator.Allocate<T>(num); }
497 };
498 
499 } // end namespace llvm
500 
501 template <typename AllocatorT, size_t SlabSize, size_t SizeThreshold>
502 void *operator new(size_t Size,
503                    llvm::BumpPtrAllocatorImpl<AllocatorT, SlabSize,
504                                               SizeThreshold> &Allocator) {
505   struct S {
506     char c;
507     union {
508       double D;
509       long double LD;
510       long long L;
511       void *P;
512     } x;
513   };
514   return Allocator.Allocate(
515       Size, std::min((size_t)llvm::NextPowerOf2(Size), offsetof(S, x)));
516 }
517 
518 template <typename AllocatorT, size_t SlabSize, size_t SizeThreshold>
519 void operator delete(
520     void *, llvm::BumpPtrAllocatorImpl<AllocatorT, SlabSize, SizeThreshold> &) {
521 }
522 
523 #endif // LLVM_SUPPORT_ALLOCATOR_H
524