File indexing completed on 2025-01-18 10:13:00
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #ifndef __TBB_tbb_allocator_H
0018 #define __TBB_tbb_allocator_H
0019
0020 #include "tbb_stddef.h"
0021 #include <new>
0022 #if __TBB_ALLOCATOR_CONSTRUCT_VARIADIC
0023 #include <utility> // std::forward
0024 #endif
0025 #include <cstring>
0026
0027 namespace tbb {
0028
0029
0030 namespace internal {
0031
0032
0033
0034 void __TBB_EXPORTED_FUNC deallocate_via_handler_v3( void *p );
0035
0036
0037
0038 void* __TBB_EXPORTED_FUNC allocate_via_handler_v3( size_t n );
0039
0040
0041 bool __TBB_EXPORTED_FUNC is_malloc_used_v3();
0042 }
0043
0044
0045 #if _MSC_VER && !defined(__INTEL_COMPILER)
0046
0047 #pragma warning (push)
0048 #pragma warning (disable: 4100)
0049 #endif
0050
0051
0052
0053
0054
0055
0056
0057 template<typename T>
0058 class tbb_allocator {
0059 public:
0060 typedef typename internal::allocator_type<T>::value_type value_type;
0061 typedef value_type* pointer;
0062 typedef const value_type* const_pointer;
0063 typedef value_type& reference;
0064 typedef const value_type& const_reference;
0065 typedef size_t size_type;
0066 typedef ptrdiff_t difference_type;
0067 template<typename U> struct rebind {
0068 typedef tbb_allocator<U> other;
0069 };
0070
0071
0072 enum malloc_type {
0073 scalable,
0074 standard
0075 };
0076
0077 tbb_allocator() throw() {}
0078 tbb_allocator( const tbb_allocator& ) throw() {}
0079 template<typename U> tbb_allocator(const tbb_allocator<U>&) throw() {}
0080
0081 pointer address(reference x) const {return &x;}
0082 const_pointer address(const_reference x) const {return &x;}
0083
0084
0085 pointer allocate( size_type n, const void* = 0) {
0086 return pointer(internal::allocate_via_handler_v3( n * sizeof(value_type) ));
0087 }
0088
0089
0090 void deallocate( pointer p, size_type ) {
0091 internal::deallocate_via_handler_v3(p);
0092 }
0093
0094
0095 size_type max_size() const throw() {
0096 size_type max = static_cast<size_type>(-1) / sizeof (value_type);
0097 return (max > 0 ? max : 1);
0098 }
0099
0100
0101 #if __TBB_ALLOCATOR_CONSTRUCT_VARIADIC
0102 template<typename U, typename... Args>
0103 void construct(U *p, Args&&... args)
0104 { ::new((void *)p) U(std::forward<Args>(args)...); }
0105 #else
0106 #if __TBB_CPP11_RVALUE_REF_PRESENT
0107 void construct( pointer p, value_type&& value ) {::new((void*)(p)) value_type(std::move(value));}
0108 #endif
0109 void construct( pointer p, const value_type& value ) {::new((void*)(p)) value_type(value);}
0110 #endif
0111
0112
0113 void destroy( pointer p ) {p->~value_type();}
0114
0115
0116 static malloc_type allocator_type() {
0117 return internal::is_malloc_used_v3() ? standard : scalable;
0118 }
0119 };
0120
0121 #if _MSC_VER && !defined(__INTEL_COMPILER)
0122 #pragma warning (pop)
0123 #endif
0124
0125
0126
0127 template<>
0128 class tbb_allocator<void> {
0129 public:
0130 typedef void* pointer;
0131 typedef const void* const_pointer;
0132 typedef void value_type;
0133 template<typename U> struct rebind {
0134 typedef tbb_allocator<U> other;
0135 };
0136 };
0137
0138 template<typename T, typename U>
0139 inline bool operator==( const tbb_allocator<T>&, const tbb_allocator<U>& ) {return true;}
0140
0141 template<typename T, typename U>
0142 inline bool operator!=( const tbb_allocator<T>&, const tbb_allocator<U>& ) {return false;}
0143
0144
0145
0146
0147
0148
0149
0150 template <typename T, template<typename X> class Allocator = tbb_allocator>
0151 class zero_allocator : public Allocator<T>
0152 {
0153 public:
0154 typedef Allocator<T> base_allocator_type;
0155 typedef typename base_allocator_type::value_type value_type;
0156 typedef typename base_allocator_type::pointer pointer;
0157 typedef typename base_allocator_type::const_pointer const_pointer;
0158 typedef typename base_allocator_type::reference reference;
0159 typedef typename base_allocator_type::const_reference const_reference;
0160 typedef typename base_allocator_type::size_type size_type;
0161 typedef typename base_allocator_type::difference_type difference_type;
0162 template<typename U> struct rebind {
0163 typedef zero_allocator<U, Allocator> other;
0164 };
0165
0166 zero_allocator() throw() { }
0167 zero_allocator(const zero_allocator &a) throw() : base_allocator_type( a ) { }
0168 template<typename U>
0169 zero_allocator(const zero_allocator<U> &a) throw() : base_allocator_type( Allocator<U>( a ) ) { }
0170
0171 pointer allocate(const size_type n, const void *hint = 0 ) {
0172 pointer ptr = base_allocator_type::allocate( n, hint );
0173 std::memset( static_cast<void*>(ptr), 0, n * sizeof(value_type) );
0174 return ptr;
0175 }
0176 };
0177
0178
0179
0180 template<template<typename T> class Allocator>
0181 class zero_allocator<void, Allocator> : public Allocator<void> {
0182 public:
0183 typedef Allocator<void> base_allocator_type;
0184 typedef typename base_allocator_type::value_type value_type;
0185 typedef typename base_allocator_type::pointer pointer;
0186 typedef typename base_allocator_type::const_pointer const_pointer;
0187 template<typename U> struct rebind {
0188 typedef zero_allocator<U, Allocator> other;
0189 };
0190 };
0191
0192 template<typename T1, template<typename X1> class B1, typename T2, template<typename X2> class B2>
0193 inline bool operator==( const zero_allocator<T1,B1> &a, const zero_allocator<T2,B2> &b) {
0194 return static_cast< B1<T1> >(a) == static_cast< B2<T2> >(b);
0195 }
0196 template<typename T1, template<typename X1> class B1, typename T2, template<typename X2> class B2>
0197 inline bool operator!=( const zero_allocator<T1,B1> &a, const zero_allocator<T2,B2> &b) {
0198 return static_cast< B1<T1> >(a) != static_cast< B2<T2> >(b);
0199 }
0200
0201 }
0202
0203 #endif