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
15 #include <boost/container/pmr/monotonic_buffer_resource.hpp>
16 #include <boost/container/pmr/global_resource.hpp>
17
18 #include <boost/container/detail/min_max.hpp>
19 #include <boost/intrusive/detail/math.hpp>
20 #include <boost/container/throw_exception.hpp>
21
22
23 #include <cstddef>
24
25 namespace {
26
27 #ifdef BOOST_HAS_INTPTR_T
28 typedef boost::uintptr_t uintptr_type;
29 #else
30 typedef std::size_t uintptr_type;
31 #endif
32
33 static const std::size_t minimum_buffer_size = 2*sizeof(void*);
34
35 } //namespace {
36
37 namespace boost {
38 namespace container {
39 namespace pmr {
40
increase_next_buffer()41 void monotonic_buffer_resource::increase_next_buffer()
42 {
43 m_next_buffer_size = (std::size_t(-1)/2 < m_next_buffer_size) ? std::size_t(-1) : m_next_buffer_size*2;
44 }
45
increase_next_buffer_at_least_to(std::size_t minimum_size)46 void monotonic_buffer_resource::increase_next_buffer_at_least_to(std::size_t minimum_size)
47 {
48 if(m_next_buffer_size < minimum_size){
49 if(bi::detail::is_pow2(minimum_size)){
50 m_next_buffer_size = minimum_size;
51 }
52 else if(std::size_t(-1)/2 < minimum_size){
53 m_next_buffer_size = minimum_size;
54 }
55 else{
56 m_next_buffer_size = bi::detail::ceil_pow2(minimum_size);
57 }
58 }
59 }
60
monotonic_buffer_resource(memory_resource * upstream)61 monotonic_buffer_resource::monotonic_buffer_resource(memory_resource* upstream) BOOST_NOEXCEPT
62 : m_memory_blocks(upstream ? *upstream : *get_default_resource())
63 , m_current_buffer(0)
64 , m_current_buffer_size(0u)
65 , m_next_buffer_size(initial_next_buffer_size)
66 , m_initial_buffer(0)
67 , m_initial_buffer_size(0u)
68 {}
69
monotonic_buffer_resource(std::size_t initial_size,memory_resource * upstream)70 monotonic_buffer_resource::monotonic_buffer_resource(std::size_t initial_size, memory_resource* upstream) BOOST_NOEXCEPT
71 : m_memory_blocks(upstream ? *upstream : *get_default_resource())
72 , m_current_buffer(0)
73 , m_current_buffer_size(0u)
74 , m_next_buffer_size(minimum_buffer_size)
75 , m_initial_buffer(0)
76 , m_initial_buffer_size(0u)
77 { //In case initial_size is zero
78 this->increase_next_buffer_at_least_to(initial_size + !initial_size);
79 }
80
monotonic_buffer_resource(void * buffer,std::size_t buffer_size,memory_resource * upstream)81 monotonic_buffer_resource::monotonic_buffer_resource(void* buffer, std::size_t buffer_size, memory_resource* upstream) BOOST_NOEXCEPT
82 : m_memory_blocks(upstream ? *upstream : *get_default_resource())
83 , m_current_buffer(buffer)
84 , m_current_buffer_size(buffer_size)
85 , m_next_buffer_size
86 (bi::detail::previous_or_equal_pow2
87 (boost::container::dtl::max_value(buffer_size, std::size_t(initial_next_buffer_size))))
88 , m_initial_buffer(buffer)
89 , m_initial_buffer_size(buffer_size)
90 { this->increase_next_buffer(); }
91
~monotonic_buffer_resource()92 monotonic_buffer_resource::~monotonic_buffer_resource()
93 { this->release(); }
94
release()95 void monotonic_buffer_resource::release() BOOST_NOEXCEPT
96 {
97 m_memory_blocks.release();
98 m_current_buffer = m_initial_buffer;
99 m_current_buffer_size = m_initial_buffer_size;
100 m_next_buffer_size = initial_next_buffer_size;
101 }
102
upstream_resource() const103 memory_resource* monotonic_buffer_resource::upstream_resource() const BOOST_NOEXCEPT
104 { return &m_memory_blocks.upstream_resource(); }
105
remaining_storage(std::size_t alignment,std::size_t & wasted_due_to_alignment) const106 std::size_t monotonic_buffer_resource::remaining_storage(std::size_t alignment, std::size_t &wasted_due_to_alignment) const BOOST_NOEXCEPT
107 {
108 const uintptr_type up_alignment_minus1 = alignment - 1u;
109 const uintptr_type up_alignment_mask = ~up_alignment_minus1;
110 const uintptr_type up_addr = uintptr_type(m_current_buffer);
111 const uintptr_type up_aligned_addr = (up_addr + up_alignment_minus1) & up_alignment_mask;
112 wasted_due_to_alignment = std::size_t(up_aligned_addr - up_addr);
113 return m_current_buffer_size <= wasted_due_to_alignment ? 0u : m_current_buffer_size - wasted_due_to_alignment;
114 }
115
remaining_storage(std::size_t alignment) const116 std::size_t monotonic_buffer_resource::remaining_storage(std::size_t alignment) const BOOST_NOEXCEPT
117 {
118 std::size_t ignore_this;
119 return this->remaining_storage(alignment, ignore_this);
120 }
121
current_buffer() const122 const void *monotonic_buffer_resource::current_buffer() const BOOST_NOEXCEPT
123 { return m_current_buffer; }
124
next_buffer_size() const125 std::size_t monotonic_buffer_resource::next_buffer_size() const BOOST_NOEXCEPT
126 { return m_next_buffer_size; }
127
allocate_from_current(std::size_t aligner,std::size_t bytes)128 void *monotonic_buffer_resource::allocate_from_current(std::size_t aligner, std::size_t bytes)
129 {
130 char * p = (char*)m_current_buffer + aligner;
131 m_current_buffer = p + bytes;
132 m_current_buffer_size -= aligner + bytes;
133 return p;
134 }
135
do_allocate(std::size_t bytes,std::size_t alignment)136 void* monotonic_buffer_resource::do_allocate(std::size_t bytes, std::size_t alignment)
137 {
138 if(alignment > memory_resource::max_align)
139 throw_bad_alloc();
140
141 //See if there is room in current buffer
142 std::size_t aligner = 0u;
143 if(this->remaining_storage(alignment, aligner) < bytes){
144 //The new buffer will be aligned to the strictest alignment so reset
145 //the aligner, which was needed for the old buffer.
146 aligner = 0u;
147 //Update next_buffer_size to at least bytes
148 this->increase_next_buffer_at_least_to(bytes);
149 //Now allocate and update internal data
150 m_current_buffer = (char*)m_memory_blocks.allocate(m_next_buffer_size);
151 m_current_buffer_size = m_next_buffer_size;
152 this->increase_next_buffer();
153 }
154 //Enough internal storage, extract from it
155 return this->allocate_from_current(aligner, bytes);
156 }
157
do_deallocate(void * p,std::size_t bytes,std::size_t alignment)158 void monotonic_buffer_resource::do_deallocate(void* p, std::size_t bytes, std::size_t alignment) BOOST_NOEXCEPT
159 { (void)p; (void)bytes; (void)alignment; }
160
do_is_equal(const memory_resource & other) const161 bool monotonic_buffer_resource::do_is_equal(const memory_resource& other) const BOOST_NOEXCEPT
162 { return this == dynamic_cast<const monotonic_buffer_resource*>(&other); }
163
164 } //namespace pmr {
165 } //namespace container {
166 } //namespace boost {
167
168 #include <boost/container/detail/config_end.hpp>
169