Stxxl
1.4.0
|
00001 /*************************************************************************** 00002 * include/stxxl/bits/common/new_alloc.h 00003 * 00004 * Part of the STXXL. See http://stxxl.sourceforge.net 00005 * 00006 * Copyright (C) 2002-2006 Roman Dementiev <dementiev@mpi-sb.mpg.de> 00007 * Copyright (C) 2007, 2008, 2010 Andreas Beckmann <beckmann@cs.uni-frankfurt.de> 00008 * 00009 * Distributed under the Boost Software License, Version 1.0. 00010 * (See accompanying file LICENSE_1_0.txt or copy at 00011 * http://www.boost.org/LICENSE_1_0.txt) 00012 **************************************************************************/ 00013 00014 #ifndef STXXL_NEW_ALLOC_HEADER 00015 #define STXXL_NEW_ALLOC_HEADER 00016 00017 #include <memory> 00018 #include <limits> 00019 #include <stxxl/bits/namespace.h> 00020 00021 00022 __STXXL_BEGIN_NAMESPACE 00023 00024 template <class T> 00025 class new_alloc; 00026 00027 template <typename T, typename U> 00028 struct new_alloc_rebind; 00029 00030 template <typename T> 00031 struct new_alloc_rebind<T, T>{ 00032 typedef new_alloc<T> other; 00033 }; 00034 00035 template <typename T, typename U> 00036 struct new_alloc_rebind { 00037 typedef std::allocator<U> other; 00038 }; 00039 00040 00041 // designed for typed_block (to use with std::vector) 00042 template <class T> 00043 class new_alloc { 00044 public: 00045 // type definitions 00046 typedef T value_type; 00047 typedef T * pointer; 00048 typedef const T * const_pointer; 00049 typedef T & reference; 00050 typedef const T & const_reference; 00051 typedef std::size_t size_type; 00052 typedef std::ptrdiff_t difference_type; 00053 00054 // rebind allocator to type U, use new_alloc only if U == T 00055 template <class U> 00056 struct rebind { 00057 typedef typename new_alloc_rebind<T, U>::other other; 00058 }; 00059 00060 // return address of values 00061 pointer address(reference value) const 00062 { 00063 return &value; 00064 } 00065 const_pointer address(const_reference value) const 00066 { 00067 return &value; 00068 } 00069 00070 new_alloc() throw () { } 00071 new_alloc(const new_alloc &) throw () { } 00072 template <class U> 00073 new_alloc(const new_alloc<U> &) throw () { } 00074 ~new_alloc() throw () { } 00075 00076 template <class U> 00077 operator std::allocator<U>() 00078 { 00079 static std::allocator<U> helper_allocator; 00080 return helper_allocator; 00081 } 00082 00083 // return maximum number of elements that can be allocated 00084 size_type max_size() const throw () 00085 { 00086 return (std::numeric_limits<size_type>::max)() / sizeof(T); 00087 } 00088 00089 // allocate but don't initialize num elements of type T 00090 pointer allocate(size_type num, const void * = 0) 00091 { 00092 return static_cast<T *>(T::operator new (num * sizeof(T))); 00093 } 00094 00095 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00096 // 402. wrong new expression in [some_] allocator::construct 00097 // initialize elements of allocated storage p with value value 00098 void construct(pointer p, const T & value) 00099 { 00100 // initialize memory with placement new 00101 ::new ((void *)p)T(value); 00102 } 00103 00104 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00105 template <typename ... Args> 00106 void construct(pointer p, Args && ... args) 00107 { 00108 ::new ((void *)p)T(std::forward<Args>(args) ...); 00109 } 00110 #endif 00111 00112 // destroy elements of initialized storage p 00113 void destroy(pointer p) 00114 { 00115 // destroy objects by calling their destructor 00116 p->~T(); 00117 } 00118 00119 // deallocate storage p of deleted elements 00120 void deallocate(pointer p, size_type /*num*/) 00121 { 00122 T::operator delete (p); 00123 } 00124 }; 00125 00126 // return that all specializations of this allocator are interchangeable 00127 template <class T1, class T2> 00128 inline bool operator == (const new_alloc<T1> &, 00129 const new_alloc<T2> &) throw () 00130 { 00131 return true; 00132 } 00133 00134 template <class T1, class T2> 00135 inline bool operator != (const new_alloc<T1> &, 00136 const new_alloc<T2> &) throw () 00137 { 00138 return false; 00139 } 00140 00141 __STXXL_END_NAMESPACE 00142 00143 #endif // !STXXL_NEW_ALLOC_HEADER 00144 // vim: et:ts=4:sw=4