summaryrefslogtreecommitdiffstats
path: root/src/third-party/scnlib/include/scn/util/memory.h
blob: 9dfb9705a74e956272ae3f7db1dc75fb718b626e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
// Copyright 2017 Elias Kosunen
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// This file is a part of scnlib:
//     https://github.com/eliaskosunen/scnlib

#ifndef SCN_UTIL_MEMORY_H
#define SCN_UTIL_MEMORY_H

#include "meta.h"

#include <cstring>
#include <new>

SCN_GCC_PUSH
SCN_GCC_IGNORE("-Wnoexcept")
#include <iterator>
SCN_GCC_POP

#if SCN_MSVC && SCN_HAS_STRING_VIEW
#include <string_view>
#endif

namespace scn {
    SCN_BEGIN_NAMESPACE

    namespace detail {
        template <typename T>
        struct pointer_traits;

        template <typename T>
        struct pointer_traits<T*> {
            using pointer = T*;
            using element_type = T;
            using difference_type = std::ptrdiff_t;

            template <typename U>
            using rebind = U*;

            template <typename U = T,
                      typename std::enable_if<!std::is_void<U>::value>::type* =
                          nullptr>
            static constexpr pointer pointer_to(U& r) noexcept
            {
                return &r;
            }
        };

        template <typename T>
        constexpr T* to_address_impl(T* p, priority_tag<2>) noexcept
        {
            return p;
        }
        template <typename Ptr>
        SCN_CONSTEXPR14 auto to_address_impl(const Ptr& p,
                                             priority_tag<1>) noexcept
            -> decltype(::scn::detail::pointer_traits<Ptr>::to_address(p))
        {
            return ::scn::detail::pointer_traits<Ptr>::to_address(p);
        }
        template <typename Ptr>
        constexpr auto to_address_impl(const Ptr& p, priority_tag<0>) noexcept
            -> decltype(::scn::detail::to_address_impl(p.operator->(),
                                                       priority_tag<2>{}))
        {
            return ::scn::detail::to_address_impl(p.operator->(),
                                                  priority_tag<2>{});
        }

        template <typename Ptr>
        constexpr auto to_address(Ptr&& p) noexcept
            -> decltype(::scn::detail::to_address_impl(SCN_FWD(p),
                                                       priority_tag<2>{}))
        {
            return ::scn::detail::to_address_impl(SCN_FWD(p),
                                                  priority_tag<2>{});
        }

#if SCN_WINDOWS
        template <typename I, typename B, typename E>
        SCN_CONSTEXPR14 auto to_address_safe(I&& p, B begin, E end) noexcept
            -> decltype(to_address(SCN_FWD(p)))
        {
            if (p >= begin && p < end) {
                return to_address(SCN_FWD(p));
            }
            if (begin == end) {
                return to_address(SCN_FWD(p));
            }
            if (p == end) {
                return to_address(SCN_FWD(p) - 1) + 1;
            }
            SCN_ENSURE(false);
            SCN_UNREACHABLE;
        }
#else
        template <typename I, typename B, typename E>
        SCN_CONSTEXPR14 auto to_address_safe(I&& p, B, E) noexcept
            -> decltype(to_address(SCN_FWD(p)))
        {
            return to_address(SCN_FWD(p));
        }
#endif

        // Workaround for MSVC _String_view_iterator
#if SCN_MSVC && SCN_HAS_STRING_VIEW
        template <typename Traits>
        struct pointer_traits<std::_String_view_iterator<Traits>> {
            using iterator = std::_String_view_iterator<Traits>;
            using pointer = typename iterator::pointer;
            using element_type = typename iterator::value_type;
            using difference_type = typename iterator::difference_type;

            static constexpr pointer to_address(const iterator& it) noexcept
            {
                // operator-> of _String_view_iterator
                // is checked for past-the-end dereference,
                // even though operator-> isn't dereferencing anything :)))
                return it._Unwrapped();
            }
        };
#endif

        template <typename T>
        constexpr T* launder(T* p) noexcept
        {
#if SCN_HAS_LAUNDER
            return std::launder(p);
#else
            return p;
#endif
        }

