]> git.sesse.net Git - casparcg/blob - tbb30_20100406oss/include/tbb/cache_aligned_allocator.h
5889682d62917c037d84d02fb1133a790bd6c182
[casparcg] / tbb30_20100406oss / include / tbb / cache_aligned_allocator.h
1 /*
2     Copyright 2005-2010 Intel Corporation.  All Rights Reserved.
3
4     This file is part of Threading Building Blocks.
5
6     Threading Building Blocks is free software; you can redistribute it
7     and/or modify it under the terms of the GNU General Public License
8     version 2 as published by the Free Software Foundation.
9
10     Threading Building Blocks is distributed in the hope that it will be
11     useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12     of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with Threading Building Blocks; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
19     As a special exception, you may use this file as part of a free software
20     library without restriction.  Specifically, if other files instantiate
21     templates or use macros or inline functions from this file, or you compile
22     this file and link it with other files to produce an executable, this
23     file does not by itself cause the resulting executable to be covered by
24     the GNU General Public License.  This exception does not however
25     invalidate any other reasons why the executable file might be covered by
26     the GNU General Public License.
27 */
28
29 #ifndef __TBB_cache_aligned_allocator_H
30 #define __TBB_cache_aligned_allocator_H
31
32 #include <new>
33 #include "tbb_stddef.h"
34
35 namespace tbb {
36
37 //! @cond INTERNAL
38 namespace internal {
39     //! Cache/sector line size.
40     /** @ingroup memory_allocation */
41     size_t __TBB_EXPORTED_FUNC NFS_GetLineSize();
42
43     //! Allocate memory on cache/sector line boundary.
44     /** @ingroup memory_allocation */
45     void* __TBB_EXPORTED_FUNC NFS_Allocate( size_t n_element, size_t element_size, void* hint );
46
47     //! Free memory allocated by NFS_Allocate.
48     /** Freeing a NULL pointer is allowed, but has no effect.
49         @ingroup memory_allocation */
50     void __TBB_EXPORTED_FUNC NFS_Free( void* );
51 }
52 //! @endcond
53
54 #if _MSC_VER && !defined(__INTEL_COMPILER)
55     // Workaround for erroneous "unreferenced parameter" warning in method destroy.
56     #pragma warning (push)
57     #pragma warning (disable: 4100)
58 #endif
59
60 //! Meets "allocator" requirements of ISO C++ Standard, Section 20.1.5
61 /** The members are ordered the same way they are in section 20.4.1
62     of the ISO C++ standard.
63     @ingroup memory_allocation */
64 template<typename T>
65 class cache_aligned_allocator {
66 public:
67     typedef typename internal::allocator_type<T>::value_type value_type;
68     typedef value_type* pointer;
69     typedef const value_type* const_pointer;
70     typedef value_type& reference;
71     typedef const value_type& const_reference;
72     typedef size_t size_type;
73     typedef ptrdiff_t difference_type;
74     template<typename U> struct rebind {
75         typedef cache_aligned_allocator<U> other;
76     };
77
78     cache_aligned_allocator() throw() {}
79     cache_aligned_allocator( const cache_aligned_allocator& ) throw() {}
80     template<typename U> cache_aligned_allocator(const cache_aligned_allocator<U>&) throw() {}
81
82     pointer address(reference x) const {return &x;}
83     const_pointer address(const_reference x) const {return &x;}
84     
85     //! Allocate space for n objects, starting on a cache/sector line.
86     pointer allocate( size_type n, const void* hint=0 ) {
87         // The "hint" argument is always ignored in NFS_Allocate thus const_cast shouldn't hurt
88         return pointer(internal::NFS_Allocate( n, sizeof(value_type), const_cast<void*>(hint) ));
89     }
90
91     //! Free block of memory that starts on a cache line
92     void deallocate( pointer p, size_type ) {
93         internal::NFS_Free(p);
94     }
95
96     //! Largest value for which method allocate might succeed.
97     size_type max_size() const throw() {
98         return (~size_t(0)-internal::NFS_MaxLineSize)/sizeof(value_type);
99     }
100
101     //! Copy-construct value at location pointed to by p.
102     void construct( pointer p, const value_type& value ) {::new((void*)(p)) value_type(value);}
103
104     //! Destroy value at location pointed to by p.
105     void destroy( pointer p ) {p->~value_type();}
106 };
107
108 #if _MSC_VER && !defined(__INTEL_COMPILER)
109     #pragma warning (pop)
110 #endif // warning 4100 is back
111
112 //! Analogous to std::allocator<void>, as defined in ISO C++ Standard, Section 20.4.1
113 /** @ingroup memory_allocation */
114 template<> 
115 class cache_aligned_allocator<void> {
116 public:
117     typedef void* pointer;
118     typedef const void* const_pointer;
119     typedef void value_type;
120     template<typename U> struct rebind {
121         typedef cache_aligned_allocator<U> other;
122     };
123 };
124
125 template<typename T, typename U>
126 inline bool operator==( const cache_aligned_allocator<T>&, const cache_aligned_allocator<U>& ) {return true;}
127
128 template<typename T, typename U>
129 inline bool operator!=( const cache_aligned_allocator<T>&, const cache_aligned_allocator<U>& ) {return false;}
130
131 } // namespace tbb
132
133 #endif /* __TBB_cache_aligned_allocator_H */