2 Copyright 2005-2010 Intel Corporation. All Rights Reserved.
4 This file is part of Threading Building Blocks.
6 Threading Building Blocks is free software; you can redistribute it
7 and/or modify it under the terms of the GNU General Public License
8 version 2 as published by the Free Software Foundation.
10 Threading Building Blocks is distributed in the hope that it will be
11 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with Threading Building Blocks; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 As a special exception, you may use this file as part of a free software
20 library without restriction. Specifically, if other files instantiate
21 templates or use macros or inline functions from this file, or you compile
22 this file and link it with other files to produce an executable, this
23 file does not by itself cause the resulting executable to be covered by
24 the GNU General Public License. This exception does not however
25 invalidate any other reasons why the executable file might be covered by
26 the GNU General Public License.
29 #ifndef __TBB_tbb_allocator_H
30 #define __TBB_tbb_allocator_H
32 #include "tbb_stddef.h"
35 #if !TBB_USE_EXCEPTIONS && _MSC_VER
36 // Suppress "C++ exception handler used, but unwind semantics are not enabled" warning in STL headers
37 #pragma warning (push)
38 #pragma warning (disable: 4530)
43 #if !TBB_USE_EXCEPTIONS && _MSC_VER
52 //! Deallocates memory using FreeHandler
53 /** The function uses scalable_free if scalable allocator is available and free if not*/
54 void __TBB_EXPORTED_FUNC deallocate_via_handler_v3( void *p );
56 //! Allocates memory using MallocHandler
57 /** The function uses scalable_malloc if scalable allocator is available and malloc if not*/
58 void* __TBB_EXPORTED_FUNC allocate_via_handler_v3( size_t n );
60 //! Returns true if standard malloc/free are used to work with memory.
61 bool __TBB_EXPORTED_FUNC is_malloc_used_v3();
65 #if _MSC_VER && !defined(__INTEL_COMPILER)
66 // Workaround for erroneous "unreferenced parameter" warning in method destroy.
67 #pragma warning (push)
68 #pragma warning (disable: 4100)
71 //! Meets "allocator" requirements of ISO C++ Standard, Section 20.1.5
72 /** The class selects the best memory allocation mechanism available
73 from scalable_malloc and standard malloc.
74 The members are ordered the same way they are in section 20.4.1
75 of the ISO C++ standard.
76 @ingroup memory_allocation */
80 typedef typename internal::allocator_type<T>::value_type value_type;
81 typedef value_type* pointer;
82 typedef const value_type* const_pointer;
83 typedef value_type& reference;
84 typedef const value_type& const_reference;
85 typedef size_t size_type;
86 typedef ptrdiff_t difference_type;
87 template<typename U> struct rebind {
88 typedef tbb_allocator<U> other;
91 //! Specifies current allocator
97 tbb_allocator() throw() {}
98 tbb_allocator( const tbb_allocator& ) throw() {}
99 template<typename U> tbb_allocator(const tbb_allocator<U>&) throw() {}
101 pointer address(reference x) const {return &x;}
102 const_pointer address(const_reference x) const {return &x;}
104 //! Allocate space for n objects.
105 pointer allocate( size_type n, const void* /*hint*/ = 0) {
106 return pointer(internal::allocate_via_handler_v3( n * sizeof(value_type) ));
109 //! Free previously allocated block of memory.
110 void deallocate( pointer p, size_type ) {
111 internal::deallocate_via_handler_v3(p);
114 //! Largest value for which method allocate might succeed.
115 size_type max_size() const throw() {
116 size_type max = static_cast<size_type>(-1) / sizeof (value_type);
117 return (max > 0 ? max : 1);
120 //! Copy-construct value at location pointed to by p.
121 void construct( pointer p, const value_type& value ) {::new((void*)(p)) value_type(value);}
123 //! Destroy value at location pointed to by p.
124 void destroy( pointer p ) {p->~value_type();}
126 //! Returns current allocator
127 static malloc_type allocator_type() {
128 return internal::is_malloc_used_v3() ? standard : scalable;
132 #if _MSC_VER && !defined(__INTEL_COMPILER)
133 #pragma warning (pop)
134 #endif // warning 4100 is back
136 //! Analogous to std::allocator<void>, as defined in ISO C++ Standard, Section 20.4.1
137 /** @ingroup memory_allocation */
139 class tbb_allocator<void> {
141 typedef void* pointer;
142 typedef const void* const_pointer;
143 typedef void value_type;
144 template<typename U> struct rebind {
145 typedef tbb_allocator<U> other;
149 template<typename T, typename U>
150 inline bool operator==( const tbb_allocator<T>&, const tbb_allocator<U>& ) {return true;}
152 template<typename T, typename U>
153 inline bool operator!=( const tbb_allocator<T>&, const tbb_allocator<U>& ) {return false;}
155 //! Meets "allocator" requirements of ISO C++ Standard, Section 20.1.5
156 /** The class is an adapter over an actual allocator that fills the allocation
157 using memset function with template argument C as the value.
158 The members are ordered the same way they are in section 20.4.1
159 of the ISO C++ standard.
160 @ingroup memory_allocation */
161 template <typename T, template<typename X> class Allocator = tbb_allocator>
162 class zero_allocator : public Allocator<T>
165 typedef Allocator<T> base_allocator_type;
166 typedef typename base_allocator_type::value_type value_type;
167 typedef typename base_allocator_type::pointer pointer;
168 typedef typename base_allocator_type::const_pointer const_pointer;
169 typedef typename base_allocator_type::reference reference;
170 typedef typename base_allocator_type::const_reference const_reference;
171 typedef typename base_allocator_type::size_type size_type;
172 typedef typename base_allocator_type::difference_type difference_type;
173 template<typename U> struct rebind {
174 typedef zero_allocator<U, Allocator> other;
177 zero_allocator() throw() { }
178 zero_allocator(const zero_allocator &a) throw() : base_allocator_type( a ) { }
180 zero_allocator(const zero_allocator<U> &a) throw() : base_allocator_type( Allocator<U>( a ) ) { }
182 pointer allocate(const size_type n, const void *hint = 0 ) {
183 pointer ptr = base_allocator_type::allocate( n, hint );
184 std::memset( ptr, 0, n * sizeof(value_type) );
189 //! Analogous to std::allocator<void>, as defined in ISO C++ Standard, Section 20.4.1
190 /** @ingroup memory_allocation */
191 template<template<typename T> class Allocator>
192 class zero_allocator<void, Allocator> : public Allocator<void> {
194 typedef Allocator<void> base_allocator_type;
195 typedef typename base_allocator_type::value_type value_type;
196 typedef typename base_allocator_type::pointer pointer;
197 typedef typename base_allocator_type::const_pointer const_pointer;
198 template<typename U> struct rebind {
199 typedef zero_allocator<U, Allocator> other;
203 template<typename T1, template<typename X1> class B1, typename T2, template<typename X2> class B2>
204 inline bool operator==( const zero_allocator<T1,B1> &a, const zero_allocator<T2,B2> &b) {
205 return static_cast< B1<T1> >(a) == static_cast< B2<T2> >(b);
207 template<typename T1, template<typename X1> class B1, typename T2, template<typename X2> class B2>
208 inline bool operator!=( const zero_allocator<T1,B1> &a, const zero_allocator<T2,B2> &b) {
209 return static_cast< B1<T1> >(a) != static_cast< B2<T2> >(b);
214 #endif /* __TBB_tbb_allocator_H */