memory_pool.h
Go to the documentation of this file.
/*
Copyright (c) 2005-2020 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef __TBB_memory_pool_H
#define __TBB_memory_pool_H
#if !TBB_PREVIEW_MEMORY_POOL
#error Set TBB_PREVIEW_MEMORY_POOL to include memory_pool.h
#endif
#include "scalable_allocator.h"
#include <new> // std::bad_alloc
#include <stdexcept> // std::runtime_error, std::invalid_argument
// required in C++03 to construct std::runtime_error and std::invalid_argument
#include <string>
#if __TBB_ALLOCATOR_CONSTRUCT_VARIADIC
#include <utility> // std::forward
#endif
#if __TBB_EXTRA_DEBUG
#define __TBBMALLOC_ASSERT ASSERT
#else
#define __TBBMALLOC_ASSERT(a,b) ((void)0)
#endif
namespace tbb {
namespace interface6 {
namespace internal {
class pool_base : tbb::internal::no_copy {
// Pool interface is separate from standard allocator classes because it has
// to maintain internal state, no copy or assignment. Move and swap are possible.
public:
void recycle() { rml::pool_reset(my_pool); }
void *malloc(size_t size) { return rml::pool_malloc(my_pool, size); }
void free(void* ptr) { rml::pool_free(my_pool, ptr); }
// Enables some low-level optimization possibilities
void *realloc(void* ptr, size_t size) {
return rml::pool_realloc(my_pool, ptr, size);
}
protected:
void destroy() { rml::pool_destroy(my_pool); }
rml::MemoryPool *my_pool;
};
} // namespace internal
#if _MSC_VER && !defined(__INTEL_COMPILER)
// Workaround for erroneous "unreferenced parameter" warning in method destroy.
#pragma warning (push)
#pragma warning (disable: 4100)
#endif
template<typename T, typename P = internal::pool_base>
class memory_pool_allocator {
protected:
typedef P pool_type;
pool_type *my_pool;
template<typename U, typename R>
friend class memory_pool_allocator;
template<typename V, typename U, typename R>
friend bool operator==( const memory_pool_allocator<V,R>& a, const memory_pool_allocator<U,R>& b);
template<typename V, typename U, typename R>
friend bool operator!=( const memory_pool_allocator<V,R>& a, const memory_pool_allocator<U,R>& b);
public:
typedef typename tbb::internal::allocator_type<T>::value_type value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
template<typename U> struct rebind {
typedef memory_pool_allocator<U, P> other;
};
explicit memory_pool_allocator(pool_type &pool) throw() : my_pool(&pool) {}
memory_pool_allocator(const memory_pool_allocator& src) throw() : my_pool(src.my_pool) {}
template<typename U>
memory_pool_allocator(const memory_pool_allocator<U,P>& src) throw() : my_pool(src.my_pool) {}
pointer address(reference x) const { return &x; }
const_pointer address(const_reference x) const { return &x; }
pointer allocate( size_type n, const void* /*hint*/ = 0) {
pointer p = static_cast<pointer>( my_pool->malloc( n*sizeof(value_type) ) );
if (!p)
tbb::internal::throw_exception(std::bad_alloc());
return p;
}
void deallocate( pointer p, size_type ) {
my_pool->free(p);
}
size_type max_size() const throw() {
size_type max = static_cast<size_type>(-1) / sizeof (value_type);
return (max > 0 ? max : 1);
}
#if __TBB_ALLOCATOR_CONSTRUCT_VARIADIC
template<typename U, typename... Args>
void construct(U *p, Args&&... args)
{ ::new((void *)p) U(std::forward<Args>(args)...); }
#else // __TBB_ALLOCATOR_CONSTRUCT_VARIADIC
#if __TBB_CPP11_RVALUE_REF_PRESENT
void construct( pointer p, value_type&& value ) {::new((void*)(p)) value_type(std::move(value));}
#endif
void construct( pointer p, const value_type& value ) { ::new((void*)(p)) value_type(value); }
#endif // __TBB_ALLOCATOR_CONSTRUCT_VARIADIC
void destroy( pointer p ) { p->~value_type(); }
};
#if _MSC_VER && !defined(__INTEL_COMPILER)
#pragma warning (pop)
#endif // warning 4100 is back
template<typename P>
class memory_pool_allocator<void, P> {
public:
typedef P pool_type;
typedef void* pointer;
typedef const void* const_pointer;
typedef void value_type;
template<typename U> struct rebind {
typedef memory_pool_allocator<U, P> other;
};
explicit memory_pool_allocator( pool_type &pool) throw() : my_pool(&pool) {}
memory_pool_allocator( const memory_pool_allocator& src) throw() : my_pool(src.my_pool) {}
template<typename U>
memory_pool_allocator(const memory_pool_allocator<U,P>& src) throw() : my_pool(src.my_pool) {}
protected:
pool_type *my_pool;
template<typename U, typename R>
friend class memory_pool_allocator;
template<typename V, typename U, typename R>
friend bool operator==( const memory_pool_allocator<V,R>& a, const memory_pool_allocator<U,R>& b);
template<typename V, typename U, typename R>
friend bool operator!=( const memory_pool_allocator<V,R>& a, const memory_pool_allocator<U,R>& b);
};
template<typename T, typename U, typename P>
inline bool operator==( const memory_pool_allocator<T,P>& a, const memory_pool_allocator<U,P>& b) {return a.my_pool==b.my_pool;}
template<typename T, typename U, typename P>
inline bool operator!=( const memory_pool_allocator<T,P>& a, const memory_pool_allocator<U,P>& b) {return a.my_pool!=b.my_pool;}
template <typename Alloc>
class memory_pool : public internal::pool_base {
Alloc my_alloc; // TODO: base-class optimization
static void *allocate_request(intptr_t pool_id, size_t & bytes);
static int deallocate_request(intptr_t pool_id, void*, size_t raw_bytes);
public:
explicit memory_pool(const Alloc &src = Alloc());
~memory_pool() { destroy(); } // call the callbacks first and destroy my_alloc latter
};
class fixed_pool : public internal::pool_base {
void *my_buffer;
size_t my_size;
inline static void *allocate_request(intptr_t pool_id, size_t & bytes);
public:
inline fixed_pool(void *buf, size_t size);
~fixed_pool() { destroy(); }
};
template <typename Alloc>
memory_pool<Alloc>::memory_pool(const Alloc &src) : my_alloc(src) {
rml::MemPoolPolicy args(allocate_request, deallocate_request,
sizeof(typename Alloc::value_type));
rml::MemPoolError res = rml::pool_create_v1(intptr_t(this), &args, &my_pool);
if (res!=rml::POOL_OK)
tbb::internal::throw_exception(std::runtime_error("Can't create pool"));
}
template <typename Alloc>
void *memory_pool<Alloc>::allocate_request(intptr_t pool_id, size_t & bytes) {
memory_pool<Alloc> &self = *reinterpret_cast<memory_pool<Alloc>*>(pool_id);
const size_t unit_size = sizeof(typename Alloc::value_type);
__TBBMALLOC_ASSERT( 0 == bytes%unit_size, NULL);
void *ptr;
__TBB_TRY { ptr = self.my_alloc.allocate( bytes/unit_size ); }
__TBB_CATCH(...) { return 0; }
return ptr;
}
#if __TBB_MSVC_UNREACHABLE_CODE_IGNORED
// Workaround for erroneous "unreachable code" warning in the template below.
// Specific for VC++ 17-18 compiler
#pragma warning (push)
#pragma warning (disable: 4702)
#endif
template <typename Alloc>
int memory_pool<Alloc>::deallocate_request(intptr_t pool_id, void* raw_ptr, size_t raw_bytes) {
memory_pool<Alloc> &self = *reinterpret_cast<memory_pool<Alloc>*>(pool_id);
const size_t unit_size = sizeof(typename Alloc::value_type);
__TBBMALLOC_ASSERT( 0 == raw_bytes%unit_size, NULL);
self.my_alloc.deallocate( static_cast<typename Alloc::value_type*>(raw_ptr), raw_bytes/unit_size );
return 0;
}
#if __TBB_MSVC_UNREACHABLE_CODE_IGNORED
#pragma warning (pop)
#endif
inline fixed_pool::fixed_pool(void *buf, size_t size) : my_buffer(buf), my_size(size) {
if (!buf || !size)
// TODO: improve support for mode with exceptions disabled
tbb::internal::throw_exception(std::invalid_argument("Zero in parameter is invalid"));
rml::MemPoolPolicy args(allocate_request, 0, size, /*fixedPool=*/true);
rml::MemPoolError res = rml::pool_create_v1(intptr_t(this), &args, &my_pool);
if (res!=rml::POOL_OK)
tbb::internal::throw_exception(std::runtime_error("Can't create pool"));
}
inline void *fixed_pool::allocate_request(intptr_t pool_id, size_t & bytes) {
fixed_pool &self = *reinterpret_cast<fixed_pool*>(pool_id);
__TBBMALLOC_ASSERT(0 != self.my_size, "The buffer must not be used twice.");
bytes = self.my_size;
self.my_size = 0; // remember that buffer has been used
return self.my_buffer;
}
} //namespace interface6
using interface6::memory_pool_allocator;
using interface6::memory_pool;
using interface6::fixed_pool;
} //namespace tbb
#undef __TBBMALLOC_ASSERT
#endif// __TBB_memory_pool_H
__TBBMALLOC_ASSERT
#define __TBBMALLOC_ASSERT(a, b)
Definition: memory_pool.h:37
scalable_allocator.h
__TBB_CATCH
#define __TBB_CATCH(e)
Definition: tbb_stddef.h:284
__TBB_TRY
#define __TBB_TRY
Definition: tbb_stddef.h:283
void
void
Definition: ittnotify_static.h:91
value
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t ITT_FORMAT d no args const wchar_t const wchar_t ITT_FORMAT s __itt_heap_function void size_t int ITT_FORMAT d __itt_heap_function void ITT_FORMAT p __itt_heap_function void void size_t int ITT_FORMAT d no args no args unsigned int ITT_FORMAT u const __itt_domain __itt_id ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain __itt_id ITT_FORMAT p const __itt_domain __itt_id __itt_timestamp __itt_timestamp ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain ITT_FORMAT p const __itt_domain __itt_string_handle unsigned long long value
Definition: ittnotify_static.h:192
size
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t size
Definition: ittnotify_static.h:109
p
void const char const char int ITT_FORMAT __itt_group_sync p
Definition: ittnotify_static.h:91
tbb
The graph class.
Definition: serial/tbb/parallel_for.h:46
tbb::move
void move(tbb_thread &t1, tbb_thread &t2)
Definition: tbb_thread.h:319
tbb::interface6::operator==
bool operator==(const memory_pool_allocator< T, P > &a, const memory_pool_allocator< U, P > &b)
Definition: memory_pool.h:180
tbb::interface6::operator!=
bool operator!=(const memory_pool_allocator< T, P > &a, const memory_pool_allocator< U, P > &b)
Definition: memory_pool.h:183
tbb::internal::throw_exception
void throw_exception(exception_id eid)
Versionless convenience wrapper for throw_exception_v4()
Definition: tbb_exception.h:105
tbb::internal::max
T max(const T &val1, const T &val2)
Utility template function returning greater of the two values.
Definition: tbb_misc.h:119
internal
Definition: _flow_graph_async_msg_impl.h:24
tbb::interface6::internal::pool_base
Base of thread-safe pool allocator for variable-size requests.
Definition: memory_pool.h:46
tbb::interface6::internal::pool_base::my_pool
rml::MemoryPool * my_pool
Definition: memory_pool.h:69
tbb::interface6::internal::pool_base::realloc
void * realloc(void *ptr, size_t size)
The "realloc" analogue complementing pool_malloc.
Definition: memory_pool.h:61
tbb::interface6::internal::pool_base::destroy
void destroy()
destroy pool - must be called in a child class
Definition: memory_pool.h:67
tbb::interface6::internal::pool_base::free
void free(void *ptr)
The "free" analogue to discard a previously allocated piece of memory.
Definition: memory_pool.h:57
tbb::interface6::internal::pool_base::recycle
void recycle()
Reset pool to reuse its memory (free all objects at once)
Definition: memory_pool.h:51
tbb::interface6::internal::pool_base::malloc
void * malloc(size_t size)
The "malloc" analogue to allocate block of memory of size bytes.
Definition: memory_pool.h:54
tbb::interface6::memory_pool_allocator
Meets "allocator" requirements of ISO C++ Standard, Section 20.1.5.
Definition: memory_pool.h:84
tbb::interface6::memory_pool_allocator::pointer
value_type * pointer
Definition: memory_pool.h:96
tbb::interface6::memory_pool_allocator::my_pool
pool_type * my_pool
Definition: memory_pool.h:87
tbb::interface6::memory_pool_allocator::allocate
pointer allocate(size_type n, const void *=0)
Allocate space for n objects.
Definition: memory_pool.h:115
tbb::interface6::memory_pool_allocator::const_reference
const value_type & const_reference
Definition: memory_pool.h:99
tbb::interface6::memory_pool_allocator::address
const_pointer address(const_reference x) const
Definition: memory_pool.h:112
tbb::interface6::memory_pool_allocator::destroy
void destroy(pointer p)
Destroy value at location pointed to by p.
Definition: memory_pool.h:143
tbb::interface6::memory_pool_allocator::difference_type
ptrdiff_t difference_type
Definition: memory_pool.h:101
tbb::interface6::memory_pool_allocator::operator!=
friend bool operator!=(const memory_pool_allocator< V, R > &a, const memory_pool_allocator< U, R > &b)
tbb::interface6::memory_pool_allocator::max_size
size_type max_size() const
Largest value for which method allocate might succeed.
Definition: memory_pool.h:126
tbb::interface6::memory_pool_allocator::memory_pool_allocator
memory_pool_allocator(pool_type &pool)
Definition: memory_pool.h:106
tbb::interface6::memory_pool_allocator::value_type
tbb::internal::allocator_type< T >::value_type value_type
Definition: memory_pool.h:95
tbb::interface6::memory_pool_allocator::address
pointer address(reference x) const
Definition: memory_pool.h:111
tbb::interface6::memory_pool_allocator::reference
value_type & reference
Definition: memory_pool.h:98
tbb::interface6::memory_pool_allocator::construct
void construct(pointer p, value_type &&value)
Copy-construct value at location pointed to by p.
Definition: memory_pool.h:137
tbb::interface6::memory_pool_allocator::const_pointer
const value_type * const_pointer
Definition: memory_pool.h:97
tbb::interface6::memory_pool_allocator::deallocate
void deallocate(pointer p, size_type)
Free previously allocated block of memory.
Definition: memory_pool.h:122
tbb::interface6::memory_pool_allocator::memory_pool_allocator
memory_pool_allocator(const memory_pool_allocator &src)
Definition: memory_pool.h:107
tbb::interface6::memory_pool_allocator::size_type
size_t size_type
Definition: memory_pool.h:100
tbb::interface6::memory_pool_allocator::operator==
friend bool operator==(const memory_pool_allocator< V, R > &a, const memory_pool_allocator< U, R > &b)
tbb::interface6::memory_pool_allocator::memory_pool_allocator
memory_pool_allocator(const memory_pool_allocator< U, P > &src)
Definition: memory_pool.h:109
tbb::interface6::memory_pool_allocator::pool_type
P pool_type
Definition: memory_pool.h:86
tbb::interface6::memory_pool_allocator::construct
void construct(pointer p, const value_type &value)
Definition: memory_pool.h:139
tbb::interface6::memory_pool_allocator::rebind
Definition: memory_pool.h:102
tbb::interface6::memory_pool_allocator::rebind::other
memory_pool_allocator< U, P > other
Definition: memory_pool.h:103
tbb::interface6::memory_pool_allocator< void, P >::memory_pool_allocator
memory_pool_allocator(const memory_pool_allocator &src)
Definition: memory_pool.h:165
tbb::interface6::memory_pool_allocator< void, P >::my_pool
pool_type * my_pool
Definition: memory_pool.h:170
tbb::interface6::memory_pool_allocator< void, P >::operator!=
friend bool operator!=(const memory_pool_allocator< V, R > &a, const memory_pool_allocator< U, R > &b)
tbb::interface6::memory_pool_allocator< void, P >::const_pointer
const void * const_pointer
Definition: memory_pool.h:158
tbb::interface6::memory_pool_allocator< void, P >::value_type
void value_type
Definition: memory_pool.h:159
tbb::interface6::memory_pool_allocator< void, P >::memory_pool_allocator
memory_pool_allocator(const memory_pool_allocator< U, P > &src)
Definition: memory_pool.h:167
tbb::interface6::memory_pool_allocator< void, P >::memory_pool_allocator
memory_pool_allocator(pool_type &pool)
Definition: memory_pool.h:164
tbb::interface6::memory_pool_allocator< void, P >::pool_type
P pool_type
Definition: memory_pool.h:156
tbb::interface6::memory_pool_allocator< void, P >::operator==
friend bool operator==(const memory_pool_allocator< V, R > &a, const memory_pool_allocator< U, R > &b)
tbb::interface6::memory_pool_allocator< void, P >::pointer
void * pointer
Definition: memory_pool.h:157
tbb::interface6::memory_pool_allocator< void, P >::rebind::other
memory_pool_allocator< U, P > other
Definition: memory_pool.h:161
tbb::interface6::memory_pool
Thread-safe growable pool allocator for variable-size requests.
Definition: memory_pool.h:188
tbb::interface6::memory_pool::deallocate_request
static int deallocate_request(intptr_t pool_id, void *, size_t raw_bytes)
Definition: memory_pool.h:241
tbb::interface6::memory_pool::~memory_pool
~memory_pool()
destroy pool
Definition: memory_pool.h:198
tbb::interface6::memory_pool::my_alloc
Alloc my_alloc
Definition: memory_pool.h:189
tbb::interface6::memory_pool::allocate_request
static void * allocate_request(intptr_t pool_id, size_t &bytes)
Definition: memory_pool.h:225
tbb::interface6::memory_pool::memory_pool
memory_pool(const Alloc &src=Alloc())
construct pool with underlying allocator
Definition: memory_pool.h:217
tbb::interface6::fixed_pool
Definition: memory_pool.h:202
tbb::interface6::fixed_pool::allocate_request
static void * allocate_request(intptr_t pool_id, size_t &bytes)
Definition: memory_pool.h:260
tbb::interface6::fixed_pool::my_buffer
void * my_buffer
Definition: memory_pool.h:203
tbb::interface6::fixed_pool::fixed_pool
fixed_pool(void *buf, size_t size)
construct pool with underlying allocator
Definition: memory_pool.h:251
tbb::interface6::fixed_pool::my_size
size_t my_size
Definition: memory_pool.h:204
tbb::interface6::fixed_pool::~fixed_pool
~fixed_pool()
destroy pool
Definition: memory_pool.h:211
tbb::internal::no_copy
Base class for types that should not be copied or assigned.
Definition: tbb_stddef.h:330
tbb::internal::allocator_type::value_type
T value_type
Definition: tbb_stddef.h:472

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