- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

# tbb_allocator.h

    00001 /*
    00002     Copyright 2005-2012 Intel Corporation.  All Rights Reserved.
    00003
    00004     This file is part of Threading Building Blocks.
    00005
    00006     Threading Building Blocks is free software; you can redistribute it
    00007     and/or modify it under the terms of the GNU General Public License
    00008     version 2 as published by the Free Software Foundation.
    00009
    00010     Threading Building Blocks is distributed in the hope that it will be
    00011     useful, but WITHOUT ANY WARRANTY; without even the implied warranty
    00012     of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00013     GNU General Public License for more details.
    00014
    00015     You should have received a copy of the GNU General Public License
    00016     along with Threading Building Blocks; if not, write to the Free Software
    00017     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    00018
    00019     As a special exception, you may use this file as part of a free software
    00020     library without restriction.  Specifically, if other files instantiate
    00021     templates or use macros or inline functions from this file, or you compile
    00022     this file and link it with other files to produce an executable, this
    00023     file does not by itself cause the resulting executable to be covered by
    00024     the GNU General Public License.  This exception does not however
    00025     invalidate any other reasons why the executable file might be covered by
    00026     the GNU General Public License.
    00027 */
    00028
    00029 #ifndef __TBB_tbb_allocator_H
    00030 #define __TBB_tbb_allocator_H
    00031
    00032 #include "tbb_stddef.h"
    00033 #include <new>
    00034 #if __TBB_CPP11_RVALUE_REF_PRESENT && !__TBB_CPP11_STD_FORWARD_BROKEN
    00035  #include <utility> // std::forward
    00036 #endif
    00037
    00038 #if !TBB_USE_EXCEPTIONS && _MSC_VER
    00039     // Suppress "C++ exception handler used, but unwind semantics are not enabled" warning in STL headers
    00040     #pragma warning (push)
    00041     #pragma warning (disable: 4530)
    00042 #endif
    00043
    00044 #include <cstring>
    00045
    00046 #if !TBB_USE_EXCEPTIONS && _MSC_VER
    00047     #pragma warning (pop)
    00048 #endif
    00049
    00050 namespace tbb {
    00051
    00053 namespace internal {
    00054
    00056
    00057     void __TBB_EXPORTED_FUNC deallocate_via_handler_v3( void *p );
    00058
    00060
    00061     void* __TBB_EXPORTED_FUNC allocate_via_handler_v3( size_t n );
    00062
    00064     bool __TBB_EXPORTED_FUNC is_malloc_used_v3();
    00065 }
    00067
    00068 #if _MSC_VER && !defined(__INTEL_COMPILER)
    00069     // Workaround for erroneous "unreferenced parameter" warning in method destroy.
    00070     #pragma warning (push)
    00071     #pragma warning (disable: 4100)
    00072 #endif
    00073
    00075
    00080 template<typename T>
    00081 class tbb_allocator {
    00082 public:
    00083     typedef typename internal::allocator_type<T>::value_type value_type;
    00084     typedef value_type* pointer;
    00085     typedef const value_type* const_pointer;
    00086     typedef value_type& reference;
    00087     typedef const value_type& const_reference;
    00088     typedef size_t size_type;
    00089     typedef ptrdiff_t difference_type;
    00090     template<typename U> struct rebind {
    00091         typedef tbb_allocator<U> other;
    00092     };
    00093
    00095     enum malloc_type {
    00096         scalable,
    00097         standard
    00098     };
    00099
    00100     tbb_allocator() throw() {}
    00101     tbb_allocator( const tbb_allocator& ) throw() {}
    00102     template<typename U> tbb_allocator(const tbb_allocator<U>&) throw() {}
    00103
    00104     pointer address(reference x) const {return &x;}
    00105     const_pointer address(const_reference x) const {return &x;}
    00106
    00108     pointer allocate( size_type n, const void* /*hint*/ = 0) {
    00109         return pointer(internal::allocate_via_handler_v3( n * sizeof(value_type) ));
    00110     }
    00111
    00113     void deallocate( pointer p, size_type ) {
    00114         internal::deallocate_via_handler_v3(p);
    00115     }
    00116
    00118     size_type max_size() const throw() {
    00119         size_type max = static_cast<size_type>(-1) / sizeof (value_type);
    00120         return (max > 0 ? max : 1);
    00121     }
    00122
    00124 #if __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT && __TBB_CPP11_RVALUE_REF_PRESENT
    00125     template<typename... Args>
    00126     void construct(pointer p, Args&&... args)
    00127  #if __TBB_CPP11_STD_FORWARD_BROKEN
    00128         { ::new((void *)p) T((args)...); }
    00129  #else
    00130         { ::new((void *)p) T(std::forward<Args>(args)...); }
    00131  #endif
    00132 #else // __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT && __TBB_CPP11_RVALUE_REF_PRESENT
    00133     void construct( pointer p, const value_type& value ) {::new((void*)(p)) value_type(value);}
    00134 #endif // __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT && __TBB_CPP11_RVALUE_REF_PRESENT
    00135
    00137     void destroy( pointer p ) {p->~value_type();}
    00138
    00140     static malloc_type allocator_type() {
    00141         return internal::is_malloc_used_v3() ? standard : scalable;
    00142     }
    00143 };
    00144
    00145 #if _MSC_VER && !defined(__INTEL_COMPILER)
    00146     #pragma warning (pop)
    00147 #endif // warning 4100 is back
    00148
    00150
    00151 template<>
    00152 class tbb_allocator<void> {
    00153 public:
    00154     typedef void* pointer;
    00155     typedef const void* const_pointer;
    00156     typedef void value_type;
    00157     template<typename U> struct rebind {
    00158         typedef tbb_allocator<U> other;
    00159     };
    00160 };
    00161
    00162 template<typename T, typename U>
    00163 inline bool operator==( const tbb_allocator<T>&, const tbb_allocator<U>& ) {return true;}
    00164
    00165 template<typename T, typename U>
    00166 inline bool operator!=( const tbb_allocator<T>&, const tbb_allocator<U>& ) {return false;}
    00167
    00169
    00174 template <typename T, template<typename X> class Allocator = tbb_allocator>
    00175 class zero_allocator : public Allocator<T>
    00176 {
    00177 public:
    00178     typedef Allocator<T> base_allocator_type;
    00179     typedef typename base_allocator_type::value_type value_type;
    00180     typedef typename base_allocator_type::pointer pointer;
    00181     typedef typename base_allocator_type::const_pointer const_pointer;
    00182     typedef typename base_allocator_type::reference reference;
    00183     typedef typename base_allocator_type::const_reference const_reference;
    00184     typedef typename base_allocator_type::size_type size_type;
    00185     typedef typename base_allocator_type::difference_type difference_type;
    00186     template<typename U> struct rebind {
    00187         typedef zero_allocator<U, Allocator> other;
    00188     };
    00189
    00190     zero_allocator() throw() { }
    00191     zero_allocator(const zero_allocator &a) throw() : base_allocator_type( a ) { }
    00192     template<typename U>
    00193     zero_allocator(const zero_allocator<U> &a) throw() : base_allocator_type( Allocator<U>( a ) ) { }
    00194
    00195     pointer allocate(const size_type n, const void *hint = 0 ) {
    00196         pointer ptr = base_allocator_type::allocate( n, hint );
    00197         std::memset( ptr, 0, n * sizeof(value_type) );
    00198         return ptr;
    00199     }
    00200 };
    00201
    00203
    00204 template<template<typename T> class Allocator>
    00205 class zero_allocator<void, Allocator> : public Allocator<void> {
    00206 public:
    00207     typedef Allocator<void> base_allocator_type;
    00208     typedef typename base_allocator_type::value_type value_type;
    00209     typedef typename base_allocator_type::pointer pointer;
    00210     typedef typename base_allocator_type::const_pointer const_pointer;
    00211     template<typename U> struct rebind {
    00212         typedef zero_allocator<U, Allocator> other;
    00213     };
    00214 };
    00215
    00216 template<typename T1, template<typename X1> class B1, typename T2, template<typename X2> class B2>
    00217 inline bool operator==( const zero_allocator<T1,B1> &a, const zero_allocator<T2,B2> &b) {
    00218     return static_cast< B1<T1> >(a) == static_cast< B2<T2> >(b);
    00219 }
    00220 template<typename T1, template<typename X1> class B1, typename T2, template<typename X2> class B2>
    00221 inline bool operator!=( const zero_allocator<T1,B1> &a, const zero_allocator<T2,B2> &b) {
    00222     return static_cast< B1<T1> >(a) != static_cast< B2<T2> >(b);
    00223 }
    00224
    00225 } // namespace tbb
    00226
    00227 #endif /* __TBB_tbb_allocator_H */

Copyright © 2005-2012 Intel Corporation. All Rights Reserved.

Intel, Pentium, Intel Xeon, Itanium, Intel XScale and VTune are registered trademarks or trademarks of Intel Corporation or its subsidiaries in the United States and other countries.

* Other names and brands may be claimed as the property of others.