2 Copyright 2005-2010 Intel Corporation. All Rights Reserved.
4 This file is part of Threading Building Blocks.
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.
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.
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
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.
29 #ifndef __TBB_scalable_allocator_H
30 #define __TBB_scalable_allocator_H
33 #include <stddef.h> /* Need ptrdiff_t and size_t from here. */
35 #if !defined(__cplusplus) && __ICC==1100
36 #pragma warning (push)
37 #pragma warning (disable: 991)
42 #endif /* __cplusplus */
45 #define __TBB_EXPORTED_FUNC __cdecl
47 #define __TBB_EXPORTED_FUNC
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);
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);
58 /** The "realloc" analogue complementing scalable_malloc.
59 @ingroup memory_allocation */
60 void * __TBB_EXPORTED_FUNC scalable_realloc (void* ptr, size_t size);
62 /** The "calloc" analogue complementing scalable_malloc.
63 @ingroup memory_allocation */
64 void * __TBB_EXPORTED_FUNC scalable_calloc (size_t nobj, size_t size);
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);
70 /** The "_aligned_malloc" analogue.
71 @ingroup memory_allocation */
72 void * __TBB_EXPORTED_FUNC scalable_aligned_malloc (size_t size, size_t alignment);
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);
78 /** The "_aligned_free" analogue.
79 @ingroup memory_allocation */
80 void __TBB_EXPORTED_FUNC scalable_aligned_free (void* ptr);
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);
90 #endif /* __cplusplus */
94 #include <new> /* To use new with the placement argument */
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
102 #include "tbb_stddef.h"
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)
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 */
119 class scalable_allocator {
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;
132 scalable_allocator() throw() {}
133 scalable_allocator( const scalable_allocator& ) throw() {}
134 template<typename U> scalable_allocator(const scalable_allocator<U>&) throw() {}
136 pointer address(reference x) const {return &x;}
137 const_pointer address(const_reference x) const {return &x;}
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) ) );
144 //! Free previously allocated block of memory
145 void deallocate( pointer p, size_type ) {
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);
154 void construct( pointer p, const value_type& value ) {::new((void*)(p)) value_type(value);}
155 void destroy( pointer p ) {p->~value_type();}
158 #if _MSC_VER && !defined(__INTEL_COMPILER)
159 #pragma warning (pop)
160 #endif // warning 4100 is back
162 //! Analogous to std::allocator<void>, as defined in ISO C++ Standard, Section 20.4.1
163 /** @ingroup memory_allocation */
165 class scalable_allocator<void> {
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;
175 template<typename T, typename U>
176 inline bool operator==( const scalable_allocator<T>&, const scalable_allocator<U>& ) {return true;}
178 template<typename T, typename U>
179 inline bool operator!=( const scalable_allocator<T>&, const scalable_allocator<U>& ) {return false;}
184 #if __TBB_BUILD && !defined(__TBBMALLOC_NO_IMPLICIT_LINKAGE)
185 #define __TBBMALLOC_NO_IMPLICIT_LINKAGE 1
188 #if !__TBBMALLOC_NO_IMPLICIT_LINKAGE
190 #pragma comment(lib, "tbbmalloc_debug.lib")
192 #pragma comment(lib, "tbbmalloc.lib")
199 #endif /* __cplusplus */
201 #if !defined(__cplusplus) && __ICC==1100
202 #pragma warning (pop)
203 #endif // ICC 11.0 warning 991 is back
205 #endif /* __TBB_scalable_allocator_H */