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_DETAIL_SHARED_RESOURCE_HPP
10  
#ifndef BOOST_JSON_DETAIL_SHARED_RESOURCE_HPP
11  
#define BOOST_JSON_DETAIL_SHARED_RESOURCE_HPP
11  
#define BOOST_JSON_DETAIL_SHARED_RESOURCE_HPP
12  

12  

13  
#include <boost/container/pmr/memory_resource.hpp>
13  
#include <boost/container/pmr/memory_resource.hpp>
14  
#include <atomic>
14  
#include <atomic>
15  
#include <utility>
15  
#include <utility>
16  

16  

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

20  

21  
#ifdef _MSC_VER
21  
#ifdef _MSC_VER
22  
#pragma warning(push)
22  
#pragma warning(push)
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  
struct BOOST_SYMBOL_VISIBLE
26  
struct BOOST_SYMBOL_VISIBLE
27  
    shared_resource
27  
    shared_resource
28  
    : container::pmr::memory_resource
28  
    : container::pmr::memory_resource
29  
{
29  
{
30  
    BOOST_JSON_DECL
30  
    BOOST_JSON_DECL
31  
    shared_resource();
31  
    shared_resource();
32  

32  

33  
    BOOST_JSON_DECL
33  
    BOOST_JSON_DECL
34  
    ~shared_resource();
34  
    ~shared_resource();
35  

35  

36  
    std::atomic<std::size_t> refs{ 1 };
36  
    std::atomic<std::size_t> refs{ 1 };
37  
};
37  
};
38  

38  

39  
template<class T>
39  
template<class T>
40  
class shared_resource_impl final
40  
class shared_resource_impl final
41  
    : public shared_resource
41  
    : public shared_resource
42  
{
42  
{
43  
    T t;
43  
    T t;
44  

44  

45  
public:
45  
public:
46  
    template<class... Args>
46  
    template<class... Args>
47  
    shared_resource_impl(
47  
    shared_resource_impl(
48  
        Args&&... args)
48  
        Args&&... args)
49  
        : t(std::forward<Args>(args)...)
49  
        : t(std::forward<Args>(args)...)
50  
    {
50  
    {
51  
    }
51  
    }
52  

52  

53  
    void*
53  
    void*
54  
    do_allocate(
54  
    do_allocate(
55  
        std::size_t n,
55  
        std::size_t n,
56  
        std::size_t align) override
56  
        std::size_t align) override
57  
    {
57  
    {
58  
        return t.allocate(n, align);
58  
        return t.allocate(n, align);
59  
    }
59  
    }
60  

60  

61  
    void
61  
    void
62  
    do_deallocate(
62  
    do_deallocate(
63  
        void* p,
63  
        void* p,
64  
        std::size_t n,
64  
        std::size_t n,
65  
        std::size_t align) override
65  
        std::size_t align) override
66  
    {
66  
    {
67  
        return t.deallocate(p, n, align);
67  
        return t.deallocate(p, n, align);
68  
    }
68  
    }
69  

69  

70  
    bool
70  
    bool
71  
    do_is_equal(
71  
    do_is_equal(
72  
        memory_resource const&) const noexcept override
72  
        memory_resource const&) const noexcept override
73  
    {
73  
    {
74  
        // VFALCO Is always false ok?
74  
        // VFALCO Is always false ok?
75  
        return false;
75  
        return false;
76  
    }
76  
    }
77  
};
77  
};
78  

78  

79  
#ifdef _MSC_VER
79  
#ifdef _MSC_VER
80  
#pragma warning(pop)
80  
#pragma warning(pop)
81  
#endif
81  
#endif
82  

82  

83  
} // detail
83  
} // detail
84  
} // namespace json
84  
} // namespace json
85  
} // namespace boost
85  
} // namespace boost
86  

86  

87  
#endif
87  
#endif