static_resource.ipp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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/except.hpp>
  13. #include <boost/align/align.hpp>
  14. #include <memory>
  15. namespace boost {
  16. namespace json {
  17. static_resource::
  18. ~static_resource() noexcept = default;
  19. static_resource::
  20. static_resource(
  21. unsigned char* buffer,
  22. std::size_t size) noexcept
  23. : p_(buffer)
  24. , n_(size)
  25. , size_(size)
  26. {
  27. }
  28. void
  29. static_resource::
  30. release() noexcept
  31. {
  32. p_ = reinterpret_cast<
  33. char*>(p_) - (size_ - n_);
  34. n_ = size_;
  35. }
  36. void*
  37. static_resource::
  38. do_allocate(
  39. std::size_t n,
  40. std::size_t align)
  41. {
  42. auto p = alignment::align(
  43. align, n, p_, n_);
  44. if(! p)
  45. detail::throw_bad_alloc();
  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. } // namespace json
  67. } // namespace boost
  68. #endif