stack.ipp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. //
  2. // Copyright (c) 2019 Vinnie Falco ([email protected])
  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/json
  8. //
  9. #ifndef BOOST_JSON_DETAIL_IMPL_STACK_IPP
  10. #define BOOST_JSON_DETAIL_IMPL_STACK_IPP
  11. #include <boost/json/detail/stack.hpp>
  12. namespace boost {
  13. namespace json {
  14. namespace detail {
  15. stack::
  16. ~stack()
  17. {
  18. if(base_ != buf_)
  19. sp_->deallocate(
  20. base_, cap_);
  21. }
  22. stack::
  23. stack(
  24. storage_ptr sp,
  25. unsigned char* buf,
  26. std::size_t buf_size) noexcept
  27. : sp_(std::move(sp))
  28. , cap_(buf_size)
  29. , base_(buf)
  30. , buf_(buf)
  31. {
  32. }
  33. void
  34. stack::
  35. reserve(std::size_t n)
  36. {
  37. if(cap_ >= n)
  38. return;
  39. auto const base = static_cast<
  40. unsigned char*>(sp_->allocate(n));
  41. if(base_)
  42. {
  43. if(size_ > 0)
  44. std::memcpy(base, base_, size_);
  45. if(base_ != buf_)
  46. sp_->deallocate(base_, cap_);
  47. }
  48. base_ = base;
  49. cap_ = n;
  50. }
  51. } // detail
  52. } // namespace json
  53. } // namespace boost
  54. #endif