00001 #pragma once 00002 #include <xmemory> 00003 #include <limits> 00004 #include "Base/GCTypes.h" 00005 00006 namespace GENAPI_NAMESPACE 00007 { 00008 00009 void* MyAllocate( std::size_t ); 00010 void DeAllocate( void * ); 00011 00012 template <class T> 00013 class MyAlloc { 00014 public: 00015 // type definitions 00016 typedef T value_type; 00017 typedef T* pointer; 00018 typedef const T* const_pointer; 00019 typedef T& reference; 00020 typedef const T& const_reference; 00021 typedef std::size_t size_type; 00022 typedef std::ptrdiff_t difference_type; 00023 00024 // rebind allocator to type U 00025 template <class U> 00026 struct rebind { 00027 typedef MyAlloc<U> other; 00028 }; 00029 00030 // return address of values 00031 pointer address (reference value) const { 00032 return &value; 00033 } 00034 const_pointer address (const_reference value) const { 00035 return &value; 00036 } 00037 00038 /* constructors and destructor 00039 * - nothing to do because the allocator has no state 00040 */ 00041 MyAlloc() throw() { 00042 } 00043 MyAlloc(const MyAlloc&) throw() { 00044 } 00045 template <class U> 00046 MyAlloc (const MyAlloc<U>&) throw() { 00047 } 00048 ~MyAlloc() throw() { 00049 } 00050 00051 // return maximum number of elements that can be allocated 00052 #undef max 00053 size_type max_size () const throw() { 00054 return std::numeric_limits<std::size_t>::max() / sizeof(T); 00055 } 00056 00057 // allocate but don't initialize num elements of type T 00058 pointer allocate (size_type num, const void* = 0) { 00059 // print message and allocate memory with global new 00060 #if 0 00061 std::cerr << "allocate " << num << " element(s)" 00062 << " of size " << sizeof(T) << std::endl; 00063 #endif 00064 pointer ret = (pointer)MyAllocate( num*sizeof(T) ); 00065 #if 0 00066 std::cerr << " allocated at: " << (void*)ret << std::endl; 00067 #endif 00068 return ret; 00069 } 00070 00071 // initialize elements of allocated storage p with value value 00072 void construct (pointer p, const T& value) { 00073 // initialize memory with placement new 00074 new((void*)p)T(value); 00075 } 00076 00077 // destroy elements of initialized storage p 00078 void destroy (pointer p) { 00079 p=p; 00080 // destroy objects by calling their destructor 00081 p->~T(); 00082 } 00083 00084 // deallocate storage p of deleted elements 00085 void deallocate (pointer p, size_type /* num */) { 00086 // print message and deallocate memory with global delete 00087 #if 0 00088 std::cerr << "deallocate " << num << " element(s)" 00089 << " of size " << sizeof(T) 00090 << " at: " << (void*)p << std::endl; 00091 #endif 00092 DeAllocate( p ); 00093 } 00094 }; 00095 00096 // return that all specializations of this allocator are interchangeable 00097 template <class T1, class T2> 00098 bool operator== (const MyAlloc<T1>&, 00099 const MyAlloc<T2>&) throw() { 00100 return true; 00101 } 00102 template <class T1, class T2> 00103 bool operator!= (const MyAlloc<T1>&, 00104 const MyAlloc<T2>&) throw() { 00105 return false; 00106 } 00107 }