Implement P0040R3, Extending memory management tools.

* include/bits/stl_uninitialized.h (utility): New include
	in C++17 mode.
	(uninitialized_default_construct): New.
	(uninitialized_default_construct_n): Likewise.
	(uninitialized_value_construct): Likewise.
	(uninitialized_value_construct_n): Likewise.
	(uninitialized_move): Likewise.
	(uninitialized_move_n): Likewise.
	(destroy_at, destroy, destroy_n): Likewise.
	* testsuite/20_util/specialized_algorithms/memory_management_tools/1.cc:
	New.

From-SVN: r240122
This commit is contained in:
Ville Voutilainen 2016-09-13 22:26:02 +03:00 committed by Ville Voutilainen
parent 9952908a4b
commit 8e14a10cab
3 changed files with 243 additions and 0 deletions

View File

@ -1,3 +1,18 @@
2016-09-13 Ville Voutilainen <ville.voutilainen@gmail.com>
Implement P0040R3, Extending memory management tools.
* include/bits/stl_uninitialized.h (utility): New include
in C++17 mode.
(uninitialized_default_construct): New.
(uninitialized_default_construct_n): Likewise.
(uninitialized_value_construct): Likewise.
(uninitialized_value_construct_n): Likewise.
(uninitialized_move): Likewise.
(uninitialized_move_n): Likewise.
(destroy_at, destroy, destroy_n): Likewise.
* testsuite/20_util/specialized_algorithms/memory_management_tools/1.cc:
New.
2016-09-12 Jason Merrill <jason@redhat.com>
* config/abi/pre/gnu.ver: Use [jmy] for size_t.

View File

@ -56,6 +56,10 @@
#ifndef _STL_UNINITIALIZED_H
#define _STL_UNINITIALIZED_H 1
#if __cplusplus > 201402L
#include <utility>
#endif
namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
@ -682,6 +686,98 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
std::__iterator_category(__first)); }
#endif
#if __cplusplus > 201402L
template <typename _ForwardIterator>
inline void
uninitialized_default_construct(_ForwardIterator __first,
_ForwardIterator __last)
{
for (; __first != __last; ++__first)
::new (static_cast<void*>(std::__addressof(*__first)))
typename iterator_traits<_ForwardIterator>::value_type;
}
template <typename _ForwardIterator, typename _Size>
inline _ForwardIterator
uninitialized_default_construct_n(_ForwardIterator __first, _Size __count)
{
for (; __count > 0; (void)++__first, --__count)
::new (static_cast<void*>(std::__addressof(*__first)))
typename iterator_traits<_ForwardIterator>::value_type;
return __first;
}
template <typename _ForwardIterator>
inline void
uninitialized_value_construct(_ForwardIterator __first,
_ForwardIterator __last)
{
for (; __first != __last; ++__first)
::new (static_cast<void*>(std::__addressof(*__first)))
typename iterator_traits<_ForwardIterator>::value_type();
}
template <typename _ForwardIterator, typename _Size>
inline _ForwardIterator
uninitialized_value_construct_n(_ForwardIterator __first, _Size __count)
{
for (; __count > 0; (void)++__first, --__count)
::new (static_cast<void*>(std::__addressof(*__first)))
typename iterator_traits<_ForwardIterator>::value_type();
return __first;
}
template <typename _InputIterator, typename _ForwardIterator>
inline _ForwardIterator
uninitialized_move(_InputIterator __first, _InputIterator __last,
_ForwardIterator __result)
{
for (; __first != __last; (void)++__result, ++__first)
::new (static_cast<void*>(std::__addressof(*__result)))
typename
iterator_traits<_ForwardIterator>::value_type(std::move(*__first));
return __result;
}
template <typename _InputIterator, typename _Size, typename _ForwardIterator>
inline pair<_InputIterator, _ForwardIterator>
uninitialized_move_n(_InputIterator __first, _Size __count,
_ForwardIterator __result)
{
for (; __count > 0; ++__result, (void) ++__first, --__count)
::new (static_cast<void*>(std::__addressof(*__result)))
typename
iterator_traits<_ForwardIterator>::value_type(std::move(*__first));
return {__first, __result};
}
template <typename _Tp>
inline void
destroy_at(_Tp* __location)
{
__location->~_Tp();
}
template <typename _ForwardIterator>
inline void
destroy(_ForwardIterator __first, _ForwardIterator __last)
{
for (; __first != __last; ++__first)
std::destroy_at(std::__addressof(*__first));
}
template <typename _ForwardIterator, typename _Size>
inline _ForwardIterator
destroy_n(_ForwardIterator __first, _Size __count)
{
for (; __count > 0; (void)++__first, --__count)
std::destroy_at(std::__addressof(*__first));
return __first;
}
#endif
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace

