36 #ifndef VIGRA_MEMORY_HXX
37 #define VIGRA_MEMORY_HXX
42 #ifdef VIGRA_SHARED_PTR_IN_TR1
43 # include <tr1/memory>
49 #include "metaprogramming.hxx"
53 enum SkipInitializationTag { SkipInitialization};
56 struct CanSkipInitialization
58 typedef typename TypeTraits<T>::isBuiltinType type;
59 static const bool value = type::asBool;
65 template <
class Src,
class Dest>
66 Dest uninitializedCopy(Src s, Src end, Dest d)
68 typedef typename std::iterator_traits<Dest>::value_type T;
69 for(; s != end; ++s, ++d)
70 new(d) T(static_cast<T const &>(*s));
75 struct PlacementNewAllocator
77 T * allocate(std::size_t n)
79 return (T*)::operator
new(n*
sizeof(T));
82 void deallocate(T * p, std::size_t)
84 return ::operator
delete(p);
87 void construct(T * p, T
const & initial)
100 destroy_n(T * , std::size_t , VigraTrueType )
105 destroy_n(T * p, std::size_t n, VigraFalseType )
107 for(std::size_t i=0; i < n; ++i, ++p)
113 destroy_n(T * p, std::size_t n)
115 destroy_n(p, n,
typename TypeTraits<T>::isPOD());
118 template <
class T,
class Alloc>
120 alloc_initialize_n(std::size_t n, T
const & initial, Alloc & alloc)
122 T * p = alloc.allocate(n);
123 bool useMemset = TypeTraits<T>::isPOD::value &&
127 std::memset(p, 0, n*
sizeof(T));
135 alloc.construct(p+i, initial);
139 for (std::size_t j=0; j < i; ++j)
141 alloc.deallocate(p, n);
150 alloc_initialize_n(std::size_t n, T
const & initial)
152 PlacementNewAllocator<T> alloc;
153 return alloc_initialize_n<T>(n, initial, alloc);
158 alloc_initialize_n(std::size_t n)
160 PlacementNewAllocator<T> alloc;
161 return alloc_initialize_n<T>(n, T(), alloc);
164 template <
class T,
class Alloc>
166 destroy_dealloc_impl(T * p, std::size_t n, Alloc & alloc, VigraTrueType )
168 alloc.deallocate(p, n);
171 template <
class T,
class Alloc>
173 destroy_dealloc_impl(T * p, std::size_t n, Alloc & alloc, VigraFalseType )
175 for (std::size_t i=0; i < n; ++i)
176 alloc.destroy(p + i);
177 alloc.deallocate(p, n);
180 template <
class T,
class Alloc>
182 destroy_dealloc_n(T * p, std::size_t n, Alloc & alloc)
185 destroy_dealloc_impl(p, n, alloc,
typename TypeTraits<T>::isPOD());
191 destroy_dealloc_n(T * p, std::size_t n)
195 PlacementNewAllocator<T> alloc;
196 destroy_dealloc_impl(p, n, alloc,
typename TypeTraits<T>::isPOD());
204 #if !defined(__GNUC__) || __GNUC__ >= 3
207 inline void destroy(T * , VigraTrueType )
212 inline void destroy(T * p, VigraFalseType )
218 inline void destroy(T * p)
220 destroy(p,
typename TypeTraits<T>::isPOD());
229 namespace vigra {
namespace detail {
237 #endif // VIGRA_MEMORY_HXX