00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00034 
00035 
00036 #ifndef _REALLOCATOR_H_
00037 #define _REALLOCATOR_H_
00038 
00039 #include "Mbo.h"
00040 
00041 #include "string.h"  
00042 #include <new>
00043 #include <memory>
00044 
00045 #if defined(_MSC_VER)
00046 # pragma warning(push)
00047 # pragma warning(disable: 4290) // C++ exception specification ignored except to indicate a function is not __declspec(nothrow)
00048 #endif
00049 
00050 namespace mbo
00051 {
00052 
00057 template< class __ValueType, class __ReferenceType = __ValueType>
00058 class allocator: public std::allocator<__ValueType>
00059 {
00060 public:
00061 
00062     typedef std::allocator<__ValueType>             __Allocator;      
00063     typedef typename __Allocator::size_type         size_type;        
00064     typedef typename __Allocator::value_type        value_type;       
00065     typedef typename __Allocator::pointer           pointer;          
00066     typedef typename __Allocator::const_pointer     const_pointer;    
00067     typedef typename __Allocator::difference_type   difference_type;  
00069     typedef __ReferenceType &        reference;        
00070     typedef const __ReferenceType &  const_reference;  
00074     pointer address(reference ref)
00075     {
00076         return &ref;
00077     }
00078 
00081     const_pointer address(const_reference ref)
00082     {
00083         return &ref;
00084     }
00085 
00094     pointer allocate(size_type size, std::allocator<void>::const_pointer hint = 0L)
00095     {
00096         return size ? reinterpret_cast<pointer>(new char [size * sizeof(value_type)]) : 0L;
00097     }
00098 
00106     void deallocate(pointer ptr, size_type size)
00107     {
00108         delete [] reinterpret_cast<char*>(ptr);
00109     }
00110 
00118     void construct(pointer ptr, const_reference oth)
00119     {
00120 #if _MSC_VER >= 1300
00121 # pragma push_macro("new")
00122 # undef new
00123 #endif
00124         new (ptr) value_type(oth);
00125 #if _MSC_VER >= 1300
00126 # pragma pop_macro("new")
00127 #endif
00128     }
00129 
00133     size_type max_size() const
00134     {
00135         return static_cast<size_type>(~0) / sizeof(value_type);
00136     }
00137 
00138 #if MBO_0
00139     allocator()
00140     {
00141     }
00142 
00143     template< class __Other>
00144     allocator(const allocator<__Other> &)
00145     {
00146     }
00147 #endif
00148 
00152     template<class __Other>
00153     struct rebind
00154     {
00155         typedef allocator<__Other> other;
00156     };
00157 };
00158 
00168 template< class __ValueType>
00169 class reallocator: public allocator<__ValueType>
00170 {
00171 public:
00172 
00181     pointer reallocate(pointer ptr, size_type old_size, size_type new_size) __THROWS(bad_alloc)
00182     {
00183         pointer dst = allocate(new_size);
00184 
00185         mbo_throw_if_not(bad_alloc, dst || !new_size);
00186         if (dst && ptr)
00187         {
00188             move(dst, ptr, min(new_size, old_size));
00189         }
00190         if (ptr && (dst || !new_size))
00191         {
00192             deallocate(ptr, old_size);
00193         }
00194         return dst;
00195     }
00196 
00212     void move(pointer dst, pointer src, size_type size)
00213     {
00214         
00215         memmove(dst, src, size * sizeof(value_type));
00216     }
00217 };
00218 
00231 template< class __ValueType>
00232 class mem_reallocator: public reallocator<__ValueType>
00233 {
00234 public:
00235 
00242     pointer allocate(size_type size, std::allocator<void>::const_pointer hint = 0L)
00243     {
00244         return static_cast<pointer>(mbo_malloc(size * sizeof(value_type)));
00245     }
00246 
00254     void deallocate(pointer ptr, size_type)
00255     {
00256         free(ptr);
00257     }
00258 
00269     pointer reallocate(pointer ptr, size_type, size_type new_size)
00270     {
00271         return static_cast<pointer>(mbo_realloc(ptr, new_size * sizeof(value_type)));
00272     }
00273 
00282     void move(pointer dst, pointer src, size_type size)
00283     {
00284         memmove(dst, src, size * sizeof(value_type));
00285     }
00286 };
00287 
00288 }; 
00289 
00290 #if defined(_MSC_VER)
00291 # pragma warning(pop)
00292 #endif
00293 
00294 #endif // _REALLOCATOR_H_