BRE12
tbb_allocator.h
1 /*
2  Copyright 2005-2016 Intel Corporation. All Rights Reserved.
3 
4  This file is part of Threading Building Blocks. Threading Building Blocks is free software;
5  you can redistribute it and/or modify it under the terms of the GNU General Public License
6  version 2 as published by the Free Software Foundation. Threading Building Blocks is
7  distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
8  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9  See the GNU General Public License for more details. You should have received a copy of
10  the GNU General Public License along with Threading Building Blocks; if not, write to the
11  Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
12 
13  As a special exception, you may use this file as part of a free software library without
14  restriction. Specifically, if other files instantiate templates or use macros or inline
15  functions from this file, or you compile this file and link it with other files to produce
16  an executable, this file does not by itself cause the resulting executable to be covered
17  by the GNU General Public License. This exception does not however invalidate any other
18  reasons why the executable file might be covered by the GNU General Public License.
19 */
20 
21 #ifndef __TBB_tbb_allocator_H
22 #define __TBB_tbb_allocator_H
23 
24 #include "tbb_stddef.h"
25 #include <new>
26 #if __TBB_ALLOCATOR_CONSTRUCT_VARIADIC
27  #include <utility> // std::forward
28 #endif
29 
30 #if !TBB_USE_EXCEPTIONS && _MSC_VER
31  // Suppress "C++ exception handler used, but unwind semantics are not enabled" warning in STL headers
32  #pragma warning (push)
33  #pragma warning (disable: 4530)
34 #endif
35 
36 #include <cstring>
37 
38 #if !TBB_USE_EXCEPTIONS && _MSC_VER
39  #pragma warning (pop)
40 #endif
41 
42 namespace tbb {
43 
45 namespace internal {
46 
48 
49  void __TBB_EXPORTED_FUNC deallocate_via_handler_v3( void *p );
50 
52 
53  void* __TBB_EXPORTED_FUNC allocate_via_handler_v3( size_t n );
54 
56  bool __TBB_EXPORTED_FUNC is_malloc_used_v3();
57 }
59 
60 #if _MSC_VER && !defined(__INTEL_COMPILER)
61  // Workaround for erroneous "unreferenced parameter" warning in method destroy.
62  #pragma warning (push)
63  #pragma warning (disable: 4100)
64 #endif
65 
67 
72 template<typename T>
74 public:
75  typedef typename internal::allocator_type<T>::value_type value_type;
76  typedef value_type* pointer;
77  typedef const value_type* const_pointer;
78  typedef value_type& reference;
79  typedef const value_type& const_reference;
80  typedef size_t size_type;
81  typedef ptrdiff_t difference_type;
82  template<typename U> struct rebind {
83  typedef tbb_allocator<U> other;
84  };
85 
87  enum malloc_type {
88  scalable,
89  standard
90  };
91 
92  tbb_allocator() throw() {}
93  tbb_allocator( const tbb_allocator& ) throw() {}
94  template<typename U> tbb_allocator(const tbb_allocator<U>&) throw() {}
95 
96  pointer address(reference x) const {return &x;}
97  const_pointer address(const_reference x) const {return &x;}
98 
100  pointer allocate( size_type n, const void* /*hint*/ = 0) {
101  return pointer(internal::allocate_via_handler_v3( n * sizeof(value_type) ));
102  }
103 
105  void deallocate( pointer p, size_type ) {
106  internal::deallocate_via_handler_v3(p);
107  }
108 
110  size_type max_size() const throw() {
111  size_type max = static_cast<size_type>(-1) / sizeof (value_type);
112  return (max > 0 ? max : 1);
113  }
114 
116 #if __TBB_ALLOCATOR_CONSTRUCT_VARIADIC
117  template<typename U, typename... Args>
118  void construct(U *p, Args&&... args)
119  { ::new((void *)p) U(std::forward<Args>(args)...); }
120 #else // __TBB_ALLOCATOR_CONSTRUCT_VARIADIC
121 #if __TBB_CPP11_RVALUE_REF_PRESENT
122  void construct( pointer p, value_type&& value ) {::new((void*)(p)) value_type(std::move(value));}
123 #endif
124  void construct( pointer p, const value_type& value ) {::new((void*)(p)) value_type(value);}
125 #endif // __TBB_ALLOCATOR_CONSTRUCT_VARIADIC
126 
128  void destroy( pointer p ) {p->~value_type();}
129 
132  return internal::is_malloc_used_v3() ? standard : scalable;
133  }
134 };
135 
136 #if _MSC_VER && !defined(__INTEL_COMPILER)
137  #pragma warning (pop)
138 #endif // warning 4100 is back
139 
141 
142 template<>
143 class tbb_allocator<void> {
144 public:
145  typedef void* pointer;
146  typedef const void* const_pointer;
147  typedef void value_type;
148  template<typename U> struct rebind {
149  typedef tbb_allocator<U> other;
150  };
151 };
152 
153 template<typename T, typename U>
154 inline bool operator==( const tbb_allocator<T>&, const tbb_allocator<U>& ) {return true;}
155 
156 template<typename T, typename U>
157 inline bool operator!=( const tbb_allocator<T>&, const tbb_allocator<U>& ) {return false;}
158 
160 
165 template <typename T, template<typename X> class Allocator = tbb_allocator>
166 class zero_allocator : public Allocator<T>
167 {
168 public:
169  typedef Allocator<T> base_allocator_type;
170  typedef typename base_allocator_type::value_type value_type;
171  typedef typename base_allocator_type::pointer pointer;
172  typedef typename base_allocator_type::const_pointer const_pointer;
173  typedef typename base_allocator_type::reference reference;
174  typedef typename base_allocator_type::const_reference const_reference;
175  typedef typename base_allocator_type::size_type size_type;
176  typedef typename base_allocator_type::difference_type difference_type;
177  template<typename U> struct rebind {
179  };
180 
181  zero_allocator() throw() { }
182  zero_allocator(const zero_allocator &a) throw() : base_allocator_type( a ) { }
183  template<typename U>
184  zero_allocator(const zero_allocator<U> &a) throw() : base_allocator_type( Allocator<U>( a ) ) { }
185 
186  pointer allocate(const size_type n, const void *hint = 0 ) {
187  pointer ptr = base_allocator_type::allocate( n, hint );
188  std::memset( ptr, 0, n * sizeof(value_type) );
189  return ptr;
190  }
191 };
192 
194 
195 template<template<typename T> class Allocator>
196 class zero_allocator<void, Allocator> : public Allocator<void> {
197 public:
198  typedef Allocator<void> base_allocator_type;
199  typedef typename base_allocator_type::value_type value_type;
200  typedef typename base_allocator_type::pointer pointer;
201  typedef typename base_allocator_type::const_pointer const_pointer;
202  template<typename U> struct rebind {
204  };
205 };
206 
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);
210 }
211 template<typename T1, template<typename X1> class B1, typename T2, template<typename X2> class B2>
212 inline bool operator!=( const zero_allocator<T1,B1> &a, const zero_allocator<T2,B2> &b) {
213  return static_cast< B1<T1> >(a) != static_cast< B2<T2> >(b);
214 }
215 
216 } // namespace tbb
217 
218 #endif /* __TBB_tbb_allocator_H */
Meets "allocator" requirements of ISO C++ Standard, Section 20.1.5.
Definition: tbb_allocator.h:166
malloc_type
Specifies current allocator.
Definition: tbb_allocator.h:87
Definition: tbb_allocator.h:82
size_type max_size() const
Largest value for which method allocate might succeed.
Definition: tbb_allocator.h:110
pointer allocate(size_type n, const void *=0)
Allocate space for n objects.
Definition: tbb_allocator.h:100
Definition: _flow_graph_async_msg_impl.h:32
void deallocate(pointer p, size_type)
Free previously allocated block of memory.
Definition: tbb_allocator.h:105
The namespace tbb contains all components of the library.
Definition: parallel_for.h:44
Meets "allocator" requirements of ISO C++ Standard, Section 20.1.5.
Definition: tbb_allocator.h:73
void destroy(pointer p)
Destroy value at location pointed to by p.
Definition: tbb_allocator.h:128
Definition: tbb_allocator.h:177
void construct(pointer p, const value_type &value)
Copy-construct value at location pointed to by p.
Definition: tbb_allocator.h:124
static malloc_type allocator_type()
Returns current allocator.
Definition: tbb_allocator.h:131