Shadowrun: Awakened 29 September 2011 - Build 871
tbb_allocator.h
Go to the documentation of this file.
00001 /*
00002     Copyright 2005-2010 Intel Corporation.  All Rights Reserved.
00003 
00004     This file is part of Threading Building Blocks.
00005 
00006     Threading Building Blocks is free software; you can redistribute it
00007     and/or modify it under the terms of the GNU General Public License
00008     version 2 as published by the Free Software Foundation.
00009 
00010     Threading Building Blocks is distributed in the hope that it will be
00011     useful, but WITHOUT ANY WARRANTY; without even the implied warranty
00012     of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with Threading Building Blocks; if not, write to the Free Software
00017     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 
00019     As a special exception, you may use this file as part of a free software
00020     library without restriction.  Specifically, if other files instantiate
00021     templates or use macros or inline functions from this file, or you compile
00022     this file and link it with other files to produce an executable, this
00023     file does not by itself cause the resulting executable to be covered by
00024     the GNU General Public License.  This exception does not however
00025     invalidate any other reasons why the executable file might be covered by
00026     the GNU General Public License.
00027 */
00028 
00029 #ifndef __TBB_tbb_allocator_H
00030 #define __TBB_tbb_allocator_H
00031 
00032 #include "tbb_stddef.h"
00033 #include <new>
00034 
00035 #if !TBB_USE_EXCEPTIONS && _MSC_VER
00036     // Suppress "C++ exception handler used, but unwind semantics are not enabled" warning in STL headers
00037     #pragma warning (push)
00038     #pragma warning (disable: 4530)
00039 #endif
00040 
00041 #include <cstring>
00042 
00043 #if !TBB_USE_EXCEPTIONS && _MSC_VER
00044     #pragma warning (pop)
00045 #endif
00046 
00047 namespace tbb {
00048 
00050 namespace internal {
00051 
00053 
00054     void __TBB_EXPORTED_FUNC deallocate_via_handler_v3( void *p );
00055 
00057 
00058     void* __TBB_EXPORTED_FUNC allocate_via_handler_v3( size_t n );
00059 
00061     bool __TBB_EXPORTED_FUNC is_malloc_used_v3();
00062 }
00064 
00065 #if _MSC_VER && !defined(__INTEL_COMPILER)
00066     // Workaround for erroneous "unreferenced parameter" warning in method destroy.
00067     #pragma warning (push)
00068     #pragma warning (disable: 4100)
00069 #endif
00070 
00072 
00077 template<typename T>
00078 class tbb_allocator {
00079 public:
00080     typedef typename internal::allocator_type<T>::value_type value_type;
00081     typedef value_type* pointer;
00082     typedef const value_type* const_pointer;
00083     typedef value_type& reference;
00084     typedef const value_type& const_reference;
00085     typedef size_t size_type;
00086     typedef ptrdiff_t difference_type;
00087     template<typename U> struct rebind {
00088         typedef tbb_allocator<U> other;
00089     };
00090 
00092     enum malloc_type {
00093         scalable, 
00094         standard
00095     };
00096 
00097     tbb_allocator() throw() {}
00098     tbb_allocator( const tbb_allocator& ) throw() {}
00099     template<typename U> tbb_allocator(const tbb_allocator<U>&) throw() {}
00100 
00101     pointer address(reference x) const {return &x;}
00102     const_pointer address(const_reference x) const {return &x;}
00103     
00105     pointer allocate( size_type n, const void* /*hint*/ = 0) {
00106         return pointer(internal::allocate_via_handler_v3( n * sizeof(value_type) ));
00107     }
00108 
00110     void deallocate( pointer p, size_type ) {
00111         internal::deallocate_via_handler_v3(p);        
00112     }
00113 
00115     size_type max_size() const throw() {
00116         size_type max = static_cast<size_type>(-1) / sizeof (value_type);
00117         return (max > 0 ? max : 1);
00118     }
00119     
00121     void construct( pointer p, const value_type& value ) {::new((void*)(p)) value_type(value);}
00122 
00124     void destroy( pointer p ) {p->~value_type();}
00125 
00127     static malloc_type allocator_type() {
00128         return internal::is_malloc_used_v3() ? standard : scalable;
00129     }
00130 };
00131 
00132 #if _MSC_VER && !defined(__INTEL_COMPILER)
00133     #pragma warning (pop)
00134 #endif // warning 4100 is back
00135 
00137 
00138 template<> 
00139 class tbb_allocator<void> {
00140 public:
00141     typedef void* pointer;
00142     typedef const void* const_pointer;
00143     typedef void value_type;
00144     template<typename U> struct rebind {
00145         typedef tbb_allocator<U> other;
00146     };
00147 };
00148 
00149 template<typename T, typename U>
00150 inline bool operator==( const tbb_allocator<T>&, const tbb_allocator<U>& ) {return true;}
00151 
00152 template<typename T, typename U>
00153 inline bool operator!=( const tbb_allocator<T>&, const tbb_allocator<U>& ) {return false;}
00154 
00156 
00161 template <typename T, template<typename X> class Allocator = tbb_allocator>
00162 class zero_allocator : public Allocator<T>
00163 {
00164 public:
00165     typedef Allocator<T> base_allocator_type;
00166     typedef typename base_allocator_type::value_type value_type;
00167     typedef typename base_allocator_type::pointer pointer;
00168     typedef typename base_allocator_type::const_pointer const_pointer;
00169     typedef typename base_allocator_type::reference reference;
00170     typedef typename base_allocator_type::const_reference const_reference;
00171     typedef typename base_allocator_type::size_type size_type;
00172     typedef typename base_allocator_type::difference_type difference_type;
00173     template<typename U> struct rebind {
00174         typedef zero_allocator<U, Allocator> other;
00175     };
00176 
00177     zero_allocator() throw() { }
00178     zero_allocator(const zero_allocator &a) throw() : base_allocator_type( a ) { }
00179     template<typename U>
00180     zero_allocator(const zero_allocator<U> &a) throw() : base_allocator_type( Allocator<U>( a ) ) { }
00181 
00182     pointer allocate(const size_type n, const void *hint = 0 ) {
00183         pointer ptr = base_allocator_type::allocate( n, hint );
00184         std::memset( ptr, 0, n * sizeof(value_type) );
00185         return ptr;
00186     }
00187 };
00188 
00190 
00191 template<template<typename T> class Allocator> 
00192 class zero_allocator<void, Allocator> : public Allocator<void> {
00193 public:
00194     typedef Allocator<void> base_allocator_type;
00195     typedef typename base_allocator_type::value_type value_type;
00196     typedef typename base_allocator_type::pointer pointer;
00197     typedef typename base_allocator_type::const_pointer const_pointer;
00198     template<typename U> struct rebind {
00199         typedef zero_allocator<U, Allocator> other;
00200     };
00201 };
00202 
00203 template<typename T1, template<typename X1> class B1, typename T2, template<typename X2> class B2>
00204 inline bool operator==( const zero_allocator<T1,B1> &a, const zero_allocator<T2,B2> &b) {
00205     return static_cast< B1<T1> >(a) == static_cast< B2<T2> >(b);
00206 }
00207 template<typename T1, template<typename X1> class B1, typename T2, template<typename X2> class B2>
00208 inline bool operator!=( const zero_allocator<T1,B1> &a, const zero_allocator<T2,B2> &b) {
00209     return static_cast< B1<T1> >(a) != static_cast< B2<T2> >(b);
00210 }
00211 
00212 } // namespace tbb 
00213 
00214 #endif /* __TBB_tbb_allocator_H */

Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.

GNU Lesser General Public License 3 Sourceforge.net