1  
//
1  
//
2  
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
2  
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
3  
//
3  
//
4  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
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)
5  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6  
//
6  
//
7  
// Official repository: https://github.com/boostorg/json
7  
// Official repository: https://github.com/boostorg/json
8  
//
8  
//
9  

9  

10  
#ifndef BOOST_JSON_IMPL_STATIC_RESOURCE_IPP
10  
#ifndef BOOST_JSON_IMPL_STATIC_RESOURCE_IPP
11  
#define BOOST_JSON_IMPL_STATIC_RESOURCE_IPP
11  
#define BOOST_JSON_IMPL_STATIC_RESOURCE_IPP
12  

12  

13  
#include <boost/json/static_resource.hpp>
13  
#include <boost/json/static_resource.hpp>
14  
#include <boost/throw_exception.hpp>
14  
#include <boost/throw_exception.hpp>
15  
#include <memory>
15  
#include <memory>
16  

16  

17  
namespace boost {
17  
namespace boost {
18  
namespace json {
18  
namespace json {
19  

19  

20  
static_resource::
20  
static_resource::
21  
static_resource(
21  
static_resource(
22  
    unsigned char* buffer,
22  
    unsigned char* buffer,
23  
    std::size_t size) noexcept
23  
    std::size_t size) noexcept
24  
    : p_(buffer)
24  
    : p_(buffer)
25  
    , n_(size)
25  
    , n_(size)
26  
    , size_(size)
26  
    , size_(size)
27  
{
27  
{
28  
}
28  
}
29  

29  

30  
void
30  
void
31  
static_resource::
31  
static_resource::
32  
release() noexcept
32  
release() noexcept
33  
{
33  
{
34  
    p_ = reinterpret_cast<
34  
    p_ = reinterpret_cast<
35  
        char*>(p_) - (size_ - n_);
35  
        char*>(p_) - (size_ - n_);
36  
    n_ = size_;
36  
    n_ = size_;
37  
}
37  
}
38  

38  

39  
void*
39  
void*
40  
static_resource::
40  
static_resource::
41  
do_allocate(
41  
do_allocate(
42  
    std::size_t n,
42  
    std::size_t n,
43  
    std::size_t align)
43  
    std::size_t align)
44  
{
44  
{
45  
    auto p = std::align(align, n, p_, n_);
45  
    auto p = std::align(align, n, p_, n_);
46  
    if(! p)
46  
    if(! p)
47  
        throw_exception( std::bad_alloc(), BOOST_CURRENT_LOCATION );
47  
        throw_exception( std::bad_alloc(), BOOST_CURRENT_LOCATION );
48  
    p_ = reinterpret_cast<char*>(p) + n;
48  
    p_ = reinterpret_cast<char*>(p) + n;
49  
    n_ -= n;
49  
    n_ -= n;
50  
    return p;
50  
    return p;
51  
}
51  
}
52  

52  

53  
void
53  
void
54  
static_resource::
54  
static_resource::
55  
do_deallocate(
55  
do_deallocate(
56  
    void*,
56  
    void*,
57  
    std::size_t,
57  
    std::size_t,
58  
    std::size_t)
58  
    std::size_t)
59  
{
59  
{
60  
    // do nothing
60  
    // do nothing
61  
}
61  
}
62  

62  

63  
bool
63  
bool
64  
static_resource::
64  
static_resource::
65  
do_is_equal(
65  
do_is_equal(
66  
    memory_resource const& mr) const noexcept
66  
    memory_resource const& mr) const noexcept
67  
{
67  
{
68  
    return this == &mr;
68  
    return this == &mr;
69  
}
69  
}
70  

70  

71  
} // namespace json
71  
} // namespace json
72  
} // namespace boost
72  
} // namespace boost
73  

73  

74  
#endif
74  
#endif