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_DEFAULT_RESOURCE_HPP
10  
#ifndef BOOST_JSON_DEFAULT_RESOURCE_HPP
11  
#define BOOST_JSON_DEFAULT_RESOURCE_HPP
11  
#define BOOST_JSON_DEFAULT_RESOURCE_HPP
12  

12  

13  
#include <boost/json/detail/config.hpp>
13  
#include <boost/json/detail/config.hpp>
14  
#include <boost/container/pmr/memory_resource.hpp>
14  
#include <boost/container/pmr/memory_resource.hpp>
15  

15  

16  
namespace boost {
16  
namespace boost {
17  
namespace json {
17  
namespace json {
18  
namespace detail {
18  
namespace detail {
19  

19  

20  
#ifdef _MSC_VER
20  
#ifdef _MSC_VER
21  
#pragma warning(push)
21  
#pragma warning(push)
22  
#pragma warning(disable: 4251) // class needs to have dll-interface to be used by clients of class
22  
#pragma warning(disable: 4251) // class needs to have dll-interface to be used by clients of class
23  
#pragma warning(disable: 4275) // non dll-interface class used as base for dll-interface class
23  
#pragma warning(disable: 4275) // non dll-interface class used as base for dll-interface class
24  
#endif
24  
#endif
25  

25  

26  
// A simple memory resource that uses operator new and delete.
26  
// A simple memory resource that uses operator new and delete.
27  
class
27  
class
28  
    BOOST_SYMBOL_VISIBLE
28  
    BOOST_SYMBOL_VISIBLE
29  
    BOOST_JSON_DECL
29  
    BOOST_JSON_DECL
30  
default_resource final
30  
default_resource final
31  
    : public container::pmr::memory_resource
31  
    : public container::pmr::memory_resource
32  
{
32  
{
33  
    union holder;
33  
    union holder;
34  

34  

35  
#ifndef BOOST_JSON_WEAK_CONSTINIT
35  
#ifndef BOOST_JSON_WEAK_CONSTINIT
36  
# ifndef BOOST_JSON_NO_DESTROY
36  
# ifndef BOOST_JSON_NO_DESTROY
37  
    static holder instance_;
37  
    static holder instance_;
38  
# else
38  
# else
39  
    BOOST_JSON_NO_DESTROY
39  
    BOOST_JSON_NO_DESTROY
40  
    static default_resource instance_;
40  
    static default_resource instance_;
41  
# endif
41  
# endif
42  
#endif
42  
#endif
43  

43  

44  
public:
44  
public:
45  
    static
45  
    static
46  
    container::pmr::memory_resource*
46  
    container::pmr::memory_resource*
47  
    get() noexcept
47  
    get() noexcept
48  
    {
48  
    {
49  
    #ifdef BOOST_JSON_WEAK_CONSTINIT
49  
    #ifdef BOOST_JSON_WEAK_CONSTINIT
50  
        static default_resource instance_;
50  
        static default_resource instance_;
51  
    #endif
51  
    #endif
52  
        return reinterpret_cast<memory_resource*>(
52  
        return reinterpret_cast<memory_resource*>(
53  
            reinterpret_cast<std::uintptr_t*>(
53  
            reinterpret_cast<std::uintptr_t*>(
54  
                &instance_));
54  
                &instance_));
55  
    }
55  
    }
56  

56  

57  
    ~default_resource();
57  
    ~default_resource();
58  

58  

59  
    void*
59  
    void*
60  
    do_allocate(
60  
    do_allocate(
61  
        std::size_t n,
61  
        std::size_t n,
62  
        std::size_t) override;
62  
        std::size_t) override;
63  

63  

64  
    void
64  
    void
65  
    do_deallocate(
65  
    do_deallocate(
66  
        void* p,
66  
        void* p,
67  
        std::size_t,
67  
        std::size_t,
68  
        std::size_t) override;
68  
        std::size_t) override;
69  

69  

70  
    bool
70  
    bool
71  
    do_is_equal(
71  
    do_is_equal(
72  
        memory_resource const& mr) const noexcept override;
72  
        memory_resource const& mr) const noexcept override;
73  
};
73  
};
74  

74  

75  
#ifdef _MSC_VER
75  
#ifdef _MSC_VER
76  
#pragma warning(pop)
76  
#pragma warning(pop)
77  
#endif
77  
#endif
78  

78  

79  
union default_resource::
79  
union default_resource::
80  
    holder
80  
    holder
81  
{
81  
{
82  
#ifndef BOOST_JSON_WEAK_CONSTINIT
82  
#ifndef BOOST_JSON_WEAK_CONSTINIT
83  
    constexpr
83  
    constexpr
84  
#endif
84  
#endif
85  
    holder()
85  
    holder()
86  
        : mr()
86  
        : mr()
87  
    {
87  
    {
88  
    }
88  
    }
89  

89  

90  
    ~holder()
90  
    ~holder()
91  
    {
91  
    {
92  
    }
92  
    }
93  

93  

94  
    default_resource mr;
94  
    default_resource mr;
95  
};
95  
};
96  

96  

97  
} // detail
97  
} // detail
98  
} // namespace json
98  
} // namespace json
99  
} // namespace boost
99  
} // namespace boost
100  

100  

101  
#endif
101  
#endif