View File

@ -0,0 +1,132 @@
// Copyright (C) 2016 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" }
#include <memory>
#include <testsuite_hooks.h>
#include <string>
#include <array>
int del_count = 0;
struct DelCount
{
~DelCount() { ++del_count; }
};
void test01()
{
char test_data[] = "123456";
std::uninitialized_default_construct(std::begin(test_data),
std::end(test_data));
VERIFY(std::string(test_data) == "123456");
}
void test02()
{
char test_data[] = "123456";
std::uninitialized_value_construct(std::begin(test_data),
std::end(test_data));
VERIFY(std::string(test_data, 6) == std::string("\0\0\0\0\0\0", 6));
}
void test03()
{
char test_data[] = "123456";
std::uninitialized_default_construct_n(std::begin(test_data), 6);
VERIFY(std::string(test_data) == "123456");
}
void test04()
{
char test_data[] = "123456";
std::uninitialized_value_construct_n(std::begin(test_data), 6);
VERIFY(std::string(test_data, 6) == std::string("\0\0\0\0\0\0", 6));
}
void test05()
{
del_count = 0;
DelCount* x = (DelCount*)malloc(sizeof(DelCount));
new (x) DelCount;
std::destroy_at(&x[0]);
VERIFY(del_count == 1);
del_count = 0;
free(x);
}
void test06()
{
del_count = 0;
DelCount* x = (DelCount*)malloc(sizeof(DelCount)*10);
for (int i = 0; i < 10; ++i) new (x+i) DelCount;
std::destroy(x, x+10);
VERIFY(del_count == 10);
del_count = 0;
free(x);
}
void test07()
{
del_count = 0;
DelCount* x = (DelCount*)malloc(sizeof(DelCount)*10);
for (int i = 0; i < 10; ++i) new (x+i) DelCount;
std::destroy_n(x, 10);
VERIFY(del_count == 10);
del_count = 0;
free(x);
}
void test08()
{
std::vector<std::unique_ptr<int>> source;
for (int i = 0; i < 10; ++i) source.push_back(std::make_unique<int>(i));
std::unique_ptr<int>* target =
(std::unique_ptr<int>*)malloc(sizeof(std::unique_ptr<int>)*10);
std::uninitialized_move(source.begin(), source.end(), target);
for (const auto& x : source) VERIFY(!x);
for (int i = 0; i < 10; ++i) VERIFY(bool(*(target+i)));
std::destroy_n(target, 10);
free(target);
}
void test09()
{
std::vector<std::unique_ptr<int>> source;
for (int i = 0; i < 10; ++i) source.push_back(std::make_unique<int>(i));
std::unique_ptr<int>* target =
(std::unique_ptr<int>*)malloc(sizeof(std::unique_ptr<int>)*10);
std::uninitialized_move_n(source.begin(), 10, target);
for (const auto& x : source) VERIFY(!x);
for (int i = 0; i < 10; ++i) VERIFY(bool(*(target+i)));
std::destroy_n(target, 10);
free(target);
}
int main()
{
test01();
test02();
test03();
test04();
test05();
test06();
test07();
test08();
test09();
}