1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2015-2015. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // See http://www.boost.org/libs/container for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #define BOOST_CONTAINER_SOURCE
12 #include <boost/container/detail/config_begin.hpp>
13 #include <boost/container/detail/workaround.hpp>
14 #include <boost/container/detail/dlmalloc.hpp>
16 #include <boost/container/pmr/synchronized_pool_resource.hpp>
21 using namespace boost::container
;
23 class dlmalloc_sync_scoped_lock
28 explicit dlmalloc_sync_scoped_lock(void *sync
)
31 if(!dlmalloc_sync_lock(m_sync
)){
36 ~dlmalloc_sync_scoped_lock()
38 dlmalloc_sync_unlock(m_sync
);
48 synchronized_pool_resource::synchronized_pool_resource(const pool_options
& opts
, memory_resource
* upstream
) BOOST_NOEXCEPT
49 : m_pool_resource(opts
, upstream
), m_opaque_sync()
52 synchronized_pool_resource::synchronized_pool_resource() BOOST_NOEXCEPT
53 : m_pool_resource(), m_opaque_sync()
56 synchronized_pool_resource::synchronized_pool_resource(memory_resource
* upstream
) BOOST_NOEXCEPT
57 : m_pool_resource(upstream
), m_opaque_sync()
60 synchronized_pool_resource::synchronized_pool_resource(const pool_options
& opts
) BOOST_NOEXCEPT
61 : m_pool_resource(opts
), m_opaque_sync()
64 synchronized_pool_resource::~synchronized_pool_resource() //virtual
67 dlmalloc_sync_destroy(m_opaque_sync
);
70 void synchronized_pool_resource::release()
72 if(m_opaque_sync
){ //If there is no mutex, no allocation could be done
73 m_pool_resource
.release();
77 memory_resource
* synchronized_pool_resource::upstream_resource() const
78 { return m_pool_resource
.upstream_resource(); }
80 pool_options
synchronized_pool_resource::options() const
81 { return m_pool_resource
.options(); }
83 void* synchronized_pool_resource::do_allocate(std::size_t bytes
, std::size_t alignment
) //virtual
85 if(!m_opaque_sync
){ //If there is no mutex, no allocation could be done
86 m_opaque_sync
= dlmalloc_sync_create();
91 dlmalloc_sync_scoped_lock
lock(m_opaque_sync
); (void)lock
;
92 return m_pool_resource
.do_allocate(bytes
, alignment
);
95 void synchronized_pool_resource::do_deallocate(void* p
, std::size_t bytes
, std::size_t alignment
) //virtual
97 dlmalloc_sync_scoped_lock
lock(m_opaque_sync
); (void)lock
;
98 return m_pool_resource
.do_deallocate(p
, bytes
, alignment
);
101 bool synchronized_pool_resource::do_is_equal(const memory_resource
& other
) const BOOST_NOEXCEPT
//virtual
102 { return this == dynamic_cast<const synchronized_pool_resource
*>(&other
); }
104 std::size_t synchronized_pool_resource::pool_count() const
105 { return m_pool_resource
.pool_count(); }
107 std::size_t synchronized_pool_resource::pool_index(std::size_t bytes
) const
108 { return m_pool_resource
.pool_index(bytes
); }
110 std::size_t synchronized_pool_resource::pool_next_blocks_per_chunk(std::size_t pool_idx
) const
111 { return m_pool_resource
.pool_next_blocks_per_chunk(pool_idx
); }
113 std::size_t synchronized_pool_resource::pool_block(std::size_t pool_idx
) const
114 { return m_pool_resource
.pool_block(pool_idx
); }
116 std::size_t synchronized_pool_resource::pool_cached_blocks(std::size_t pool_idx
) const
117 { return m_pool_resource
.pool_cached_blocks(pool_idx
); }
120 } //namespace container {
121 } //namespace boost {
123 #include <boost/container/detail/config_end.hpp>