Main Page | Modules | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | Related Pages

scalable_allocator.h

00001 /*
00002     Copyright 2005-2008 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_scalable_allocator_H
00022 #define __TBB_scalable_allocator_H
00023 
00024 #include <stddef.h> // Need ptrdiff_t and size_t from here.
00025 
00026 #ifdef __cplusplus
00027 extern "C" {
00028 #endif /* __cplusplus */
00029 
00031 
00032 void * scalable_malloc (size_t size);
00033 
00035 
00036 void   scalable_free (void* ptr);
00037 
00039 
00040 void * scalable_realloc (void* ptr, size_t size);
00041 
00043 
00044 void * scalable_calloc (size_t nobj, size_t size);
00045 
00046 #ifdef __cplusplus
00047 } // extern "C"
00048 #endif /* __cplusplus */
00049 
00050 #ifdef __cplusplus
00051 
00052 #include <new>      // To use new with the placement argument
00053 
00054 namespace tbb {
00055 
00057 
00060 template<typename T>
00061 class scalable_allocator {
00062 public:
00063     typedef T* pointer;
00064     typedef const T* const_pointer;
00065     typedef T& reference;
00066     typedef const T& const_reference;
00067     typedef T value_type;
00068     typedef size_t size_type;
00069     typedef ptrdiff_t difference_type;
00070     template<class U> struct rebind {
00071         typedef scalable_allocator<U> other;
00072     };
00073 
00074     scalable_allocator() throw() {}
00075     scalable_allocator( const scalable_allocator& ) throw() {}
00076     template<typename U> scalable_allocator(const scalable_allocator<U>&) throw() {}
00077 
00078     pointer address(reference x) const {return &x;}
00079     const_pointer address(const_reference x) const {return &x;}
00080 
00082     pointer allocate( size_type n, const void* /*hint*/ =0 ) {
00083         return static_cast<pointer>( scalable_malloc( n * sizeof(value_type) ) );
00084     }
00085 
00087     void deallocate( pointer p, size_type ) {
00088         scalable_free( p );
00089     }
00090 
00092     size_type max_size() const throw() {
00093         size_type absolutemax = static_cast<size_type>(-1) / sizeof (T);
00094         return (absolutemax > 0 ? absolutemax : 1);
00095     }
00096     void construct( pointer p, const T& val ) { new(static_cast<void*>(p)) T(val); }
00097     void destroy( pointer p ) {(static_cast<T*>(p))->~T();}
00098 };
00099 
00101 
00102 template<>
00103 class scalable_allocator<void> {
00104 public:
00105     typedef void* pointer;
00106     typedef const void* const_pointer;
00107     typedef void value_type;
00108     template<class U> struct rebind {
00109         typedef scalable_allocator<U> other;
00110     };
00111 };
00112 
00113 template<typename T, typename U>
00114 inline bool operator==( const scalable_allocator<T>&, const scalable_allocator<U>& ) {return true;}
00115 
00116 template<typename T, typename U>
00117 inline bool operator!=( const scalable_allocator<T>&, const scalable_allocator<U>& ) {return false;}
00118 
00119 } // namespace tbb
00120 
00121 #if _MSC_VER
00122     #if __TBB_BUILD && !defined(__TBBMALLOC_NO_IMPLICIT_LINKAGE)
00123         #define __TBBMALLOC_NO_IMPLICIT_LINKAGE 1
00124     #endif
00125 
00126     #if !__TBBMALLOC_NO_IMPLICIT_LINKAGE
00127         #ifdef _DEBUG
00128             #pragma comment(lib, "tbbmalloc_debug.lib")
00129         #else
00130             #pragma comment(lib, "tbbmalloc.lib")
00131         #endif
00132     #endif
00133 #endif
00134 
00135 #endif /* __cplusplus */
00136 
00137 #endif /* __TBB_scalable_allocator_H */

Copyright © 2005-2008 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.