@@ -35,30 +35,76 @@ POSSIBILITY OF SUCH DAMAGE.
35
35
#include " libtorrent/config.hpp"
36
36
#include " libtorrent/aux_/typed_span.hpp"
37
37
#include " libtorrent/aux_/numeric_cast.hpp"
38
+ #include < iterator> // for iterator_traits
39
+ #include < memory> // for addressof
40
+
41
+ namespace libtorrent { namespace aux {
42
+
43
+ template <class ForwardIt >
44
+ inline void uninitialized_default_construct (ForwardIt first, ForwardIt last)
45
+ {
46
+ using Value = typename std::iterator_traits<ForwardIt>::value_type;
47
+ ForwardIt current = first;
48
+ try {
49
+ for (; current != last; ++current) {
50
+ ::new (static_cast <void *>(std::addressof (*current))) Value;
51
+ }
52
+ } catch (...) {
53
+ for (; first != current; ++first) {
54
+ first->~Value ();
55
+ }
56
+ throw ;
57
+ }
58
+ }
59
+
60
+ template <typename T>
61
+ struct alloca_destructor
62
+ {
63
+ span<T> objects;
64
+ ~alloca_destructor ()
65
+ {
66
+ for (auto & o : objects)
67
+ {
68
+ TORRENT_UNUSED (o);
69
+ o.~T ();
70
+ }
71
+ }
72
+ };
73
+
74
+ }}
38
75
39
76
#if defined TORRENT_WINDOWS || defined TORRENT_MINGW
40
77
41
78
#include < malloc.h>
42
79
#define TORRENT_ALLOCA (v, t, n ) ::libtorrent::aux::typed_span<t> v; { \
43
80
std::size_t TORRENT_ALLOCA_size = ::libtorrent::aux::numeric_cast<std::size_t >(n); \
44
81
t* TORRENT_ALLOCA_tmp = static_cast <t*>(_alloca (sizeof (t) * TORRENT_ALLOCA_size)); \
45
- v = ::libtorrent::aux::typed_span<t>(TORRENT_ALLOCA_tmp, TORRENT_ALLOCA_size); }
82
+ v = ::libtorrent::aux::typed_span<t>(TORRENT_ALLOCA_tmp, TORRENT_ALLOCA_size); \
83
+ ::libtorrent::aux::uninitialized_default_construct (v.begin(), v.end()); \
84
+ } \
85
+ ::libtorrent::aux::alloca_destructor<t> v##_destructor{v};
46
86
47
87
#elif defined TORRENT_BSD
48
88
49
89
#include < stdlib.h>
50
90
#define TORRENT_ALLOCA (v, t, n ) ::libtorrent::aux::typed_span<t> v; { \
51
91
std::size_t TORRENT_ALLOCA_size = ::libtorrent::aux::numeric_cast<std::size_t >(n); \
52
92
t* TORRENT_ALLOCA_tmp = static_cast <t*>(alloca (sizeof (t) * TORRENT_ALLOCA_size)); \
53
- v = ::libtorrent::aux::typed_span<t>(TORRENT_ALLOCA_tmp, TORRENT_ALLOCA_size); }
93
+ v = ::libtorrent::aux::typed_span<t>(TORRENT_ALLOCA_tmp, TORRENT_ALLOCA_size); \
94
+ ::libtorrent::aux::uninitialized_default_construct (v.begin(), v.end()); \
95
+ } \
96
+ ::libtorrent::aux::alloca_destructor<t> v##_destructor{v};
54
97
55
98
#else
56
99
57
100
#include < alloca.h>
58
101
#define TORRENT_ALLOCA (v, t, n ) ::libtorrent::aux::typed_span<t> v; { \
59
102
std::size_t TORRENT_ALLOCA_size = ::libtorrent::aux::numeric_cast<std::size_t >(n); \
60
103
t* TORRENT_ALLOCA_tmp = static_cast <t*>(alloca (sizeof (t) * TORRENT_ALLOCA_size)); \
61
- v = ::libtorrent::aux::typed_span<t>(TORRENT_ALLOCA_tmp, TORRENT_ALLOCA_size); }
104
+ v = ::libtorrent::aux::typed_span<t>(TORRENT_ALLOCA_tmp, TORRENT_ALLOCA_size); \
105
+ ::libtorrent::aux::uninitialized_default_construct (v.begin(), v.end()); \
106
+ } \
107
+ ::libtorrent::aux::alloca_destructor<t> v##_destructor{v};
62
108
63
109
#endif
64
110
0 commit comments