scalable_allocator.h Source File

- Main Page
- Modules
- Namespaces
- Classes
- Files
- Related Pages

- File List
- File Members

# 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 #if __TBB_CPP11_RVALUE_REF_PRESENT && !__TBB_CPP11_STD_FORWARD_BROKEN
    00161  #include <utility> // std::forward
    00162 #endif
    00163
    00164 namespace tbb {
    00165
    00166 #if _MSC_VER && !defined(__INTEL_COMPILER)
    00167     // Workaround for erroneous "unreferenced parameter" warning in method destroy.
    00168     #pragma warning (push)
    00169     #pragma warning (disable: 4100)
    00170 #endif
    00171
    00173
    00176 template<typename T>
    00177 class scalable_allocator {
    00178 public:
    00179     typedef typename internal::allocator_type<T>::value_type value_type;
    00180     typedef value_type* pointer;
    00181     typedef const value_type* const_pointer;
    00182     typedef value_type& reference;
    00183     typedef const value_type& const_reference;
    00184     typedef size_t size_type;
    00185     typedef ptrdiff_t difference_type;
    00186     template<class U> struct rebind {
    00187         typedef scalable_allocator<U> other;
    00188     };
    00189
    00190     scalable_allocator() throw() {}
    00191     scalable_allocator( const scalable_allocator& ) throw() {}
    00192     template<typename U> scalable_allocator(const scalable_allocator<U>&) throw() {}
    00193
    00194     pointer address(reference x) const {return &x;}
    00195     const_pointer address(const_reference x) const {return &x;}
    00196
    00198     pointer allocate( size_type n, const void* /*hint*/ =0 ) {
    00199         return static_cast<pointer>( scalable_malloc( n * sizeof(value_type) ) );
    00200     }
    00201
    00203     void deallocate( pointer p, size_type ) {
    00204         scalable_free( p );
    00205     }
    00206
    00208     size_type max_size() const throw() {
    00209         size_type absolutemax = static_cast<size_type>(-1) / sizeof (value_type);
    00210         return (absolutemax > 0 ? absolutemax : 1);
    00211     }
    00212 #if __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT && __TBB_CPP11_RVALUE_REF_PRESENT
    00213     template<typename... Args>
    00214     void construct(pointer p, Args&&... args)
    00215  #if __TBB_CPP11_STD_FORWARD_BROKEN
    00216         { ::new((void *)p) T((args)...); }
    00217  #else
    00218         { ::new((void *)p) T(std::forward<Args>(args)...); }
    00219  #endif
    00220 #else // __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT && __TBB_CPP11_RVALUE_REF_PRESENT
    00221     void construct( pointer p, const value_type& value ) {::new((void*)(p)) value_type(value);}
    00222 #endif // __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT && __TBB_CPP11_RVALUE_REF_PRESENT
    00223     void destroy( pointer p ) {p->~value_type();}
    00224 };
    00225
    00226 #if _MSC_VER && !defined(__INTEL_COMPILER)
    00227     #pragma warning (pop)
    00228 #endif // warning 4100 is back
    00229
    00231
    00232 template<>
    00233 class scalable_allocator<void> {
    00234 public:
    00235     typedef void* pointer;
    00236     typedef const void* const_pointer;
    00237     typedef void value_type;
    00238     template<class U> struct rebind {
    00239         typedef scalable_allocator<U> other;
    00240     };
    00241 };
    00242
    00243 template<typename T, typename U>
    00244 inline bool operator==( const scalable_allocator<T>&, const scalable_allocator<U>& ) {return true;}
    00245
    00246 template<typename T, typename U>
    00247 inline bool operator!=( const scalable_allocator<T>&, const scalable_allocator<U>& ) {return false;}
    00248
    00249 } // namespace tbb
    00250
    00251 #if _MSC_VER
    00252     #if (__TBB_BUILD || __TBBMALLOC_BUILD) && !defined(__TBBMALLOC_NO_IMPLICIT_LINKAGE)
    00253         #define __TBBMALLOC_NO_IMPLICIT_LINKAGE 1
    00254     #endif
    00255
    00256     #if !__TBBMALLOC_NO_IMPLICIT_LINKAGE
    00257         #ifdef _DEBUG
    00258             #pragma comment(lib, "tbbmalloc_debug.lib")
    00259         #else
    00260             #pragma comment(lib, "tbbmalloc.lib")
    00261         #endif
    00262     #endif
    00263
    00264
    00265 #endif
    00266
    00267 #endif /* __cplusplus */
    00268
    00269 #if !defined(__cplusplus) && __ICC==1100
    00270     #pragma warning (pop)
    00271 #endif // ICC 11.0 warning 991 is back
    00272
    00273 #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.