        template <typename ForwardIt, typename T>
        void uninitialized_fill(ForwardIt first,
                                ForwardIt last,
                                const T& value,
                                std::true_type) noexcept
        {
            using value_type =
                typename std::iterator_traits<ForwardIt>::value_type;
            const auto dist = static_cast<size_t>(std::distance(first, last)) *
                              sizeof(value_type);
            std::memset(&*first, static_cast<unsigned char>(value), dist);
        }
        template <typename ForwardIt, typename T>
        void uninitialized_fill(ForwardIt first,
                                ForwardIt last,
                                const T& value,
                                std::false_type) noexcept
        {
            using value_type =
                typename std::iterator_traits<ForwardIt>::value_type;
            ForwardIt current = first;
            for (; current != last; ++current) {
                ::new (static_cast<void*>(std::addressof(*current)))
                    value_type(value);
            }
        }
        template <typename ForwardIt, typename T>
        void uninitialized_fill(ForwardIt first,
                                ForwardIt last,
                                const T& value) noexcept
        {
            constexpr bool B = std::is_trivially_copyable<T>::value &&
                               std::is_pointer<ForwardIt>::value &&
                               sizeof(T) == 1;
            return uninitialized_fill(first, last, value,
                                      std::integral_constant<bool, B>{});
        }

        template <typename ForwardIt>
        void uninitialized_fill_default_construct(ForwardIt first,
                                                  ForwardIt last) noexcept
        {
            using value_type =
                typename std::iterator_traits<ForwardIt>::value_type;
            ForwardIt current = first;
            for (; current != last; ++current) {
                ::new (static_cast<void*>(std::addressof(*current))) value_type;
            }
        }
        template <typename ForwardIt>
        void uninitialized_fill_value_init(ForwardIt first,
                                           ForwardIt last) noexcept
        {
            using value_type =
                typename std::iterator_traits<ForwardIt>::value_type;
            ForwardIt current = first;
            for (; current != last; ++current) {
                ::new (static_cast<void*>(std::addressof(*current)))
                    value_type();
            }
        }

        template <typename InputIt,
                  typename ForwardIt,
                  typename std::enable_if<
                      !std::is_trivially_copyable<typename std::iterator_traits<
                          ForwardIt>::value_type>::value>::type* = nullptr>
        ForwardIt uninitialized_copy(InputIt first,
                                     InputIt last,
                                     ForwardIt d_first) noexcept
        {
            using value_type =
                typename std::iterator_traits<ForwardIt>::value_type;
            ForwardIt current = d_first;
            for (; first != last; ++first, (void)++current) {
                ::new (static_cast<void*>(std::addressof(*current)))
                    value_type(*first);
            }
            return current;
        }
        template <typename InputIt,
                  typename ForwardIt,
                  typename std::enable_if<
                      std::is_trivially_copyable<typename std::iterator_traits<
                          ForwardIt>::value_type>::value>::type* = nullptr>
        ForwardIt uninitialized_copy(InputIt first,
                                     InputIt last,
                                     ForwardIt d_first) noexcept
        {
            using value_type =
                typename std::iterator_traits<ForwardIt>::value_type;
            using pointer = typename std::iterator_traits<ForwardIt>::pointer;
            auto ptr =
                std::memcpy(std::addressof(*d_first), std::addressof(*first),
                            static_cast<size_t>(std::distance(first, last)) *
                                sizeof(value_type));
            return ForwardIt{static_cast<pointer>(ptr)};
        }

