Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:13:00

0001 /*
0002     Copyright (c) 2005-2020 Intel Corporation
0003 
0004     Licensed under the Apache License, Version 2.0 (the "License");
0005     you may not use this file except in compliance with the License.
0006     You may obtain a copy of the License at
0007 
0008         http://www.apache.org/licenses/LICENSE-2.0
0009 
0010     Unless required by applicable law or agreed to in writing, software
0011     distributed under the License is distributed on an "AS IS" BASIS,
0012     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013     See the License for the specific language governing permissions and
0014     limitations under the License.
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 //! @cond INTERNAL
0030 namespace internal {
0031 
0032     //! Deallocates memory using FreeHandler
0033     /** The function uses scalable_free if scalable allocator is available and free if not*/
0034     void __TBB_EXPORTED_FUNC deallocate_via_handler_v3( void *p );
0035 
0036     //! Allocates memory using MallocHandler
0037     /** The function uses scalable_malloc if scalable allocator is available and malloc if not*/
0038     void* __TBB_EXPORTED_FUNC allocate_via_handler_v3( size_t n );
0039 
0040     //! Returns true if standard malloc/free are used to work with memory.
0041     bool __TBB_EXPORTED_FUNC is_malloc_used_v3();
0042 }
0043 //! @endcond
0044 
0045 #if _MSC_VER && !defined(__INTEL_COMPILER)
0046     // Workaround for erroneous "unreferenced parameter" warning in method destroy.
0047     #pragma warning (push)
0048     #pragma warning (disable: 4100)
0049 #endif
0050 
0051 //! Meets "allocator" requirements of ISO C++ Standard, Section 20.1.5
0052 /** The class selects the best memory allocation mechanism available
0053     from scalable_malloc and standard malloc.
0054     The members are ordered the same way they are in section 20.4.1
0055     of the ISO C++ standard.
0056     @ingroup memory_allocation */
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     //! Specifies current allocator
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     //! Allocate space for n objects.
0085     pointer allocate( size_type n, const void* /*hint*/ = 0) {
0086         return pointer(internal::allocate_via_handler_v3( n * sizeof(value_type) ));
0087     }
0088 
0089     //! Free previously allocated block of memory.
0090     void deallocate( pointer p, size_type ) {
0091         internal::deallocate_via_handler_v3(p);
0092     }
0093 
0094     //! Largest value for which method allocate might succeed.
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     //! Copy-construct value at location pointed to by p.
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 // __TBB_ALLOCATOR_CONSTRUCT_VARIADIC
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 // __TBB_ALLOCATOR_CONSTRUCT_VARIADIC
0111 
0112     //! Destroy value at location pointed to by p.
0113     void destroy( pointer p ) {p->~value_type();}
0114 
0115     //! Returns current allocator
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 // warning 4100 is back
0124 
0125 //! Analogous to std::allocator<void>, as defined in ISO C++ Standard, Section 20.4.1
0126 /** @ingroup memory_allocation */
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 //! Meets "allocator" requirements of ISO C++ Standard, Section 20.1.5
0145 /** The class is an adapter over an actual allocator that fills the allocation
0146     using memset function with template argument C as the value.
0147     The members are ordered the same way they are in section 20.4.1
0148     of the ISO C++ standard.
0149     @ingroup memory_allocation */
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 //! Analogous to std::allocator<void>, as defined in ISO C++ Standard, Section 20.4.1
0179 /** @ingroup memory_allocation */
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 } // namespace tbb
0202 
0203 #endif /* __TBB_tbb_allocator_H */