LLVM API Documentation
00001 //===--- Allocator.h - Simple memory allocation abstraction -----*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines the MallocAllocator and BumpPtrAllocator interfaces. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef LLVM_SUPPORT_ALLOCATOR_H 00015 #define LLVM_SUPPORT_ALLOCATOR_H 00016 00017 #include "llvm/Support/AlignOf.h" 00018 #include "llvm/Support/DataTypes.h" 00019 #include <cassert> 00020 #include <cstdlib> 00021 00022 namespace llvm { 00023 00024 class MallocAllocator { 00025 public: 00026 MallocAllocator() {} 00027 ~MallocAllocator() {} 00028 00029 void Reset() {} 00030 00031 void *Allocate(size_t Size, size_t /*Alignment*/) { return malloc(Size); } 00032 00033 template <typename T> 00034 T *Allocate() { return static_cast<T*>(malloc(sizeof(T))); } 00035 00036 template <typename T> 00037 T *Allocate(size_t Num) { 00038 return static_cast<T*>(malloc(sizeof(T)*Num)); 00039 } 00040 00041 void Deallocate(const void *Ptr) { free(const_cast<void*>(Ptr)); } 00042 00043 void PrintStats() const {} 00044 }; 00045 00046 /// MemSlab - This structure lives at the beginning of every slab allocated by 00047 /// the bump allocator. 00048 class MemSlab { 00049 public: 00050 size_t Size; 00051 MemSlab *NextPtr; 00052 }; 00053 00054 /// SlabAllocator - This class can be used to parameterize the underlying 00055 /// allocation strategy for the bump allocator. In particular, this is used 00056 /// by the JIT to allocate contiguous swathes of executable memory. The 00057 /// interface uses MemSlab's instead of void *'s so that the allocator 00058 /// doesn't have to remember the size of the pointer it allocated. 00059 class SlabAllocator { 00060 public: 00061 virtual ~SlabAllocator(); 00062 virtual MemSlab *Allocate(size_t Size) = 0; 00063 virtual void Deallocate(MemSlab *Slab) = 0; 00064 }; 00065 00066 /// MallocSlabAllocator - The default slab allocator for the bump allocator 00067 /// is an adapter class for MallocAllocator that just forwards the method 00068 /// calls and translates the arguments. 00069 class MallocSlabAllocator : public SlabAllocator { 00070 /// Allocator - The underlying allocator that we forward to. 00071 /// 00072 MallocAllocator Allocator; 00073 00074 public: 00075 MallocSlabAllocator() : Allocator() { } 00076 virtual ~MallocSlabAllocator(); 00077 virtual MemSlab *Allocate(size_t Size); 00078 virtual void Deallocate(MemSlab *Slab); 00079 }; 00080 00081 /// BumpPtrAllocator - This allocator is useful for containers that need 00082 /// very simple memory allocation strategies. In particular, this just keeps 00083 /// allocating memory, and never deletes it until the entire block is dead. This 00084 /// makes allocation speedy, but must only be used when the trade-off is ok. 00085 class BumpPtrAllocator { 00086 BumpPtrAllocator(const BumpPtrAllocator &); // do not implement 00087 void operator=(const BumpPtrAllocator &); // do not implement 00088 00089 /// SlabSize - Allocate data into slabs of this size unless we get an 00090 /// allocation above SizeThreshold. 00091 size_t SlabSize; 00092 00093 /// SizeThreshold - For any allocation larger than this threshold, we should 00094 /// allocate a separate slab. 00095 size_t SizeThreshold; 00096 00097 /// Allocator - The underlying allocator we use to get slabs of memory. This 00098 /// defaults to MallocSlabAllocator, which wraps malloc, but it could be 00099 /// changed to use a custom allocator. 00100 SlabAllocator &Allocator; 00101 00102 /// CurSlab - The slab that we are currently allocating into. 00103 /// 00104 MemSlab *CurSlab; 00105 00106 /// CurPtr - The current pointer into the current slab. This points to the 00107 /// next free byte in the slab. 00108 char *CurPtr; 00109 00110 /// End - The end of the current slab. 00111 /// 00112 char *End; 00113 00114 /// BytesAllocated - This field tracks how many bytes we've allocated, so 00115 /// that we can compute how much space was wasted. 00116 size_t BytesAllocated; 00117 00118 /// AlignPtr - Align Ptr to Alignment bytes, rounding up. Alignment should 00119 /// be a power of two. This method rounds up, so AlignPtr(7, 4) == 8 and 00120 /// AlignPtr(8, 4) == 8. 00121 static char *AlignPtr(char *Ptr, size_t Alignment); 00122 00123 /// StartNewSlab - Allocate a new slab and move the bump pointers over into 00124 /// the new slab. Modifies CurPtr and End. 00125 void StartNewSlab(); 00126 00127 /// DeallocateSlabs - Deallocate all memory slabs after and including this 00128 /// one. 00129 void DeallocateSlabs(MemSlab *Slab); 00130 00131 static MallocSlabAllocator DefaultSlabAllocator; 00132 00133 public: 00134 BumpPtrAllocator(size_t size = 4096, size_t threshold = 4096, 00135 SlabAllocator &allocator = DefaultSlabAllocator); 00136 ~BumpPtrAllocator(); 00137 00138 /// Reset - Deallocate all but the current slab and reset the current pointer 00139 /// to the beginning of it, freeing all memory allocated so far. 00140 void Reset(); 00141 00142 /// Allocate - Allocate space at the specified alignment. 00143 /// 00144 void *Allocate(size_t Size, size_t Alignment); 00145 00146 /// Allocate space, but do not construct, one object. 00147 /// 00148 template <typename T> 00149 T *Allocate() { 00150 return static_cast<T*>(Allocate(sizeof(T),AlignOf<T>::Alignment)); 00151 } 00152 00153 /// Allocate space for an array of objects. This does not construct the 00154 /// objects though. 00155 template <typename T> 00156 T *Allocate(size_t Num) { 00157 return static_cast<T*>(Allocate(Num * sizeof(T), AlignOf<T>::Alignment)); 00158 } 00159 00160 /// Allocate space for a specific count of elements and with a specified 00161 /// alignment. 00162 template <typename T> 00163 T *Allocate(size_t Num, size_t Alignment) { 00164 // Round EltSize up to the specified alignment. 00165 size_t EltSize = (sizeof(T)+Alignment-1)&(-Alignment); 00166 return static_cast<T*>(Allocate(Num * EltSize, Alignment)); 00167 } 00168 00169 void Deallocate(const void * /*Ptr*/) {} 00170 00171 unsigned GetNumSlabs() const; 00172 00173 void PrintStats() const; 00174 }; 00175 00176 } // end namespace llvm 00177 00178 #endif // LLVM_SUPPORT_ALLOCATOR_H