LLVM API Documentation

RecyclingAllocator.h
Go to the documentation of this file.
00001 //==- llvm/Support/RecyclingAllocator.h - Recycling Allocator ----*- 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 RecyclingAllocator class.  See the doxygen comment for
00011 // RecyclingAllocator for more details on the implementation.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_SUPPORT_RECYCLINGALLOCATOR_H
00016 #define LLVM_SUPPORT_RECYCLINGALLOCATOR_H
00017 
00018 #include "llvm/Support/Recycler.h"
00019 
00020 namespace llvm {
00021 
00022 /// RecyclingAllocator - This class wraps an Allocator, adding the
00023 /// functionality of recycling deleted objects.
00024 ///
00025 template<class AllocatorType, class T,
00026          size_t Size = sizeof(T), size_t Align = AlignOf<T>::Alignment>
00027 class RecyclingAllocator {
00028 private:
00029   /// Base - Implementation details.
00030   ///
00031   Recycler<T, Size, Align> Base;
00032 
00033   /// Allocator - The wrapped allocator.
00034   ///
00035   AllocatorType Allocator;
00036 
00037 public:
00038   ~RecyclingAllocator() { Base.clear(Allocator); }
00039 
00040   /// Allocate - Return a pointer to storage for an object of type
00041   /// SubClass. The storage may be either newly allocated or recycled.
00042   ///
00043   template<class SubClass>
00044   SubClass *Allocate() { return Base.template Allocate<SubClass>(Allocator); }
00045 
00046   T *Allocate() { return Base.Allocate(Allocator); }
00047 
00048   /// Deallocate - Release storage for the pointed-to object. The
00049   /// storage will be kept track of and may be recycled.
00050   ///
00051   template<class SubClass>
00052   void Deallocate(SubClass* E) { return Base.Deallocate(Allocator, E); }
00053 
00054   void PrintStats() {
00055     Allocator.PrintStats();
00056     Base.PrintStats();
00057   }
00058 };
00059 
00060 }
00061 
00062 template<class AllocatorType, class T, size_t Size, size_t Align>
00063 inline void *operator new(size_t size,
00064                           llvm::RecyclingAllocator<AllocatorType,
00065                                                    T, Size, Align> &Allocator) {
00066   assert(size <= Size && "allocation size exceeded");
00067   return Allocator.Allocate();
00068 }
00069 
00070 template<class AllocatorType, class T, size_t Size, size_t Align>
00071 inline void operator delete(void *E,
00072                             llvm::RecyclingAllocator<AllocatorType,
00073                                                      T, Size, Align> &A) {
00074   A.Deallocate(E);
00075 }
00076 
00077 #endif