Add Polymorphic Memory Resource utilities and rework the separately compiled library:

- Dlmalloc's based C function are boost_cont_xxx no longer exported, but wrapped into C++ linkage dlmalloc_xxx functions to effectively reuse Boost's dynamic library and autolink machinery instead of rewriting machinery to compile the C source file.
- Refactored scoped_allocator_adaptor's construct logic as it was shared with polymorphic allocator's one. Moved common logic to detail/dispatch_uses_allocator.hpp. Refactored also scoped_allocator_adaptor test utilities to be reused with polymorphic_allocator tests.
This commit is contained in:
Ion Gaztañaga
2015-09-07 19:16:46 +02:00
parent 77100c0bfc
commit 33d2f0f7af
110 changed files with 10143 additions and 1160 deletions
+108
View File
@@ -0,0 +1,108 @@
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2012-2013. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/container for documentation.
//
//////////////////////////////////////////////////////////////////////////////
#define BOOST_CONTAINER_SOURCE
#include <boost/container/detail/dlmalloc.hpp>
namespace boost{
namespace container{
BOOST_CONTAINER_DECL size_t dlmalloc_size(const void *p)
{ return boost_cont_size(p); }
BOOST_CONTAINER_DECL void* dlmalloc_malloc(size_t bytes)
{ return boost_cont_malloc(bytes); }
BOOST_CONTAINER_DECL void dlmalloc_free(void* mem)
{ return boost_cont_free(mem); }
BOOST_CONTAINER_DECL void* dlmalloc_memalign(size_t bytes, size_t alignment)
{ return boost_cont_memalign(bytes, alignment); }
BOOST_CONTAINER_DECL int dlmalloc_multialloc_nodes
(size_t n_elements, size_t elem_size, size_t contiguous_elements, boost_cont_memchain *pchain)
{ return boost_cont_multialloc_nodes(n_elements, elem_size, contiguous_elements, pchain); }
BOOST_CONTAINER_DECL int dlmalloc_multialloc_arrays
(size_t n_elements, const size_t *sizes, size_t sizeof_element, size_t contiguous_elements, boost_cont_memchain *pchain)
{ return boost_cont_multialloc_arrays(n_elements, sizes, sizeof_element, contiguous_elements, pchain); }
BOOST_CONTAINER_DECL void dlmalloc_multidealloc(boost_cont_memchain *pchain)
{ return boost_cont_multidealloc(pchain); }
BOOST_CONTAINER_DECL size_t dlmalloc_footprint()
{ return boost_cont_footprint(); }
BOOST_CONTAINER_DECL size_t dlmalloc_allocated_memory()
{ return boost_cont_allocated_memory(); }
BOOST_CONTAINER_DECL size_t dlmalloc_chunksize(const void *p)
{ return boost_cont_chunksize(p); }
BOOST_CONTAINER_DECL int dlmalloc_all_deallocated()
{ return boost_cont_all_deallocated(); }
BOOST_CONTAINER_DECL boost_cont_malloc_stats_t dlmalloc_malloc_stats()
{ return boost_cont_malloc_stats(); }
BOOST_CONTAINER_DECL size_t dlmalloc_in_use_memory()
{ return boost_cont_in_use_memory(); }
BOOST_CONTAINER_DECL int dlmalloc_trim(size_t pad)
{ return boost_cont_trim(pad); }
BOOST_CONTAINER_DECL int dlmalloc_mallopt(int parameter_number, int parameter_value)
{ return boost_cont_mallopt(parameter_number, parameter_value); }
BOOST_CONTAINER_DECL int dlmalloc_grow
(void* oldmem, size_t minbytes, size_t maxbytes, size_t *received)
{ return boost_cont_grow(oldmem, minbytes, maxbytes, received); }
BOOST_CONTAINER_DECL int dlmalloc_shrink
(void* oldmem, size_t minbytes, size_t maxbytes, size_t *received, int do_commit)
{ return boost_cont_shrink(oldmem, minbytes, maxbytes, received, do_commit); }
BOOST_CONTAINER_DECL void* dlmalloc_alloc
(size_t minbytes, size_t preferred_bytes, size_t *received_bytes)
{ return boost_cont_alloc(minbytes, preferred_bytes, received_bytes); }
BOOST_CONTAINER_DECL int dlmalloc_malloc_check()
{ return boost_cont_malloc_check(); }
BOOST_CONTAINER_DECL boost_cont_command_ret_t dlmalloc_allocation_command
( allocation_type command
, size_t sizeof_object
, size_t limit_objects
, size_t preferred_objects
, size_t *received_objects
, void *reuse_ptr
)
{ return boost_cont_allocation_command(command, sizeof_object, limit_objects, preferred_objects, received_objects, reuse_ptr); }
BOOST_CONTAINER_DECL void *dlmalloc_sync_create()
{ return boost_cont_sync_create(); }
BOOST_CONTAINER_DECL void dlmalloc_sync_destroy(void *sync)
{ return boost_cont_sync_destroy(sync); }
BOOST_CONTAINER_DECL bool dlmalloc_sync_lock(void *sync)
{ return boost_cont_sync_lock(sync) != 0; }
BOOST_CONTAINER_DECL void dlmalloc_sync_unlock(void *sync)
{ return boost_cont_sync_unlock(sync); }
BOOST_CONTAINER_DECL bool dlmalloc_global_sync_lock()
{ return boost_cont_global_sync_lock() != 0; }
BOOST_CONTAINER_DECL void dlmalloc_global_sync_unlock()
{ return boost_cont_global_sync_unlock(); }
} //namespace container{
} //namespace boost{
+60 -23
View File
@@ -1,6 +1,6 @@
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2007-2013. Distributed under the Boost
// (C) Copyright Ion Gaztanaga 2007-2015. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
@@ -8,8 +8,6 @@
//
//////////////////////////////////////////////////////////////////////////////
#define BOOST_CONTAINER_SOURCE
#include <boost/container/detail/alloc_lib.h>
#include "errno.h" //dlmalloc bug EINVAL is used in posix_memalign without checking LACKS_ERRNO_H
@@ -1074,7 +1072,7 @@ static int internal_multialloc_arrays
return 1;
}
BOOST_CONTAINER_DECL int boost_cont_multialloc_arrays
int boost_cont_multialloc_arrays
(size_t n_elements, const size_t *sizes, size_t element_size, size_t contiguous_elements, boost_cont_memchain *pchain)
{
int ret = 0;
@@ -1127,10 +1125,10 @@ static boost_cont_malloc_stats_t get_malloc_stats(mstate m)
return ret;
}
BOOST_CONTAINER_DECL size_t boost_cont_size(const void *p)
size_t boost_cont_size(const void *p)
{ return DL_SIZE_IMPL(p); }
BOOST_CONTAINER_DECL void* boost_cont_malloc(size_t bytes)
void* boost_cont_malloc(size_t bytes)
{
size_t received_bytes;
ensure_initialization();
@@ -1138,7 +1136,7 @@ BOOST_CONTAINER_DECL void* boost_cont_malloc(size_t bytes)
(BOOST_CONTAINER_ALLOCATE_NEW, 1, bytes, bytes, &received_bytes, 0).first;
}
BOOST_CONTAINER_DECL void boost_cont_free(void* mem)
void boost_cont_free(void* mem)
{
mstate ms = (mstate)gm;
if (!ok_magic(ms)) {
@@ -1150,7 +1148,7 @@ BOOST_CONTAINER_DECL void boost_cont_free(void* mem)
}
}
BOOST_CONTAINER_DECL void* boost_cont_memalign(size_t bytes, size_t alignment)
void* boost_cont_memalign(size_t bytes, size_t alignment)
{
void *addr;
ensure_initialization();
@@ -1161,7 +1159,7 @@ BOOST_CONTAINER_DECL void* boost_cont_memalign(size_t bytes, size_t alignment)
return addr;
}
BOOST_CONTAINER_DECL int boost_cont_multialloc_nodes
int boost_cont_multialloc_nodes
(size_t n_elements, size_t elem_size, size_t contiguous_elements, boost_cont_memchain *pchain)
{
int ret = 0;
@@ -1177,12 +1175,12 @@ BOOST_CONTAINER_DECL int boost_cont_multialloc_nodes
return ret;
}
BOOST_CONTAINER_DECL size_t boost_cont_footprint()
size_t boost_cont_footprint()
{
return ((mstate)gm)->footprint;
}
BOOST_CONTAINER_DECL size_t boost_cont_allocated_memory()
size_t boost_cont_allocated_memory()
{
size_t alloc_mem = 0;
mstate m = (mstate)gm;
@@ -1227,13 +1225,13 @@ BOOST_CONTAINER_DECL size_t boost_cont_allocated_memory()
return alloc_mem;
}
BOOST_CONTAINER_DECL size_t boost_cont_chunksize(const void *p)
size_t boost_cont_chunksize(const void *p)
{ return chunksize(mem2chunk(p)); }
BOOST_CONTAINER_DECL int boost_cont_all_deallocated()
int boost_cont_all_deallocated()
{ return !s_allocated_memory; }
BOOST_CONTAINER_DECL boost_cont_malloc_stats_t boost_cont_malloc_stats()
boost_cont_malloc_stats_t boost_cont_malloc_stats()
{
mstate ms = (mstate)gm;
if (ok_magic(ms)) {
@@ -1246,16 +1244,16 @@ BOOST_CONTAINER_DECL boost_cont_malloc_stats_t boost_cont_malloc_stats()
}
}
BOOST_CONTAINER_DECL size_t boost_cont_in_use_memory()
size_t boost_cont_in_use_memory()
{ return s_allocated_memory; }
BOOST_CONTAINER_DECL int boost_cont_trim(size_t pad)
int boost_cont_trim(size_t pad)
{
ensure_initialization();
return dlmalloc_trim(pad);
}
BOOST_CONTAINER_DECL int boost_cont_grow
int boost_cont_grow
(void* oldmem, size_t minbytes, size_t maxbytes, size_t *received)
{
mstate ms = (mstate)gm;
@@ -1279,7 +1277,7 @@ BOOST_CONTAINER_DECL int boost_cont_grow
return 0;
}
BOOST_CONTAINER_DECL int boost_cont_shrink
int boost_cont_shrink
(void* oldmem, size_t minbytes, size_t maxbytes, size_t *received, int do_commit)
{
mstate ms = (mstate)gm;
@@ -1297,7 +1295,7 @@ BOOST_CONTAINER_DECL int boost_cont_shrink
}
BOOST_CONTAINER_DECL void* boost_cont_alloc
void* boost_cont_alloc
(size_t minbytes, size_t preferred_bytes, size_t *received_bytes)
{
//ensure_initialization provided by boost_cont_allocation_command
@@ -1305,7 +1303,7 @@ BOOST_CONTAINER_DECL void* boost_cont_alloc
(BOOST_CONTAINER_ALLOCATE_NEW, 1, minbytes, preferred_bytes, received_bytes, 0).first;
}
BOOST_CONTAINER_DECL void boost_cont_multidealloc(boost_cont_memchain *pchain)
void boost_cont_multidealloc(boost_cont_memchain *pchain)
{
mstate ms = (mstate)gm;
if (!ok_magic(ms)) {
@@ -1315,7 +1313,7 @@ BOOST_CONTAINER_DECL void boost_cont_multidealloc(boost_cont_memchain *pchain)
internal_multialloc_free(ms, pchain);
}
BOOST_CONTAINER_DECL int boost_cont_malloc_check()
int boost_cont_malloc_check()
{
#ifdef DEBUG
mstate ms = (mstate)gm;
@@ -1333,7 +1331,7 @@ BOOST_CONTAINER_DECL int boost_cont_malloc_check()
}
BOOST_CONTAINER_DECL boost_cont_command_ret_t boost_cont_allocation_command
boost_cont_command_ret_t boost_cont_allocation_command
(allocation_type command, size_t sizeof_object, size_t limit_size
, size_t preferred_size, size_t *received_size, void *reuse_ptr)
{
@@ -1408,11 +1406,50 @@ BOOST_CONTAINER_DECL boost_cont_command_ret_t boost_cont_allocation_command
return ret;
}
BOOST_CONTAINER_DECL int boost_cont_mallopt(int param_number, int value)
int boost_cont_mallopt(int param_number, int value)
{
return change_mparam(param_number, value);
}
void *boost_cont_sync_create()
{
void *p = boost_cont_malloc(sizeof(MLOCK_T));
if(p){
if(0 != INITIAL_LOCK((MLOCK_T*)p)){
boost_cont_free(p);
p = 0;
}
}
return p;
}
void boost_cont_sync_destroy(void *sync)
{
if(sync){
(void)DESTROY_LOCK((MLOCK_T*)sync);
boost_cont_free(sync);
}
}
int boost_cont_sync_lock(void *sync)
{ return 0 == (ACQUIRE_LOCK((MLOCK_T*)sync)); }
void boost_cont_sync_unlock(void *sync)
{ RELEASE_LOCK((MLOCK_T*)sync); }
int boost_cont_global_sync_lock()
{
int ret;
ensure_initialization();
ret = ACQUIRE_MALLOC_GLOBAL_LOCK();
return 0 == ret;
}
void boost_cont_global_sync_unlock()
{
RELEASE_MALLOC_GLOBAL_LOCK()
}
//#ifdef DL_DEBUG_DEFINED
// #undef DEBUG
//#endif
+106
View File
@@ -0,0 +1,106 @@
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2015-2015. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/container for documentation.
//
//////////////////////////////////////////////////////////////////////////////
#define BOOST_CONTAINER_SOURCE
#include <boost/container/pmr/memory_resource.hpp>
#include <boost/core/no_exceptions_support.hpp>
#include <boost/container/throw_exception.hpp>
#include <boost/container/detail/dlmalloc.hpp> //For global lock
#include <cstddef>
#include <new>
namespace boost {
namespace container {
namespace pmr {
class new_delete_resource_imp
: public memory_resource
{
public:
virtual ~new_delete_resource_imp()
{}
virtual void* do_allocate(std::size_t bytes, std::size_t alignment)
{ return new char[bytes]; (void)bytes; (void)alignment; }
virtual void do_deallocate(void* p, std::size_t bytes, std::size_t alignment)
{ delete[]((char*)p); (void)bytes; (void)alignment; }
virtual bool do_is_equal(const memory_resource& other) const BOOST_NOEXCEPT
{ return &other == this; }
} new_delete_resource_instance;
struct null_memory_resource_imp
: public memory_resource
{
public:
virtual ~null_memory_resource_imp()
{}
virtual void* do_allocate(std::size_t bytes, std::size_t alignment)
{
(void)bytes; (void)alignment;
throw_bad_alloc();
return 0;
}
virtual void do_deallocate(void* p, std::size_t bytes, std::size_t alignment)
{ (void)p; (void)bytes; (void)alignment; }
virtual bool do_is_equal(const memory_resource& other) const BOOST_NOEXCEPT
{ return &other == this; }
} null_memory_resource_instance;
BOOST_CONTAINER_DECL memory_resource* new_delete_resource() BOOST_NOEXCEPT
{
return &new_delete_resource_instance;
}
BOOST_CONTAINER_DECL memory_resource* null_memory_resource() BOOST_NOEXCEPT
{
return &null_memory_resource_instance;
}
static memory_resource *default_memory_resource = &new_delete_resource_instance;
BOOST_CONTAINER_DECL memory_resource* set_default_resource(memory_resource* r) BOOST_NOEXCEPT
{
//TO-DO: synchronizes-with part using atomics
if(dlmalloc_global_sync_lock()){
memory_resource *previous = default_memory_resource;
default_memory_resource = r ? r : new_delete_resource();
dlmalloc_global_sync_unlock();
return previous;
}
else{
return new_delete_resource();
}
}
BOOST_CONTAINER_DECL memory_resource* get_default_resource() BOOST_NOEXCEPT
{
//TO-DO: synchronizes-with part using atomics
if(dlmalloc_global_sync_lock()){
memory_resource *current = default_memory_resource;
dlmalloc_global_sync_unlock();
return current;
}
else{
return new_delete_resource();
}
}
} //namespace pmr {
} //namespace container {
} //namespace boost {
+154
View File
@@ -0,0 +1,154 @@
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2015-2015. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/container for documentation.
//
//////////////////////////////////////////////////////////////////////////////
#define BOOST_CONTAINER_SOURCE
#include <boost/container/detail/config_begin.hpp>
#include <boost/container/detail/workaround.hpp>
#include <boost/container/pmr/monotonic_buffer_resource.hpp>
#include <boost/container/pmr/global_resource.hpp>
#include <boost/container/detail/min_max.hpp>
#include <boost/intrusive/detail/math.hpp>
#include <boost/container/throw_exception.hpp>
#include <cstddef>
namespace {
#ifdef BOOST_HAS_INTPTR_T
typedef boost::uintptr_t uintptr_type;
#else
typedef std::size_t uintptr_type;
#endif
static const std::size_t minimum_buffer_size = 2*sizeof(void*);
} //namespace {
namespace boost {
namespace container {
namespace pmr {
void monotonic_buffer_resource::increase_next_buffer()
{
m_next_buffer_size = (std::size_t(-1)/2 < m_next_buffer_size) ? std::size_t(-1) : m_next_buffer_size*2;
}
void monotonic_buffer_resource::increase_next_buffer_at_least_to(std::size_t minimum_size)
{
if(m_next_buffer_size < minimum_size){
if(bi::detail::is_pow2(minimum_size)){
m_next_buffer_size = minimum_size;
}
else if(std::size_t(-1)/2 < minimum_size){
m_next_buffer_size = minimum_size;
}
else{
m_next_buffer_size = bi::detail::ceil_pow2(minimum_size);
}
}
}
monotonic_buffer_resource::monotonic_buffer_resource(memory_resource* upstream) BOOST_NOEXCEPT
: m_memory_blocks(upstream ? *upstream : *get_default_resource())
, m_current_buffer(0u)
, m_current_buffer_size(0u)
, m_next_buffer_size(initial_next_buffer_size)
{}
monotonic_buffer_resource::monotonic_buffer_resource(std::size_t initial_size, memory_resource* upstream) BOOST_NOEXCEPT
: m_memory_blocks(upstream ? *upstream : *get_default_resource())
, m_current_buffer(0u)
, m_current_buffer_size(0u)
, m_next_buffer_size(minimum_buffer_size)
{ //In case initial_size is zero
this->increase_next_buffer_at_least_to(initial_size + !initial_size);
}
monotonic_buffer_resource::monotonic_buffer_resource(void* buffer, std::size_t buffer_size, memory_resource* upstream) BOOST_NOEXCEPT
: m_memory_blocks(upstream ? *upstream : *get_default_resource())
, m_current_buffer(buffer)
, m_current_buffer_size(buffer_size)
, m_next_buffer_size
(bi::detail::previous_or_equal_pow2
(boost::container::container_detail::max_value(buffer_size, std::size_t(initial_next_buffer_size))))
{ this->increase_next_buffer(); }
monotonic_buffer_resource::~monotonic_buffer_resource()
{ this->release(); }
void monotonic_buffer_resource::release() BOOST_NOEXCEPT
{ m_memory_blocks.release(); }
memory_resource* monotonic_buffer_resource::upstream_resource() const BOOST_NOEXCEPT
{ return &m_memory_blocks.upstream_resource(); }
std::size_t monotonic_buffer_resource::remaining_storage(std::size_t alignment, std::size_t &wasted_due_to_alignment) const BOOST_NOEXCEPT
{
const uintptr_type up_alignment_minus1 = alignment - 1u;
const uintptr_type up_alignment_mask = ~up_alignment_minus1;
const uintptr_type up_addr = uintptr_type(m_current_buffer);
const uintptr_type up_aligned_addr = (up_addr + up_alignment_minus1) & up_alignment_mask;
wasted_due_to_alignment = std::size_t(up_aligned_addr - up_addr);
return m_current_buffer_size <= wasted_due_to_alignment ? 0u : m_current_buffer_size - wasted_due_to_alignment;
}
std::size_t monotonic_buffer_resource::remaining_storage(std::size_t alignment) const BOOST_NOEXCEPT
{
std::size_t ignore_this;
return this->remaining_storage(alignment, ignore_this);
}
const void *monotonic_buffer_resource::current_buffer() const BOOST_NOEXCEPT
{ return m_current_buffer; }
std::size_t monotonic_buffer_resource::next_buffer_size() const BOOST_NOEXCEPT
{ return m_next_buffer_size; }
void *monotonic_buffer_resource::allocate_from_current(std::size_t aligner, std::size_t bytes)
{
char * p = (char*)m_current_buffer + aligner;
m_current_buffer = p + bytes;
m_current_buffer_size -= aligner + bytes;
return p;
}
void* monotonic_buffer_resource::do_allocate(std::size_t bytes, std::size_t alignment)
{
if(alignment > memory_resource::max_align)
throw_bad_alloc();
//See if there is room in current buffer
std::size_t aligner = 0u;
if(this->remaining_storage(alignment, aligner) < bytes){
//Update next_buffer_size to at least bytes
this->increase_next_buffer_at_least_to(bytes);
//Now allocate and update internal data
m_current_buffer = (char*)m_memory_blocks.allocate(m_next_buffer_size);
m_current_buffer_size = m_next_buffer_size;
this->increase_next_buffer();
}
//Enough internal storage, extract from it
return this->allocate_from_current(aligner, bytes);
}
void monotonic_buffer_resource::do_deallocate(void* p, std::size_t bytes, std::size_t alignment) BOOST_NOEXCEPT
{ (void)p; (void)bytes; (void)alignment; }
bool monotonic_buffer_resource::do_is_equal(const memory_resource& other) const BOOST_NOEXCEPT
{ return this == dynamic_cast<const monotonic_buffer_resource*>(&other); }
} //namespace pmr {
} //namespace container {
} //namespace boost {
#include <boost/container/detail/config_end.hpp>
+291
View File
@@ -0,0 +1,291 @@
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2015-2015. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/container for documentation.
//
//////////////////////////////////////////////////////////////////////////////
#define BOOST_CONTAINER_SOURCE
#include <boost/container/detail/config_begin.hpp>
#include <boost/container/detail/workaround.hpp>
#include <boost/container/pmr/global_resource.hpp>
#include <boost/container/detail/pool_resource.hpp>
#include <boost/container/detail/block_slist.hpp>
#include <boost/container/detail/min_max.hpp>
#include <boost/container/detail/placement_new.hpp>
#include <boost/intrusive/linear_slist_algorithms.hpp>
#include <boost/intrusive/detail/math.hpp>
#include <cstddef>
namespace boost {
namespace container {
namespace pmr {
//pool_data_t
class pool_data_t
: public block_slist_base<>
{
typedef block_slist_base<> block_slist_base;
public:
explicit pool_data_t(std::size_t initial_blocks_per_chunk)
: block_slist_base(), next_blocks_per_chunk(initial_blocks_per_chunk)
{ slist_algo::init_header(&free_slist); }
void *allocate_block() BOOST_NOEXCEPT
{
if(slist_algo::unique(&free_slist)){
return 0;
}
slist_node *pv = slist_algo::node_traits::get_next(&free_slist);
slist_algo::unlink_after(&free_slist);
pv->~slist_node();
return pv;
}
void deallocate_block(void *p) BOOST_NOEXCEPT
{
slist_node *pv = ::new(p, boost_container_new_t()) slist_node();
slist_algo::link_after(&free_slist, pv);
}
void release(memory_resource &upstream)
{
slist_algo::init_header(&free_slist);
this->block_slist_base::release(upstream);
next_blocks_per_chunk = pool_options_minimum_max_blocks_per_chunk;
}
void replenish(memory_resource &mr, std::size_t pool_block, std::size_t max_blocks_per_chunk)
{
//Limit max value
std::size_t blocks_per_chunk = boost::container::container_detail::min_value(max_blocks_per_chunk, next_blocks_per_chunk);
//Avoid overflow
blocks_per_chunk = boost::container::container_detail::min_value(blocks_per_chunk, std::size_t(-1)/pool_block);
//Minimum block size is at least max_align, so all pools allocate sizes that are multiple of max_align,
//meaning that all blocks are max_align-aligned.
char *p = static_cast<char *>(block_slist_base::allocate(blocks_per_chunk*pool_block, mr));
//Create header types. This is no-throw
for(std::size_t i = 0, max = blocks_per_chunk; i != max; ++i){
slist_node *const pv = ::new(p, boost_container_new_t()) slist_node();
slist_algo::link_after(&free_slist, pv);
p += pool_block;
}
//Update next block per chunk
next_blocks_per_chunk = max_blocks_per_chunk/2u < blocks_per_chunk ? max_blocks_per_chunk : blocks_per_chunk*2u;
}
std::size_t cache_count() const
{ return slist_algo::count(&free_slist) - 1u; }
slist_node free_slist;
std::size_t next_blocks_per_chunk;
};
//pool_resource
//Detect overflow in ceil_pow2
BOOST_STATIC_ASSERT(pool_options_default_max_blocks_per_chunk <= (std::size_t(-1)/2u+1u));
//Sanity checks
BOOST_STATIC_ASSERT(bi::detail::static_is_pow2<pool_options_default_max_blocks_per_chunk>::value);
BOOST_STATIC_ASSERT(bi::detail::static_is_pow2<pool_options_minimum_largest_required_pool_block>::value);
//unsynchronized_pool_resource
void pool_resource::priv_limit_option(std::size_t &val, std::size_t min, std::size_t max) //static
{
if(!val){
val = max;
}
else{
val = val < min ? min : boost::container::container_detail::min_value(val, max);
}
}
std::size_t pool_resource::priv_pool_index(std::size_t block_size) //static
{
//For allocations equal or less than pool_options_minimum_largest_required_pool_block
//the smallest pool is used
block_size = boost::container::container_detail::max_value(block_size, pool_options_minimum_largest_required_pool_block);
return bi::detail::ceil_log2(block_size)
- bi::detail::ceil_log2(pool_options_minimum_largest_required_pool_block);
}
std::size_t pool_resource::priv_pool_block(std::size_t index) //static
{
//For allocations equal or less than pool_options_minimum_largest_required_pool_block
//the smallest pool is used
return pool_options_minimum_largest_required_pool_block << index;
}
void pool_resource::priv_fix_options()
{
priv_limit_option(m_options.max_blocks_per_chunk
, pool_options_minimum_max_blocks_per_chunk
, pool_options_default_max_blocks_per_chunk);
priv_limit_option
( m_options.largest_required_pool_block
, pool_options_minimum_largest_required_pool_block
, pool_options_default_largest_required_pool_block);
m_options.largest_required_pool_block = bi::detail::ceil_pow2(m_options.largest_required_pool_block);
}
void pool_resource::priv_init_pools()
{
const std::size_t num_pools = priv_pool_index(m_options.largest_required_pool_block)+1u;
//Otherwise, just use the default alloc (zero pools)
void *p = 0;
//This can throw
p = m_upstream.allocate(sizeof(pool_data_t)*num_pools);
//This is nothrow
m_pool_data = static_cast<pool_data_t *>(p);
for(std::size_t i = 0, max = num_pools; i != max; ++i){
::new(&m_pool_data[i], boost_container_new_t()) pool_data_t(pool_options_minimum_max_blocks_per_chunk);
}
m_pool_count = num_pools;
}
void pool_resource::priv_constructor_body()
{
this->priv_fix_options();
}
pool_resource::pool_resource(const pool_options& opts, memory_resource* upstream) BOOST_NOEXCEPT
: m_options(opts), m_upstream(*upstream), m_oversized_list(), m_pool_data(), m_pool_count()
{ this->priv_constructor_body(); }
pool_resource::pool_resource() BOOST_NOEXCEPT
: m_options(), m_upstream(*get_default_resource()), m_oversized_list(), m_pool_data(), m_pool_count()
{ this->priv_constructor_body(); }
pool_resource::pool_resource(memory_resource* upstream) BOOST_NOEXCEPT
: m_options(), m_upstream(*upstream), m_oversized_list(), m_pool_data(), m_pool_count()
{ this->priv_constructor_body(); }
pool_resource::pool_resource(const pool_options& opts) BOOST_NOEXCEPT
: m_options(opts), m_upstream(*get_default_resource()), m_oversized_list(), m_pool_data(), m_pool_count()
{ this->priv_constructor_body(); }
pool_resource::~pool_resource() //virtual
{
this->release();
for(std::size_t i = 0, max = m_pool_count; i != max; ++i){
m_pool_data[i].~pool_data_t();
}
if(m_pool_data){
m_upstream.deallocate((void*)m_pool_data, sizeof(pool_data_t)*m_pool_count);
}
}
void pool_resource::release()
{
m_oversized_list.release(m_upstream);
for(std::size_t i = 0, max = m_pool_count; i != max; ++i)
{
m_pool_data[i].release(m_upstream);
}
}
memory_resource* pool_resource::upstream_resource() const
{ return &m_upstream; }
pool_options pool_resource::options() const
{ return m_options; }
void* pool_resource::do_allocate(std::size_t bytes, std::size_t alignment) //virtual
{
if(!m_pool_data){
this->priv_init_pools();
}
(void)alignment; //alignment ignored here, max_align is used by pools
if(bytes > m_options.largest_required_pool_block){
return m_oversized_list.allocate(bytes, m_upstream);
}
else{
const std::size_t pool_idx = priv_pool_index(bytes);
pool_data_t & pool = m_pool_data[pool_idx];
void *p = pool.allocate_block();
if(!p){
pool.replenish(m_upstream, priv_pool_block(pool_idx), m_options.max_blocks_per_chunk);
p = pool.allocate_block();
}
return p;
}
}
void pool_resource::do_deallocate(void* p, std::size_t bytes, std::size_t alignment) //virtual
{
(void)alignment; //alignment ignored here, max_align is used by pools
if(bytes > m_options.largest_required_pool_block){
//Just cached
return m_oversized_list.deallocate(p, m_upstream);
}
else{
const std::size_t pool_idx = priv_pool_index(bytes);
return m_pool_data[pool_idx].deallocate_block(p);
}
}
bool pool_resource::do_is_equal(const memory_resource& other) const BOOST_NOEXCEPT //virtual
{ return this == dynamic_cast<const pool_resource*>(&other); }
std::size_t pool_resource::pool_count() const
{
if(BOOST_LIKELY((0 != m_pool_data))){
return m_pool_count;
}
else{
return priv_pool_index(m_options.largest_required_pool_block)+1u;
}
}
std::size_t pool_resource::pool_index(std::size_t bytes) const
{
if(bytes > m_options.largest_required_pool_block){
return pool_count();
}
else{
return priv_pool_index(bytes);
}
}
std::size_t pool_resource::pool_next_blocks_per_chunk(std::size_t pool_idx) const
{
if(BOOST_LIKELY((m_pool_data && pool_idx < m_pool_count))){
return m_pool_data[pool_idx].next_blocks_per_chunk;
}
else{
return 1u;
}
}
std::size_t pool_resource::pool_block(std::size_t pool_idx) const
{ return priv_pool_block(pool_idx); }
std::size_t pool_resource::pool_cached_blocks(std::size_t pool_idx) const
{
if(BOOST_LIKELY((m_pool_data && pool_idx < m_pool_count))){
return m_pool_data[pool_idx].cache_count();
}
else{
return 0u;
}
}
} //namespace pmr {
} //namespace container {
} //namespace boost {
#include <boost/container/detail/config_end.hpp>
+123
View File
@@ -0,0 +1,123 @@
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2015-2015. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/container for documentation.
//
//////////////////////////////////////////////////////////////////////////////
#define BOOST_CONTAINER_SOURCE
#include <boost/container/detail/config_begin.hpp>
#include <boost/container/detail/workaround.hpp>
#include <boost/container/detail/dlmalloc.hpp>
#include <boost/container/pmr/synchronized_pool_resource.hpp>
#include <cstddef>
namespace {
using namespace boost::container;
class dlmalloc_sync_scoped_lock
{
void *m_sync;
public:
explicit dlmalloc_sync_scoped_lock(void *sync)
: m_sync(sync)
{
if(!dlmalloc_sync_lock(m_sync)){
throw_bad_alloc();
}
}
~dlmalloc_sync_scoped_lock()
{
dlmalloc_sync_unlock(m_sync);
}
};
} //namespace {
namespace boost {
namespace container {
namespace pmr {
synchronized_pool_resource::synchronized_pool_resource(const pool_options& opts, memory_resource* upstream) BOOST_NOEXCEPT
: m_pool_resource(opts, upstream), m_opaque_sync()
{}
synchronized_pool_resource::synchronized_pool_resource() BOOST_NOEXCEPT
: m_pool_resource(), m_opaque_sync()
{}
synchronized_pool_resource::synchronized_pool_resource(memory_resource* upstream) BOOST_NOEXCEPT
: m_pool_resource(upstream), m_opaque_sync()
{}
synchronized_pool_resource::synchronized_pool_resource(const pool_options& opts) BOOST_NOEXCEPT
: m_pool_resource(opts), m_opaque_sync()
{}
synchronized_pool_resource::~synchronized_pool_resource() //virtual
{
if(m_opaque_sync)
dlmalloc_sync_destroy(m_opaque_sync);
}
void synchronized_pool_resource::release()
{
if(m_opaque_sync){ //If there is no mutex, no allocation could be done
m_pool_resource.release();
}
}
memory_resource* synchronized_pool_resource::upstream_resource() const
{ return m_pool_resource.upstream_resource(); }
pool_options synchronized_pool_resource::options() const
{ return m_pool_resource.options(); }
void* synchronized_pool_resource::do_allocate(std::size_t bytes, std::size_t alignment) //virtual
{
if(!m_opaque_sync){ //If there is no mutex, no allocation could be done
m_opaque_sync = dlmalloc_sync_create();
if(!m_opaque_sync){
throw_bad_alloc();
}
}
dlmalloc_sync_scoped_lock lock(m_opaque_sync); (void)lock;
return m_pool_resource.do_allocate(bytes, alignment);
}
void synchronized_pool_resource::do_deallocate(void* p, std::size_t bytes, std::size_t alignment) //virtual
{
dlmalloc_sync_scoped_lock lock(m_opaque_sync); (void)lock;
return m_pool_resource.do_deallocate(p, bytes, alignment);
}
bool synchronized_pool_resource::do_is_equal(const memory_resource& other) const BOOST_NOEXCEPT //virtual
{ return this == dynamic_cast<const synchronized_pool_resource*>(&other); }
std::size_t synchronized_pool_resource::pool_count() const
{ return m_pool_resource.pool_count(); }
std::size_t synchronized_pool_resource::pool_index(std::size_t bytes) const
{ return m_pool_resource.pool_index(bytes); }
std::size_t synchronized_pool_resource::pool_next_blocks_per_chunk(std::size_t pool_idx) const
{ return m_pool_resource.pool_next_blocks_per_chunk(pool_idx); }
std::size_t synchronized_pool_resource::pool_block(std::size_t pool_idx) const
{ return m_pool_resource.pool_block(pool_idx); }
std::size_t synchronized_pool_resource::pool_cached_blocks(std::size_t pool_idx) const
{ return m_pool_resource.pool_cached_blocks(pool_idx); }
} //namespace pmr {
} //namespace container {
} //namespace boost {
#include <boost/container/detail/config_end.hpp>
+79
View File
@@ -0,0 +1,79 @@
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2015-2015. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/container for documentation.
//
//////////////////////////////////////////////////////////////////////////////
#define BOOST_CONTAINER_SOURCE
#include <boost/container/detail/config_begin.hpp>
#include <boost/container/detail/workaround.hpp>
#include <boost/container/pmr/unsynchronized_pool_resource.hpp>
namespace boost {
namespace container {
namespace pmr {
unsynchronized_pool_resource::unsynchronized_pool_resource(const pool_options& opts, memory_resource* upstream) BOOST_NOEXCEPT
: m_resource(opts, upstream)
{}
unsynchronized_pool_resource::unsynchronized_pool_resource() BOOST_NOEXCEPT
: m_resource()
{}
unsynchronized_pool_resource::unsynchronized_pool_resource(memory_resource* upstream) BOOST_NOEXCEPT
: m_resource(upstream)
{}
unsynchronized_pool_resource::unsynchronized_pool_resource(const pool_options& opts) BOOST_NOEXCEPT
: m_resource(opts)
{}
unsynchronized_pool_resource::~unsynchronized_pool_resource() //virtual
{}
void unsynchronized_pool_resource::release()
{
m_resource.release();
}
memory_resource* unsynchronized_pool_resource::upstream_resource() const
{ return m_resource.upstream_resource(); }
pool_options unsynchronized_pool_resource::options() const
{ return m_resource.options(); }
void* unsynchronized_pool_resource::do_allocate(std::size_t bytes, std::size_t alignment) //virtual
{ return m_resource.do_allocate(bytes, alignment); }
void unsynchronized_pool_resource::do_deallocate(void* p, std::size_t bytes, std::size_t alignment) //virtual
{ return m_resource.do_deallocate(p, bytes, alignment); }
bool unsynchronized_pool_resource::do_is_equal(const memory_resource& other) const BOOST_NOEXCEPT //virtual
{ return this == dynamic_cast<const unsynchronized_pool_resource*>(&other); }
std::size_t unsynchronized_pool_resource::pool_count() const
{ return m_resource.pool_count(); }
std::size_t unsynchronized_pool_resource::pool_index(std::size_t bytes) const
{ return m_resource.pool_index(bytes); }
std::size_t unsynchronized_pool_resource::pool_next_blocks_per_chunk(std::size_t pool_idx) const
{ return m_resource.pool_next_blocks_per_chunk(pool_idx); }
std::size_t unsynchronized_pool_resource::pool_block(std::size_t pool_idx) const
{ return m_resource.pool_block(pool_idx); }
std::size_t unsynchronized_pool_resource::pool_cached_blocks(std::size_t pool_idx) const
{ return m_resource.pool_cached_blocks(pool_idx); }
} //namespace pmr {
} //namespace container {
} //namespace boost {
#include <boost/container/detail/config_end.hpp>