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#if (__cplusplus >= 202002L) || (defined(_MSC_VER) && _MSVC_LANG >= 202002L)
136 std::construct_at(p+i, initial);
138 alloc.construct(p+i, initial);
144 for (std::size_t j=0; j < i; ++j) {
145#if (__cplusplus >= 202002L) || (defined(_MSC_VER) && _MSVC_LANG >= 202002L)
146 std::destroy_at(p+j);
151 alloc.deallocate(p, n);
160alloc_initialize_n(std::size_t n, T
const & initial)
162 PlacementNewAllocator<T> alloc;
163 return alloc_initialize_n<T>(n, initial, alloc);
168alloc_initialize_n(std::size_t n)
170 PlacementNewAllocator<T> alloc;
171 return alloc_initialize_n<T>(n, T(), alloc);
174template <
class T,
class Alloc>
176destroy_dealloc_impl(T * p, std::size_t n, Alloc & alloc, VigraTrueType )
178 alloc.deallocate(p, n);
181template <
class T,
class Alloc>
183destroy_dealloc_impl(T * p, std::size_t n, Alloc & alloc, VigraFalseType )
185 for (std::size_t i=0; i < n; ++i) {
186#if (__cplusplus >= 202002L) || (defined(_MSC_VER) && _MSVC_LANG >= 202002L)
187 std::destroy_at(p + i);
189 alloc.destroy(p + i);
192 alloc.deallocate(p, n);
195template <
class T,
class Alloc>
197destroy_dealloc_n(T * p, std::size_t n, Alloc & alloc)
200 destroy_dealloc_impl(p, n, alloc,
typename TypeTraits<T>::isPOD());
206destroy_dealloc_n(T * p, std::size_t n)
210 PlacementNewAllocator<T> alloc;
211 destroy_dealloc_impl(p, n, alloc,
typename TypeTraits<T>::isPOD());
219#if !defined(__GNUC__) || __GNUC__ >= 3
222inline void destroy(T * , VigraTrueType )
227inline void destroy(T * p, VigraFalseType )
233inline void destroy(T * p)
235 destroy(p,
typename TypeTraits<T>::isPOD());
244namespace vigra {
namespace detail {