scalable_allocator.h

Go to the documentation of this file.
00001 /*
00002     Copyright 2005-2012 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 typedef void *(*rawAllocType)(intptr_t pool_id, size_t &bytes);
00093 typedef int   (*rawFreeType)(intptr_t pool_id, void* raw_ptr, size_t raw_bytes);
00094 
00095 /*
00096 MemPoolPolicy extension must be compatible with such structure fields layout
00097 
00098 struct MemPoolPolicy {
00099     rawAllocType pAlloc;
00100     rawFreeType  pFree;
00101     size_t       granularity;   // granularity of pAlloc allocations
00102 };
00103 */
00104 
00105 struct MemPoolPolicy {
00106     enum {
00107         VERSION = 1
00108     };
00109 
00110     rawAllocType pAlloc;
00111     rawFreeType  pFree;
00112                  // granularity of pAlloc allocations. 0 means default used.
00113     size_t       granularity;
00114     int          version;
00115                  // all memory consumed at 1st pAlloc call and never returned,
00116                  // no more pAlloc calls after 1st
00117     unsigned     fixedPool : 1,
00118                  // memory consumed but returned only at pool termination
00119                  keepAllMemory : 1,
00120                  reserved : 30;
00121 
00122     MemPoolPolicy(rawAllocType pAlloc_, rawFreeType pFree_,
00123                   size_t granularity_ = 0, bool fixedPool_ = false,
00124                   bool keepAllMemory_ = false) :
00125         pAlloc(pAlloc_), pFree(pFree_), granularity(granularity_), version(VERSION),
00126         fixedPool(fixedPool_), keepAllMemory(keepAllMemory_),
00127         reserved(0) {}
00128 };
00129 
00130 enum MemPoolError {
00131     POOL_OK,            // pool created successfully
00132     INVALID_POLICY,     // invalid policy parameters found
00133     UNSUPPORTED_POLICY, // requested pool policy is not supported by allocator library
00134     NO_MEMORY           // lack of memory during pool creation
00135 };
00136 
00137 MemPoolError pool_create_v1(intptr_t pool_id, const MemPoolPolicy *policy,
00138                             rml::MemoryPool **pool);
00139 
00140 bool  pool_destroy(MemoryPool* memPool);
00141 void *pool_malloc(MemoryPool* memPool, size_t size);
00142 void *pool_realloc(MemoryPool* memPool, void *object, size_t size);
00143 void *pool_aligned_malloc(MemoryPool* mPool, size_t size, size_t alignment);
00144 void *pool_aligned_realloc(MemoryPool* mPool, void *ptr, size_t size, size_t alignment);
00145 bool  pool_reset(MemoryPool* memPool);
00146 bool  pool_free(MemoryPool *memPool, void *object);
00147 }
00148 
00149 #include <new>      /* To use new with the placement argument */
00150 
00151 /* Ensure that including this header does not cause implicit linkage with TBB */
00152 #ifndef __TBB_NO_IMPLICIT_LINKAGE
00153     #define __TBB_NO_IMPLICIT_LINKAGE 1
00154     #include "tbb_stddef.h"
00155     #undef  __TBB_NO_IMPLICIT_LINKAGE
00156 #else
00157     #include "tbb_stddef.h"
00158 #endif
00159 
00160 
00161 namespace tbb {
00162 
00163 #if _MSC_VER && !defined(__INTEL_COMPILER)
00164     // Workaround for erroneous "unreferenced parameter" warning in method destroy.
00165     #pragma warning (push)
00166     #pragma warning (disable: 4100)
00167 #endif
00168 
00170 
00173 template<typename T>
00174 class scalable_allocator {
00175 public:
00176     typedef typename internal::allocator_type<T>::value_type value_type;
00177     typedef value_type* pointer;
00178     typedef const value_type* const_pointer;
00179     typedef value_type& reference;
00180     typedef const value_type& const_reference;
00181     typedef size_t size_type;
00182     typedef ptrdiff_t difference_type;
00183     template<class U> struct rebind {
00184         typedef scalable_allocator<U> other;
00185     };
00186 
00187     scalable_allocator() throw() {}
00188     scalable_allocator( const scalable_allocator& ) throw() {}
00189     template<typename U> scalable_allocator(const scalable_allocator<U>&) throw() {}
00190 
00191     pointer address(reference x) const {return &x;}
00192     const_pointer address(const_reference x) const {return &x;}
00193 
00195     pointer allocate( size_type n, const void* /*hint*/ =0 ) {
00196         return static_cast<pointer>( scalable_malloc( n * sizeof(value_type) ) );
00197     }
00198 
00200     void deallocate( pointer p, size_type ) {
00201         scalable_free( p );
00202     }
00203 
00205     size_type max_size() const throw() {
00206         size_type absolutemax = static_cast<size_type>(-1) / sizeof (value_type);
00207         return (absolutemax > 0 ? absolutemax : 1);
00208     }
00209     void construct( pointer p, const value_type& value ) {::new((void*)(p)) value_type(value);}
00210     void destroy( pointer p ) {p->~value_type();}
00211 };
00212 
00213 #if _MSC_VER && !defined(__INTEL_COMPILER)
00214     #pragma warning (pop)
00215 #endif // warning 4100 is back
00216 
00218 
00219 template<>
00220 class scalable_allocator<void> {
00221 public:
00222     typedef void* pointer;
00223     typedef const void* const_pointer;
00224     typedef void value_type;
00225     template<class U> struct rebind {
00226         typedef scalable_allocator<U> other;
00227     };
00228 };
00229 
00230 template<typename T, typename U>
00231 inline bool operator==( const scalable_allocator<T>&, const scalable_allocator<U>& ) {return true;}
00232 
00233 template<typename T, typename U>
00234 inline bool operator!=( const scalable_allocator<T>&, const scalable_allocator<U>& ) {return false;}
00235 
00236 } // namespace tbb
00237 
00238 #if _MSC_VER
00239     #if __TBB_BUILD && !defined(__TBBMALLOC_NO_IMPLICIT_LINKAGE)
00240         #define __TBBMALLOC_NO_IMPLICIT_LINKAGE 1
00241     #endif
00242 
00243     #if !__TBBMALLOC_NO_IMPLICIT_LINKAGE
00244         #ifdef _DEBUG
00245             #pragma comment(lib, "tbbmalloc_debug.lib")
00246         #else
00247             #pragma comment(lib, "tbbmalloc.lib")
00248         #endif
00249     #endif
00250 
00251 
00252 #endif
00253 
00254 #endif /* __cplusplus */
00255 
00256 #if !defined(__cplusplus) && __ICC==1100
00257     #pragma warning (pop)
00258 #endif // ICC 11.0 warning 991 is back
00259 
00260 #endif /* __TBB_scalable_allocator_H */

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