Allocator.h revision 221345
1201546Sdavidxu//===--- Allocator.h - Simple memory allocation abstraction -----*- C++ -*-===//
2201546Sdavidxu//
3201546Sdavidxu//                     The LLVM Compiler Infrastructure
4201546Sdavidxu//
5201546Sdavidxu// This file is distributed under the University of Illinois Open Source
6201546Sdavidxu// License. See LICENSE.TXT for details.
7201546Sdavidxu//
8201546Sdavidxu//===----------------------------------------------------------------------===//
9201546Sdavidxu//
10201546Sdavidxu// This file defines the MallocAllocator and BumpPtrAllocator interfaces.
11201546Sdavidxu//
12201546Sdavidxu//===----------------------------------------------------------------------===//
13201546Sdavidxu
14201546Sdavidxu#ifndef LLVM_SUPPORT_ALLOCATOR_H
15201546Sdavidxu#define LLVM_SUPPORT_ALLOCATOR_H
16201546Sdavidxu
17201546Sdavidxu#include "llvm/Support/AlignOf.h"
18201546Sdavidxu#include "llvm/Support/MathExtras.h"
19201546Sdavidxu#include "llvm/Support/DataTypes.h"
20201546Sdavidxu#include <algorithm>
21201546Sdavidxu#include <cassert>
22201546Sdavidxu#include <cstdlib>
23201546Sdavidxu#include <cstddef>
24201546Sdavidxu
25201546Sdavidxunamespace llvm {
26201546Sdavidxutemplate <typename T> struct ReferenceAdder { typedef T& result; };
27201546Sdavidxutemplate <typename T> struct ReferenceAdder<T&> { typedef T result; };
28201546Sdavidxu
29201546Sdavidxuclass MallocAllocator {
30201546Sdavidxupublic:
31201546Sdavidxu  MallocAllocator() {}
32201546Sdavidxu  ~MallocAllocator() {}
33201546Sdavidxu
34201546Sdavidxu  void Reset() {}
35201546Sdavidxu
36201546Sdavidxu  void *Allocate(size_t Size, size_t /*Alignment*/) { return malloc(Size); }
37201546Sdavidxu
38201546Sdavidxu  template <typename T>
39201546Sdavidxu  T *Allocate() { return static_cast<T*>(malloc(sizeof(T))); }
40201546Sdavidxu
41201546Sdavidxu  template <typename T>
42201546Sdavidxu  T *Allocate(size_t Num) {
43201546Sdavidxu    return static_cast<T*>(malloc(sizeof(T)*Num));
44201546Sdavidxu  }
45201546Sdavidxu
46201546Sdavidxu  void Deallocate(const void *Ptr) { free(const_cast<void*>(Ptr)); }
47201546Sdavidxu
48201546Sdavidxu  void PrintStats() const {}
49201546Sdavidxu};
50201546Sdavidxu
51201546Sdavidxu/// MemSlab - This structure lives at the beginning of every slab allocated by
52201546Sdavidxu/// the bump allocator.
53201546Sdavidxuclass MemSlab {
54201546Sdavidxupublic:
55201546Sdavidxu  size_t Size;
56201546Sdavidxu  MemSlab *NextPtr;
57201546Sdavidxu};
58201546Sdavidxu
59201546Sdavidxu/// SlabAllocator - This class can be used to parameterize the underlying
60201546Sdavidxu/// allocation strategy for the bump allocator.  In particular, this is used
61201546Sdavidxu/// by the JIT to allocate contiguous swathes of executable memory.  The
62201546Sdavidxu/// interface uses MemSlab's instead of void *'s so that the allocator
63201546Sdavidxu/// doesn't have to remember the size of the pointer it allocated.
64201546Sdavidxuclass SlabAllocator {
65public:
66  virtual ~SlabAllocator();
67  virtual MemSlab *Allocate(size_t Size) = 0;
68  virtual void Deallocate(MemSlab *Slab) = 0;
69};
70
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.
74class MallocSlabAllocator : public SlabAllocator {
75  /// Allocator - The underlying allocator that we forward to.
76  ///
77  MallocAllocator Allocator;
78
79public:
80  MallocSlabAllocator() : Allocator() { }
81  virtual ~MallocSlabAllocator();
82  virtual MemSlab *Allocate(size_t Size);
83  virtual void Deallocate(MemSlab *Slab);
84};
85
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.
90class BumpPtrAllocator {
91  BumpPtrAllocator(const BumpPtrAllocator &); // do not implement
92  void operator=(const BumpPtrAllocator &);   // do not implement
93
94  /// SlabSize - Allocate data into slabs of this size unless we get an
95  /// allocation above SizeThreshold.
96  size_t SlabSize;
97
98  /// SizeThreshold - For any allocation larger than this threshold, we should
99  /// allocate a separate slab.
100  size_t SizeThreshold;
101
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;
106
107  /// CurSlab - The slab that we are currently allocating into.
108  ///
109  MemSlab *CurSlab;
110
111  /// CurPtr - The current pointer into the current slab.  This points to the
112  /// next free byte in the slab.
113  char *CurPtr;
114
115  /// End - The end of the current slab.
116  ///
117  char *End;
118
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;
122
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);
127
128  /// StartNewSlab - Allocate a new slab and move the bump pointers over into
129  /// the new slab.  Modifies CurPtr and End.
130  void StartNewSlab();
131
132  /// DeallocateSlabs - Deallocate all memory slabs after and including this
133  /// one.
134  void DeallocateSlabs(MemSlab *Slab);
135
136  static MallocSlabAllocator DefaultSlabAllocator;
137
138  template<typename T> friend class SpecificBumpPtrAllocator;
139public:
140  BumpPtrAllocator(size_t size = 4096, size_t threshold = 4096,
141                   SlabAllocator &allocator = DefaultSlabAllocator);
142  ~BumpPtrAllocator();
143
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.
146  void Reset();
147
148  /// Allocate - Allocate space at the specified alignment.
149  ///
150  void *Allocate(size_t Size, size_t Alignment);
151
152  /// Allocate space, but do not construct, one object.
153  ///
154  template <typename T>
155  T *Allocate() {
156    return static_cast<T*>(Allocate(sizeof(T),AlignOf<T>::Alignment));
157  }
158
159  /// Allocate space for an array of objects.  This does not construct the
160  /// objects though.
161  template <typename T>
162  T *Allocate(size_t Num) {
163    return static_cast<T*>(Allocate(Num * sizeof(T), AlignOf<T>::Alignment));
164  }
165
166  /// Allocate space for a specific count of elements and with a specified
167  /// alignment.
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));
173  }
174
175  void Deallocate(const void * /*Ptr*/) {}
176
177  unsigned GetNumSlabs() const;
178
179  void PrintStats() const;
180
181  /// Compute the total physical memory allocated by this allocator.
182  size_t getTotalMemory() const;
183};
184
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.
188template <typename T>
189class SpecificBumpPtrAllocator {
190  BumpPtrAllocator Allocator;
191public:
192  SpecificBumpPtrAllocator(size_t size = 4096, size_t threshold = 4096,
193              SlabAllocator &allocator = BumpPtrAllocator::DefaultSlabAllocator)
194    : Allocator(size, threshold, allocator) {}
195
196  ~SpecificBumpPtrAllocator() {
197    DestroyAll();
198  }
199
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.
203  void DestroyAll() {
204    MemSlab *Slab = Allocator.CurSlab;
205    while (Slab) {
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();
212      }
213      Slab = Slab->NextPtr;
214    }
215    Allocator.Reset();
216  }
217
218  /// Allocate space for a specific count of elements.
219  T *Allocate(size_t num = 1) {
220    return Allocator.Allocate<T>(num);
221  }
222};
223
224}  // end namespace llvm
225
226inline void *operator new(size_t Size, llvm::BumpPtrAllocator &Allocator) {
227  struct S {
228    char c;
229    union {
230      double D;
231      long double LD;
232      long long L;
233      void *P;
234    } x;
235  };
236  return Allocator.Allocate(Size, std::min((size_t)llvm::NextPowerOf2(Size),
237                                           offsetof(S, x)));
238}
239
240inline void operator delete(void *, llvm::BumpPtrAllocator &) {}
241
242#endif // LLVM_SUPPORT_ALLOCATOR_H
243