* [PATCH] Define std::__to_address helper
@ 2017-09-13 7:28 Jonathan Wakely
0 siblings, 0 replies; only message in thread
From: Jonathan Wakely @ 2017-09-13 7:28 UTC (permalink / raw)
To: libstdc++, gcc-patches
[-- Attachment #1: Type: text/plain, Size: 925 bytes --]
As discussed a few weeks ago, this adds a helper to get a raw pointer
from any pointer-like type, and usees that helper where appropriate.
* include/bits/allocated_ptr.h (__allocated_ptr::get): Use
__to_address.
(__allocated_ptr::_S_raw_ptr): Remove.
* include/bits/forward_list.h (_Fwd_list_base::_M_get_node): Use
__to_address.
* include/bits/hashtable_policy.h (_Hashtable_alloc): Likewise.
* include/bits/ptr_traits.h (__to_address): Define new function
template.
* include/bits/shared_ptr_base.h (__shared_ptr): Use __to_address.
(__shared_ptr::_S_raw_ptr): Remove.
* include/bits/stl_vector.h [__cplusplus >= 201103L]
(vector::_M_data_ptr): Use __to_address.
[__cplusplus < 201103L] (vector::_M_data_ptr): Don't dereference
possibly invalid pointers.
* include/ext/alloc_traits.h (__alloc_traits::construct)
(__alloc_traits::destroy): Use __to_address.
Tested powerpc64le-linux, committed to trunk.
[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 7627 bytes --]
commit 32492631ebcb63c78539e02a65f4ca3352e4ecfd
Author: Jonathan Wakely <jwakely@redhat.com>
Date: Thu Jul 20 16:19:53 2017 +0100
Define std::__to_address helper
* include/bits/allocated_ptr.h (__allocated_ptr::get): Use
__to_address.
(__allocated_ptr::_S_raw_ptr): Remove.
* include/bits/forward_list.h (_Fwd_list_base::_M_get_node): Use
__to_address.
* include/bits/hashtable_policy.h (_Hashtable_alloc): Likewise.
* include/bits/ptr_traits.h (__to_address): Define new function
template.
* include/bits/shared_ptr_base.h (__shared_ptr): Use __to_address.
(__shared_ptr::_S_raw_ptr): Remove.
* include/bits/stl_vector.h [__cplusplus >= 201103L]
(vector::_M_data_ptr): Use __to_address.
[__cplusplus < 201103L] (vector::_M_data_ptr): Don't dereference
possibly invalid pointers.
* include/ext/alloc_traits.h (__alloc_traits::construct)
(__alloc_traits::destroy): Use __to_address.
diff --git a/libstdc++-v3/include/bits/allocated_ptr.h b/libstdc++-v3/include/bits/allocated_ptr.h
index 773b3f500f0..96d7841b65b 100644
--- a/libstdc++-v3/include/bits/allocated_ptr.h
+++ b/libstdc++-v3/include/bits/allocated_ptr.h
@@ -82,16 +82,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
/// Get the address that the owned pointer refers to.
- value_type* get() { return _S_raw_ptr(_M_ptr); }
+ value_type* get() { return std::__to_address(_M_ptr); }
private:
- static value_type* _S_raw_ptr(value_type* __ptr) { return __ptr; }
-
- template<typename _Ptr>
- static auto
- _S_raw_ptr(_Ptr __ptr) -> decltype(_S_raw_ptr(__ptr.operator->()))
- { return _S_raw_ptr(__ptr.operator->()); }
-
_Alloc* _M_alloc;
pointer _M_ptr;
};
diff --git a/libstdc++-v3/include/bits/forward_list.h b/libstdc++-v3/include/bits/forward_list.h
index 9d86fcc1568..855a72815ae 100644
--- a/libstdc++-v3/include/bits/forward_list.h
+++ b/libstdc++-v3/include/bits/forward_list.h
@@ -336,7 +336,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
_M_get_node()
{
auto __ptr = _Node_alloc_traits::allocate(_M_get_Node_allocator(), 1);
- return std::__addressof(*__ptr);
+ return std::__to_address(__ptr);
}
template<typename... _Args>
diff --git a/libstdc++-v3/include/bits/hashtable_policy.h b/libstdc++-v3/include/bits/hashtable_policy.h
index 5f2d8776aaa..da6d49c96c4 100644
--- a/libstdc++-v3/include/bits/hashtable_policy.h
+++ b/libstdc++-v3/include/bits/hashtable_policy.h
@@ -2048,7 +2048,7 @@ namespace __detail
_Hashtable_alloc<_NodeAlloc>::_M_allocate_node(_Args&&... __args)
{
auto __nptr = __node_alloc_traits::allocate(_M_node_allocator(), 1);
- __node_type* __n = std::__addressof(*__nptr);
+ __node_type* __n = std::__to_address(__nptr);
__try
{
::new ((void*)__n) __node_type;
@@ -2094,7 +2094,7 @@ namespace __detail
__bucket_alloc_type __alloc(_M_node_allocator());
auto __ptr = __bucket_alloc_traits::allocate(__alloc, __n);
- __bucket_type* __p = std::__addressof(*__ptr);
+ __bucket_type* __p = std::__to_address(__ptr);
__builtin_memset(__p, 0, __n * sizeof(__bucket_type));
return __p;
}
diff --git a/libstdc++-v3/include/bits/ptr_traits.h b/libstdc++-v3/include/bits/ptr_traits.h
index 797e7fcec39..74d4c18126c 100644
--- a/libstdc++-v3/include/bits/ptr_traits.h
+++ b/libstdc++-v3/include/bits/ptr_traits.h
@@ -146,6 +146,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
template<typename _Ptr, typename _Tp>
using __ptr_rebind = typename pointer_traits<_Ptr>::template rebind<_Tp>;
+ template<typename _Tp>
+ constexpr _Tp*
+ __to_address(_Tp* __ptr) noexcept
+ { return __ptr; }
+
+ template<typename _Ptr>
+ constexpr typename std::pointer_traits<_Ptr>::element_type*
+ __to_address(const _Ptr& __ptr)
+ { return std::__to_address(__ptr.operator->()); }
+
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace std
diff --git a/libstdc++-v3/include/bits/shared_ptr_base.h b/libstdc++-v3/include/bits/shared_ptr_base.h
index 7e6766ba7d5..20504f326e8 100644
--- a/libstdc++-v3/include/bits/shared_ptr_base.h
+++ b/libstdc++-v3/include/bits/shared_ptr_base.h
@@ -1185,7 +1185,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
__shared_ptr(unique_ptr<_Yp, _Del>&& __r)
: _M_ptr(__r.get()), _M_refcount()
{
- auto __raw = _S_raw_ptr(__r.get());
+ auto __raw = __to_address(__r.get());
_M_refcount = __shared_count<_Lp>(std::move(__r));
_M_enable_shared_from_this_with(__raw);
}
@@ -1201,7 +1201,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
__shared_ptr(unique_ptr<_Tp1, _Del>&& __r, __sp_array_delete)
: _M_ptr(__r.get()), _M_refcount()
{
- auto __raw = _S_raw_ptr(__r.get());
+ auto __raw = __to_address(__r.get());
_M_refcount = __shared_count<_Lp>(std::move(__r));
_M_enable_shared_from_this_with(__raw);
}
@@ -1386,16 +1386,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
_M_get_deleter(const std::type_info& __ti) const noexcept
{ return _M_refcount._M_get_deleter(__ti); }
- template<typename _Tp1>
- static _Tp1*
- _S_raw_ptr(_Tp1* __ptr)
- { return __ptr; }
-
- template<typename _Tp1>
- static auto
- _S_raw_ptr(_Tp1 __ptr) -> decltype(std::__addressof(*__ptr))
- { return std::__addressof(*__ptr); }
-
template<typename _Tp1, _Lock_policy _Lp1> friend class __shared_ptr;
template<typename _Tp1, _Lock_policy _Lp1> friend class __weak_ptr;
diff --git a/libstdc++-v3/include/bits/stl_vector.h b/libstdc++-v3/include/bits/stl_vector.h
index 69cb8030208..c7634216c01 100644
--- a/libstdc++-v3/include/bits/stl_vector.h
+++ b/libstdc++-v3/include/bits/stl_vector.h
@@ -1695,7 +1695,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
template<typename _Ptr>
typename std::pointer_traits<_Ptr>::element_type*
_M_data_ptr(_Ptr __ptr) const
- { return empty() ? nullptr : std::__addressof(*__ptr); }
+ { return empty() ? nullptr : std::__to_address(__ptr); }
#else
template<typename _Up>
_Up*
@@ -1705,12 +1705,12 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
template<typename _Ptr>
value_type*
_M_data_ptr(_Ptr __ptr)
- { return __ptr.operator->(); }
+ { return empty() ? (value_type*)0 : __ptr.operator->(); }
template<typename _Ptr>
const value_type*
_M_data_ptr(_Ptr __ptr) const
- { return __ptr.operator->(); }
+ { return empty() ? (const value_type*)0 : __ptr.operator->(); }
#endif
};
diff --git a/libstdc++-v3/include/ext/alloc_traits.h b/libstdc++-v3/include/ext/alloc_traits.h
index fe5655100a4..c6f44950007 100644
--- a/libstdc++-v3/include/ext/alloc_traits.h
+++ b/libstdc++-v3/include/ext/alloc_traits.h
@@ -81,7 +81,7 @@ template<typename _Alloc, typename = typename _Alloc::value_type>
static typename std::enable_if<__is_custom_pointer<_Ptr>::value>::type
construct(_Alloc& __a, _Ptr __p, _Args&&... __args)
{
- _Base_type::construct(__a, std::addressof(*__p),
+ _Base_type::construct(__a, std::__to_address(__p),
std::forward<_Args>(__args)...);
}
@@ -89,7 +89,7 @@ template<typename _Alloc, typename = typename _Alloc::value_type>
template<typename _Ptr>
static typename std::enable_if<__is_custom_pointer<_Ptr>::value>::type
destroy(_Alloc& __a, _Ptr __p)
- { _Base_type::destroy(__a, std::addressof(*__p)); }
+ { _Base_type::destroy(__a, std::__to_address(__p)); }
static _Alloc _S_select_on_copy(const _Alloc& __a)
{ return _Base_type::select_on_container_copy_construction(__a); }
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2017-09-13 7:28 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-09-13 7:28 [PATCH] Define std::__to_address helper Jonathan Wakely
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).