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 <cstdlib> 00019 00020 namespace llvm { 00021 00022 class MallocAllocator { 00023 public: 00024 MallocAllocator() {} 00025 ~MallocAllocator() {} 00026 00027 void Reset() {} 00028 00029 void *Allocate(size_t Size, size_t /*Alignment*/) { return malloc(Size); } 00030 00031 template <typename T> 00032 T *Allocate() { return static_cast<T*>(malloc(sizeof(T))); } 00033 00034 void Deallocate(void *Ptr) { free(Ptr); } 00035 00036 void PrintStats() const {} 00037 }; 00038 00039 /// BumpPtrAllocator - This allocator is useful for containers that need very 00040 /// simple memory allocation strategies. In particular, this just keeps 00041 /// allocating memory, and never deletes it until the entire block is dead. This 00042 /// makes allocation speedy, but must only be used when the trade-off is ok. 00043 class BumpPtrAllocator { 00044 BumpPtrAllocator(const BumpPtrAllocator &); // do not implement 00045 void operator=(const BumpPtrAllocator &); // do not implement 00046 00047 void *TheMemory; 00048 public: 00049 BumpPtrAllocator(); 00050 ~BumpPtrAllocator(); 00051 00052 void Reset(); 00053 00054 void *Allocate(size_t Size, size_t Alignment); 00055 00056 template <typename T> 00057 T *Allocate() { 00058 return static_cast<T*>(Allocate(sizeof(T),AlignOf<T>::Alignment)); 00059 } 00060 00061 template <typename T> 00062 T *Allocate(size_t Num) { 00063 return static_cast<T*>(Allocate(Num * sizeof(T), AlignOf<T>::Alignment)); 00064 } 00065 00066 void Deallocate(void * /*Ptr*/) {} 00067 00068 void PrintStats() const; 00069 }; 00070 00071 } // end namespace llvm 00072 00073 #endif