        template <typename InputIt,
                  typename ForwardIt,
                  typename std::enable_if<
                      !std::is_trivially_copyable<typename std::iterator_traits<
                          ForwardIt>::value_type>::value>::type* = nullptr>
        ForwardIt uninitialized_move(InputIt first,
                                     InputIt last,
                                     ForwardIt d_first) noexcept
        {
            using value_type =
                typename std::iterator_traits<ForwardIt>::value_type;
            ForwardIt current = d_first;
            for (; first != last; ++first, (void)++current) {
                ::new (static_cast<void*>(std::addressof(*current)))
                    value_type(std::move(*first));
            }
            return current;
        }
        template <typename InputIt,
                  typename ForwardIt,
                  typename std::enable_if<
                      std::is_trivially_copyable<typename std::iterator_traits<
                          ForwardIt>::value_type>::value>::type* = nullptr>
        ForwardIt uninitialized_move(InputIt first,
                                     InputIt last,
                                     ForwardIt d_first) noexcept
        {
            using value_type =
                typename std::iterator_traits<ForwardIt>::value_type;
            using pointer = typename std::iterator_traits<ForwardIt>::pointer;
            auto ptr =
                std::memcpy(std::addressof(*d_first), std::addressof(*first),
                            static_cast<size_t>(std::distance(first, last)) *
                                sizeof(value_type));
            return ForwardIt(static_cast<pointer>(ptr));
        }

        template <typename T>
        class SCN_TRIVIAL_ABI erased_storage {
        public:
            using value_type = T;
            using pointer = T*;
            using storage_type = unsigned char[sizeof(T)];

            constexpr erased_storage() noexcept = default;

            erased_storage(T val) noexcept(
                std::is_nothrow_move_constructible<T>::value)
                : m_ptr(::new (static_cast<void*>(&m_data)) T(SCN_MOVE(val)))
            {
            }

            erased_storage(const erased_storage& other)
                : m_ptr(other ? ::new (static_cast<void*>(&m_data))
                                    T(other.get())
                              : nullptr)
            {
            }
            erased_storage& operator=(const erased_storage& other)
            {
                _destruct();
                if (other) {
                    m_ptr = ::new (static_cast<void*>(&m_data)) T(other.get());
                }
                return *this;
            }

            erased_storage(erased_storage&& other) noexcept
                : m_ptr(other ? ::new (static_cast<void*>(&m_data))
                                    T(SCN_MOVE(other.get()))
                              : nullptr)
            {
                other.m_ptr = nullptr;
            }
            erased_storage& operator=(erased_storage&& other) noexcept
            {
                _destruct();
                if (other) {
                    m_ptr = ::new (static_cast<void*>(&m_data))
                        T(SCN_MOVE(other.get()));
                    other.m_ptr = nullptr;
                }
                return *this;
            }

            ~erased_storage() noexcept
            {
                _destruct();
            }

            SCN_NODISCARD constexpr bool has_value() const noexcept
            {
                return m_ptr != nullptr;
            }
            constexpr explicit operator bool() const noexcept
            {
                return has_value();
            }

            SCN_CONSTEXPR14 T& get() noexcept
            {
                SCN_EXPECT(has_value());
                return _get();
            }
            SCN_CONSTEXPR14 const T& get() const noexcept
            {
                SCN_EXPECT(has_value());
                return _get();
            }

            SCN_CONSTEXPR14 T& operator*() noexcept
            {
                SCN_EXPECT(has_value());
                return _get();
            }
            SCN_CONSTEXPR14 const T& operator*() const noexcept
            {
                SCN_EXPECT(has_value());
                return _get();
            }

            SCN_CONSTEXPR14 T* operator->() noexcept
            {
                return m_ptr;
            }
            SCN_CONSTEXPR14 const T* operator->() const noexcept
            {
                return m_ptr;
            }

        private:
            void _destruct()
            {
                if (m_ptr) {
                    _get().~T();
                }
                m_ptr = nullptr;
            }
            static pointer _toptr(storage_type& data)
            {
                return ::scn::detail::launder(
                    reinterpret_cast<T*>(reinterpret_cast<void*>(data.data())));
            }
            SCN_CONSTEXPR14 T& _get() noexcept
            {
                return *m_ptr;
            }
            SCN_CONSTEXPR14 const T& _get() const noexcept
            {
                return *m_ptr;
            }

            alignas(T) storage_type m_data{};
            pointer m_ptr{nullptr};
        };
    }  // namespace detail

    SCN_END_NAMESPACE
}  // namespace scn

#endif