Intel(R) Threading Building Blocks Doxygen Documentation  version 4.2.3
scalable_allocator.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2005-2018 Intel Corporation
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 
16 
17 
18 
19 */
20 
21 #ifndef __TBB_scalable_allocator_H
22 #define __TBB_scalable_allocator_H
23 
25 #include <stddef.h> /* Need ptrdiff_t and size_t from here. */
26 #if !_MSC_VER
27 #include <stdint.h> /* Need intptr_t from here. */
28 #endif
29 
30 #if !defined(__cplusplus) && __ICC==1100
31  #pragma warning (push)
32  #pragma warning (disable: 991)
33 #endif
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif /* __cplusplus */
38 
39 #if _MSC_VER >= 1400
40 #define __TBB_EXPORTED_FUNC __cdecl
41 #else
42 #define __TBB_EXPORTED_FUNC
43 #endif
44 
48 
51 void __TBB_EXPORTED_FUNC scalable_free (void* ptr);
52 
55 void * __TBB_EXPORTED_FUNC scalable_realloc (void* ptr, size_t size);
56 
59 void * __TBB_EXPORTED_FUNC scalable_calloc (size_t nobj, size_t size);
60 
63 int __TBB_EXPORTED_FUNC scalable_posix_memalign (void** memptr, size_t alignment, size_t size);
64 
67 void * __TBB_EXPORTED_FUNC scalable_aligned_malloc (size_t size, size_t alignment);
68 
71 void * __TBB_EXPORTED_FUNC scalable_aligned_realloc (void* ptr, size_t size, size_t alignment);
72 
76 
81 size_t __TBB_EXPORTED_FUNC scalable_msize (void* ptr);
82 
83 /* Results for scalable_allocation_* functions */
84 typedef enum {
91 
92 /* Setting TBB_MALLOC_USE_HUGE_PAGES environment variable to 1 enables huge pages.
93  scalable_allocation_mode call has priority over environment variable. */
94 typedef enum {
95  TBBMALLOC_USE_HUGE_PAGES, /* value turns using huge pages on and off */
96  /* deprecated, kept for backward compatibility only */
98  /* try to limit memory consumption value Bytes, clean internal buffers
99  if limit is exceeded, but not prevents from requesting memory from OS */
102 
105 int __TBB_EXPORTED_FUNC scalable_allocation_mode(int param, intptr_t value);
106 
107 typedef enum {
108  /* Clean internal allocator buffers for all threads.
109  Returns TBBMALLOC_NO_EFFECT if no buffers cleaned,
110  TBBMALLOC_OK if some memory released from buffers. */
112  /* Clean internal allocator buffer for current thread only.
113  Return values same as for TBBMALLOC_CLEAN_ALL_BUFFERS. */
116 
119 int __TBB_EXPORTED_FUNC scalable_allocation_command(int cmd, void *param);
120 
121 #ifdef __cplusplus
122 } /* extern "C" */
123 #endif /* __cplusplus */
124 
125 #ifdef __cplusplus
126 
128 namespace rml {
129 class MemoryPool;
130 
131 typedef void *(*rawAllocType)(intptr_t pool_id, size_t &bytes);
132 // returns non-zero in case of error
133 typedef int (*rawFreeType)(intptr_t pool_id, void* raw_ptr, size_t raw_bytes);
134 
135 /*
136 MemPoolPolicy extension must be compatible with such structure fields layout
137 
138 struct MemPoolPolicy {
139  rawAllocType pAlloc;
140  rawFreeType pFree;
141  size_t granularity; // granularity of pAlloc allocations
142 };
143 */
144 
145 struct MemPoolPolicy {
146  enum {
147  TBBMALLOC_POOL_VERSION = 1
148  };
149 
150  rawAllocType pAlloc;
151  rawFreeType pFree;
152  // granularity of pAlloc allocations. 0 means default used.
153  size_t granularity;
154  int version;
155  // all memory consumed at 1st pAlloc call and never returned,
156  // no more pAlloc calls after 1st
157  unsigned fixedPool : 1,
158  // memory consumed but returned only at pool termination
159  keepAllMemory : 1,
160  reserved : 30;
161 
162  MemPoolPolicy(rawAllocType pAlloc_, rawFreeType pFree_,
163  size_t granularity_ = 0, bool fixedPool_ = false,
164  bool keepAllMemory_ = false) :
165  pAlloc(pAlloc_), pFree(pFree_), granularity(granularity_), version(TBBMALLOC_POOL_VERSION),
166  fixedPool(fixedPool_), keepAllMemory(keepAllMemory_),
167  reserved(0) {}
168 };
169 
170 // enums have same values as appropriate enums from ScalableAllocationResult
171 // TODO: use ScalableAllocationResult in pool_create directly
172 enum MemPoolError {
173  // pool created successfully
174  POOL_OK = TBBMALLOC_OK,
175  // invalid policy parameters found
176  INVALID_POLICY = TBBMALLOC_INVALID_PARAM,
177  // requested pool policy is not supported by allocator library
178  UNSUPPORTED_POLICY = TBBMALLOC_UNSUPPORTED,
179  // lack of memory during pool creation
180  NO_MEMORY = TBBMALLOC_NO_MEMORY,
181  // action takes no effect
182  NO_EFFECT = TBBMALLOC_NO_EFFECT
183 };
184 
185 MemPoolError pool_create_v1(intptr_t pool_id, const MemPoolPolicy *policy,
186  rml::MemoryPool **pool);
187 
188 bool pool_destroy(MemoryPool* memPool);
189 void *pool_malloc(MemoryPool* memPool, size_t size);
190 void *pool_realloc(MemoryPool* memPool, void *object, size_t size);
191 void *pool_aligned_malloc(MemoryPool* mPool, size_t size, size_t alignment);
192 void *pool_aligned_realloc(MemoryPool* mPool, void *ptr, size_t size, size_t alignment);
193 bool pool_reset(MemoryPool* memPool);
194 bool pool_free(MemoryPool *memPool, void *object);
195 MemoryPool *pool_identify(void *object);
196 
197 } // namespace rml
198 
199 #include <new> /* To use new with the placement argument */
200 
201 /* Ensure that including this header does not cause implicit linkage with TBB */
202 #ifndef __TBB_NO_IMPLICIT_LINKAGE
203  #define __TBB_NO_IMPLICIT_LINKAGE 1
204  #include "tbb_stddef.h"
205  #undef __TBB_NO_IMPLICIT_LINKAGE
206 #else
207  #include "tbb_stddef.h"
208 #endif
209 
210 #if __TBB_ALLOCATOR_CONSTRUCT_VARIADIC
211  #include <utility> // std::forward
212 #endif
213 
214 namespace tbb {
215 
216 #if _MSC_VER && !defined(__INTEL_COMPILER)
217  // Workaround for erroneous "unreferenced parameter" warning in method destroy.
218  #pragma warning (push)
219  #pragma warning (disable: 4100)
220 #endif
221 
223 namespace internal {
224 
225 #if TBB_USE_EXCEPTIONS
226 // forward declaration is for inlining prevention
227 template<typename E> __TBB_NOINLINE( void throw_exception(const E &e) );
228 #endif
229 
230 // keep throw in a separate function to prevent code bloat
231 template<typename E>
232 void throw_exception(const E &e) {
233  __TBB_THROW(e);
234 }
235 
236 } // namespace internal
238 
240 
243 template<typename T>
244 class scalable_allocator {
245 public:
246  typedef typename internal::allocator_type<T>::value_type value_type;
247  typedef value_type* pointer;
248  typedef const value_type* const_pointer;
249  typedef value_type& reference;
250  typedef const value_type& const_reference;
251  typedef size_t size_type;
252  typedef ptrdiff_t difference_type;
253  template<class U> struct rebind {
254  typedef scalable_allocator<U> other;
255  };
256 
257  scalable_allocator() throw() {}
258  scalable_allocator( const scalable_allocator& ) throw() {}
259  template<typename U> scalable_allocator(const scalable_allocator<U>&) throw() {}
260 
261  pointer address(reference x) const {return &x;}
262  const_pointer address(const_reference x) const {return &x;}
263 
265  pointer allocate( size_type n, const void* /*hint*/ =0 ) {
266  pointer p = static_cast<pointer>( scalable_malloc( n * sizeof(value_type) ) );
267  if (!p)
268  internal::throw_exception(std::bad_alloc());
269  return p;
270  }
271 
273  void deallocate( pointer p, size_type ) {
274  scalable_free( p );
275  }
276 
278  size_type max_size() const throw() {
279  size_type absolutemax = static_cast<size_type>(-1) / sizeof (value_type);
280  return (absolutemax > 0 ? absolutemax : 1);
281  }
282 #if __TBB_ALLOCATOR_CONSTRUCT_VARIADIC
283  template<typename U, typename... Args>
284  void construct(U *p, Args&&... args)
285  { ::new((void *)p) U(std::forward<Args>(args)...); }
286 #else /* __TBB_ALLOCATOR_CONSTRUCT_VARIADIC */
287 #if __TBB_CPP11_RVALUE_REF_PRESENT
288  void construct( pointer p, value_type&& value ) { ::new((void*)(p)) value_type( std::move( value ) ); }
289 #endif
290  void construct( pointer p, const value_type& value ) {::new((void*)(p)) value_type(value);}
291 #endif /* __TBB_ALLOCATOR_CONSTRUCT_VARIADIC */
292  void destroy( pointer p ) {p->~value_type();}
293 };
294 
295 #if _MSC_VER && !defined(__INTEL_COMPILER)
296  #pragma warning (pop)
297 #endif /* warning 4100 is back */
298 
300 
301 template<>
302 class scalable_allocator<void> {
303 public:
304  typedef void* pointer;
305  typedef const void* const_pointer;
306  typedef void value_type;
307  template<class U> struct rebind {
308  typedef scalable_allocator<U> other;
309  };
310 };
311 
312 template<typename T, typename U>
313 inline bool operator==( const scalable_allocator<T>&, const scalable_allocator<U>& ) {return true;}
314 
315 template<typename T, typename U>
316 inline bool operator!=( const scalable_allocator<T>&, const scalable_allocator<U>& ) {return false;}
317 
318 } // namespace tbb
319 
320 #if _MSC_VER
321  #if (__TBB_BUILD || __TBBMALLOC_BUILD) && !defined(__TBBMALLOC_NO_IMPLICIT_LINKAGE)
322  #define __TBBMALLOC_NO_IMPLICIT_LINKAGE 1
323  #endif
324 
325  #if !__TBBMALLOC_NO_IMPLICIT_LINKAGE
326  #ifdef _DEBUG
327  #pragma comment(lib, "tbbmalloc_debug.lib")
328  #else
329  #pragma comment(lib, "tbbmalloc.lib")
330  #endif
331  #endif
332 
333 
334 #endif
335 
336 #endif /* __cplusplus */
337 
338 #if !defined(__cplusplus) && __ICC==1100
339  #pragma warning (pop)
340 #endif /* ICC 11.0 warning 991 is back */
341 
342 #endif /* __TBB_scalable_allocator_H */
int __TBB_EXPORTED_FUNC scalable_allocation_mode(int param, intptr_t value)
ScalableAllocationResult
ScalableAllocationCmd
#define __TBB_EXPORTED_FUNC
bool operator==(const cache_aligned_allocator< T > &, const cache_aligned_allocator< U > &)
void *__TBB_EXPORTED_FUNC scalable_malloc(size_t size)
#define __TBB_THROW(e)
Definition: tbb_stddef.h:289
void const char const char int ITT_FORMAT __itt_group_sync p
void *__TBB_EXPORTED_FUNC scalable_aligned_realloc(void *ptr, size_t size, size_t alignment)
int __TBB_EXPORTED_FUNC scalable_posix_memalign(void **memptr, size_t alignment, size_t size)
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t size
void *__TBB_EXPORTED_FUNC scalable_realloc(void *ptr, size_t size)
void __TBB_EXPORTED_FUNC scalable_aligned_free(void *ptr)
size_t __TBB_EXPORTED_FUNC scalable_msize(void *ptr)
void move(tbb_thread &t1, tbb_thread &t2)
Definition: tbb_thread.h:309
void *__TBB_EXPORTED_FUNC scalable_calloc(size_t nobj, size_t size)
int __TBB_EXPORTED_FUNC scalable_allocation_command(int cmd, void *param)
void __TBB_EXPORTED_FUNC scalable_free(void *ptr)
AllocationModeParam
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void * address
The graph class.
bool operator!=(const cache_aligned_allocator< T > &, const cache_aligned_allocator< U > &)
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t ITT_FORMAT d no args const wchar_t const wchar_t ITT_FORMAT s __itt_heap_function void size_t int ITT_FORMAT d __itt_heap_function void ITT_FORMAT p __itt_heap_function void void size_t int ITT_FORMAT d no args no args unsigned int ITT_FORMAT u const __itt_domain __itt_id ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain __itt_id ITT_FORMAT p const __itt_domain __itt_id __itt_timestamp __itt_timestamp ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain ITT_FORMAT p const __itt_domain __itt_string_handle unsigned long long ITT_FORMAT lu const __itt_domain __itt_id __itt_string_handle __itt_metadata_type size_t void ITT_FORMAT p const __itt_domain __itt_id __itt_string_handle const wchar_t size_t ITT_FORMAT lu const __itt_domain __itt_id __itt_relation __itt_id ITT_FORMAT p const wchar_t int ITT_FORMAT __itt_group_mark d int
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t ITT_FORMAT d no args const wchar_t const wchar_t ITT_FORMAT s __itt_heap_function void size_t int ITT_FORMAT d __itt_heap_function void ITT_FORMAT p __itt_heap_function void void size_t int ITT_FORMAT d no args no args unsigned int ITT_FORMAT u const __itt_domain __itt_id ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain __itt_id ITT_FORMAT p const __itt_domain __itt_id __itt_timestamp __itt_timestamp ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain ITT_FORMAT p const __itt_domain __itt_string_handle unsigned long long value
void *__TBB_EXPORTED_FUNC scalable_aligned_malloc(size_t size, size_t alignment)
void throw_exception(exception_id eid)
Versionless convenience wrapper for throw_exception_v4()
#define __TBB_NOINLINE(decl)
Definition: tbb_stddef.h:110

Copyright © 2005-2018 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.