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 #ifndef __TBB_scalable_allocator_H
18 #define __TBB_scalable_allocator_H
19 
21 #include <stddef.h> /* Need ptrdiff_t and size_t from here. */
22 #if !_MSC_VER
23 #include <stdint.h> /* Need intptr_t from here. */
24 #endif
25 
26 #if !defined(__cplusplus) && __ICC==1100
27  #pragma warning (push)
28  #pragma warning (disable: 991)
29 #endif
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif /* __cplusplus */
34 
35 #if _MSC_VER >= 1400
36 #define __TBB_EXPORTED_FUNC __cdecl
37 #else
38 #define __TBB_EXPORTED_FUNC
39 #endif
40 
44 
47 void __TBB_EXPORTED_FUNC scalable_free (void* ptr);
48 
51 void * __TBB_EXPORTED_FUNC scalable_realloc (void* ptr, size_t size);
52 
55 void * __TBB_EXPORTED_FUNC scalable_calloc (size_t nobj, size_t size);
56 
59 int __TBB_EXPORTED_FUNC scalable_posix_memalign (void** memptr, size_t alignment, size_t size);
60 
63 void * __TBB_EXPORTED_FUNC scalable_aligned_malloc (size_t size, size_t alignment);
64 
67 void * __TBB_EXPORTED_FUNC scalable_aligned_realloc (void* ptr, size_t size, size_t alignment);
68 
72 
77 size_t __TBB_EXPORTED_FUNC scalable_msize (void* ptr);
78 
79 /* Results for scalable_allocation_* functions */
80 typedef enum {
87 
88 /* Setting TBB_MALLOC_USE_HUGE_PAGES environment variable to 1 enables huge pages.
89  scalable_allocation_mode call has priority over environment variable. */
90 typedef enum {
91  TBBMALLOC_USE_HUGE_PAGES, /* value turns using huge pages on and off */
92  /* deprecated, kept for backward compatibility only */
94  /* try to limit memory consumption value Bytes, clean internal buffers
95  if limit is exceeded, but not prevents from requesting memory from OS */
98 
101 int __TBB_EXPORTED_FUNC scalable_allocation_mode(int param, intptr_t value);
102 
103 typedef enum {
104  /* Clean internal allocator buffers for all threads.
105  Returns TBBMALLOC_NO_EFFECT if no buffers cleaned,
106  TBBMALLOC_OK if some memory released from buffers. */
108  /* Clean internal allocator buffer for current thread only.
109  Return values same as for TBBMALLOC_CLEAN_ALL_BUFFERS. */
112 
115 int __TBB_EXPORTED_FUNC scalable_allocation_command(int cmd, void *param);
116 
117 #ifdef __cplusplus
118 } /* extern "C" */
119 #endif /* __cplusplus */
120 
121 #ifdef __cplusplus
122 
124 namespace rml {
125 class MemoryPool;
126 
127 typedef void *(*rawAllocType)(intptr_t pool_id, size_t &bytes);
128 // returns non-zero in case of error
129 typedef int (*rawFreeType)(intptr_t pool_id, void* raw_ptr, size_t raw_bytes);
130 
131 /*
132 MemPoolPolicy extension must be compatible with such structure fields layout
133 
134 struct MemPoolPolicy {
135  rawAllocType pAlloc;
136  rawFreeType pFree;
137  size_t granularity; // granularity of pAlloc allocations
138 };
139 */
140 
141 struct MemPoolPolicy {
142  enum {
143  TBBMALLOC_POOL_VERSION = 1
144  };
145 
146  rawAllocType pAlloc;
147  rawFreeType pFree;
148  // granularity of pAlloc allocations. 0 means default used.
149  size_t granularity;
150  int version;
151  // all memory consumed at 1st pAlloc call and never returned,
152  // no more pAlloc calls after 1st
153  unsigned fixedPool : 1,
154  // memory consumed but returned only at pool termination
155  keepAllMemory : 1,
156  reserved : 30;
157 
158  MemPoolPolicy(rawAllocType pAlloc_, rawFreeType pFree_,
159  size_t granularity_ = 0, bool fixedPool_ = false,
160  bool keepAllMemory_ = false) :
161  pAlloc(pAlloc_), pFree(pFree_), granularity(granularity_), version(TBBMALLOC_POOL_VERSION),
162  fixedPool(fixedPool_), keepAllMemory(keepAllMemory_),
163  reserved(0) {}
164 };
165 
166 // enums have same values as appropriate enums from ScalableAllocationResult
167 // TODO: use ScalableAllocationResult in pool_create directly
168 enum MemPoolError {
169  // pool created successfully
170  POOL_OK = TBBMALLOC_OK,
171  // invalid policy parameters found
172  INVALID_POLICY = TBBMALLOC_INVALID_PARAM,
173  // requested pool policy is not supported by allocator library
174  UNSUPPORTED_POLICY = TBBMALLOC_UNSUPPORTED,
175  // lack of memory during pool creation
176  NO_MEMORY = TBBMALLOC_NO_MEMORY,
177  // action takes no effect
178  NO_EFFECT = TBBMALLOC_NO_EFFECT
179 };
180 
181 MemPoolError pool_create_v1(intptr_t pool_id, const MemPoolPolicy *policy,
182  rml::MemoryPool **pool);
183 
184 bool pool_destroy(MemoryPool* memPool);
185 void *pool_malloc(MemoryPool* memPool, size_t size);
186 void *pool_realloc(MemoryPool* memPool, void *object, size_t size);
187 void *pool_aligned_malloc(MemoryPool* mPool, size_t size, size_t alignment);
188 void *pool_aligned_realloc(MemoryPool* mPool, void *ptr, size_t size, size_t alignment);
189 bool pool_reset(MemoryPool* memPool);
190 bool pool_free(MemoryPool *memPool, void *object);
191 MemoryPool *pool_identify(void *object);
192 size_t pool_msize(MemoryPool *memPool, void *object);
193 
194 } // namespace rml
195 
196 #include <new> /* To use new with the placement argument */
197 
198 /* Ensure that including this header does not cause implicit linkage with TBB */
199 #ifndef __TBB_NO_IMPLICIT_LINKAGE
200  #define __TBB_NO_IMPLICIT_LINKAGE 1
201  #include "tbb_stddef.h"
202  #undef __TBB_NO_IMPLICIT_LINKAGE
203 #else
204  #include "tbb_stddef.h"
205 #endif
206 
207 #if __TBB_ALLOCATOR_CONSTRUCT_VARIADIC
208 #include <utility> // std::forward
209 #endif
210 
211 #if __TBB_CPP17_MEMORY_RESOURCE_PRESENT
212 #include <memory_resource>
213 #endif
214 
215 namespace tbb {
216 
217 #if _MSC_VER && !defined(__INTEL_COMPILER)
218  // Workaround for erroneous "unreferenced parameter" warning in method destroy.
219  #pragma warning (push)
220  #pragma warning (disable: 4100)
221 #endif
222 
224 namespace internal {
225 
226 #if TBB_USE_EXCEPTIONS
227 // forward declaration is for inlining prevention
228 template<typename E> __TBB_NOINLINE( void throw_exception(const E &e) );
229 #endif
230 
231 // keep throw in a separate function to prevent code bloat
232 template<typename E>
233 void throw_exception(const E &e) {
234  __TBB_THROW(e);
235 }
236 
237 } // namespace internal
239 
241 
244 template<typename T>
245 class scalable_allocator {
246 public:
247  typedef typename internal::allocator_type<T>::value_type value_type;
248  typedef value_type* pointer;
249  typedef const value_type* const_pointer;
250  typedef value_type& reference;
251  typedef const value_type& const_reference;
252  typedef size_t size_type;
253  typedef ptrdiff_t difference_type;
254  template<class U> struct rebind {
255  typedef scalable_allocator<U> other;
256  };
257 
258  scalable_allocator() throw() {}
259  scalable_allocator( const scalable_allocator& ) throw() {}
260  template<typename U> scalable_allocator(const scalable_allocator<U>&) throw() {}
261 
262  pointer address(reference x) const {return &x;}
263  const_pointer address(const_reference x) const {return &x;}
264 
266  pointer allocate( size_type n, const void* /*hint*/ =0 ) {
267  pointer p = static_cast<pointer>( scalable_malloc( n * sizeof(value_type) ) );
268  if (!p)
269  internal::throw_exception(std::bad_alloc());
270  return p;
271  }
272 
274  void deallocate( pointer p, size_type ) {
275  scalable_free( p );
276  }
277 
279  size_type max_size() const throw() {
280  size_type absolutemax = static_cast<size_type>(-1) / sizeof (value_type);
281  return (absolutemax > 0 ? absolutemax : 1);
282  }
283 #if __TBB_ALLOCATOR_CONSTRUCT_VARIADIC
284  template<typename U, typename... Args>
285  void construct(U *p, Args&&... args)
286  { ::new((void *)p) U(std::forward<Args>(args)...); }
287 #else /* __TBB_ALLOCATOR_CONSTRUCT_VARIADIC */
288 #if __TBB_CPP11_RVALUE_REF_PRESENT
289  void construct( pointer p, value_type&& value ) { ::new((void*)(p)) value_type( std::move( value ) ); }
290 #endif
291  void construct( pointer p, const value_type& value ) {::new((void*)(p)) value_type(value);}
292 #endif /* __TBB_ALLOCATOR_CONSTRUCT_VARIADIC */
293  void destroy( pointer p ) {p->~value_type();}
294 };
295 
296 #if _MSC_VER && !defined(__INTEL_COMPILER)
297  #pragma warning (pop)
298 #endif /* warning 4100 is back */
299 
301 
302 template<>
303 class scalable_allocator<void> {
304 public:
305  typedef void* pointer;
306  typedef const void* const_pointer;
307  typedef void value_type;
308  template<class U> struct rebind {
309  typedef scalable_allocator<U> other;
310  };
311 };
312 
313 template<typename T, typename U>
314 inline bool operator==( const scalable_allocator<T>&, const scalable_allocator<U>& ) {return true;}
315 
316 template<typename T, typename U>
317 inline bool operator!=( const scalable_allocator<T>&, const scalable_allocator<U>& ) {return false;}
318 
319 #if __TBB_CPP17_MEMORY_RESOURCE_PRESENT
320 
321 namespace internal {
322 
325 class scalable_resource_impl : public std::pmr::memory_resource {
326 private:
327  void* do_allocate(size_t bytes, size_t alignment) override {
328  void* ptr = scalable_aligned_malloc( bytes, alignment );
329  if (!ptr) {
330  throw_exception(std::bad_alloc());
331  }
332  return ptr;
333  }
334 
335  void do_deallocate(void* ptr, size_t /*bytes*/, size_t /*alignment*/) override {
336  scalable_free(ptr);
337  }
338 
341  bool do_is_equal(const std::pmr::memory_resource& other) const noexcept override {
342  return this == &other ||
343 #if __TBB_USE_OPTIONAL_RTTI
344  dynamic_cast<const scalable_resource_impl*>(&other) != NULL;
345 #else
346  false;
347 #endif
348  }
349 };
350 
351 } // namespace internal
352 
354 inline std::pmr::memory_resource* scalable_memory_resource() noexcept {
355  static tbb::internal::scalable_resource_impl scalable_res;
356  return &scalable_res;
357 }
358 
359 #endif /* __TBB_CPP17_MEMORY_RESOURCE_PRESENT */
360 
361 } // namespace tbb
362 
363 #if _MSC_VER
364  #if (__TBB_BUILD || __TBBMALLOC_BUILD) && !defined(__TBBMALLOC_NO_IMPLICIT_LINKAGE)
365  #define __TBBMALLOC_NO_IMPLICIT_LINKAGE 1
366  #endif
367 
368  #if !__TBBMALLOC_NO_IMPLICIT_LINKAGE
369  #ifdef _DEBUG
370  #pragma comment(lib, "tbbmalloc_debug.lib")
371  #else
372  #pragma comment(lib, "tbbmalloc.lib")
373  #endif
374  #endif
375 
376 
377 #endif
378 
379 #endif /* __cplusplus */
380 
381 #if !defined(__cplusplus) && __ICC==1100
382  #pragma warning (pop)
383 #endif /* ICC 11.0 warning 991 is back */
384 
385 #endif /* __TBB_scalable_allocator_H */
void *__TBB_EXPORTED_FUNC scalable_aligned_malloc(size_t size, size_t alignment)
size_t __TBB_EXPORTED_FUNC scalable_msize(void *ptr)
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_calloc(size_t nobj, size_t size)
ScalableAllocationResult
ScalableAllocationCmd
#define __TBB_EXPORTED_FUNC
void move(tbb_thread &t1, tbb_thread &t2)
Definition: tbb_thread.h:305
bool operator!=(const cache_aligned_allocator< T > &, const cache_aligned_allocator< U > &)
void *__TBB_EXPORTED_FUNC scalable_aligned_realloc(void *ptr, size_t size, size_t alignment)
void __TBB_EXPORTED_FUNC scalable_free(void *ptr)
void *__TBB_EXPORTED_FUNC scalable_realloc(void *ptr, size_t size)
#define __TBB_THROW(e)
Definition: tbb_stddef.h:285
The graph class.
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
AllocationModeParam
bool operator==(const cache_aligned_allocator< T > &, const cache_aligned_allocator< U > &)
void throw_exception(exception_id eid)
Versionless convenience wrapper for throw_exception_v4()
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
void *__TBB_EXPORTED_FUNC scalable_malloc(size_t size)
void const char const char int ITT_FORMAT __itt_group_sync p
int __TBB_EXPORTED_FUNC scalable_posix_memalign(void **memptr, size_t alignment, size_t size)
int __TBB_EXPORTED_FUNC scalable_allocation_mode(int param, intptr_t value)
void __TBB_EXPORTED_FUNC scalable_aligned_free(void *ptr)
#define __TBB_NOINLINE(decl)
Definition: tbb_stddef.h:106
int __TBB_EXPORTED_FUNC scalable_allocation_command(int cmd, void *param)

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.