From patchwork Wed Sep 13 07:28:35 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 813190 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-461999-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="jXK8ff5x"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3xsYF74wDwz9sNw for ; Wed, 13 Sep 2017 17:28:58 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; q=dns; s= default; b=WP5kxo6IGlAuVVxw8dEL6lQTuKBHEFup9ppTkXB6QuyRVk459gRC5 UzaBGtOBhfkMAf8ig1brdLhOqNczfc5UCRTnmNGmx9w6C9X2X9bM6f2nBf/yUF5a pvJdfdgwlBGMSl7H5X+05GD+fgEzSc2Lk7ahKTybXoxoqdIBb5vYds= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; s= default; bh=KjT3DrLQRYxgkdClf50zJKL6OsI=; b=jXK8ff5xsyRxVD/Psw2U tHOnC8bngTSUlgfZry1NPDp5DrvwDHDp7yC/cMfQZOnqO5y7sFT0KzUzvvRIHKJw uow+LiUNZufYCDwHQwRgs13b3P2JIdYHdDkgTqLsm+I6Pqv0ysLILzCb6zIALwrK CmOcFDZE+D5OVUrSkU4jyVw= Received: (qmail 44393 invoked by alias); 13 Sep 2017 07:28:41 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 44364 invoked by uid 89); 13 Sep 2017 07:28:39 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=friend X-Spam-User: qpsmtpd, 2 recipients X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 13 Sep 2017 07:28:38 +0000 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id EF8D983F45; Wed, 13 Sep 2017 07:28:36 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com EF8D983F45 Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=jwakely@redhat.com Received: from localhost (unknown [10.33.36.37]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9FFB05C6D7; Wed, 13 Sep 2017 07:28:36 +0000 (UTC) Date: Wed, 13 Sep 2017 08:28:35 +0100 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH] Define std::__to_address helper Message-ID: <20170913072835.GA25521@redhat.com> MIME-Version: 1.0 Content-Disposition: inline X-Clacks-Overhead: GNU Terry Pratchett User-Agent: Mutt/1.8.3 (2017-05-23) 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. commit 32492631ebcb63c78539e02a65f4ca3352e4ecfd Author: Jonathan Wakely 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 - 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 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 using __ptr_rebind = typename pointer_traits<_Ptr>::template rebind<_Tp>; + template + constexpr _Tp* + __to_address(_Tp* __ptr) noexcept + { return __ptr; } + + template + 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 - static _Tp1* - _S_raw_ptr(_Tp1* __ptr) - { return __ptr; } - - template - static auto - _S_raw_ptr(_Tp1 __ptr) -> decltype(std::__addressof(*__ptr)) - { return std::__addressof(*__ptr); } - template friend class __shared_ptr; template 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 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 _Up* @@ -1705,12 +1705,12 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER template value_type* _M_data_ptr(_Ptr __ptr) - { return __ptr.operator->(); } + { return empty() ? (value_type*)0 : __ptr.operator->(); } template 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 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 template 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); }