Gamma  0.9.5
Generic Synthesis Library
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
/Users/ljp/code/gamma/trunk/Gamma/Allocator.h
00001 #ifndef GAMMA_ALLOCATOR_H_INC
00002 #define GAMMA_ALLOCATOR_H_INC
00003 
00004 /*  Gamma - Generic processing library
00005     See COPYRIGHT file for authors and license information
00006 
00007     File Description:
00008     Interface for and default implementation of memory allocator
00009 */
00010 
00011 #include <stddef.h>
00012 #include <stdlib.h>     /* size_t */
00013 
00014 namespace gam{
00015 
00016 
00017 template <class T> class Allocator;
00018 
00019 // specialize for void:
00020 template<> class Allocator<void> {
00021 public:
00022   typedef void*       pointer;
00023   typedef const void* const_pointer;
00024   // reference to void members are impossible.
00025   typedef void value_type;
00026   template <class U> struct rebind { typedef Allocator<U> other; };
00027 };
00028 
00029 template <class T> class Allocator{
00030 public:
00031     typedef size_t    size_type;
00032     typedef ptrdiff_t difference_type;
00033     typedef T*        pointer;
00034     typedef const T*  const_pointer;
00035     typedef T&        reference;
00036     typedef const T&  const_reference;
00037     typedef T         value_type;
00038     template <class U> struct rebind { typedef Allocator<U> other; };
00039 
00040 public:
00041     explicit Allocator(){}
00042     explicit Allocator(const Allocator&){}
00043     template <class U> explicit Allocator(const Allocator<U>&){}
00044     ~Allocator(){}
00045 
00046     pointer address(reference x) const { return &x; }
00047     const_pointer address(const_reference x) const { return &x; }
00048 
00049     pointer allocate(size_type n, Allocator<void>::const_pointer hint = 0){
00050         return reinterpret_cast<pointer>(::operator new(n * sizeof(T)));
00051     }
00052 
00053     void deallocate(pointer p, size_type n){ ::operator delete(p); }
00054 
00055     size_type max_size() const {
00056         return static_cast<size_type>(-1) / sizeof(T);
00057     }
00058 
00059     void construct(pointer p, const T& val){ new(p) T(val); }
00060     void destroy(pointer p){ p->~T(); }
00061 };
00062 
00063 template <class T1, class T2>
00064 bool operator==(const Allocator<T1>&, const Allocator<T2>&){ return true; }
00065 
00066 template <class T1, class T2>
00067 bool operator!=(const Allocator<T1>&, const Allocator<T2>&){ return false; }
00068 
00069 
00070 
00071 //template <class T, class Alloc=Allocator<T> >
00072 //class Buffer : private Alloc{
00073 //
00074 //  explicit Buffer(int n, const T& v=T(), const Alloc& a=Alloc())
00075 //  : Alloc(a), mMemBegin(0), mMemEnd(0)
00076 //  {
00077 //      resize(n);
00078 //  }
00079 //  
00080 //  ~Buffer(){
00081 //      clear();
00082 //  }
00083 //  
00084 //  
00085 //  
00086 //  
00087 //  void clear(){
00088 //  
00089 //      
00090 //  
00096 //  }
00097 //  
00098 //  void resize(int n){
00099 //      if((mMemEnd - mMemBegin) < n){
00100 //          mMemBegin = Alloc::allocate(n);
00101 //          for(int i=0; i<n; ++i){
00102 //              Alloc::construct(mMemBegin + i, v);
00103 //          }       
00104 //      }
00105 //  }
00106 //  
00107 //  void reserve(int n){
00108 //      
00109 //  }
00110 //
00111 //protected:
00112 //  T * mMemBegin;      // First element in memory block
00113 //  T * mMemEnd;        // 1 past last allocated element
00114 //  T * mEnd;           // 1 past last element in buffer
00115 //  
00116 //};
00117 
00118 // vector
00119 
00120 
00121 /*
00122 
00123 Full ISO C++ standard (20.4.1)
00124 
00125 namespace std {
00126     template <class T> class allocator;
00127 
00128     // specialize for void:
00129     template <> class allocator<void> {
00130     public:
00131         typedef void*       pointer;
00132         typedef const void* const_pointer;
00133         // reference to void members are impossible.
00134         typedef void value_type;
00135         template <class U> struct rebind { typedef allocator<U> other; };
00136     };
00137 
00138     template <class T> class allocator {
00139     public:
00140         typedef size_t    size_type;
00141         typedef ptrdiff_t difference_type;
00142         typedef T*        pointer;
00143         typedef const T*  const_pointer;
00144         typedef T&        reference;
00145         typedef const T&  const_reference;
00146         typedef T         value_type;
00147         template <class U> struct rebind { typedef allocator<U> other; };
00148 
00149         allocator() throw();
00150         allocator(const allocator&) throw();
00151         template <class U> allocator(const allocator<U>&) throw();
00152         ~allocator() throw();
00153 
00154         pointer address(reference x) const;
00155         const_pointer address(const_reference x) const;
00156 
00157         pointer allocate(size_type, allocator<void>::const_pointer hint = 0);
00158         void deallocate(pointer p, size_type n);
00159         size_type max_size() const throw();
00160 
00161         void construct(pointer p, const T& val);
00162         void destroy(pointer p);
00163     };
00164     
00165     template <class T1, class T2>
00166     bool operator==(const allocator<T1>&, const allocator<T2>&) throw();
00167 
00168     template <class T1, class T2>
00169     bool operator!=(const allocator<T1>&, const allocator<T2>&) throw();
00170     
00171 }
00172   
00173   
00174   
00175   
00176 */
00177 
00178 } //gam::
00179 
00180 #endif