static_resource.ipp 1.3 KB

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