static_resource.ipp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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_IMPL_STATIC_RESOURCE_IPP
  10. #define BOOST_JSON_IMPL_STATIC_RESOURCE_IPP
  11. #include <boost/json/static_resource.hpp>
  12. #include <boost/throw_exception.hpp>
  13. #include <boost/align/align.hpp>
  14. #include <memory>
  15. namespace boost {
  16. namespace json {
  17. static_resource::
  18. static_resource(
  19. unsigned char* buffer,
  20. std::size_t size) noexcept
  21. : p_(buffer)
  22. , n_(size)
  23. , size_(size)
  24. {
  25. }
  26. void
  27. static_resource::
  28. release() noexcept
  29. {
  30. p_ = reinterpret_cast<
  31. char*>(p_) - (size_ - n_);
  32. n_ = size_;
  33. }
  34. void*
  35. static_resource::
  36. do_allocate(
  37. std::size_t n,
  38. std::size_t align)
  39. {
  40. auto p = alignment::align(
  41. align, n, p_, n_);
  42. if(! p)
  43. throw_exception( std::bad_alloc(), BOOST_CURRENT_LOCATION );
  44. p_ = reinterpret_cast<char*>(p) + n;
  45. n_ -= n;
  46. return p;
  47. }
  48. void
  49. static_resource::
  50. do_deallocate(
  51. void*,
  52. std::size_t,
  53. std::size_t)
  54. {
  55. // do nothing
  56. }
  57. bool
  58. static_resource::
  59. do_is_equal(
  60. memory_resource const& mr) const noexcept
  61. {
  62. return this == &mr;
  63. }
  64. } // namespace json
  65. } // namespace boost
  66. #endif