1  
//
1  
//
2  
// Copyright (c) 2022 Vinnie Falco (vinnie.falco@gmail.com)
2  
// Copyright (c) 2022 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/url
7  
// Official repository: https://github.com/boostorg/url
8  
//
8  
//
9  

9  

10  
#ifndef BOOST_URL_GRAMMAR_DETAIL_RECYCLED_HPP
10  
#ifndef BOOST_URL_GRAMMAR_DETAIL_RECYCLED_HPP
11  
#define BOOST_URL_GRAMMAR_DETAIL_RECYCLED_HPP
11  
#define BOOST_URL_GRAMMAR_DETAIL_RECYCLED_HPP
12  

12  

13  
#include <utility>
13  
#include <utility>
14  

14  

15  
namespace boost {
15  
namespace boost {
16  
namespace urls {
16  
namespace urls {
17  
namespace grammar {
17  
namespace grammar {
18  
namespace implementation_defined {
18  
namespace implementation_defined {
19  

19  

20  
template<
20  
template<
21  
    std::size_t Size,
21  
    std::size_t Size,
22  
    std::size_t Align>
22  
    std::size_t Align>
23  
struct aligned_storage_impl
23  
struct aligned_storage_impl
24  
{
24  
{
25  
    void* addr() noexcept
25  
    void* addr() noexcept
26  
    {
26  
    {
27  
        return buf_;
27  
        return buf_;
28  
    }
28  
    }
29  

29  

30  
    void const* addr() const noexcept
30  
    void const* addr() const noexcept
31  
    {
31  
    {
32  
        return buf_;
32  
        return buf_;
33  
    }
33  
    }
34  

34  

35  
private:
35  
private:
36  
    alignas(Align)
36  
    alignas(Align)
37  
    unsigned char buf_[Size];
37  
    unsigned char buf_[Size];
38  
};
38  
};
39  

39  

40  
constexpr
40  
constexpr
41  
std::size_t
41  
std::size_t
42  
nearest_pow2(
42  
nearest_pow2(
43  
    std::size_t x,
43  
    std::size_t x,
44  
    std::size_t f = 0) noexcept
44  
    std::size_t f = 0) noexcept
45  
{
45  
{
46  
    return
46  
    return
47  
        (f <= (std::size_t(-1)/2))
47  
        (f <= (std::size_t(-1)/2))
48  
        ? ( x <= f
48  
        ? ( x <= f
49  
            ? f
49  
            ? f
50  
            : nearest_pow2(x, 2 * f))
50  
            : nearest_pow2(x, 2 * f))
51  
        : x;
51  
        : x;
52  
}
52  
}
53  

53  

54  
//------------------------------------------------
54  
//------------------------------------------------
55  

55  

56  
BOOST_URL_DECL
56  
BOOST_URL_DECL
57  
void
57  
void
58  
recycled_add_impl(
58  
recycled_add_impl(
59  
    std::size_t) noexcept;
59  
    std::size_t) noexcept;
60  

60  

61  
BOOST_URL_DECL
61  
BOOST_URL_DECL
62  
void
62  
void
63  
recycled_remove_impl(
63  
recycled_remove_impl(
64  
    std::size_t) noexcept;
64  
    std::size_t) noexcept;
65  

65  

66  
#ifdef BOOST_URL_REPORT
66  
#ifdef BOOST_URL_REPORT
67  

67  

68  
inline
68  
inline
69  
void
69  
void
70  
recycled_add(
70  
recycled_add(
71  
    std::size_t n) noexcept
71  
    std::size_t n) noexcept
72  
{
72  
{
73  
    recycled_add_impl(n);
73  
    recycled_add_impl(n);
74  
}
74  
}
75  

75  

76  
inline
76  
inline
77  
void
77  
void
78  
recycled_remove(
78  
recycled_remove(
79  
    std::size_t n) noexcept
79  
    std::size_t n) noexcept
80  
{
80  
{
81  
    recycled_remove_impl(n);
81  
    recycled_remove_impl(n);
82  
}
82  
}
83  

83  

84  
#else
84  
#else
85  

85  

86  
inline void recycled_add(
86  
inline void recycled_add(
87  
    std::size_t) noexcept
87  
    std::size_t) noexcept
88  
{
88  
{
89  
}
89  
}
90  
inline void recycled_remove(
90  
inline void recycled_remove(
91  
    std::size_t) noexcept
91  
    std::size_t) noexcept
92  
{
92  
{
93  
}
93  
}
94  

94  

95  
#endif
95  
#endif
96  

96  

97  
} // implementation_defined
97  
} // implementation_defined
98  
} // grammar
98  
} // grammar
99  
} // urls
99  
} // urls
100  
} // boost
100  
} // boost
101  

101  

102  
#endif
102  
#endif