aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonathan Wakely <jwakely@redhat.com>2020-06-17 20:53:45 +0100
committerJonathan Wakely <jwakely@redhat.com>2020-06-17 20:55:06 +0100
commitc9dce3b15e89e851f59ebe9e8879c8f3a620311a (patch)
tree38e621db673a7f18290550caec087ca04ba20052
parentd392babbeb6cb531ab8b1ec68fde9ffd36373a6e (diff)
downloadgcc-c9dce3b15e89e851f59ebe9e8879c8f3a620311a.zip
gcc-c9dce3b15e89e851f59ebe9e8879c8f3a620311a.tar.gz
gcc-c9dce3b15e89e851f59ebe9e8879c8f3a620311a.tar.bz2
libstdc++: Handle non-integral sizes in std::uninitialized_fill_n
The std::uninitialized_fill_n algorithm uses sd::fill_n for trivial types, but that algorithm has a stronger requirement that the Size parameter is convertible to an integral type. As the new test shows, there are types which are valid for std::uninitialized_fill_n but which produce a different result when converted to an integral type, or cannot be converted at all. Only use the std::fill_n optimization when the Size type is already an integral type. The std::__uninitialized_default_n extension has the same problem, and so does C++17's std::uninitialized_value_construct_n which uses it. * include/bits/stl_uninitialized.h (uninitialized_fill_n): Only use std::fill_n when the size is an integral type. (__uninitialized_default_n): Likewise. * testsuite/20_util/specialized_algorithms/uninitialized_default_n/sizes.cc: New test. * testsuite/20_util/specialized_algorithms/uninitialized_fill_n/sizes.cc: New test. * testsuite/20_util/specialized_algorithms/uninitialized_value_construct_n/sizes.cc: New test.
-rw-r--r--libstdc++-v3/include/bits/stl_uninitialized.h26
-rw-r--r--libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_default_n/sizes.cc72
-rw-r--r--libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_fill_n/sizes.cc62
-rw-r--r--libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_value_construct_n/sizes.cc73
4 files changed, 224 insertions, 9 deletions
diff --git a/libstdc++-v3/include/bits/stl_uninitialized.h b/libstdc++-v3/include/bits/stl_uninitialized.h
index 3109a89..3771ddf 100644
--- a/libstdc++-v3/include/bits/stl_uninitialized.h
+++ b/libstdc++-v3/include/bits/stl_uninitialized.h
@@ -273,19 +273,26 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{
typedef typename iterator_traits<_ForwardIterator>::value_type
_ValueType;
+
+ // Trivial types do not need a constructor to begin their lifetime,
+ // so try to use std::fill_n to benefit from its memmove optimization.
+ // For arbitrary class types and floating point types we can't assume
+ // that __n > 0 and std::__size_to_integer(__n) > 0 are equivalent,
+ // so only use std::fill_n when _Size is already an integral type.
#if __cplusplus < 201103L
- const bool __assignable = true;
+ const bool __can_fill = __is_integer<_Size>::__value;
#else
- // Trivial types can have deleted copy constructor, but the std::fill
+ // Trivial types can have deleted copy constructor, but the std::fill_n
// optimization that uses memmove would happily "copy" them anyway.
static_assert(is_constructible<_ValueType, const _Tp&>::value,
"result type must be constructible from input type");
- // Trivial types can have deleted assignment, so using std::fill
- // would be ill-formed. Require assignability before using std::fill:
- const bool __assignable = is_copy_assignable<_ValueType>::value;
+ // Trivial types can have deleted assignment, so using std::fill_n
+ // would be ill-formed. Require assignability before using std::fill_n:
+ constexpr bool __can_fill
+ = __and_<is_integral<_Size>, is_copy_assignable<_ValueType>>::value;
#endif
- return __uninitialized_fill_n<__is_trivial(_ValueType) && __assignable>::
+ return __uninitialized_fill_n<__is_trivial(_ValueType) && __can_fill>::
__uninit_fill_n(__first, __n, __x);
}
@@ -615,11 +622,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{
typedef typename iterator_traits<_ForwardIterator>::value_type
_ValueType;
- // trivial types can have deleted assignment
- const bool __assignable = is_copy_assignable<_ValueType>::value;
+ // See uninitialized_fill_n for the conditions for using std::fill_n.
+ constexpr bool __can_fill
+ = __and_<is_integral<_Size>, is_copy_assignable<_ValueType>>::value;
return __uninitialized_default_n_1<__is_trivial(_ValueType)
- && __assignable>::
+ && __can_fill>::
__uninit_default_n(__first, __n);
}
diff --git a/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_default_n/sizes.cc b/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_default_n/sizes.cc
new file mode 100644
index 0000000..1bb4c6a
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_default_n/sizes.cc
@@ -0,0 +1,72 @@
+// Copyright (C) 2020 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library. This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3. If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-do run { target c++11 } }
+
+#include <memory>
+#include <algorithm>
+#include <testsuite_hooks.h>
+
+struct Value
+{
+ int value = 0x1234;
+};
+
+void
+test01()
+{
+ alignas(Value) unsigned char buf[3 * sizeof(Value) + 1];
+ std::fill(std::begin(buf), std::end(buf), 0xff);
+ const auto p = reinterpret_cast<Value*>(buf);
+ std::__uninitialized_default_n(p, 2.0001);
+ VERIFY( p[0].value == 0x1234 );
+ VERIFY( p[1].value == 0x1234 );
+ VERIFY( p[2].value == 0x1234 );
+ VERIFY( *std::prev(std::end(buf)) == 0xff );
+}
+
+void
+test02()
+{
+ // The standard only requires that n>0 and --n are valid expressions.
+ struct Size
+ {
+ int value;
+
+ void operator--() { --value; }
+
+ int operator>(void*) { return value != 0; }
+ };
+
+ alignas(Value) unsigned char buf[4 * sizeof(Value) + 1];
+ std::fill(std::begin(buf), std::end(buf), 0xff);
+ const auto p = reinterpret_cast<Value*>(buf);
+ Size n = {4};
+ std::__uninitialized_default_n(p, n);
+ VERIFY( p[0].value == 0x1234 );
+ VERIFY( p[1].value == 0x1234 );
+ VERIFY( p[2].value == 0x1234 );
+ VERIFY( p[3].value == 0x1234 );
+ VERIFY( *std::prev(std::end(buf)) == 0xff );
+}
+
+int
+main()
+{
+ test01();
+ test02();
+}
diff --git a/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_fill_n/sizes.cc b/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_fill_n/sizes.cc
new file mode 100644
index 0000000..eb957e1
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_fill_n/sizes.cc
@@ -0,0 +1,62 @@
+// Copyright (C) 2020 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library. This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3. If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-do run }
+
+#include <memory>
+#include <testsuite_hooks.h>
+
+void
+test01()
+{
+ int i[4] = { };
+ std::uninitialized_fill_n(i, 2.0001, 0xabcd);
+ VERIFY( i[0] == 0xabcd );
+ VERIFY( i[1] == 0xabcd );
+ VERIFY( i[2] == 0xabcd );
+ VERIFY( i[3] == 0 );
+}
+
+void
+test02()
+{
+ // The standard only requires that n>0 and --n are valid expressions.
+ struct Size
+ {
+ int value;
+
+ void operator--() { --value; }
+
+ int operator>(void*) { return value != 0; }
+ };
+
+ int i[5] = { };
+ Size n = {4};
+ std::uninitialized_fill_n(i, n, 0xdcba);
+ VERIFY( i[0] == 0xdcba );
+ VERIFY( i[1] == 0xdcba );
+ VERIFY( i[2] == 0xdcba );
+ VERIFY( i[3] == 0xdcba );
+ VERIFY( i[4] == 0 );
+}
+
+int
+main()
+{
+ test01();
+ test02();
+}
diff --git a/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_value_construct_n/sizes.cc b/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_value_construct_n/sizes.cc
new file mode 100644
index 0000000..e822b19
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_value_construct_n/sizes.cc
@@ -0,0 +1,73 @@
+// Copyright (C) 2020 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library. This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3. If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++17" }
+// { dg-do run { target c++17 } }
+
+#include <memory>
+#include <algorithm>
+#include <testsuite_hooks.h>
+
+struct Value
+{
+ int value = 0x1234;
+};
+
+void
+test01()
+{
+ alignas(Value) unsigned char buf[3 * sizeof(Value) + 1];
+ std::fill(std::begin(buf), std::end(buf), 0xff);
+ const auto p = reinterpret_cast<Value*>(buf);
+ std::uninitialized_value_construct_n(p, 2.0001);
+ VERIFY( p[0].value == 0x1234 );
+ VERIFY( p[1].value == 0x1234 );
+ VERIFY( p[2].value == 0x1234 );
+ VERIFY( *std::prev(std::end(buf)) == 0xff );
+}
+
+void
+test02()
+{
+ // The standard only requires that n>0 and --n are valid expressions.
+ struct Size
+ {
+ int value;
+
+ void operator--() { --value; }
+
+ int operator>(void*) { return value != 0; }
+ };
+
+ alignas(Value) unsigned char buf[4 * sizeof(Value) + 1];
+ std::fill(std::begin(buf), std::end(buf), 0xff);
+ const auto p = reinterpret_cast<Value*>(buf);
+ Size n = {4};
+ std::uninitialized_value_construct_n(p, n);
+ VERIFY( p[0].value == 0x1234 );
+ VERIFY( p[1].value == 0x1234 );
+ VERIFY( p[2].value == 0x1234 );
+ VERIFY( p[3].value == 0x1234 );
+ VERIFY( *std::prev(std::end(buf)) == 0xff );
+}
+
+int
+main()
+{
+ test01();
+ test02();
+}