]> git.sesse.net Git - casparcg/blob - tbb30_20100406oss/include/tbb/scalable_allocator.h
2293803a7299be6b617e1fef829580b298648da3
[casparcg] / tbb30_20100406oss / include / tbb / scalable_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_scalable_allocator_H
30 #define __TBB_scalable_allocator_H
31 /** @file */
32
33 #include <stddef.h> /* Need ptrdiff_t and size_t from here. */
34
35 #if !defined(__cplusplus) && __ICC==1100
36     #pragma warning (push)
37     #pragma warning (disable: 991)
38 #endif
39
40 #ifdef __cplusplus
41 extern "C" {
42 #endif /* __cplusplus */
43
44 #if _MSC_VER >= 1400
45 #define __TBB_EXPORTED_FUNC   __cdecl
46 #else
47 #define __TBB_EXPORTED_FUNC
48 #endif
49
50 /** The "malloc" analogue to allocate block of memory of size bytes.
51   * @ingroup memory_allocation */
52 void * __TBB_EXPORTED_FUNC scalable_malloc (size_t size);
53
54 /** The "free" analogue to discard a previously allocated piece of memory.
55     @ingroup memory_allocation */
56 void   __TBB_EXPORTED_FUNC scalable_free (void* ptr);
57
58 /** The "realloc" analogue complementing scalable_malloc.
59     @ingroup memory_allocation */
60 void * __TBB_EXPORTED_FUNC scalable_realloc (void* ptr, size_t size);
61
62 /** The "calloc" analogue complementing scalable_malloc.
63     @ingroup memory_allocation */
64 void * __TBB_EXPORTED_FUNC scalable_calloc (size_t nobj, size_t size);
65
66 /** The "posix_memalign" analogue.
67     @ingroup memory_allocation */
68 int __TBB_EXPORTED_FUNC scalable_posix_memalign (void** memptr, size_t alignment, size_t size);
69
70 /** The "_aligned_malloc" analogue.
71     @ingroup memory_allocation */
72 void * __TBB_EXPORTED_FUNC scalable_aligned_malloc (size_t size, size_t alignment);
73
74 /** The "_aligned_realloc" analogue.
75     @ingroup memory_allocation */
76 void * __TBB_EXPORTED_FUNC scalable_aligned_realloc (void* ptr, size_t size, size_t alignment);
77
78 /** The "_aligned_free" analogue.
79     @ingroup memory_allocation */
80 void __TBB_EXPORTED_FUNC scalable_aligned_free (void* ptr);
81
82 /** The analogue of _msize/malloc_size/malloc_usable_size.
83     Returns the usable size of a memory block previously allocated by scalable_*,
84     or 0 (zero) if ptr does not point to such a block.
85     @ingroup memory_allocation */
86 size_t __TBB_EXPORTED_FUNC scalable_msize (void* ptr);
87
88 #ifdef __cplusplus
89 } /* extern "C" */
90 #endif /* __cplusplus */
91
92 #ifdef __cplusplus
93
94 #include <new>      /* To use new with the placement argument */
95
96 /* Ensure that including this header does not cause implicit linkage with TBB */
97 #ifndef __TBB_NO_IMPLICIT_LINKAGE
98     #define __TBB_NO_IMPLICIT_LINKAGE 1
99     #include "tbb_stddef.h"
100     #undef  __TBB_NO_IMPLICIT_LINKAGE
101 #else
102     #include "tbb_stddef.h"
103 #endif
104
105
106 namespace tbb {
107
108 #if _MSC_VER && !defined(__INTEL_COMPILER)
109     // Workaround for erroneous "unreferenced parameter" warning in method destroy.
110     #pragma warning (push)
111     #pragma warning (disable: 4100)
112 #endif
113
114 //! Meets "allocator" requirements of ISO C++ Standard, Section 20.1.5
115 /** The members are ordered the same way they are in section 20.4.1
116     of the ISO C++ standard.
117     @ingroup memory_allocation */
118 template<typename T>
119 class scalable_allocator {
120 public:
121     typedef typename internal::allocator_type<T>::value_type value_type;
122     typedef value_type* pointer;
123     typedef const value_type* const_pointer;
124     typedef value_type& reference;
125     typedef const value_type& const_reference;
126     typedef size_t size_type;
127     typedef ptrdiff_t difference_type;
128     template<class U> struct rebind {
129         typedef scalable_allocator<U> other;
130     };
131
132     scalable_allocator() throw() {}
133     scalable_allocator( const scalable_allocator& ) throw() {}
134     template<typename U> scalable_allocator(const scalable_allocator<U>&) throw() {}
135
136     pointer address(reference x) const {return &x;}
137     const_pointer address(const_reference x) const {return &x;}
138
139     //! Allocate space for n objects.
140     pointer allocate( size_type n, const void* /*hint*/ =0 ) {
141         return static_cast<pointer>( scalable_malloc( n * sizeof(value_type) ) );
142     }
143
144     //! Free previously allocated block of memory
145     void deallocate( pointer p, size_type ) {
146         scalable_free( p );
147     }
148
149     //! Largest value for which method allocate might succeed.
150     size_type max_size() const throw() {
151         size_type absolutemax = static_cast<size_type>(-1) / sizeof (value_type);
152         return (absolutemax > 0 ? absolutemax : 1);
153     }
154     void construct( pointer p, const value_type& value ) {::new((void*)(p)) value_type(value);}
155     void destroy( pointer p ) {p->~value_type();}
156 };
157
158 #if _MSC_VER && !defined(__INTEL_COMPILER)
159     #pragma warning (pop)
160 #endif // warning 4100 is back
161
162 //! Analogous to std::allocator<void>, as defined in ISO C++ Standard, Section 20.4.1
163 /** @ingroup memory_allocation */
164 template<>
165 class scalable_allocator<void> {
166 public:
167     typedef void* pointer;
168     typedef const void* const_pointer;
169     typedef void value_type;
170     template<class U> struct rebind {
171         typedef scalable_allocator<U> other;
172     };
173 };
174
175 template<typename T, typename U>
176 inline bool operator==( const scalable_allocator<T>&, const scalable_allocator<U>& ) {return true;}
177
178 template<typename T, typename U>
179 inline bool operator!=( const scalable_allocator<T>&, const scalable_allocator<U>& ) {return false;}
180
181 } // namespace tbb
182
183 #if _MSC_VER
184     #if __TBB_BUILD && !defined(__TBBMALLOC_NO_IMPLICIT_LINKAGE)
185         #define __TBBMALLOC_NO_IMPLICIT_LINKAGE 1
186     #endif
187
188     #if !__TBBMALLOC_NO_IMPLICIT_LINKAGE
189         #ifdef _DEBUG
190             #pragma comment(lib, "tbbmalloc_debug.lib")
191         #else
192             #pragma comment(lib, "tbbmalloc.lib")
193         #endif
194     #endif
195
196
197 #endif
198
199 #endif /* __cplusplus */
200
201 #if !defined(__cplusplus) && __ICC==1100
202     #pragma warning (pop)
203 #endif // ICC 11.0 warning 991 is back
204
205 #endif /* __TBB_scalable_allocator_H */