36#ifndef VIGRA_MEMORY_HXX
37#define VIGRA_MEMORY_HXX
42#ifdef VIGRA_SHARED_PTR_IN_TR1
49#include "metaprogramming.hxx"
53enum SkipInitializationTag { SkipInitialization};
56struct CanSkipInitialization
58 typedef typename TypeTraits<T>::isBuiltinType type;
59 static const bool value = type::asBool;
65template <
class Src,
class Dest>
66Dest 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));
75struct 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)
100destroy_n(T * , std::size_t , VigraTrueType )
105destroy_n(T * p, std::size_t n, VigraFalseType )
107 for(std::size_t i=0; i < n; ++i, ++p)
113destroy_n(T * p, std::size_t n)
115 destroy_n(p, n,
typename TypeTraits<T>::isPOD());
118template <
class T,
class Alloc>
120alloc_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);
150alloc_initialize_n(std::size_t n, T
const & initial)
152 PlacementNewAllocator<T> alloc;
153 return alloc_initialize_n<T>(n, initial, alloc);
158alloc_initialize_n(std::size_t n)
160 PlacementNewAllocator<T> alloc;
161 return alloc_initialize_n<T>(n, T(), alloc);
164template <
class T,
class Alloc>
166destroy_dealloc_impl(T * p, std::size_t n, Alloc & alloc, VigraTrueType )
168 alloc.deallocate(p, n);
171template <
class T,
class Alloc>
173destroy_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);
180template <
class T,
class Alloc>
182destroy_dealloc_n(T * p, std::size_t n, Alloc & alloc)
185 destroy_dealloc_impl(p, n, alloc,
typename TypeTraits<T>::isPOD());
191destroy_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
207inline void destroy(T * , VigraTrueType )
212inline void destroy(T * p, VigraFalseType )
218inline void destroy(T * p)
220 destroy(p,
typename TypeTraits<T>::isPOD());
229namespace vigra {
namespace detail {