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-2019 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 size_t pool_msize(MemoryPool *memPool, void *object);
197 
198 } // namespace rml
199 
200 #include <new> /* To use new with the placement argument */
201 
202 /* Ensure that including this header does not cause implicit linkage with TBB */
203 #ifndef __TBB_NO_IMPLICIT_LINKAGE
204  #define __TBB_NO_IMPLICIT_LINKAGE 1
205  #include "tbb_stddef.h"
206  #undef __TBB_NO_IMPLICIT_LINKAGE
207 #else
208  #include "tbb_stddef.h"
209 #endif
210 
211 #if __TBB_ALLOCATOR_CONSTRUCT_VARIADIC
212 #include <utility> // std::forward
213 #endif
214 
215 #if __TBB_CPP17_MEMORY_RESOURCE_PRESENT
216 #include <memory_resource>
217 #endif
218 
219 namespace tbb {
220 
221 #if _MSC_VER && !defined(__INTEL_COMPILER)
222  // Workaround for erroneous "unreferenced parameter" warning in method destroy.
223  #pragma warning (push)
224  #pragma warning (disable: 4100)
225 #endif
226 
228 namespace internal {
229 
230 #if TBB_USE_EXCEPTIONS
231 // forward declaration is for inlining prevention
232 template<typename E> __TBB_NOINLINE( void throw_exception(const E &e) );
233 #endif
234 
235 // keep throw in a separate function to prevent code bloat
236 template<typename E>
237 void throw_exception(const E &e) {
238  __TBB_THROW(e);
239 }
240 
241 } // namespace internal
243 
245 
248 template<typename T>
249 class scalable_allocator {
250 public:
251  typedef typename internal::allocator_type<T>::value_type value_type;
252  typedef value_type* pointer;
253  typedef const value_type* const_pointer;
254  typedef value_type& reference;
255  typedef const value_type& const_reference;
256  typedef size_t size_type;
257  typedef ptrdiff_t difference_type;
258  template<class U> struct rebind {
259  typedef scalable_allocator<U> other;
260  };
261 
262  scalable_allocator() throw() {}
263  scalable_allocator( const scalable_allocator& ) throw() {}
264  template<typename U> scalable_allocator(const scalable_allocator<U>&) throw() {}
265 
266  pointer address(reference x) const {return &x;}
267  const_pointer address(const_reference x) const {return &x;}
268 
270  pointer allocate( size_type n, const void* /*hint*/ =0 ) {
271  pointer p = static_cast<pointer>( scalable_malloc( n * sizeof(value_type) ) );
272  if (!p)
273  internal::throw_exception(std::bad_alloc());
274  return p;
275  }
276 
278  void deallocate( pointer p, size_type ) {
279  scalable_free( p );
280  }
281 
283  size_type max_size() const throw() {
284  size_type absolutemax = static_cast<size_type>(-1) / sizeof (value_type);
285  return (absolutemax > 0 ? absolutemax : 1);
286  }
287 #if __TBB_ALLOCATOR_CONSTRUCT_VARIADIC
288  template<typename U, typename... Args>
289  void construct(U *p, Args&&... args)
290  { ::new((void *)p) U(std::forward<Args>(args)...); }
291 #else /* __TBB_ALLOCATOR_CONSTRUCT_VARIADIC */
292 #if __TBB_CPP11_RVALUE_REF_PRESENT
293  void construct( pointer p, value_type&& value ) { ::new((void*)(p)) value_type( std::move( value ) ); }
294 #endif
295  void construct( pointer p, const value_type& value ) {::new((void*)(p)) value_type(value);}
296 #endif /* __TBB_ALLOCATOR_CONSTRUCT_VARIADIC */
297  void destroy( pointer p ) {p->~value_type();}
298 };
299 
300 #if _MSC_VER && !defined(__INTEL_COMPILER)
301  #pragma warning (pop)
302 #endif /* warning 4100 is back */
303 
305 
306 template<>
307 class scalable_allocator<void> {
308 public:
309  typedef void* pointer;
310  typedef const void* const_pointer;
311  typedef void value_type;
312  template<class U> struct rebind {
313  typedef scalable_allocator<U> other;
314  };
315 };
316 
317 template<typename T, typename U>
318 inline bool operator==( const scalable_allocator<T>&, const scalable_allocator<U>& ) {return true;}
319 
320 template<typename T, typename U>
321 inline bool operator!=( const scalable_allocator<T>&, const scalable_allocator<U>& ) {return false;}
322 
323 #if __TBB_CPP17_MEMORY_RESOURCE_PRESENT
324 
325 namespace internal {
326 
329 class scalable_resource_impl : public std::pmr::memory_resource {
330 private:
331  void* do_allocate(size_t bytes, size_t alignment) override {
332  void* ptr = scalable_aligned_malloc( bytes, alignment );
333  if (!ptr) {
334  throw_exception(std::bad_alloc());
335  }
336  return ptr;
337  }
338 
339  void do_deallocate(void* ptr, size_t /*bytes*/, size_t /*alignment*/) override {
340  scalable_free(ptr);
341  }
342 
345  bool do_is_equal(const std::pmr::memory_resource& other) const noexcept override {
346  return this == &other ||
347 #if __TBB_USE_OPTIONAL_RTTI
348  dynamic_cast<const scalable_resource_impl*>(&other) != NULL;
349 #else
350  false;
351 #endif
352  }
353 };
354 
355 } // namespace internal
356 
358 inline std::pmr::memory_resource* scalable_memory_resource() noexcept {
359  static tbb::internal::scalable_resource_impl scalable_res;
360  return &scalable_res;
361 }
362 
363 #endif /* __TBB_CPP17_MEMORY_RESOURCE_PRESENT */
364 
365 } // namespace tbb
366 
367 #if _MSC_VER
368  #if (__TBB_BUILD || __TBBMALLOC_BUILD) && !defined(__TBBMALLOC_NO_IMPLICIT_LINKAGE)
369  #define __TBBMALLOC_NO_IMPLICIT_LINKAGE 1
370  #endif
371 
372  #if !__TBBMALLOC_NO_IMPLICIT_LINKAGE
373  #ifdef _DEBUG
374  #pragma comment(lib, "tbbmalloc_debug.lib")
375  #else
376  #pragma comment(lib, "tbbmalloc.lib")
377  #endif
378  #endif
379 
380 
381 #endif
382 
383 #endif /* __cplusplus */
384 
385 #if !defined(__cplusplus) && __ICC==1100
386  #pragma warning (pop)
387 #endif /* ICC 11.0 warning 991 is back */
388 
389 #endif /* __TBB_scalable_allocator_H */
bool operator==(const cache_aligned_allocator< T > &, const cache_aligned_allocator< U > &)
AllocationModeParam
#define __TBB_THROW(e)
Definition: tbb_stddef.h:289
void *__TBB_EXPORTED_FUNC scalable_malloc(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 * address
int __TBB_EXPORTED_FUNC scalable_posix_memalign(void **memptr, size_t alignment, size_t size)
ScalableAllocationResult
void const char const char int ITT_FORMAT __itt_group_sync p
void __TBB_EXPORTED_FUNC scalable_aligned_free(void *ptr)
void throw_exception(exception_id eid)
Versionless convenience wrapper for throw_exception_v4()
int __TBB_EXPORTED_FUNC scalable_allocation_command(int cmd, void *param)
void *__TBB_EXPORTED_FUNC scalable_aligned_malloc(size_t size, size_t alignment)
int __TBB_EXPORTED_FUNC scalable_allocation_mode(int param, intptr_t value)
void *__TBB_EXPORTED_FUNC scalable_calloc(size_t nobj, size_t size)
ScalableAllocationCmd
The graph class.
size_t __TBB_EXPORTED_FUNC scalable_msize(void *ptr)
void *__TBB_EXPORTED_FUNC scalable_aligned_realloc(void *ptr, size_t size, size_t alignment)
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_realloc(void *ptr, size_t size)
void __TBB_EXPORTED_FUNC scalable_free(void *ptr)
#define __TBB_EXPORTED_FUNC
void move(tbb_thread &t1, tbb_thread &t2)
Definition: tbb_thread.h:309
bool operator!=(const cache_aligned_allocator< T > &, const cache_aligned_allocator< U > &)
#define __TBB_NOINLINE(decl)
Definition: tbb_stddef.h:110
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 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

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