b1f59dc05e7bb892cb2c2c614efc2140c10170dc
[oota-llvm.git] / include / llvm / Support / Allocator.h
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 //
10 // This file defines the MallocAllocator and BumpPtrAllocator interfaces.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_SUPPORT_ALLOCATOR_H
15 #define LLVM_SUPPORT_ALLOCATOR_H
16
17 #include "llvm/Support/AlignOf.h"
18 #include "llvm/Support/MathExtras.h"
19 #include "llvm/System/DataTypes.h"
20 #include <algorithm>
21 #include <cassert>
22 #include <cstdlib>
23 #include <cstddef>
24
25 namespace llvm {
26
27 class MallocAllocator {
28 public:
29   MallocAllocator() {}
30   ~MallocAllocator() {}
31
32   void Reset() {}
33
34   void *Allocate(size_t Size, size_t /*Alignment*/) { return malloc(Size); }
35
36   template <typename T>
37   T *Allocate() { return static_cast<T*>(malloc(sizeof(T))); }
38
39   template <typename T>
40   T *Allocate(size_t Num) {
41     return static_cast<T*>(malloc(sizeof(T)*Num));
42   }
43
44   void Deallocate(const void *Ptr) { free(const_cast<void*>(Ptr)); }
45
46   void PrintStats() const {}
47 };
48
49 /// MemSlab - This structure lives at the beginning of every slab allocated by
50 /// the bump allocator.
51 class MemSlab {
52 public:
53   size_t Size;
54   MemSlab *NextPtr;
55 };
56
57 /// SlabAllocator - This class can be used to parameterize the underlying
58 /// allocation strategy for the bump allocator.  In particular, this is used
59 /// by the JIT to allocate contiguous swathes of executable memory.  The
60 /// interface uses MemSlab's instead of void *'s so that the allocator
61 /// doesn't have to remember the size of the pointer it allocated.
62 class SlabAllocator {
63 public:
64   virtual ~SlabAllocator();
65   virtual MemSlab *Allocate(size_t Size) = 0;
66   virtual void Deallocate(MemSlab *Slab) = 0;
67 };
68
69 /// MallocSlabAllocator - The default slab allocator for the bump allocator
70 /// is an adapter class for MallocAllocator that just forwards the method
71 /// calls and translates the arguments.
72 class MallocSlabAllocator : public SlabAllocator {
73   /// Allocator - The underlying allocator that we forward to.
74   ///
75   MallocAllocator Allocator;
76
77 public:
78   MallocSlabAllocator() : Allocator() { }
79   virtual ~MallocSlabAllocator();
80   virtual MemSlab *Allocate(size_t Size);
81   virtual void Deallocate(MemSlab *Slab);
82 };
83
84 /// BumpPtrAllocator - This allocator is useful for containers that need
85 /// very simple memory allocation strategies.  In particular, this just keeps
86 /// allocating memory, and never deletes it until the entire block is dead. This
87 /// makes allocation speedy, but must only be used when the trade-off is ok.
88 class BumpPtrAllocator {
89   BumpPtrAllocator(const BumpPtrAllocator &); // do not implement
90   void operator=(const BumpPtrAllocator &);   // do not implement
91
92   /// SlabSize - Allocate data into slabs of this size unless we get an
93   /// allocation above SizeThreshold.
94   size_t SlabSize;
95
96   /// SizeThreshold - For any allocation larger than this threshold, we should
97   /// allocate a separate slab.
98   size_t SizeThreshold;
99
100   /// Allocator - The underlying allocator we use to get slabs of memory.  This
101   /// defaults to MallocSlabAllocator, which wraps malloc, but it could be
102   /// changed to use a custom allocator.
103   SlabAllocator &Allocator;
104
105   /// CurSlab - The slab that we are currently allocating into.
106   ///
107   MemSlab *CurSlab;
108
109   /// CurPtr - The current pointer into the current slab.  This points to the
110   /// next free byte in the slab.
111   char *CurPtr;
112
113   /// End - The end of the current slab.
114   ///
115   char *End;
116
117   /// BytesAllocated - This field tracks how many bytes we've allocated, so
118   /// that we can compute how much space was wasted.
119   size_t BytesAllocated;
120
121   /// AlignPtr - Align Ptr to Alignment bytes, rounding up.  Alignment should
122   /// be a power of two.  This method rounds up, so AlignPtr(7, 4) == 8 and
123   /// AlignPtr(8, 4) == 8.
124   static char *AlignPtr(char *Ptr, size_t Alignment);
125
126   /// StartNewSlab - Allocate a new slab and move the bump pointers over into
127   /// the new slab.  Modifies CurPtr and End.
128   void StartNewSlab();
129
130   /// DeallocateSlabs - Deallocate all memory slabs after and including this
131   /// one.
132   void DeallocateSlabs(MemSlab *Slab);
133
134   static MallocSlabAllocator DefaultSlabAllocator;
135
136 public:
137   BumpPtrAllocator(size_t size = 4096, size_t threshold = 4096,
138                    SlabAllocator &allocator = DefaultSlabAllocator);
139   ~BumpPtrAllocator();
140
141   /// Reset - Deallocate all but the current slab and reset the current pointer
142   /// to the beginning of it, freeing all memory allocated so far.
143   void Reset();
144
145   /// Allocate - Allocate space at the specified alignment.
146   ///
147   void *Allocate(size_t Size, size_t Alignment);
148
149   /// Allocate space, but do not construct, one object.
150   ///
151   template <typename T>
152   T *Allocate() {
153     return static_cast<T*>(Allocate(sizeof(T),AlignOf<T>::Alignment));
154   }
155
156   /// Allocate space for an array of objects.  This does not construct the
157   /// objects though.
158   template <typename T>
159   T *Allocate(size_t Num) {
160     return static_cast<T*>(Allocate(Num * sizeof(T), AlignOf<T>::Alignment));
161   }
162
163   /// Allocate space for a specific count of elements and with a specified
164   /// alignment.
165   template <typename T>
166   T *Allocate(size_t Num, size_t Alignment) {
167     // Round EltSize up to the specified alignment.
168     size_t EltSize = (sizeof(T)+Alignment-1)&(-Alignment);
169     return static_cast<T*>(Allocate(Num * EltSize, Alignment));
170   }
171
172   void Deallocate(const void * /*Ptr*/) {}
173
174   unsigned GetNumSlabs() const;
175
176   void PrintStats() const;
177 };
178
179 }  // end namespace llvm
180
181 inline void *operator new(size_t Size, llvm::BumpPtrAllocator &Allocator) {
182   struct S {
183     char c;
184 #ifdef __GNUC__
185     char x __attribute__((aligned));
186 #else
187     union {
188       double D;
189       long double LD;
190       long long L;
191       void *P;
192     } x;
193 #endif
194   };
195   return Allocator.Allocate(Size, std::min((size_t)llvm::NextPowerOf2(Size),
196                                            offsetof(S, x)));
197 }
198
199 #endif // LLVM_SUPPORT_ALLOCATOR_H