]> git.sesse.net Git - casparcg/blob - tbb30_20100406oss/include/tbb/tbb_allocator.h
008422df30e8893d11c6c5aa466b046f02c068ec
[casparcg] / tbb30_20100406oss / include / tbb / tbb_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_tbb_allocator_H
30 #define __TBB_tbb_allocator_H
31
32 #include "tbb_stddef.h"
33 #include <new>
34
35 #if !TBB_USE_EXCEPTIONS && _MSC_VER
36     // Suppress "C++ exception handler used, but unwind semantics are not enabled" warning in STL headers
37     #pragma warning (push)
38     #pragma warning (disable: 4530)
39 #endif
40
41 #include <cstring>
42
43 #if !TBB_USE_EXCEPTIONS && _MSC_VER
44     #pragma warning (pop)
45 #endif
46
47 namespace tbb {
48
49 //! @cond INTERNAL
50 namespace internal {
51
52     //! Deallocates memory using FreeHandler
53     /** The function uses scalable_free if scalable allocator is available and free if not*/
54     void __TBB_EXPORTED_FUNC deallocate_via_handler_v3( void *p );
55
56     //! Allocates memory using MallocHandler
57     /** The function uses scalable_malloc if scalable allocator is available and malloc if not*/
58     void* __TBB_EXPORTED_FUNC allocate_via_handler_v3( size_t n );
59
60     //! Returns true if standard malloc/free are used to work with memory.
61     bool __TBB_EXPORTED_FUNC is_malloc_used_v3();
62 }
63 //! @endcond
64
65 #if _MSC_VER && !defined(__INTEL_COMPILER)
66     // Workaround for erroneous "unreferenced parameter" warning in method destroy.
67     #pragma warning (push)
68     #pragma warning (disable: 4100)
69 #endif
70
71 //! Meets "allocator" requirements of ISO C++ Standard, Section 20.1.5
72 /** The class selects the best memory allocation mechanism available 
73     from scalable_malloc and standard malloc.
74     The members are ordered the same way they are in section 20.4.1
75     of the ISO C++ standard.
76     @ingroup memory_allocation */
77 template<typename T>
78 class tbb_allocator {
79 public:
80     typedef typename internal::allocator_type<T>::value_type value_type;
81     typedef value_type* pointer;
82     typedef const value_type* const_pointer;
83     typedef value_type& reference;
84     typedef const value_type& const_reference;
85     typedef size_t size_type;
86     typedef ptrdiff_t difference_type;
87     template<typename U> struct rebind {
88         typedef tbb_allocator<U> other;
89     };
90
91     //! Specifies current allocator
92     enum malloc_type {
93         scalable, 
94         standard
95     };
96
97     tbb_allocator() throw() {}
98     tbb_allocator( const tbb_allocator& ) throw() {}
99     template<typename U> tbb_allocator(const tbb_allocator<U>&) throw() {}
100
101     pointer address(reference x) const {return &x;}
102     const_pointer address(const_reference x) const {return &x;}
103     
104     //! Allocate space for n objects.
105     pointer allocate( size_type n, const void* /*hint*/ = 0) {
106         return pointer(internal::allocate_via_handler_v3( n * sizeof(value_type) ));
107     }
108
109     //! Free previously allocated block of memory.
110     void deallocate( pointer p, size_type ) {
111         internal::deallocate_via_handler_v3(p);        
112     }
113
114     //! Largest value for which method allocate might succeed.
115     size_type max_size() const throw() {
116         size_type max = static_cast<size_type>(-1) / sizeof (value_type);
117         return (max > 0 ? max : 1);
118     }
119     
120     //! Copy-construct value at location pointed to by p.
121     void construct( pointer p, const value_type& value ) {::new((void*)(p)) value_type(value);}
122
123     //! Destroy value at location pointed to by p.
124     void destroy( pointer p ) {p->~value_type();}
125
126     //! Returns current allocator
127     static malloc_type allocator_type() {
128         return internal::is_malloc_used_v3() ? standard : scalable;
129     }
130 };
131
132 #if _MSC_VER && !defined(__INTEL_COMPILER)
133     #pragma warning (pop)
134 #endif // warning 4100 is back
135
136 //! Analogous to std::allocator<void>, as defined in ISO C++ Standard, Section 20.4.1
137 /** @ingroup memory_allocation */
138 template<> 
139 class tbb_allocator<void> {
140 public:
141     typedef void* pointer;
142     typedef const void* const_pointer;
143     typedef void value_type;
144     template<typename U> struct rebind {
145         typedef tbb_allocator<U> other;
146     };
147 };
148
149 template<typename T, typename U>
150 inline bool operator==( const tbb_allocator<T>&, const tbb_allocator<U>& ) {return true;}
151
152 template<typename T, typename U>
153 inline bool operator!=( const tbb_allocator<T>&, const tbb_allocator<U>& ) {return false;}
154
155 //! Meets "allocator" requirements of ISO C++ Standard, Section 20.1.5
156 /** The class is an adapter over an actual allocator that fills the allocation
157     using memset function with template argument C as the value.
158     The members are ordered the same way they are in section 20.4.1
159     of the ISO C++ standard.
160     @ingroup memory_allocation */
161 template <typename T, template<typename X> class Allocator = tbb_allocator>
162 class zero_allocator : public Allocator<T>
163 {
164 public:
165     typedef Allocator<T> base_allocator_type;
166     typedef typename base_allocator_type::value_type value_type;
167     typedef typename base_allocator_type::pointer pointer;
168     typedef typename base_allocator_type::const_pointer const_pointer;
169     typedef typename base_allocator_type::reference reference;
170     typedef typename base_allocator_type::const_reference const_reference;
171     typedef typename base_allocator_type::size_type size_type;
172     typedef typename base_allocator_type::difference_type difference_type;
173     template<typename U> struct rebind {
174         typedef zero_allocator<U, Allocator> other;
175     };
176
177     zero_allocator() throw() { }
178     zero_allocator(const zero_allocator &a) throw() : base_allocator_type( a ) { }
179     template<typename U>
180     zero_allocator(const zero_allocator<U> &a) throw() : base_allocator_type( Allocator<U>( a ) ) { }
181
182     pointer allocate(const size_type n, const void *hint = 0 ) {
183         pointer ptr = base_allocator_type::allocate( n, hint );
184         std::memset( ptr, 0, n * sizeof(value_type) );
185         return ptr;
186     }
187 };
188
189 //! Analogous to std::allocator<void>, as defined in ISO C++ Standard, Section 20.4.1
190 /** @ingroup memory_allocation */
191 template<template<typename T> class Allocator> 
192 class zero_allocator<void, Allocator> : public Allocator<void> {
193 public:
194     typedef Allocator<void> base_allocator_type;
195     typedef typename base_allocator_type::value_type value_type;
196     typedef typename base_allocator_type::pointer pointer;
197     typedef typename base_allocator_type::const_pointer const_pointer;
198     template<typename U> struct rebind {
199         typedef zero_allocator<U, Allocator> other;
200     };
201 };
202
203 template<typename T1, template<typename X1> class B1, typename T2, template<typename X2> class B2>
204 inline bool operator==( const zero_allocator<T1,B1> &a, const zero_allocator<T2,B2> &b) {
205     return static_cast< B1<T1> >(a) == static_cast< B2<T2> >(b);
206 }
207 template<typename T1, template<typename X1> class B1, typename T2, template<typename X2> class B2>
208 inline bool operator!=( const zero_allocator<T1,B1> &a, const zero_allocator<T2,B2> &b) {
209     return static_cast< B1<T1> >(a) != static_cast< B2<T2> >(b);
210 }
211
212 } // namespace tbb 
213
214 #endif /* __TBB_tbb_allocator_H */