cache_aligned_allocator.h

00001 /*
00002     Copyright 2005-2009 Intel Corporation.  All Rights Reserved.
00003 
00004     The source code contained or described herein and all documents related
00005     to the source code ("Material") are owned by Intel Corporation or its
00006     suppliers or licensors.  Title to the Material remains with Intel
00007     Corporation or its suppliers and licensors.  The Material is protected
00008     by worldwide copyright laws and treaty provisions.  No part of the
00009     Material may be used, copied, reproduced, modified, published, uploaded,
00010     posted, transmitted, distributed, or disclosed in any way without
00011     Intel's prior express written permission.
00012 
00013     No license under any patent, copyright, trade secret or other
00014     intellectual property right is granted to or conferred upon you by
00015     disclosure or delivery of the Materials, either expressly, by
00016     implication, inducement, estoppel or otherwise.  Any license under such
00017     intellectual property rights must be express and approved by Intel in
00018     writing.
00019 */
00020 
00021 #ifndef __TBB_cache_aligned_allocator_H
00022 #define __TBB_cache_aligned_allocator_H
00023 
00024 #include <new>
00025 #include "tbb_stddef.h"
00026 
00027 namespace tbb {
00028 
00030 namespace internal {
00032 
00035     const size_t NFS_MaxLineSize = 128;
00036 
00038 
00039     size_t __TBB_EXPORTED_FUNC NFS_GetLineSize();
00040 
00042 
00043     void* __TBB_EXPORTED_FUNC NFS_Allocate( size_t n_element, size_t element_size, void* hint );
00044 
00046 
00048     void __TBB_EXPORTED_FUNC NFS_Free( void* );
00049 }
00051 
00052 #if _MSC_VER && !defined(__INTEL_COMPILER)
00053     // Workaround for erroneous "unreferenced parameter" warning in method destroy.
00054     #pragma warning (push)
00055     #pragma warning (disable: 4100)
00056 #endif
00057 
00059 
00062 template<typename T>
00063 class cache_aligned_allocator {
00064 public:
00065     typedef typename internal::allocator_type<T>::value_type value_type;
00066     typedef value_type* pointer;
00067     typedef const value_type* const_pointer;
00068     typedef value_type& reference;
00069     typedef const value_type& const_reference;
00070     typedef size_t size_type;
00071     typedef ptrdiff_t difference_type;
00072     template<typename U> struct rebind {
00073         typedef cache_aligned_allocator<U> other;
00074     };
00075 
00076     cache_aligned_allocator() throw() {}
00077     cache_aligned_allocator( const cache_aligned_allocator& ) throw() {}
00078     template<typename U> cache_aligned_allocator(const cache_aligned_allocator<U>&) throw() {}
00079 
00080     pointer address(reference x) const {return &x;}
00081     const_pointer address(const_reference x) const {return &x;}
00082     
00084     pointer allocate( size_type n, const void* hint=0 ) {
00085         // The "hint" argument is always ignored in NFS_Allocate thus const_cast shouldn't hurt
00086         return pointer(internal::NFS_Allocate( n, sizeof(value_type), const_cast<void*>(hint) ));
00087     }
00088 
00090     void deallocate( pointer p, size_type ) {
00091         internal::NFS_Free(p);
00092     }
00093 
00095     size_type max_size() const throw() {
00096         return (~size_t(0)-internal::NFS_MaxLineSize)/sizeof(value_type);
00097     }
00098 
00100     void construct( pointer p, const value_type& value ) {new(static_cast<void*>(p)) value_type(value);}
00101 
00103     void destroy( pointer p ) {p->~value_type();}
00104 };
00105 
00106 #if _MSC_VER && !defined(__INTEL_COMPILER)
00107     #pragma warning (pop)
00108 #endif // warning 4100 is back
00109 
00111 
00112 template<> 
00113 class cache_aligned_allocator<void> {
00114 public:
00115     typedef void* pointer;
00116     typedef const void* const_pointer;
00117     typedef void value_type;
00118     template<typename U> struct rebind {
00119         typedef cache_aligned_allocator<U> other;
00120     };
00121 };
00122 
00123 template<typename T, typename U>
00124 inline bool operator==( const cache_aligned_allocator<T>&, const cache_aligned_allocator<U>& ) {return true;}
00125 
00126 template<typename T, typename U>
00127 inline bool operator!=( const cache_aligned_allocator<T>&, const cache_aligned_allocator<U>& ) {return false;}
00128 
00129 } // namespace tbb
00130 
00131 #endif /* __TBB_cache_aligned_allocator_H */

Copyright © 2005-2009 Intel Corporation. All Rights Reserved.

Intel, Pentium, Intel Xeon, Itanium, Intel XScale and VTune are registered trademarks or trademarks of Intel Corporation or its subsidiaries in the United States and other countries.

* Other names and brands may be claimed as the property of others.