BRE12
cache_aligned_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_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 namespace tbb {
31 
33 namespace internal {
35 
36  size_t __TBB_EXPORTED_FUNC NFS_GetLineSize();
37 
39 
40  void* __TBB_EXPORTED_FUNC NFS_Allocate( size_t n_element, size_t element_size, void* hint );
41 
43 
45  void __TBB_EXPORTED_FUNC NFS_Free( void* );
46 }
48 
49 #if _MSC_VER && !defined(__INTEL_COMPILER)
50  // Workaround for erroneous "unreferenced parameter" warning in method destroy.
51  #pragma warning (push)
52  #pragma warning (disable: 4100)
53 #endif
54 
56 
59 template<typename T>
61 public:
62  typedef typename internal::allocator_type<T>::value_type value_type;
63  typedef value_type* pointer;
64  typedef const value_type* const_pointer;
65  typedef value_type& reference;
66  typedef const value_type& const_reference;
67  typedef size_t size_type;
68  typedef ptrdiff_t difference_type;
69  template<typename U> struct rebind {
71  };
72 
73  cache_aligned_allocator() throw() {}
75  template<typename U> cache_aligned_allocator(const cache_aligned_allocator<U>&) throw() {}
76 
77  pointer address(reference x) const {return &x;}
78  const_pointer address(const_reference x) const {return &x;}
79 
81  pointer allocate( size_type n, const void* hint=0 ) {
82  // The "hint" argument is always ignored in NFS_Allocate thus const_cast shouldn't hurt
83  return pointer(internal::NFS_Allocate( n, sizeof(value_type), const_cast<void*>(hint) ));
84  }
85 
87  void deallocate( pointer p, size_type ) {
88  internal::NFS_Free(p);
89  }
90 
92  size_type max_size() const throw() {
93  return (~size_t(0)-internal::NFS_MaxLineSize)/sizeof(value_type);
94  }
95 
97 #if __TBB_ALLOCATOR_CONSTRUCT_VARIADIC
98  template<typename U, typename... Args>
99  void construct(U *p, Args&&... args)
100  { ::new((void *)p) U(std::forward<Args>(args)...); }
101 #else // __TBB_ALLOCATOR_CONSTRUCT_VARIADIC
102 #if __TBB_CPP11_RVALUE_REF_PRESENT
103  void construct( pointer p, value_type&& value ) {::new((void*)(p)) value_type(std::move(value));}
104 #endif
105  void construct( pointer p, const value_type& value ) {::new((void*)(p)) value_type(value);}
106 #endif // __TBB_ALLOCATOR_CONSTRUCT_VARIADIC
107 
109  void destroy( pointer p ) {p->~value_type();}
110 };
111 
112 #if _MSC_VER && !defined(__INTEL_COMPILER)
113  #pragma warning (pop)
114 #endif // warning 4100 is back
115 
117 
118 template<>
120 public:
121  typedef void* pointer;
122  typedef const void* const_pointer;
123  typedef void value_type;
124  template<typename U> struct rebind {
126  };
127 };
128 
129 template<typename T, typename U>
130 inline bool operator==( const cache_aligned_allocator<T>&, const cache_aligned_allocator<U>& ) {return true;}
131 
132 template<typename T, typename U>
133 inline bool operator!=( const cache_aligned_allocator<T>&, const cache_aligned_allocator<U>& ) {return false;}
134 
135 } // namespace tbb
136 
137 #endif /* __TBB_cache_aligned_allocator_H */
size_type max_size() const
Largest value for which method allocate might succeed.
Definition: cache_aligned_allocator.h:92
pointer allocate(size_type n, const void *hint=0)
Allocate space for n objects, starting on a cache/sector line.
Definition: cache_aligned_allocator.h:81
Meets "allocator" requirements of ISO C++ Standard, Section 20.1.5.
Definition: cache_aligned_allocator.h:60
Definition: _flow_graph_async_msg_impl.h:32
void deallocate(pointer p, size_type)
Free block of memory that starts on a cache line.
Definition: cache_aligned_allocator.h:87
The namespace tbb contains all components of the library.
Definition: parallel_for.h:44
void construct(pointer p, const value_type &value)
Copy-construct value at location pointed to by p.
Definition: cache_aligned_allocator.h:105
Definition: cache_aligned_allocator.h:69
void destroy(pointer p)
Destroy value at location pointed to by p.
Definition: cache_aligned_allocator.h:109