From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by sourceware.org (Postfix) with ESMTP id 46C16396E843 for ; Wed, 28 Oct 2020 12:32:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 46C16396E843 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-364-6r2YeSfcMhqBDOZKKS8K9g-1; Wed, 28 Oct 2020 08:32:13 -0400 X-MC-Unique: 6r2YeSfcMhqBDOZKKS8K9g-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 3CC291868420; Wed, 28 Oct 2020 12:32:12 +0000 (UTC) Received: from localhost (unknown [10.33.36.3]) by smtp.corp.redhat.com (Postfix) with ESMTP id BFE3B5C1D7; Wed, 28 Oct 2020 12:32:11 +0000 (UTC) Date: Wed, 28 Oct 2020 12:32:10 +0000 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Make std::span layout-compatible with struct iovec [PR 95609] Message-ID: <20201028123210.GA2135626@redhat.com> MIME-Version: 1.0 X-Clacks-Overhead: GNU Terry Pratchett X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: multipart/mixed; boundary="oyUTqETQ0mS9luUI" Content-Disposition: inline X-Spam-Status: No, score=-14.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libstdc++@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++ mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 28 Oct 2020 12:32:19 -0000 --oyUTqETQ0mS9luUI Content-Type: text/plain; charset=us-ascii Content-Disposition: inline This change reorders the data members of std::span so that span is layout-compatible with common implementations of struct iovec. This will allow span to be used directly in places that use a struct iovec to do scatter-gather I/O. It's important to note that POSIX doesn't specify the order of members in iovec. Also the equivalent type on Windows has members in the other order, and uses type ULONG (which is always 32-bit whereas size_t is 64-bit for Win64). So this change will only help for certain targets and an indirection between std::span and I/O system calls will still be needed for the general case. libstdc++-v3/ChangeLog: PR libstdc++/95609 * include/std/span (span): Reorder data members to match common implementations of struct iovec. * testsuite/23_containers/span/layout_compat.cc: New test. Tested powerpc64le-linux. Committed to trunk. --oyUTqETQ0mS9luUI Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="patch.txt" commit 0f7cd5e5735e5536bf7bc8ca2b998f7ce8b4ddee Author: Jonathan Wakely Date: Wed Oct 28 12:07:40 2020 libstdc++: Make std::span layout-compatible with struct iovec [PR 95609] This change reorders the data members of std::span so that span is layout-compatible with common implementations of struct iovec. This will allow span to be used directly in places that use a struct iovec to do scatter-gather I/O. It's important to note that POSIX doesn't specify the order of members in iovec. Also the equivalent type on Windows has members in the other order, and uses type ULONG (which is always 32-bit whereas size_t is 64-bit for Win64). So this change will only help for certain targets and an indirection between std::span and I/O system calls will still be needed for the general case. libstdc++-v3/ChangeLog: PR libstdc++/95609 * include/std/span (span): Reorder data members to match common implementations of struct iovec. * testsuite/23_containers/span/layout_compat.cc: New test. diff --git a/libstdc++-v3/include/std/span b/libstdc++-v3/include/std/span index fb349403c9e..24c61ba4172 100644 --- a/libstdc++-v3/include/std/span +++ b/libstdc++-v3/include/std/span @@ -38,8 +38,8 @@ #if __cplusplus > 201703L -#include #include +#include #include #include @@ -151,7 +151,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION constexpr span() noexcept requires ((_Extent + 1u) <= 1u) - : _M_extent(0), _M_ptr(nullptr) + : _M_ptr(nullptr), _M_extent(0) { } template @@ -159,7 +159,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION constexpr explicit(extent != dynamic_extent) span(_It __first, size_type __count) noexcept - : _M_extent(__count), _M_ptr(std::to_address(__first)) + : _M_ptr(std::to_address(__first)), _M_extent(__count) { if constexpr (_Extent != dynamic_extent) { @@ -173,8 +173,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION constexpr explicit(extent != dynamic_extent) span(_It __first, _End __last) noexcept(noexcept(__last - __first)) - : _M_extent(static_cast(__last - __first)), - _M_ptr(std::to_address(__first)) + : _M_ptr(std::to_address(__first)), + _M_extent(static_cast(__last - __first)) { if constexpr (_Extent != dynamic_extent) { @@ -392,8 +392,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } private: - [[no_unique_address]] __detail::__extent_storage _M_extent; pointer _M_ptr; + [[no_unique_address]] __detail::__extent_storage _M_extent; }; // deduction guides diff --git a/libstdc++-v3/testsuite/23_containers/span/layout_compat.cc b/libstdc++-v3/testsuite/23_containers/span/layout_compat.cc new file mode 100644 index 00000000000..efc5b8e4706 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/span/layout_compat.cc @@ -0,0 +1,48 @@ +// 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 +// . + +// { dg-options "-std=gnu++2a" } +// { dg-do compile { target c++2a } } + +#include +#include + +#if __has_include() +#include +#else +struct iovec { void* iov_base; std::size_t iov_len; }; +#endif + +#if __cpp_lib_is_pointer_interconvertible +using std::is_layout_compatible_v; +#else +// A poor substitute for is_layout_compatible_v +template + constexpr bool is_layout_compatible_v + = std::is_standard_layout_v && std::is_standard_layout_v + && sizeof(T) == sizeof(U) && alignof(T) == alignof(U); +#endif + +void +test_pr95609() +{ + using rbuf = std::span; + using wbuf = std::span; + + static_assert(is_layout_compatible_v); + static_assert(is_layout_compatible_v); +} --oyUTqETQ0mS9luUI--