LLVM API Documentation

Allocator.h

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