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

- 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     This file is part of Threading Building Blocks.
    00005
    00006     Threading Building Blocks is free software; you can redistribute it
    00007     and/or modify it under the terms of the GNU General Public License
    00008     version 2 as published by the Free Software Foundation.
    00009
    00010     Threading Building Blocks is distributed in the hope that it will be
    00011     useful, but WITHOUT ANY WARRANTY; without even the implied warranty
    00012     of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00013     GNU General Public License for more details.
    00014
    00015     You should have received a copy of the GNU General Public License
    00016     along with Threading Building Blocks; if not, write to the Free Software
    00017     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    00018
    00019     As a special exception, you may use this file as part of a free software
    00020     library without restriction.  Specifically, if other files instantiate
    00021     templates or use macros or inline functions from this file, or you compile
    00022     this file and link it with other files to produce an executable, this
    00023     file does not by itself cause the resulting executable to be covered by
    00024     the GNU General Public License.  This exception does not however
    00025     invalidate any other reasons why the executable file might be covered by
    00026     the GNU General Public License.
    00027 */
    00028
    00029 #ifndef __TBB_scalable_allocator_H
    00030 #define __TBB_scalable_allocator_H
    00031
    00033 #include <stddef.h> /* Need ptrdiff_t and size_t from here. */
    00034 #if !_MSC_VER
    00035 #include <stdint.h> /* Need intptr_t from here. */
    00036 #endif
    00037
    00038 #if !defined(__cplusplus) && __ICC==1100
    00039     #pragma warning (push)
    00040     #pragma warning (disable: 991)
    00041 #endif
    00042
    00043 #ifdef __cplusplus
    00044 extern "C" {
    00045 #endif /* __cplusplus */
    00046
    00047 #if _MSC_VER >= 1400
    00048 #define __TBB_EXPORTED_FUNC   __cdecl
    00049 #else
    00050 #define __TBB_EXPORTED_FUNC
    00051 #endif
    00052
    00055 void * __TBB_EXPORTED_FUNC scalable_malloc (size_t size);
    00056
    00059 void   __TBB_EXPORTED_FUNC scalable_free (void* ptr);
    00060
    00063 void * __TBB_EXPORTED_FUNC scalable_realloc (void* ptr, size_t size);
    00064
    00067 void * __TBB_EXPORTED_FUNC scalable_calloc (size_t nobj, size_t size);
    00068
    00071 int __TBB_EXPORTED_FUNC scalable_posix_memalign (void** memptr, size_t alignment, size_t size);
    00072
    00075 void * __TBB_EXPORTED_FUNC scalable_aligned_malloc (size_t size, size_t alignment);
    00076
    00079 void * __TBB_EXPORTED_FUNC scalable_aligned_realloc (void* ptr, size_t size, size_t alignment);
    00080
    00083 void __TBB_EXPORTED_FUNC scalable_aligned_free (void* ptr);
    00084
    00089 size_t __TBB_EXPORTED_FUNC scalable_msize (void* ptr);
    00090
    00091 #ifdef __cplusplus
    00092 } /* extern "C" */
    00093 #endif /* __cplusplus */
    00094
    00095 #ifdef __cplusplus
    00096
    00097 namespace rml {
    00098 class MemoryPool;
    00099
    00100 typedef void *(*rawAllocType)(intptr_t pool_id, size_t &bytes);
    00101 typedef int   (*rawFreeType)(intptr_t pool_id, void* raw_ptr, size_t raw_bytes);
    00102
    00103 /*
    00104 MemPoolPolicy extension must be compatible with such structure fields layout
    00105
    00106 struct MemPoolPolicy {
    00107     rawAllocType pAlloc;
    00108     rawFreeType  pFree;
    00109     size_t       granularity;   // granularity of pAlloc allocations
    00110 };
    00111 */
    00112
    00113 struct MemPoolPolicy {
    00114     enum {
    00115         VERSION = 1
    00116     };
    00117
    00118     rawAllocType pAlloc;
    00119     rawFreeType  pFree;
    00120                  // granularity of pAlloc allocations. 0 means default used.
    00121     size_t       granularity;
    00122     int          version;
    00123                  // all memory consumed at 1st pAlloc call and never returned,
    00124                  // no more pAlloc calls after 1st
    00125     unsigned     fixedPool : 1,
    00126                  // memory consumed but returned only at pool termination
    00127                  keepAllMemory : 1,
    00128                  reserved : 30;
    00129
    00130     MemPoolPolicy(rawAllocType pAlloc_, rawFreeType pFree_,
    00131                   size_t granularity_ = 0, bool fixedPool_ = false,
    00132                   bool keepAllMemory_ = false) :
    00133         pAlloc(pAlloc_), pFree(pFree_), granularity(granularity_), version(VERSION),
    00134         fixedPool(fixedPool_), keepAllMemory(keepAllMemory_),
    00135         reserved(0) {}
    00136 };
    00137
    00138 enum MemPoolError {
    00139     POOL_OK,            // pool created successfully
    00140     INVALID_POLICY,     // invalid policy parameters found
    00141     UNSUPPORTED_POLICY, // requested pool policy is not supported by allocator library
    00142     NO_MEMORY           // lack of memory during pool creation
    00143 };
    00144
    00145 MemPoolError pool_create_v1(intptr_t pool_id, const MemPoolPolicy *policy,
    00146                             rml::MemoryPool **pool);
    00147
    00148 bool  pool_destroy(MemoryPool* memPool);
    00149 void *pool_malloc(MemoryPool* memPool, size_t size);
    00150 void *pool_realloc(MemoryPool* memPool, void *object, size_t size);
    00151 void *pool_aligned_malloc(MemoryPool* mPool, size_t size, size_t alignment);
    00152 void *pool_aligned_realloc(MemoryPool* mPool, void *ptr, size_t size, size_t alignment);
    00153 bool  pool_reset(MemoryPool* memPool);
    00154 bool  pool_free(MemoryPool *memPool, void *object);
    00155 }
    00156
    00157 #include <new>      /* To use new with the placement argument */
    00158
    00159 /* Ensure that including this header does not cause implicit linkage with TBB */
    00160 #ifndef __TBB_NO_IMPLICIT_LINKAGE
    00161     #define __TBB_NO_IMPLICIT_LINKAGE 1
    00162     #include "tbb_stddef.h"
    00163     #undef  __TBB_NO_IMPLICIT_LINKAGE
    00164 #else
    00165     #include "tbb_stddef.h"
    00166 #endif
    00167
    00168 #if __TBB_CPP11_RVALUE_REF_PRESENT && !__TBB_CPP11_STD_FORWARD_BROKEN
    00169  #include <utility> // std::forward
    00170 #endif
    00171
    00172 namespace tbb {
    00173
    00174 #if _MSC_VER && !defined(__INTEL_COMPILER)
    00175     // Workaround for erroneous "unreferenced parameter" warning in method destroy.
    00176     #pragma warning (push)
    00177     #pragma warning (disable: 4100)
    00178 #endif
    00179
    00181
    00184 template<typename T>
    00185 class scalable_allocator {
    00186 public:
    00187     typedef typename internal::allocator_type<T>::value_type value_type;
    00188     typedef value_type* pointer;
    00189     typedef const value_type* const_pointer;
    00190     typedef value_type& reference;
    00191     typedef const value_type& const_reference;
    00192     typedef size_t size_type;
    00193     typedef ptrdiff_t difference_type;
    00194     template<class U> struct rebind {
    00195         typedef scalable_allocator<U> other;
    00196     };
    00197
    00198     scalable_allocator() throw() {}
    00199     scalable_allocator( const scalable_allocator& ) throw() {}
    00200     template<typename U> scalable_allocator(const scalable_allocator<U>&) throw() {}
    00201
    00202     pointer address(reference x) const {return &x;}
    00203     const_pointer address(const_reference x) const {return &x;}
    00204
    00206     pointer allocate( size_type n, const void* /*hint*/ =0 ) {
    00207         return static_cast<pointer>( scalable_malloc( n * sizeof(value_type) ) );
    00208     }
    00209
    00211     void deallocate( pointer p, size_type ) {
    00212         scalable_free( p );
    00213     }
    00214
    00216     size_type max_size() const throw() {
    00217         size_type absolutemax = static_cast<size_type>(-1) / sizeof (value_type);
    00218         return (absolutemax > 0 ? absolutemax : 1);
    00219     }
    00220 #if __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT && __TBB_CPP11_RVALUE_REF_PRESENT
    00221     template<typename... Args>
    00222     void construct(pointer p, Args&&... args)
    00223  #if __TBB_CPP11_STD_FORWARD_BROKEN
    00224         { ::new((void *)p) T((args)...); }
    00225  #else
    00226         { ::new((void *)p) T(std::forward<Args>(args)...); }
    00227  #endif
    00228 #else // __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT && __TBB_CPP11_RVALUE_REF_PRESENT
    00229     void construct( pointer p, const value_type& value ) {::new((void*)(p)) value_type(value);}
    00230 #endif // __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT && __TBB_CPP11_RVALUE_REF_PRESENT
    00231     void destroy( pointer p ) {p->~value_type();}
    00232 };
    00233
    00234 #if _MSC_VER && !defined(__INTEL_COMPILER)
    00235     #pragma warning (pop)
    00236 #endif // warning 4100 is back
    00237
    00239
    00240 template<>
    00241 class scalable_allocator<void> {
    00242 public:
    00243     typedef void* pointer;
    00244     typedef const void* const_pointer;
    00245     typedef void value_type;
    00246     template<class U> struct rebind {
    00247         typedef scalable_allocator<U> other;
    00248     };
    00249 };
    00250
    00251 template<typename T, typename U>
    00252 inline bool operator==( const scalable_allocator<T>&, const scalable_allocator<U>& ) {return true;}
    00253
    00254 template<typename T, typename U>
    00255 inline bool operator!=( const scalable_allocator<T>&, const scalable_allocator<U>& ) {return false;}
    00256
    00257 } // namespace tbb
    00258
    00259 #if _MSC_VER
    00260     #if (__TBB_BUILD || __TBBMALLOC_BUILD) && !defined(__TBBMALLOC_NO_IMPLICIT_LINKAGE)
    00261         #define __TBBMALLOC_NO_IMPLICIT_LINKAGE 1
    00262     #endif
    00263
    00264     #if !__TBBMALLOC_NO_IMPLICIT_LINKAGE
    00265         #ifdef _DEBUG
    00266             #pragma comment(lib, "tbbmalloc_debug.lib")
    00267         #else
    00268             #pragma comment(lib, "tbbmalloc.lib")
    00269         #endif
    00270     #endif
    00271
    00272
    00273 #endif
    00274
    00275 #endif /* __cplusplus */
    00276
    00277 #if !defined(__cplusplus) && __ICC==1100
    00278     #pragma warning (pop)
    00279 #endif // ICC 11.0 warning 991 is back
    00280
    00281 #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.