]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/container/src/synchronized_pool_resource.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / container / src / synchronized_pool_resource.cpp
1 //////////////////////////////////////////////////////////////////////////////
2 //
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)
6 //
7 // See http://www.boost.org/libs/container for documentation.
8 //
9 //////////////////////////////////////////////////////////////////////////////
10
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>
15
16 #include <boost/container/pmr/synchronized_pool_resource.hpp>
17 #include <cstddef>
18
19 namespace {
20
21 using namespace boost::container;
22
23 class dlmalloc_sync_scoped_lock
24 {
25 void *m_sync;
26
27 public:
28 explicit dlmalloc_sync_scoped_lock(void *sync)
29 : m_sync(sync)
30 {
31 if(!dlmalloc_sync_lock(m_sync)){
32 throw_bad_alloc();
33 }
34 }
35
36 ~dlmalloc_sync_scoped_lock()
37 {
38 dlmalloc_sync_unlock(m_sync);
39 }
40 };
41
42 } //namespace {
43
44 namespace boost {
45 namespace container {
46 namespace pmr {
47
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()
50 {}
51
52 synchronized_pool_resource::synchronized_pool_resource() BOOST_NOEXCEPT
53 : m_pool_resource(), m_opaque_sync()
54 {}
55
56 synchronized_pool_resource::synchronized_pool_resource(memory_resource* upstream) BOOST_NOEXCEPT
57 : m_pool_resource(upstream), m_opaque_sync()
58 {}
59
60 synchronized_pool_resource::synchronized_pool_resource(const pool_options& opts) BOOST_NOEXCEPT
61 : m_pool_resource(opts), m_opaque_sync()
62 {}
63
64 synchronized_pool_resource::~synchronized_pool_resource() //virtual
65 {
66 if(m_opaque_sync)
67 dlmalloc_sync_destroy(m_opaque_sync);
68 }
69
70 void synchronized_pool_resource::release()
71 {
72 if(m_opaque_sync){ //If there is no mutex, no allocation could be done
73 m_pool_resource.release();
74 }
75 }
76
77 memory_resource* synchronized_pool_resource::upstream_resource() const
78 { return m_pool_resource.upstream_resource(); }
79
80 pool_options synchronized_pool_resource::options() const
81 { return m_pool_resource.options(); }
82
83 void* synchronized_pool_resource::do_allocate(std::size_t bytes, std::size_t alignment) //virtual
84 {
85 if(!m_opaque_sync){ //If there is no mutex, no allocation could be done
86 m_opaque_sync = dlmalloc_sync_create();
87 if(!m_opaque_sync){
88 throw_bad_alloc();
89 }
90 }
91 dlmalloc_sync_scoped_lock lock(m_opaque_sync); (void)lock;
92 return m_pool_resource.do_allocate(bytes, alignment);
93 }
94
95 void synchronized_pool_resource::do_deallocate(void* p, std::size_t bytes, std::size_t alignment) //virtual
96 {
97 dlmalloc_sync_scoped_lock lock(m_opaque_sync); (void)lock;
98 return m_pool_resource.do_deallocate(p, bytes, alignment);
99 }
100
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); }
103
104 std::size_t synchronized_pool_resource::pool_count() const
105 { return m_pool_resource.pool_count(); }
106
107 std::size_t synchronized_pool_resource::pool_index(std::size_t bytes) const
108 { return m_pool_resource.pool_index(bytes); }
109
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); }
112
113 std::size_t synchronized_pool_resource::pool_block(std::size_t pool_idx) const
114 { return m_pool_resource.pool_block(pool_idx); }
115
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); }
118
119 } //namespace pmr {
120 } //namespace container {
121 } //namespace boost {
122
123 #include <boost/container/detail/config_end.hpp>