static_resource.ipp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //
  2. // Copyright (c) 2019 Vinnie Falco (vinnie.falco@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/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/json/detail/align.hpp>
  13. #include <boost/json/detail/except.hpp>
  14. #include <memory>
  15. BOOST_JSON_NS_BEGIN
  16. static_resource::
  17. ~static_resource() noexcept = default;
  18. static_resource::
  19. static_resource(
  20. unsigned char* buffer,
  21. std::size_t size) noexcept
  22. : p_(buffer)
  23. , n_(size)
  24. , size_(size)
  25. {
  26. }
  27. void
  28. static_resource::
  29. release() noexcept
  30. {
  31. p_ = reinterpret_cast<
  32. char*>(p_) - (size_ - n_);
  33. n_ = size_;
  34. }
  35. void*
  36. static_resource::
  37. do_allocate(
  38. std::size_t n,
  39. std::size_t align)
  40. {
  41. auto p = detail::align(
  42. align, n, p_, n_);
  43. if(! p)
  44. detail::throw_bad_alloc(
  45. BOOST_JSON_SOURCE_POS);
  46. p_ = reinterpret_cast<char*>(p) + n;
  47. n_ -= n;
  48. return p;
  49. }
  50. void
  51. static_resource::
  52. do_deallocate(
  53. void*,
  54. std::size_t,
  55. std::size_t)
  56. {
  57. // do nothing
  58. }
  59. bool
  60. static_resource::
  61. do_is_equal(
  62. memory_resource const& mr) const noexcept
  63. {
  64. return this == &mr;
  65. }
  66. BOOST_JSON_NS_END
  67. #endif