temporary_buffer.ipp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. //
  2. // Copyright (c) 2019 Damian Jarek(damian.jarek93@gmail.com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/beast
  8. //
  9. #ifndef BOOST_BEAST_DETAIL_IMPL_TEMPORARY_BUFFER_IPP
  10. #define BOOST_BEAST_DETAIL_IMPL_TEMPORARY_BUFFER_IPP
  11. #include <boost/beast/core/detail/temporary_buffer.hpp>
  12. #include <boost/beast/core/detail/clamp.hpp>
  13. #include <boost/core/exchange.hpp>
  14. #include <boost/assert.hpp>
  15. #include <memory>
  16. #include <cstring>
  17. namespace boost {
  18. namespace beast {
  19. namespace detail {
  20. void
  21. temporary_buffer::
  22. append(string_view s)
  23. {
  24. grow(s.size());
  25. unchecked_append(s);
  26. }
  27. void
  28. temporary_buffer::
  29. append(string_view s1, string_view s2)
  30. {
  31. grow(s1.size() + s2.size());
  32. unchecked_append(s1);
  33. unchecked_append(s2);
  34. }
  35. void
  36. temporary_buffer::
  37. unchecked_append(string_view s)
  38. {
  39. auto n = s.size();
  40. std::memcpy(&data_[size_], s.data(), n);
  41. size_ += n;
  42. }
  43. void
  44. temporary_buffer::
  45. grow(std::size_t n)
  46. {
  47. if (capacity_ - size_ >= n)
  48. return;
  49. auto const capacity = (n + size_) * 2u;
  50. BOOST_ASSERT(! detail::sum_exceeds(
  51. n, size_, capacity));
  52. char* const p = new char[capacity];
  53. std::memcpy(p, data_, size_);
  54. deallocate(boost::exchange(data_, p));
  55. capacity_ = capacity;
  56. }
  57. } // detail
  58. } // beast
  59. } // boost
  60. #endif