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