diff --git a/CHANGELOG.md b/CHANGELOG.md index c3a8d386..71fa244a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ Version XXX: +* flat_buffer shrink_to_fit is noexcept * moved-from dynamic buffers do not clear if different allocator * fix erase field diff --git a/include/boost/beast/core/flat_buffer.hpp b/include/boost/beast/core/flat_buffer.hpp index 9e8d639c..f9b7ba26 100644 --- a/include/boost/beast/core/flat_buffer.hpp +++ b/include/boost/beast/core/flat_buffer.hpp @@ -358,17 +358,17 @@ public: void reserve(std::size_t n); - /** Reallocate the buffer to fit the readable bytes exactly. + /** Request the removal of unused capacity. - Buffer sequences previously obtained using @ref data or - @ref prepare become invalid. + This function attempts to reduce @ref capacity() + to @ref size(), which may not succeed. @esafe - Strong guarantee. + No-throw guarantee. */ void - shrink_to_fit(); + shrink_to_fit() noexcept; /** Set the size of the readable and writable bytes to zero. diff --git a/include/boost/beast/core/impl/flat_buffer.hpp b/include/boost/beast/core/impl/flat_buffer.hpp index e0792e0c..d4f55023 100644 --- a/include/boost/beast/core/impl/flat_buffer.hpp +++ b/include/boost/beast/core/impl/flat_buffer.hpp @@ -260,17 +260,32 @@ reserve(std::size_t n) template void basic_flat_buffer:: -shrink_to_fit() +shrink_to_fit() noexcept { auto const len = size(); + if(len == capacity()) return; + char* p; if(len > 0) { BOOST_ASSERT(begin_); BOOST_ASSERT(in_); - p = alloc(len); +#ifndef BOOST_NO_EXCEPTIONS + try + { +#endif + p = alloc(len); +#ifndef BOOST_NO_EXCEPTIONS + } + catch(std::exception const&) + { + // request could not be fulfilled, + // squelch the exception + return; + } +#endif std::memcpy(p, in_, len); } else