Intel(R) Threading Building Blocks Doxygen Documentation  version 4.2.3
cache_aligned_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_cache_aligned_allocator_H
22 #define __TBB_cache_aligned_allocator_H
23 
24 #include <new>
25 #include "tbb_stddef.h"
26 #if __TBB_ALLOCATOR_CONSTRUCT_VARIADIC
27 #include <utility> // std::forward
28 #endif
29 
30 #if __TBB_CPP17_MEMORY_RESOURCE_PRESENT
31 #include <memory_resource>
32 #endif
33 
34 namespace tbb {
35 
37 namespace internal {
39 
41 
43 
44  void* __TBB_EXPORTED_FUNC NFS_Allocate( size_t n_element, size_t element_size, void* hint );
45 
47 
49  void __TBB_EXPORTED_FUNC NFS_Free( void* );
50 }
52 
53 #if _MSC_VER && !defined(__INTEL_COMPILER)
54  // Workaround for erroneous "unreferenced parameter" warning in method destroy.
55  #pragma warning (push)
56  #pragma warning (disable: 4100)
57 #endif
58 
60 
63 template<typename T>
65 public:
67  typedef value_type* pointer;
68  typedef const value_type* const_pointer;
70  typedef const value_type& const_reference;
71  typedef size_t size_type;
72  typedef ptrdiff_t difference_type;
73  template<typename U> struct rebind {
75  };
78  template<typename U> cache_aligned_allocator(const cache_aligned_allocator<U>&) throw() {}
79 
80  pointer address(reference x) const {return &x;}
81  const_pointer address(const_reference x) const {return &x;}
82 
84  pointer allocate( size_type n, const void* hint=0 ) {
85  // The "hint" argument is always ignored in NFS_Allocate thus const_cast shouldn't hurt
86  return pointer(internal::NFS_Allocate( n, sizeof(value_type), const_cast<void*>(hint) ));
87  }
88 
92  }
93 
95  size_type max_size() const throw() {
96  return (~size_t(0)-internal::NFS_MaxLineSize)/sizeof(value_type);
97  }
98 
100 #if __TBB_ALLOCATOR_CONSTRUCT_VARIADIC
101  template<typename U, typename... Args>
102  void construct(U *p, Args&&... args)
103  { ::new((void *)p) U(std::forward<Args>(args)...); }
104 #else // __TBB_ALLOCATOR_CONSTRUCT_VARIADIC
105 #if __TBB_CPP11_RVALUE_REF_PRESENT
106  void construct( pointer p, value_type&& value ) {::new((void*)(p)) value_type(std::move(value));}
107 #endif
108  void construct( pointer p, const value_type& value ) {::new((void*)(p)) value_type(value);}
109 #endif // __TBB_ALLOCATOR_CONSTRUCT_VARIADIC
110 
112  void destroy( pointer p ) {p->~value_type();}
113 };
114 
115 #if _MSC_VER && !defined(__INTEL_COMPILER)
116  #pragma warning (pop)
117 #endif // warning 4100 is back
118 
120 
121 template<>
123 public:
124  typedef void* pointer;
125  typedef const void* const_pointer;
126  typedef void value_type;
127  template<typename U> struct rebind {
129  };
130 };
131 
132 template<typename T, typename U>
133 inline bool operator==( const cache_aligned_allocator<T>&, const cache_aligned_allocator<U>& ) {return true;}
134 
135 template<typename T, typename U>
136 inline bool operator!=( const cache_aligned_allocator<T>&, const cache_aligned_allocator<U>& ) {return false;}
137 
138 #if __TBB_CPP17_MEMORY_RESOURCE_PRESENT
139 
141 class cache_aligned_resource : public std::pmr::memory_resource {
142 public:
143  cache_aligned_resource() : cache_aligned_resource(std::pmr::get_default_resource()) {}
144  explicit cache_aligned_resource(std::pmr::memory_resource* upstream) : m_upstream(upstream) {}
145 
146  std::pmr::memory_resource* upstream_resource() const {
147  return m_upstream;
148  }
149 
150 private:
152  void* do_allocate(size_t bytes, size_t alignment) override {
153  size_t cache_line_alignment = correct_alignment(alignment);
154  uintptr_t base = (uintptr_t)m_upstream->allocate(correct_size(bytes) + cache_line_alignment);
155  __TBB_ASSERT(base != 0, "Upstream resource returned NULL.");
156 #if _MSC_VER && !defined(__INTEL_COMPILER)
157  // unary minus operator applied to unsigned type, result still unsigned
158  #pragma warning(push)
159  #pragma warning(disable: 4146 4706)
160 #endif
161  // Round up to the next cache line (align the base address)
162  uintptr_t result = (base + cache_line_alignment) & -cache_line_alignment;
163 #if _MSC_VER && !defined(__INTEL_COMPILER)
164  #pragma warning(pop)
165 #endif
166  // Record where block actually starts.
167  ((uintptr_t*)result)[-1] = base;
168  return (void*)result;
169  }
170 
171  void do_deallocate(void* ptr, size_t bytes, size_t alignment) override {
172  if (ptr) {
173  // Recover where block actually starts
174  uintptr_t base = ((uintptr_t*)ptr)[-1];
175  m_upstream->deallocate((void*)base, correct_size(bytes) + correct_alignment(alignment));
176  }
177  }
178 
179  bool do_is_equal(const std::pmr::memory_resource& other) const noexcept override {
180  if (this == &other) { return true; }
181 #if __TBB_USE_OPTIONAL_RTTI
182  const cache_aligned_resource* other_res = dynamic_cast<const cache_aligned_resource*>(&other);
183  return other_res && (this->upstream_resource() == other_res->upstream_resource());
184 #else
185  return false;
186 #endif
187  }
188 
189  size_t correct_alignment(size_t alignment) {
190  __TBB_ASSERT(tbb::internal::is_power_of_two(alignment), "Alignemnt is not a power of 2");
191 #if __TBB_CPP17_HW_INTERFERENCE_SIZE_PRESENT
192  size_t cache_line_size = std::hardware_destructive_interference_size;
193 #else
195 #endif
196  return alignment < cache_line_size ? cache_line_size : alignment;
197  }
198 
199  size_t correct_size(size_t bytes) {
200  // To handle the case, when small size requested. There could be not
201  // enough space to store the original pointer.
202  return bytes < sizeof(uintptr_t) ? sizeof(uintptr_t) : bytes;
203  }
204 
205  std::pmr::memory_resource* m_upstream;
206 };
207 
208 #endif /* __TBB_CPP17_MEMORY_RESOURCE_PRESENT */
209 
210 } // namespace tbb
211 
212 #endif /* __TBB_cache_aligned_allocator_H */
213 
void *__TBB_EXPORTED_FUNC NFS_Allocate(size_t n_element, size_t element_size, void *hint)
Allocate memory on cache/sector line boundary.
bool operator==(const cache_aligned_allocator< T > &, const cache_aligned_allocator< U > &)
const_pointer address(const_reference x) const
size_t __TBB_EXPORTED_FUNC NFS_GetLineSize()
Cache/sector line size.
Meets "allocator" requirements of ISO C++ Standard, Section 20.1.5.
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:169
internal::allocator_type< T >::value_type value_type
pointer address(reference x) const
cache_aligned_allocator(const cache_aligned_allocator &)
bool is_power_of_two(integer_type arg)
A function to check if passed integer is a power of 2.
Definition: tbb_stddef.h:359
static const size_t cache_line_size
pointer allocate(size_type n, const void *hint=0)
Allocate space for n objects, starting on a cache/sector line.
void deallocate(pointer p, size_type)
Free block of memory that starts on a cache line.
void const char const char int ITT_FORMAT __itt_group_sync p
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 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 construct(U *p, Args &&... args)
Copy-construct value at location pointed to by p.
#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 > &)
const size_t NFS_MaxLineSize
Compile-time constant that is upper bound on cache line/sector size.
Definition: tbb_stddef.h:220
size_type max_size() const
Largest value for which method allocate might succeed.
void __TBB_EXPORTED_FUNC NFS_Free(void *)
Free memory allocated by NFS_Allocate.
void destroy(pointer p)
Destroy value at location pointed to by p.
cache_aligned_allocator(const cache_aligned_allocator< U > &)

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.