1 //===--- Allocator.h - Simple memory allocation abstraction -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the MallocAllocator and BumpPtrAllocator interfaces.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_SUPPORT_ALLOCATOR_H
15 #define LLVM_SUPPORT_ALLOCATOR_H
17 #include "llvm/Support/AlignOf.h"
18 #include "llvm/Support/MathExtras.h"
19 #include "llvm/Support/DataTypes.h"
26 template <typename T> struct ReferenceAdder { typedef T& result; };
27 template <typename T> struct ReferenceAdder<T&> { typedef T result; };
29 class MallocAllocator {
36 void *Allocate(size_t Size, size_t /*Alignment*/) { return malloc(Size); }
39 T *Allocate() { return static_cast<T*>(malloc(sizeof(T))); }
42 T *Allocate(size_t Num) {
43 return static_cast<T*>(malloc(sizeof(T)*Num));
46 void Deallocate(const void *Ptr) { free(const_cast<void*>(Ptr)); }
48 void PrintStats() const {}
51 /// MemSlab - This structure lives at the beginning of every slab allocated by
52 /// the bump allocator.
59 /// SlabAllocator - This class can be used to parameterize the underlying
60 /// allocation strategy for the bump allocator. In particular, this is used
61 /// by the JIT to allocate contiguous swathes of executable memory. The
62 /// interface uses MemSlab's instead of void *'s so that the allocator
63 /// doesn't have to remember the size of the pointer it allocated.
66 virtual ~SlabAllocator();
67 virtual MemSlab *Allocate(size_t Size) = 0;
68 virtual void Deallocate(MemSlab *Slab) = 0;
71 /// MallocSlabAllocator - The default slab allocator for the bump allocator
72 /// is an adapter class for MallocAllocator that just forwards the method
73 /// calls and translates the arguments.
74 class MallocSlabAllocator : public SlabAllocator {
75 /// Allocator - The underlying allocator that we forward to.
77 MallocAllocator Allocator;
80 MallocSlabAllocator() : Allocator() { }
81 virtual ~MallocSlabAllocator();
82 virtual MemSlab *Allocate(size_t Size);
83 virtual void Deallocate(MemSlab *Slab);
86 /// BumpPtrAllocator - This allocator is useful for containers that need
87 /// very simple memory allocation strategies. In particular, this just keeps
88 /// allocating memory, and never deletes it until the entire block is dead. This
89 /// makes allocation speedy, but must only be used when the trade-off is ok.
90 class BumpPtrAllocator {
91 BumpPtrAllocator(const BumpPtrAllocator &); // do not implement
92 void operator=(const BumpPtrAllocator &); // do not implement
94 /// SlabSize - Allocate data into slabs of this size unless we get an
95 /// allocation above SizeThreshold.
98 /// SizeThreshold - For any allocation larger than this threshold, we should
99 /// allocate a separate slab.
100 size_t SizeThreshold;
102 /// Allocator - The underlying allocator we use to get slabs of memory. This
103 /// defaults to MallocSlabAllocator, which wraps malloc, but it could be
104 /// changed to use a custom allocator.
105 SlabAllocator &Allocator;
107 /// CurSlab - The slab that we are currently allocating into.
111 /// CurPtr - The current pointer into the current slab. This points to the
112 /// next free byte in the slab.
115 /// End - The end of the current slab.
119 /// BytesAllocated - This field tracks how many bytes we've allocated, so
120 /// that we can compute how much space was wasted.
121 size_t BytesAllocated;
123 /// AlignPtr - Align Ptr to Alignment bytes, rounding up. Alignment should
124 /// be a power of two. This method rounds up, so AlignPtr(7, 4) == 8 and
125 /// AlignPtr(8, 4) == 8.
126 static char *AlignPtr(char *Ptr, size_t Alignment);
128 /// StartNewSlab - Allocate a new slab and move the bump pointers over into
129 /// the new slab. Modifies CurPtr and End.
132 /// DeallocateSlabs - Deallocate all memory slabs after and including this
134 void DeallocateSlabs(MemSlab *Slab);
136 static MallocSlabAllocator DefaultSlabAllocator;
138 template<typename T> friend class SpecificBumpPtrAllocator;
140 BumpPtrAllocator(size_t size = 4096, size_t threshold = 4096,
141 SlabAllocator &allocator = DefaultSlabAllocator);
144 /// Reset - Deallocate all but the current slab and reset the current pointer
145 /// to the beginning of it, freeing all memory allocated so far.
148 /// Allocate - Allocate space at the specified alignment.
150 void *Allocate(size_t Size, size_t Alignment);
152 /// Allocate space, but do not construct, one object.
154 template <typename T>
156 return static_cast<T*>(Allocate(sizeof(T),AlignOf<T>::Alignment));
159 /// Allocate space for an array of objects. This does not construct the
161 template <typename T>
162 T *Allocate(size_t Num) {
163 return static_cast<T*>(Allocate(Num * sizeof(T), AlignOf<T>::Alignment));
166 /// Allocate space for a specific count of elements and with a specified
168 template <typename T>
169 T *Allocate(size_t Num, size_t Alignment) {
170 // Round EltSize up to the specified alignment.
171 size_t EltSize = (sizeof(T)+Alignment-1)&(-Alignment);
172 return static_cast<T*>(Allocate(Num * EltSize, Alignment));
175 void Deallocate(const void * /*Ptr*/) {}
177 unsigned GetNumSlabs() const;
179 void PrintStats() const;
181 /// Compute the total physical memory allocated by this allocator.
182 size_t getTotalMemory() const;
185 /// SpecificBumpPtrAllocator - Same as BumpPtrAllocator but allows only
186 /// elements of one type to be allocated. This allows calling the destructor
187 /// in DestroyAll() and when the allocator is destroyed.
188 template <typename T>
189 class SpecificBumpPtrAllocator {
190 BumpPtrAllocator Allocator;
192 SpecificBumpPtrAllocator(size_t size = 4096, size_t threshold = 4096,
193 SlabAllocator &allocator = BumpPtrAllocator::DefaultSlabAllocator)
194 : Allocator(size, threshold, allocator) {}
196 ~SpecificBumpPtrAllocator() {
200 /// Call the destructor of each allocated object and deallocate all but the
201 /// current slab and reset the current pointer to the beginning of it, freeing
202 /// all memory allocated so far.
204 MemSlab *Slab = Allocator.CurSlab;
206 char *End = Slab == Allocator.CurSlab ? Allocator.CurPtr :
207 (char *)Slab + Slab->Size;
208 for (char *Ptr = (char*)(Slab+1); Ptr < End; Ptr += sizeof(T)) {
209 Ptr = Allocator.AlignPtr(Ptr, alignOf<T>());
210 if (Ptr + sizeof(T) <= End)
211 reinterpret_cast<T*>(Ptr)->~T();
213 Slab = Slab->NextPtr;
218 /// Allocate space for a specific count of elements.
219 T *Allocate(size_t num = 1) {
220 return Allocator.Allocate<T>(num);
224 } // end namespace llvm
226 inline void *operator new(size_t Size, llvm::BumpPtrAllocator &Allocator) {
236 return Allocator.Allocate(Size, std::min((size_t)llvm::NextPowerOf2(Size),
240 inline void operator delete(void *, llvm::BumpPtrAllocator &) {}
242 #endif // LLVM_SUPPORT_ALLOCATOR_H