scalable_allocator.h

Go to the documentation of this file.
00001 /*
00002     Copyright 2005-2011 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 
00025 #include <stddef.h> /* Need ptrdiff_t and size_t from here. */
00026 #if !_MSC_VER
00027 #include <stdint.h> /* Need intptr_t from here. */
00028 #endif
00029 
00030 #if !defined(__cplusplus) && __ICC==1100
00031     #pragma warning (push)
00032     #pragma warning (disable: 991)
00033 #endif
00034 
00035 #ifdef __cplusplus
00036 extern "C" {
00037 #endif /* __cplusplus */
00038 
00039 #if _MSC_VER >= 1400
00040 #define __TBB_EXPORTED_FUNC   __cdecl
00041 #else
00042 #define __TBB_EXPORTED_FUNC
00043 #endif
00044 
00047 void * __TBB_EXPORTED_FUNC scalable_malloc (size_t size);
00048 
00051 void   __TBB_EXPORTED_FUNC scalable_free (void* ptr);
00052 
00055 void * __TBB_EXPORTED_FUNC scalable_realloc (void* ptr, size_t size);
00056 
00059 void * __TBB_EXPORTED_FUNC scalable_calloc (size_t nobj, size_t size);
00060 
00063 int __TBB_EXPORTED_FUNC scalable_posix_memalign (void** memptr, size_t alignment, size_t size);
00064 
00067 void * __TBB_EXPORTED_FUNC scalable_aligned_malloc (size_t size, size_t alignment);
00068 
00071 void * __TBB_EXPORTED_FUNC scalable_aligned_realloc (void* ptr, size_t size, size_t alignment);
00072 
00075 void __TBB_EXPORTED_FUNC scalable_aligned_free (void* ptr);
00076 
00081 size_t __TBB_EXPORTED_FUNC scalable_msize (void* ptr);
00082 
00083 #ifdef __cplusplus
00084 } /* extern "C" */
00085 #endif /* __cplusplus */
00086 
00087 #ifdef __cplusplus
00088 
00089 namespace rml {
00090 class MemoryPool;
00091 
00092 #define MEM_POLICY_DEFINED 1
00093 typedef void *(*rawAllocType)(intptr_t pool_id, size_t &bytes);
00094 typedef int   (*rawFreeType)(intptr_t pool_id, void* raw_ptr, size_t raw_bytes);
00095 
00096 struct MemPoolPolicy {
00097     rawAllocType pAlloc;
00098     rawFreeType  pFree;
00099     size_t       granularity;   // granularity of pAlloc allocations
00100     void        *pReserved;     // reserved for future extensions
00101     size_t       szReserved;    // size of pReserved data
00102 };
00103 
00104 MemoryPool *pool_create(intptr_t pool_id, const MemPoolPolicy* memPoolPolicy);
00105 bool  pool_destroy(MemoryPool* memPool);
00106 void *pool_malloc(MemoryPool* memPool, size_t size);
00107 void *pool_realloc(MemoryPool* memPool, void *object, size_t size);
00108 bool  pool_reset(MemoryPool* memPool);
00109 bool  pool_free(MemoryPool *memPool, void *object);
00110 }
00111 
00112 #include <new>      /* To use new with the placement argument */
00113 
00114 /* Ensure that including this header does not cause implicit linkage with TBB */
00115 #ifndef __TBB_NO_IMPLICIT_LINKAGE
00116     #define __TBB_NO_IMPLICIT_LINKAGE 1
00117     #include "tbb_stddef.h"
00118     #undef  __TBB_NO_IMPLICIT_LINKAGE
00119 #else
00120     #include "tbb_stddef.h"
00121 #endif
00122 
00123 
00124 namespace tbb {
00125 
00126 #if _MSC_VER && !defined(__INTEL_COMPILER)
00127     // Workaround for erroneous "unreferenced parameter" warning in method destroy.
00128     #pragma warning (push)
00129     #pragma warning (disable: 4100)
00130 #endif
00131 
00133 
00136 template<typename T>
00137 class scalable_allocator {
00138 public:
00139     typedef typename internal::allocator_type<T>::value_type value_type;
00140     typedef value_type* pointer;
00141     typedef const value_type* const_pointer;
00142     typedef value_type& reference;
00143     typedef const value_type& const_reference;
00144     typedef size_t size_type;
00145     typedef ptrdiff_t difference_type;
00146     template<class U> struct rebind {
00147         typedef scalable_allocator<U> other;
00148     };
00149 
00150     scalable_allocator() throw() {}
00151     scalable_allocator( const scalable_allocator& ) throw() {}
00152     template<typename U> scalable_allocator(const scalable_allocator<U>&) throw() {}
00153 
00154     pointer address(reference x) const {return &x;}
00155     const_pointer address(const_reference x) const {return &x;}
00156 
00158     pointer allocate( size_type n, const void* /*hint*/ =0 ) {
00159         return static_cast<pointer>( scalable_malloc( n * sizeof(value_type) ) );
00160     }
00161 
00163     void deallocate( pointer p, size_type ) {
00164         scalable_free( p );
00165     }
00166 
00168     size_type max_size() const throw() {
00169         size_type absolutemax = static_cast<size_type>(-1) / sizeof (value_type);
00170         return (absolutemax > 0 ? absolutemax : 1);
00171     }
00172     void construct( pointer p, const value_type& value ) {::new((void*)(p)) value_type(value);}
00173     void destroy( pointer p ) {p->~value_type();}
00174 };
00175 
00176 #if _MSC_VER && !defined(__INTEL_COMPILER)
00177     #pragma warning (pop)
00178 #endif // warning 4100 is back
00179 
00181 
00182 template<>
00183 class scalable_allocator<void> {
00184 public:
00185     typedef void* pointer;
00186     typedef const void* const_pointer;
00187     typedef void value_type;
00188     template<class U> struct rebind {
00189         typedef scalable_allocator<U> other;
00190     };
00191 };
00192 
00193 template<typename T, typename U>
00194 inline bool operator==( const scalable_allocator<T>&, const scalable_allocator<U>& ) {return true;}
00195 
00196 template<typename T, typename U>
00197 inline bool operator!=( const scalable_allocator<T>&, const scalable_allocator<U>& ) {return false;}
00198 
00199 } // namespace tbb
00200 
00201 #if _MSC_VER
00202     #if __TBB_BUILD && !defined(__TBBMALLOC_NO_IMPLICIT_LINKAGE)
00203         #define __TBBMALLOC_NO_IMPLICIT_LINKAGE 1
00204     #endif
00205 
00206     #if !__TBBMALLOC_NO_IMPLICIT_LINKAGE
00207         #ifdef _DEBUG
00208             #pragma comment(lib, "tbbmalloc_debug.lib")
00209         #else
00210             #pragma comment(lib, "tbbmalloc.lib")
00211         #endif
00212     #endif
00213 
00214 
00215 #endif
00216 
00217 #endif /* __cplusplus */
00218 
00219 #if !defined(__cplusplus) && __ICC==1100
00220     #pragma warning (pop)
00221 #endif // ICC 11.0 warning 991 is back
00222 
00223 #endif /* __TBB_scalable_allocator_H */

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