1 | /*
|
---|
2 | * Copyright (c) 2018 Jaroslav Jindrak
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #ifndef LIBCPP_BITS_MEMORY_SHARED_PAYLOAD
|
---|
30 | #define LIBCPP_BITS_MEMORY_SHARED_PAYLOAD
|
---|
31 |
|
---|
32 | #include <__bits/refcount_obj.hpp>
|
---|
33 | #include <cinttypes>
|
---|
34 | #include <utility>
|
---|
35 |
|
---|
36 | namespace std
|
---|
37 | {
|
---|
38 | template<class>
|
---|
39 | struct default_delete;
|
---|
40 |
|
---|
41 | struct allocator_arg_t;
|
---|
42 | }
|
---|
43 |
|
---|
44 | namespace std::aux
|
---|
45 | {
|
---|
46 | /**
|
---|
47 | * This allows us to construct shared_ptr from
|
---|
48 | * a payload pointer in make_shared etc.
|
---|
49 | */
|
---|
50 | struct payload_tag_t
|
---|
51 | { /* DUMMY BODY */ };
|
---|
52 |
|
---|
53 | inline constexpr payload_tag_t payload_tag{};
|
---|
54 |
|
---|
55 | template<class D, class T>
|
---|
56 | void use_payload_deleter(D* deleter, T* data)
|
---|
57 | {
|
---|
58 | if (deleter)
|
---|
59 | (*deleter)(data);
|
---|
60 | }
|
---|
61 |
|
---|
62 | template<class T>
|
---|
63 | class shared_payload_base: public aux::refcount_obj
|
---|
64 | {
|
---|
65 | public:
|
---|
66 | virtual T* get() const noexcept = 0;
|
---|
67 |
|
---|
68 | virtual uint8_t* deleter() const noexcept = 0;
|
---|
69 |
|
---|
70 | virtual shared_payload_base* lock() noexcept = 0;
|
---|
71 |
|
---|
72 | virtual ~shared_payload_base() = default;
|
---|
73 | };
|
---|
74 |
|
---|
75 | template<class T, class D = default_delete<T>>
|
---|
76 | class shared_payload: public shared_payload_base<T>
|
---|
77 | {
|
---|
78 | public:
|
---|
79 | shared_payload(T* ptr, D deleter = D{})
|
---|
80 | : data_{ptr}, deleter_{deleter}
|
---|
81 | { /* DUMMY BODY */ }
|
---|
82 |
|
---|
83 | template<class... Args>
|
---|
84 | shared_payload(Args&&... args)
|
---|
85 | : data_{new T{forward<Args>(args)...}},
|
---|
86 | deleter_{}
|
---|
87 | { /* DUMMY BODY */ }
|
---|
88 |
|
---|
89 | template<class Alloc, class... Args>
|
---|
90 | shared_payload(allocator_arg_t, Alloc alloc, Args&&... args)
|
---|
91 | : data_{alloc.allocate(1)},
|
---|
92 | deleter_{}
|
---|
93 | {
|
---|
94 | alloc.construct(data_, forward<Args>(args)...);
|
---|
95 | }
|
---|
96 |
|
---|
97 | template<class Alloc, class... Args>
|
---|
98 | shared_payload(D deleter, Alloc alloc, Args&&... args)
|
---|
99 | : data_{alloc.allocate(1)},
|
---|
100 | deleter_{deleter}
|
---|
101 | {
|
---|
102 | alloc.construct(data_, forward<Args>(args)...);
|
---|
103 | }
|
---|
104 |
|
---|
105 | void destroy() override
|
---|
106 | {
|
---|
107 | if (this->refs() == 0)
|
---|
108 | {
|
---|
109 | if (data_)
|
---|
110 | {
|
---|
111 | deleter_(data_);
|
---|
112 | data_ = nullptr;
|
---|
113 | }
|
---|
114 |
|
---|
115 | if (this->weak_refs() == 0)
|
---|
116 | delete this;
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | T* get() const noexcept override
|
---|
121 | {
|
---|
122 | return data_;
|
---|
123 | }
|
---|
124 |
|
---|
125 | uint8_t* deleter() const noexcept override
|
---|
126 | {
|
---|
127 | return (uint8_t*)&deleter_;
|
---|
128 | }
|
---|
129 |
|
---|
130 | shared_payload_base<T>* lock() noexcept override
|
---|
131 | {
|
---|
132 | refcount_t rfs = this->refs();
|
---|
133 | while (rfs != 0L)
|
---|
134 | {
|
---|
135 | if (__atomic_compare_exchange_n(&this->refcount_, &rfs, rfs + 1,
|
---|
136 | true, __ATOMIC_RELAXED,
|
---|
137 | __ATOMIC_RELAXED))
|
---|
138 | {
|
---|
139 | return this;
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | return nullptr;
|
---|
144 | }
|
---|
145 |
|
---|
146 | private:
|
---|
147 | T* data_;
|
---|
148 | D deleter_;
|
---|
149 | };
|
---|
150 | }
|
---|
151 |
|
---|
152 | #endif
|
---|