From patchwork Fri Feb 24 14:28:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 61338 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp940041wrd; Fri, 24 Feb 2023 06:29:55 -0800 (PST) X-Google-Smtp-Source: AK7set9NvogFcx/EogtYcj6j6BLASUExahnIuqytP4NCcKtfX+COnavOkZW6yuqnoF9BJcPDMJ/g X-Received: by 2002:a17:906:9bc3:b0:8a5:3d1e:6302 with SMTP id de3-20020a1709069bc300b008a53d1e6302mr22133782ejc.56.1677248995236; Fri, 24 Feb 2023 06:29:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677248995; cv=none; d=google.com; s=arc-20160816; b=h/JOz6WXoyrQwiXd5dzv/2xIiA9uRRkXp6P21oinXRoQNWes8y6+jAU4v2WET5f3wM +Liuiw5uPnxaCp+GeA880jCad/dvJVGNx5x0YKijCPchWJskuQIzgLWEgvFjQbqTOYdg me0CPTFXd8CSBX3qCNquuvuey34PXPKOQDPwh3euQPrbAqLRyspmX3RJrIR2KdXd3Iay hxBIlq8iA+kKiCYkp3nfJrMbB4zTCTnsDyU6T9hS4YdbEjPWviIdkuH6wQN4MzP5XksB 3ACaYLksjslvzTCOZ5VSxFjxXlVo/dVu5zWL9uTu/Nu517fBfIUGHG5ZZb+rcf9rCeTQ 8qZg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=tZ3GqxVPL4kCCkzdESQkkSGbn8USBfxWncrfp/E3VvQ=; b=BBLRC5I15dbO8JYpEeXSl6ddfUuN8SgHwKs+f8EWALfnmA2vbAQ7Ntmkim3NFvbpP8 QzSyatBjJ3yfYe5ucQa58iPOxaRURW+id/iEj6WG8RYGrZMUjqX0Ssv33VEMCABIAcPd Y3IZYB0k3nfCrvS7Y2kQUgDL7p/ONI6bpf02gn8+Y46DCZ0BElBXlssIVVXxZHCVUw6U QYh2yqe1WU+06HklE4xdW9ZG/3DKKjE4wE3hBrNulPBZF+35V6eDdK3ch4YK3xw7O0un oCsha8p3basE5GJrdAuB9YigeLY3jbwY6K15QMKzOIh98RDm34aGK/MnunFIwuv2V4Vl O8dA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=F0BaHPqu; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id d1-20020a056402516100b004ad12c250a7si7366799ede.111.2023.02.24.06.29.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Feb 2023 06:29:55 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=F0BaHPqu; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9D8FF38493EE for ; Fri, 24 Feb 2023 14:28:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9D8FF38493EE DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1677248938; bh=tZ3GqxVPL4kCCkzdESQkkSGbn8USBfxWncrfp/E3VvQ=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=F0BaHPquVhkT0iLYm05KbPrFK39CioOClWqtLUgGaivL02jD0ec0wQaji2VsPVY7m eZf4InouAqNEEiF3h3IPv7HYnleMHAnIcmVUlUHPEOnO4Z9ufCYHqj+r0nmgU7BE1e 0L1NctvpG2CqrBnu+PnIZOfhsKHrSpnfzlNbpR3s= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id AC3E53857365 for ; Fri, 24 Feb 2023 14:28:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AC3E53857365 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-68-ynXBJp27OiOwQOYRIdMP7A-1; Fri, 24 Feb 2023 09:28:09 -0500 X-MC-Unique: ynXBJp27OiOwQOYRIdMP7A-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 26DAF95D601; Fri, 24 Feb 2023 14:28:09 +0000 (UTC) Received: from localhost (unknown [10.33.36.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id E54FD404BEC0; Fri, 24 Feb 2023 14:28:08 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed 1/5] libstdc++: Optimize net::ip::address_v4::to_string() Date: Fri, 24 Feb 2023 14:28:04 +0000 Message-Id: <20230224142808.714075-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1758723042181401218?= X-GMAIL-MSGID: =?utf-8?q?1758723042181401218?= Tested x86_64-linux. Pushed to trunk. -- >8 -- This is an order of magnitude faster than calling inet_ntop (and not only because we now avoid allocating a string that is one byte larger than the SSO buffer). libstdc++-v3/ChangeLog: * include/experimental/internet (address_v4::to_string): Optimize. * testsuite/experimental/net/internet/address/v4/members.cc: Check more addresses. --- libstdc++-v3/include/experimental/internet | 28 +++++++++++++------ .../net/internet/address/v4/members.cc | 11 ++++++++ 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/libstdc++-v3/include/experimental/internet b/libstdc++-v3/include/experimental/internet index 707370d5611..08bd0db4bb2 100644 --- a/libstdc++-v3/include/experimental/internet +++ b/libstdc++-v3/include/experimental/internet @@ -44,6 +44,7 @@ #include #include #include +#include #ifdef _GLIBCXX_HAVE_UNISTD_H # include #endif @@ -241,17 +242,28 @@ namespace ip __string_with<_Allocator> to_string(const _Allocator& __a = _Allocator()) const { -#ifdef _GLIBCXX_HAVE_ARPA_INET_H + auto __write = [__addr = to_uint()](char* __p, size_t __n) { + auto __to_chars = [](char* __p, uint8_t __v) { + unsigned __n = __v >= 100u ? 3 : __v >= 10u ? 2 : 1; + std::__detail::__to_chars_10_impl(__p, __n, __v); + return __p + __n; + }; + const auto __begin = __p; + __p = __to_chars(__p, uint8_t(__addr >> 24)); + for (int __i = 2; __i >= 0; __i--) { + *__p++ = '.'; + __p = __to_chars(__p, uint8_t(__addr >> (__i * 8))); + } + return __p - __begin; + }; __string_with<_Allocator> __str(__a); - __str.resize(INET_ADDRSTRLEN); - if (inet_ntop(AF_INET, &_M_addr, &__str.front(), __str.size())) - __str.erase(__str.find('\0')); - else - __str.resize(0); - return __str; +#if __cpp_lib_string_resize_and_overwrite + __str.resize_and_overwrite(15, __write); #else - std::__throw_system_error((int)__unsupported_err()); + __str.resize(15); + __str.resize(__write(&__str.front(), 15)); #endif + return __str; } // static members: diff --git a/libstdc++-v3/testsuite/experimental/net/internet/address/v4/members.cc b/libstdc++-v3/testsuite/experimental/net/internet/address/v4/members.cc index df19b11804d..c40a8103664 100644 --- a/libstdc++-v3/testsuite/experimental/net/internet/address/v4/members.cc +++ b/libstdc++-v3/testsuite/experimental/net/internet/address/v4/members.cc @@ -22,6 +22,7 @@ #include #include #include +#include using std::experimental::net::ip::address_v4; @@ -100,6 +101,16 @@ test04() VERIFY( address_v4::any().to_string() == "0.0.0.0" ); VERIFY( address_v4::loopback().to_string() == "127.0.0.1" ); VERIFY( address_v4::broadcast().to_string() == "255.255.255.255" ); + using b = address_v4::bytes_type; + VERIFY( address_v4(b(1, 23, 45, 67)).to_string() == "1.23.45.67" ); + VERIFY( address_v4(b(12, 34, 56, 78)).to_string() == "12.34.56.78" ); + VERIFY( address_v4(b(123, 4, 5, 6)).to_string() == "123.4.5.6" ); + VERIFY( address_v4(b(123, 234, 124, 235)).to_string() == "123.234.124.235" ); + + __gnu_test::uneq_allocator alloc(123); + auto str = address_v4(b(12, 34, 56, 78)).to_string(alloc); + VERIFY(str.get_allocator().get_personality() == alloc.get_personality()); + VERIFY( str == "12.34.56.78" ); } void From patchwork Fri Feb 24 14:28:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 61342 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp941261wrd; Fri, 24 Feb 2023 06:31:47 -0800 (PST) X-Google-Smtp-Source: AK7set8nlwJz3sAH6P9QGyEf/W4umBsi9uD09EqbHYbhYl/OjYy+p5++/KebYY1Sdbyl757WVbCi X-Received: by 2002:aa7:cfd4:0:b0:4ab:1c69:5c4 with SMTP id r20-20020aa7cfd4000000b004ab1c6905c4mr13832173edy.26.1677249107330; Fri, 24 Feb 2023 06:31:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677249107; cv=none; d=google.com; s=arc-20160816; b=JS6ukNv+dUlRe8l2Hk4sVqN6yaZRic+Bcr0N/MnBGPbwPdZ7XKYUnJf0CMvZXZ8UvA eH1D0rWxgrsbI1sMmNiP75AI9o3YBKelPd1CAyKlpTXpavzRYNMpFa14WHsKDlapi4Hq Kbjrt5jVx2jtHRxzMAZxezMLcxRZrugpB42up8YfGodlKzEEfPZaJG6oav8K7Mi5lgPm Cw03j8ny9JqGlRT1tlqqonhnGxxNkZDTTj7yRte1fhJ3CnDzvsHETYa075HiNXNimWO9 zVx5MoPJSk2ctMRhm54QHD+JIdsB3cKYjBsa+yV5ZgAngfMR6ktPRxIyQ2giNcjGVhBM 7mxA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:dmarc-filter:delivered-to:dkim-signature :dkim-filter; bh=tapazxQK7dT5gP+5bx/z1qB45sYMtrQjh+/wIWZhV6Y=; b=P/cFWDPBCTuxWW1dnHz8xbwJlPENudhy0iX+9EAkBvQFDGWe8ALTrtn3FmTEJm6pSk 4cJwc8AzcdpGOu6t77hZssdHpLdIRbQHhPbFB8ytMQX3dycBwo2phbPWlUiQ1UAIkQsV R4wDpT3LGZJivMIdGCNYbCgOTvO9PpdTRn21ZjEBZx8I7/59FQAuSr2LXEv4dNHteFpJ va6JguPta/1qaqXY3eUH2vuWsP+5hQmJnjlTg82u2rhZQaZMJ9jhEWJuSBfxFuFSCrGY UKj3t+o2wM0IE39wV7X7eFJC2ogTj1rZeulaBF+HuzwzqDZ9LzS4axB7ZbKj38+6aDNw J/tA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=ycR6x39E; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id bo13-20020a0564020b2d00b004ad15601457si10590130edb.629.2023.02.24.06.31.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Feb 2023 06:31:47 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=ycR6x39E; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BFBF038493EC for ; Fri, 24 Feb 2023 14:29:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BFBF038493EC DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1677248998; bh=tapazxQK7dT5gP+5bx/z1qB45sYMtrQjh+/wIWZhV6Y=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=ycR6x39EtcgNK2ZGhRtFnb4QKtPtAVZYZ1Tadm23S3MDRSeWLXo1hylPa1/f8HxoS MEHTMC/RYkSJOsRqc2yd9dlUKIYR+RGAjYyRqw9un/+C0xjs/uLpqmZDHJVTiknchK 27xI96jrvTlkoig0a/6/nr36HGDLiAeUEHxjIVLE= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id AB636385559A for ; Fri, 24 Feb 2023 14:28:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AB636385559A Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-541-Ens_XiQLOvSuL3Ydp46tzg-1; Fri, 24 Feb 2023 09:28:10 -0500 X-MC-Unique: Ens_XiQLOvSuL3Ydp46tzg-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 035F2887409; Fri, 24 Feb 2023 14:28:10 +0000 (UTC) Received: from localhost (unknown [10.33.36.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id BD5B6492C3E; Fri, 24 Feb 2023 14:28:09 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed 2/5] libstdc++: Fix conversion to/from net::ip::address_v4::bytes_type Date: Fri, 24 Feb 2023 14:28:05 +0000 Message-Id: <20230224142808.714075-2-jwakely@redhat.com> In-Reply-To: <20230224142808.714075-1-jwakely@redhat.com> References: <20230224142808.714075-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1758723159649261447?= X-GMAIL-MSGID: =?utf-8?q?1758723159649261447?= Tested x86_64-linux. Pushed to trunk. -- >8 -- I messed up the endianness of the address_v4::bytes_type array, which should always be in network byte order. We can just use bit_cast to convert the _M_addr member to/from bytes_type. libstdc++-v3/ChangeLog: * include/experimental/internet (address_4(const bytes_type&)): Use __builtin_bit_cast if available, otherwise convert to network byte order. (address_v4::to_bytes()): Likewise, but convert from network byte order. * testsuite/experimental/net/internet/address/v4/cons.cc: Fix incorrect tests. Check for constexpr too. * testsuite/experimental/net/internet/address/v4/creation.cc: Likewise. * testsuite/experimental/net/internet/address/v4/members.cc: Check that bytes_type is a standard-layout type. --- libstdc++-v3/include/experimental/internet | 20 ++++++++--- .../net/internet/address/v4/cons.cc | 33 ++++++++++++------- .../net/internet/address/v4/creation.cc | 24 +++++++++++--- .../net/internet/address/v4/members.cc | 3 ++ 4 files changed, 60 insertions(+), 20 deletions(-) diff --git a/libstdc++-v3/include/experimental/internet b/libstdc++-v3/include/experimental/internet index 08bd0db4bb2..3fd200251fa 100644 --- a/libstdc++-v3/include/experimental/internet +++ b/libstdc++-v3/include/experimental/internet @@ -198,7 +198,12 @@ namespace ip constexpr address_v4(const bytes_type& __b) - : _M_addr((__b[0] << 24) | (__b[1] << 16) | (__b[2] << 8) | __b[3]) +#if __has_builtin(__builtin_bit_cast) + : _M_addr(__builtin_bit_cast(uint_type, __b)) +#else + : _M_addr(_S_hton_32((__b[0] << 24) | (__b[1] << 16) + | (__b[2] << 8) | __b[3])) +#endif { } explicit constexpr @@ -227,12 +232,17 @@ namespace ip constexpr bytes_type to_bytes() const noexcept { +#if __has_builtin(__builtin_bit_cast) + return __builtin_bit_cast(bytes_type, _M_addr); +#else + auto __host = to_uint(); return bytes_type{ - (_M_addr >> 24) & 0xFF, - (_M_addr >> 16) & 0xFF, - (_M_addr >> 8) & 0xFF, - _M_addr & 0xFF + (__host >> 24) & 0xFF, + (__host >> 16) & 0xFF, + (__host >> 8) & 0xFF, + __host & 0xFF }; +#endif } constexpr uint_type diff --git a/libstdc++-v3/testsuite/experimental/net/internet/address/v4/cons.cc b/libstdc++-v3/testsuite/experimental/net/internet/address/v4/cons.cc index 65f23642de4..af9fef2215e 100644 --- a/libstdc++-v3/testsuite/experimental/net/internet/address/v4/cons.cc +++ b/libstdc++-v3/testsuite/experimental/net/internet/address/v4/cons.cc @@ -24,41 +24,45 @@ using std::experimental::net::ip::address_v4; -void +#if __cplusplus < 202002L +// Naughty, but operator== for std::array is not constexpr until C++20. +constexpr bool +operator==(const address_v4::bytes_type& lhs, const address_v4::bytes_type& rhs) +{ + return lhs[0] == rhs[0] && lhs[1] == rhs[1] + && lhs[2] == rhs[2] && lhs[3] == rhs[3]; +} +#endif + +constexpr void test01() { - bool test __attribute__((unused)) = false; - address_v4 a0; VERIFY( a0.to_uint() == 0 ); VERIFY( a0.to_bytes() == address_v4::bytes_type{} ); } -void +constexpr void test02() { - bool test __attribute__((unused)) = false; - address_v4 a0{ address_v4::bytes_type{} }; VERIFY( a0.to_uint() == 0 ); VERIFY( a0.to_bytes() == address_v4::bytes_type{} ); address_v4::bytes_type b1{ 1, 2, 3, 4 }; address_v4 a1{ b1 }; - VERIFY( a1.to_uint() == ntohl((1 << 24) | (2 << 16) | (3 << 8) | 4) ); + VERIFY( a1.to_uint() == ((1 << 24) | (2 << 16) | (3 << 8) | 4) ); VERIFY( a1.to_bytes() == b1 ); } -void +constexpr void test03() { - bool test __attribute__((unused)) = false; - address_v4 a0{ 0u }; VERIFY( a0.to_uint() == 0 ); VERIFY( a0.to_bytes() == address_v4::bytes_type{} ); - address_v4::uint_type u1 = ntohl((5 << 24) | (6 << 16) | (7 << 8) | 8); + address_v4::uint_type u1 = (5 << 24) | (6 << 16) | (7 << 8) | 8; address_v4 a1{ u1 }; VERIFY( a1.to_uint() == u1 ); VERIFY( a1.to_bytes() == address_v4::bytes_type( 5, 6, 7, 8 ) ); @@ -70,4 +74,11 @@ main() test01(); test02(); test03(); + + constexpr bool c = []{ + test01(); + test02(); + test03(); + return true; + }; } diff --git a/libstdc++-v3/testsuite/experimental/net/internet/address/v4/creation.cc b/libstdc++-v3/testsuite/experimental/net/internet/address/v4/creation.cc index 441c832bf54..84aebbb7adc 100644 --- a/libstdc++-v3/testsuite/experimental/net/internet/address/v4/creation.cc +++ b/libstdc++-v3/testsuite/experimental/net/internet/address/v4/creation.cc @@ -25,7 +25,17 @@ namespace net = std::experimental::net; using net::ip::address_v4; -void +#if __cplusplus < 202002L +// Naughty, but operator== for std::array is not constexpr until C++20. +constexpr bool +operator==(const address_v4::bytes_type& lhs, const address_v4::bytes_type& rhs) +{ + return lhs[0] == rhs[0] && lhs[1] == rhs[1] + && lhs[2] == rhs[2] && lhs[3] == rhs[3]; +} +#endif + +constexpr void test01() { auto a0 = make_address_v4( address_v4::bytes_type{} ); @@ -34,18 +44,18 @@ test01() address_v4::bytes_type b1{ 1, 2, 3, 4 }; auto a1 = make_address_v4( b1 ); - VERIFY( a1.to_uint() == ntohl((1 << 24) | (2 << 16) | (3 << 8) | 4) ); + VERIFY( a1.to_uint() == ((1 << 24) | (2 << 16) | (3 << 8) | 4) ); VERIFY( a1.to_bytes() == b1 ); } -void +constexpr void test02() { auto a0 = net::ip::make_address_v4(0u); VERIFY( a0.to_uint() == 0 ); VERIFY( a0.to_bytes() == address_v4::bytes_type{} ); - address_v4::uint_type u1 = ntohl((5 << 24) | (6 << 16) | (7 << 8) | 8); + address_v4::uint_type u1 = ((5 << 24) | (6 << 16) | (7 << 8) | 8); auto a1 = net::ip::make_address_v4( u1 ); VERIFY( a1.to_uint() == u1 ); VERIFY( a1.to_bytes() == address_v4::bytes_type( 5, 6, 7, 8 ) ); @@ -84,4 +94,10 @@ main() test01(); test02(); test03(); + + constexpr bool c = []{ + test01(); + test02(); + return true; + }; } diff --git a/libstdc++-v3/testsuite/experimental/net/internet/address/v4/members.cc b/libstdc++-v3/testsuite/experimental/net/internet/address/v4/members.cc index c40a8103664..ac59405c599 100644 --- a/libstdc++-v3/testsuite/experimental/net/internet/address/v4/members.cc +++ b/libstdc++-v3/testsuite/experimental/net/internet/address/v4/members.cc @@ -26,6 +26,9 @@ using std::experimental::net::ip::address_v4; +static_assert(std::is_standard_layout::value, + "net::ip::address_v4::bytes_type is a standard layout type"); + constexpr bool test01() { From patchwork Fri Feb 24 14:28:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 61341 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp940963wrd; Fri, 24 Feb 2023 06:31:19 -0800 (PST) X-Google-Smtp-Source: AK7set/9Br+nbiSgHONUhTTC3vfJX4FQBMqgdYamLmPZbjx7kuEWSO4ifP8TL0jFZBwGilv5y424 X-Received: by 2002:a17:906:7c96:b0:886:843b:e695 with SMTP id w22-20020a1709067c9600b00886843be695mr21603854ejo.22.1677249079071; Fri, 24 Feb 2023 06:31:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677249079; cv=none; d=google.com; s=arc-20160816; b=rapIrOfIboj0WRzQMqjWbpJtgdHb2lILoLKSS65ItbxufrLqafN4uEeZtYK7GSjzm1 KUYLTIYYrn6DcCkNq9JrChgHMkmg1G71s6THwzvFEIk//dl3ja55tEYvyThpsd78OByI amP2L9UHurjUT/mj8OoZTR2rgSJL6HKl0xZ3/maHNcHvGqH1lt3R9eh5PsciQBE3pLlI FHDufabCjrVSa/53k8SUVc6F0RqniW0XNj7525bMqjJ627K1XjNJU/uszCJdhdvmIjVR XWBL8cPod60ECf7MDQhtNiLI7Hb7gfANTAMYJGv6X4p8s/9S50oxHsxfWWX70GbtqjFF Cgkw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:dmarc-filter:delivered-to:dkim-signature :dkim-filter; bh=uPy/3MSvzJ/f4N/o59cLS7Tm7nvFcjpWbSDAHXFuvs8=; b=bsPvf1131XDfw2mjPiLHyG9CrvgSldZXwLEof7LECdIKs20zF8kRwSZcSJ3Ht6JM5H eWwQqbM9lGKtCkoYzbbFGYZI9kL+06hf8mP0cb42K6zOVIo8abxwnx7kAWpLU6zkcmXz cMLSTE4RPTisppZXpw/8A3m0egubRefAEl+h8uesIaBTTDHurpSZYOX09fBGCwapLSSz h6bPwUh3+WtKcBV7AjWQT86ESsEa62fQ1q7ozqKlKQKUEb2vsCbE/M35juiX1/SXhRCn L5cC27UfyxkOLxBYx86Y4bhIbBjGSMzZ8QlbDvIum0RSM5pkY+e8+n7UE40B5zzJO1Ya pqeA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=ctC9P2Un; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id fp17-20020a1709069e1100b008c6af8ce806si18730291ejc.197.2023.02.24.06.31.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Feb 2023 06:31:19 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=ctC9P2Un; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BCD60384D143 for ; Fri, 24 Feb 2023 14:29:38 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BCD60384D143 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1677248978; bh=uPy/3MSvzJ/f4N/o59cLS7Tm7nvFcjpWbSDAHXFuvs8=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=ctC9P2Unn+bBlGwKElJnLOWiYoD16WHO8/WiJ0I6xlHSYpPJtf8/BFtk7Ua8+3JbD 1xufJKgqLbObOUzHYDFHtaUVAb2grw4cM17nlNkMOZTdS/QMOx2Vsm74jEdQhGa3xq YeqbKTu0o7tXKJyxKp/6f50zNWZljL17zgaOcZlk= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id CE066385B501 for ; Fri, 24 Feb 2023 14:28:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CE066385B501 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-551-cEVEbGJNPoCB386JbrkjQA-1; Fri, 24 Feb 2023 09:28:11 -0500 X-MC-Unique: cEVEbGJNPoCB386JbrkjQA-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id EFB7C2806049; Fri, 24 Feb 2023 14:28:10 +0000 (UTC) Received: from localhost (unknown [10.33.36.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9A1032166B29; Fri, 24 Feb 2023 14:28:10 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed 3/5] libstdc++: Fix members of net::ip::network_v4 Date: Fri, 24 Feb 2023 14:28:06 +0000 Message-Id: <20230224142808.714075-3-jwakely@redhat.com> In-Reply-To: <20230224142808.714075-1-jwakely@redhat.com> References: <20230224142808.714075-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_NUMSUBJECT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1758723130370139905?= X-GMAIL-MSGID: =?utf-8?q?1758723130370139905?= Tested x86_64-linux. Pushed to trunk. -- >8 -- libstdc++-v3/ChangeLog: * include/experimental/internet (network_v4::netmask()): Avoid undefined shift. (network_v4::broadcast()): Optimize and fix for targets with uint_least32_t wider than 32 bits. (network_v4::to_string(const Allocator&)): Fix for custom allocators and optimize using to_chars. (operator==(const network_v4&, const network_v4&)): Add missing constexpr. (operator==(const network_v6&, const network_v6&)): Likewise. * testsuite/experimental/net/internet/network/v4/cons.cc: New test. * testsuite/experimental/net/internet/network/v4/members.cc: New test. --- libstdc++-v3/include/experimental/internet | 41 ++-- .../net/internet/network/v4/cons.cc | 129 ++++++++++++ .../net/internet/network/v4/members.cc | 186 ++++++++++++++++++ 3 files changed, 343 insertions(+), 13 deletions(-) create mode 100644 libstdc++-v3/testsuite/experimental/net/internet/network/v4/cons.cc create mode 100644 libstdc++-v3/testsuite/experimental/net/internet/network/v4/members.cc diff --git a/libstdc++-v3/include/experimental/internet b/libstdc++-v3/include/experimental/internet index 3fd200251fa..5336b8a8ce3 100644 --- a/libstdc++-v3/include/experimental/internet +++ b/libstdc++-v3/include/experimental/internet @@ -1219,10 +1219,10 @@ namespace ip /// @} - bool + constexpr bool operator==(const network_v4& __a, const network_v4& __b) noexcept; - bool + constexpr bool operator==(const network_v6& __a, const network_v6& __b) noexcept; @@ -1263,10 +1263,10 @@ namespace ip constexpr address_v4 netmask() const noexcept { - address_v4::uint_type __val = address_v4::broadcast().to_uint(); - __val >>= (32 - _M_prefix_len); - __val <<= (32 - _M_prefix_len); - return address_v4{__val}; + address_v4 __m; + if (_M_prefix_len) + __m = address_v4(0xFFFFFFFFu << (32 - _M_prefix_len)); + return __m; } constexpr address_v4 @@ -1275,7 +1275,7 @@ namespace ip constexpr address_v4 broadcast() const noexcept - { return address_v4{_M_addr.to_uint() | ~netmask().to_uint()}; } + { return address_v4{_M_addr.to_uint() | (0xFFFFFFFFu >> _M_prefix_len)}; } address_v4_range hosts() const noexcept @@ -1306,8 +1306,23 @@ namespace ip __string_with<_Allocator> to_string(const _Allocator& __a = _Allocator()) const { - return address().to_string(__a) + '/' - + std::to_string(prefix_length()); + auto __str = address().to_string(__a); + const unsigned __addrlen = __str.length(); + const unsigned __preflen = prefix_length() >= 10 ? 2 : 1; + auto __write = [=](char* __p, size_t __n) { + __p[__addrlen] = '/'; + std::__detail::__to_chars_10_impl(__p + __addrlen + 1, __preflen, + (unsigned char)prefix_length()); + return __n; + }; + const unsigned __len = __addrlen + 1 + __preflen; +#if __cpp_lib_string_resize_and_overwrite + __str.resize_and_overwrite(__len, __write); +#else + __str.resize(__len); + __write(&__str.front(), __len); +#endif + return __str; } private: @@ -1379,14 +1394,14 @@ namespace ip * @{ */ - inline bool + constexpr bool operator==(const network_v4& __a, const network_v4& __b) noexcept { return __a.address() == __b.address() && __a.prefix_length() == __b.prefix_length(); } - inline bool + constexpr bool operator!=(const network_v4& __a, const network_v4& __b) noexcept { return !(__a == __b); } @@ -1396,14 +1411,14 @@ namespace ip * @{ */ - inline bool + constexpr bool operator==(const network_v6& __a, const network_v6& __b) noexcept { return __a.address() == __b.address() && __a.prefix_length() == __b.prefix_length(); } - inline bool + constexpr bool operator!=(const network_v6& __a, const network_v6& __b) noexcept { return !(__a == __b); } diff --git a/libstdc++-v3/testsuite/experimental/net/internet/network/v4/cons.cc b/libstdc++-v3/testsuite/experimental/net/internet/network/v4/cons.cc new file mode 100644 index 00000000000..7784b6f6f58 --- /dev/null +++ b/libstdc++-v3/testsuite/experimental/net/internet/network/v4/cons.cc @@ -0,0 +1,129 @@ +// { dg-do run { target c++14 } } +// { dg-require-effective-target net_ts_ip } +// { dg-add-options net_ts } + +#include +#include +#include + +using std::experimental::net::ip::network_v4; +using std::experimental::net::ip::address_v4; + +constexpr void +test01() +{ + network_v4 n0; + VERIFY( n0.address().is_unspecified() ); + VERIFY( n0.prefix_length() == 0 ); +} + +constexpr void +test02() +{ + address_v4 a0; + network_v4 n0{ a0, 0 }; + VERIFY( n0.address() == a0 ); + VERIFY( n0.prefix_length() == 0 ); + + address_v4 a1{ address_v4::bytes_type{ 1, 2, 3, 4 } }; + network_v4 n1{ a1, 12}; + VERIFY( n1.address() == a1 ); + VERIFY( n1.prefix_length() == 12 ); +} + +void +test02_errors() +{ + address_v4 a0; + try + { + network_v4{a0, -1}; + VERIFY(false); + } + catch(const std::out_of_range&) + { + } + + try + { + network_v4{a0, 33}; + VERIFY(false); + } + catch(const std::out_of_range&) + { + } +} + +constexpr void +test03() +{ + address_v4 a0; + network_v4 n0{ a0, a0 }; + VERIFY( n0.address() == a0 ); + VERIFY( n0.prefix_length() == 0 ); + + address_v4 a1{ address_v4::bytes_type{ 1, 2, 3, 4 } }; + network_v4 n1{ a1, address_v4::broadcast() }; + VERIFY( n1.address() == a1 ); + VERIFY( n1.prefix_length() == 32 ); + + network_v4 n2{ a1, address_v4::bytes_type(128, 0, 0, 0) }; + VERIFY( n2.address() == a1 ); + VERIFY( n2.prefix_length() == 1 ); + + network_v4 n3{ a1, address_v4::bytes_type(255, 255, 255, 192) }; + VERIFY( n3.address() == a1 ); + VERIFY( n3.prefix_length() == 26 ); +} + +void +test03_errors() +{ + address_v4 a0; + try + { + // Contains non-contiguous non-zero bits. + network_v4{a0, address_v4::bytes_type(255, 1, 0, 0)}; + VERIFY(false); + } + catch(const std::invalid_argument&) + { + } + + try + { + // Most significant bit is zero and any other bits are non-zero. + network_v4{a0, address_v4::bytes_type(1, 0, 0, 0)}; + VERIFY(false); + } + catch(const std::invalid_argument&) + { + } + + try + { + // Most significant bit is zero and any other bits are non-zero. + network_v4{a0, address_v4::bytes_type(0, 1, 0, 0)}; + VERIFY(false); + } + catch(const std::invalid_argument&) + { + } +} + +int +main() +{ + test01(); + test02(); + test02_errors(); + test03(); + test03_errors(); + + constexpr bool c = []{ + test01(); + test02(); + test03(); + return true; + }; +} diff --git a/libstdc++-v3/testsuite/experimental/net/internet/network/v4/members.cc b/libstdc++-v3/testsuite/experimental/net/internet/network/v4/members.cc new file mode 100644 index 00000000000..3ea65862649 --- /dev/null +++ b/libstdc++-v3/testsuite/experimental/net/internet/network/v4/members.cc @@ -0,0 +1,186 @@ +// { dg-do run { target c++14 } } +// { dg-require-effective-target net_ts_ip } +// { dg-add-options net_ts } + +#include +#include +#include + +using std::experimental::net::ip::network_v4; +using std::experimental::net::ip::address_v4; + +constexpr void +test_netmask() +{ + network_v4 n0; + VERIFY( n0.netmask() == address_v4() ); + + network_v4 n1({}, 1); + VERIFY( n1.netmask() == address_v4(address_v4::bytes_type(128)) ); + + network_v4 n2({}, 2); + VERIFY( n2.netmask() == address_v4(address_v4::bytes_type(192)) ); + + network_v4 n3({}, 3); + VERIFY( n3.netmask() == address_v4(address_v4::bytes_type(224)) ); + + network_v4 n4({}, 17); + VERIFY( n4.netmask() == address_v4(address_v4::bytes_type(255, 255, 128)) ); +} + +constexpr void +test_network() +{ + network_v4 n0; + VERIFY( n0.network() == address_v4() ); + + network_v4 n1(address_v4::bytes_type{1, 2, 3, 4}, 1); + VERIFY( n1.network() == address_v4(address_v4::bytes_type(0, 0, 0, 0)) ); + + network_v4 n2(address_v4::bytes_type{1, 2, 3, 4}, 8); + VERIFY( n2.network() == address_v4(address_v4::bytes_type(1, 0, 0, 0)) ); + + network_v4 n3(address_v4::bytes_type{1, 2, 3, 4}, 15); + VERIFY( n3.network() == address_v4(address_v4::bytes_type(1, 2, 0, 0)) ); + + network_v4 n4(address_v4::bytes_type{1, 2, 3, 4}, 16); + VERIFY( n4.network() == address_v4(address_v4::bytes_type(1, 2, 0, 0)) ); + + network_v4 n5(address_v4::bytes_type{1, 2, 3, 4}, 23); + VERIFY( n5.network() == address_v4(address_v4::bytes_type(1, 2, 2, 0)) ); + + network_v4 n6(address_v4::bytes_type{1, 2, 3, 4}, 24); + VERIFY( n6.network() == address_v4(address_v4::bytes_type(1, 2, 3, 0)) ); + + network_v4 n7(address_v4::bytes_type{1, 2, 3, 4}, 29); + VERIFY( n7.network() == address_v4(address_v4::bytes_type(1, 2, 3, 0)) ); + + network_v4 n8(address_v4::bytes_type{1, 2, 3, 4}, 30); + VERIFY( n8.network() == address_v4(address_v4::bytes_type(1, 2, 3, 4)) ); + + network_v4 n9(address_v4::bytes_type{1, 2, 3, 4}, 32); + VERIFY( n9.network() == address_v4(address_v4::bytes_type(1, 2, 3, 4)) ); +} + +constexpr void +test_broadcast() +{ + using b = address_v4::bytes_type; + + network_v4 n0; + VERIFY( n0.broadcast() == address_v4::broadcast() ); + + network_v4 n1(b{1, 2, 3, 4}, 1); + VERIFY( n1.broadcast() == address_v4(b(127, 255, 255, 255)) ); + + network_v4 n2(b{1, 2, 3, 4}, 8); + VERIFY( n2.broadcast() == address_v4(b(1, 255, 255, 255)) ); + + network_v4 n3(b{1, 2, 3, 4}, 15); + VERIFY( n3.broadcast() == address_v4(b(1, 3, 255, 255)) ); + + network_v4 n4(b{1, 2, 3, 4}, 16); + VERIFY( n4.broadcast() == address_v4(b(1, 2, 255, 255)) ); + + network_v4 n5(b{1, 2, 3, 4}, 23); + VERIFY( n5.broadcast() == address_v4(b(1, 2, 3, 255)) ); + + network_v4 n6(b{1, 2, 3, 4}, 24); + VERIFY( n6.broadcast() == address_v4(b(1, 2, 3, 255)) ); + + network_v4 n7(b{1, 2, 3, 4}, 29); + VERIFY( n7.broadcast() == address_v4(b(1, 2, 3, 7)) ); + + network_v4 n8(b{1, 2, 3, 4}, 30); + VERIFY( n8.broadcast() == address_v4(b(1, 2, 3, 7)) ); + + network_v4 n9(b{1, 2, 3, 4}, 31); + VERIFY( n9.broadcast() == address_v4(b(1, 2, 3, 5)) ); + + network_v4 n10(b{1, 2, 3, 4}, 32); + VERIFY( n10.broadcast() == address_v4(b(1, 2, 3, 4)) ); +} + +constexpr void +test_canonical() +{ + network_v4 n0; + VERIFY( n0.canonical() == network_v4(n0.network(), n0.prefix_length()) ); + + network_v4 n1(address_v4::bytes_type{1, 2, 3, 4}, 1); + VERIFY( n1.canonical() == network_v4(n1.network(), n1.prefix_length()) ); + + network_v4 n2(address_v4::bytes_type{1, 2, 3, 4}, 8); + VERIFY( n2.canonical() == network_v4(n2.network(), n2.prefix_length()) ); + + network_v4 n3(address_v4::bytes_type{1, 2, 3, 4}, 15); + VERIFY( n3.canonical() == network_v4(n3.network(), n3.prefix_length()) ); + + network_v4 n4(address_v4::bytes_type{1, 2, 3, 4}, 16); + VERIFY( n4.canonical() == network_v4(n4.network(), n4.prefix_length()) ); + + network_v4 n5(address_v4::bytes_type{1, 2, 3, 4}, 23); + VERIFY( n5.canonical() == network_v4(n5.network(), n5.prefix_length()) ); + + network_v4 n6(address_v4::bytes_type{1, 2, 3, 4}, 24); + VERIFY( n6.canonical() == network_v4(n6.network(), n6.prefix_length()) ); + + network_v4 n7(address_v4::bytes_type{1, 2, 3, 4}, 29); + VERIFY( n7.canonical() == network_v4(n7.network(), n7.prefix_length()) ); + + network_v4 n8(address_v4::bytes_type{1, 2, 3, 4}, 30); + VERIFY( n8.canonical() == network_v4(n8.network(), n8.prefix_length()) ); + + network_v4 n9(address_v4::bytes_type{1, 2, 3, 4}, 32); + VERIFY( n9.canonical() == network_v4(n9.network(), n9.prefix_length()) ); +} + +constexpr void +test_is_host() +{ + network_v4 n0; + VERIFY( ! n0.is_host() ); + + network_v4 n1(address_v4::bytes_type{1, 2, 3, 4}, 1); + VERIFY( ! n1.is_host() ); + + network_v4 n2(address_v4::bytes_type{1, 2, 3, 4}, 8); + VERIFY( ! n2.is_host() ); + + network_v4 n3(address_v4::bytes_type{1, 2, 3, 4}, 32); + VERIFY( n3.is_host() ); +} + +void +test_to_string() +{ + using b = address_v4::bytes_type; + __gnu_test::uneq_allocator alloc(123); + auto str = network_v4(address_v4(b(12, 34, 56, 78)), 24).to_string(alloc); + VERIFY(str.get_allocator().get_personality() == alloc.get_personality()); + VERIFY( str == "12.34.56.78/24" ); + + __gnu_test::uneq_allocator alloc2(99); + auto str2 = network_v4(address_v4(b(87, 65, 43, 21)), 4).to_string(alloc2); + VERIFY(str2.get_allocator().get_personality() == alloc2.get_personality()); + VERIFY( str2 == "87.65.43.21/4" ); +} + +int main() +{ + test_netmask(); + test_network(); + test_broadcast(); + test_canonical(); + test_is_host(); + test_to_string(); + + constexpr bool c = []{ + test_netmask(); + test_network(); + test_broadcast(); + test_canonical(); + test_is_host(); + return true; + }; +} From patchwork Fri Feb 24 14:28:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 61340 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp940849wrd; Fri, 24 Feb 2023 06:31:10 -0800 (PST) X-Google-Smtp-Source: AK7set9WYDYm3VFhji5zHUbKN9dVSV14s7jIEve2PoeEpaJCStZXYP6R0ga4xz74j5GNAeLKakiO X-Received: by 2002:a17:906:f0d1:b0:888:5d34:dc79 with SMTP id dk17-20020a170906f0d100b008885d34dc79mr24437099ejb.40.1677249069919; Fri, 24 Feb 2023 06:31:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677249069; cv=none; d=google.com; s=arc-20160816; b=uG0QUwIKRHu0XHq90V0+2fh2YpPO0fJf4tIZQT1v6ZpjG9nvZI7aGRRiMc8sL9fulS xrkyGd/8HJ6aWwCE+qwA0GYqqVfAHkS764h/c+53dBZkiQNMu+XhnghK+SnfBcwTq4la TO7M1drUF4mtF6CQa/quri7+c05d5/wCdLu61qosoVVYzB5GPz2sQAnedtjIC3wvZc6L /oQoDMo5GCmWxYoYzxZIv7/somCIiUAiYVKsGIJ5SM/5vqcQAnTijzeEIsXLEhgph2f9 aKSE9Qgk7tgV1vU6Ouli9k9eOBcuVK4msIIF7cuKaGBzQljA11PFQt+i3gdhXlIGVrCv WlPg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:dmarc-filter:delivered-to:dkim-signature :dkim-filter; bh=dk0tv7r9k/K3CeXmEk6Ae5cAIfomNZnNjUGCY5on+I4=; b=V3aiqLjNpWMbi5R+gW3xXkcBbqmCf4VkWAnDqp5tWiDFC8Y1KMjoWSr/X2zWgycxow qVXLBFs6pEwGy+MmpOuVAV8l8T3QfGenGXGJTvuPx+o/p0e8PgpbaH1JoBH6c9LegO/J CRxxQpwkFdcumOsZsKef86kARmXWhWzVuvhuDuijZKUdb7FEdDqiSFgb73HMD1zf1l1g 3Ivr1KT7Y8RKSGoEl1VgbWWBg6McK+HndueSFMtb6gI+Gl8pIfsART8D9xEQ9JaJpmeq 77GSNWzLXGQzJnAgNyZad3Z9acTTA5ELDrEsBoSBGlOYBg5m7xVMmfoCbKvS3V4fPIRv OsTQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=f2CUZt3X; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id wd3-20020a170907d60300b008eeba611028si6638297ejc.478.2023.02.24.06.31.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Feb 2023 06:31:09 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=f2CUZt3X; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 77B84384F02F for ; Fri, 24 Feb 2023 14:29:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 77B84384F02F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1677248973; bh=dk0tv7r9k/K3CeXmEk6Ae5cAIfomNZnNjUGCY5on+I4=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=f2CUZt3Xhwa4KqTDbqsxUYyA/4mIrC+M+MS654tqueWNU0RcGIyvPcKmF40rUEOiq /FLZSDaE+GktLszbg8v/5i9e/Y1oO8neAaVYMyNDrPbfs4LPOmGmkcr2ZqlsAhRoir AuL6DW5Ypi6GK105iz40kLBvLFMQRpg8vcXNdqYM= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id CD9B1385480C for ; Fri, 24 Feb 2023 14:28:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CD9B1385480C Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-473-MtZAc2HsNNOd6t2Y_o04Og-1; Fri, 24 Feb 2023 09:28:12 -0500 X-MC-Unique: MtZAc2HsNNOd6t2Y_o04Og-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C8A62101A521; Fri, 24 Feb 2023 14:28:11 +0000 (UTC) Received: from localhost (unknown [10.33.36.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9272B404BEC0; Fri, 24 Feb 2023 14:28:11 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed 4/5] libstdc++: Make net::ip::basic_endpoint comparisons constexpr Date: Fri, 24 Feb 2023 14:28:07 +0000 Message-Id: <20230224142808.714075-4-jwakely@redhat.com> In-Reply-To: <20230224142808.714075-1-jwakely@redhat.com> References: <20230224142808.714075-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1758723120861268888?= X-GMAIL-MSGID: =?utf-8?q?1758723120861268888?= Tested x86_64-linux. Pushed to trunk. -- >8 -- libstdc++-v3/ChangeLog: * include/experimental/internet (basic_endpoint): Add missing constexpr to comparison operators. * testsuite/experimental/net/internet/endpoint/cons.cc: New test. --- libstdc++-v3/include/experimental/internet | 12 ++-- .../net/internet/endpoint/cons.cc | 66 +++++++++++++++++++ 2 files changed, 72 insertions(+), 6 deletions(-) create mode 100644 libstdc++-v3/testsuite/experimental/net/internet/endpoint/cons.cc diff --git a/libstdc++-v3/include/experimental/internet b/libstdc++-v3/include/experimental/internet index 5336b8a8ce3..cae07f466da 100644 --- a/libstdc++-v3/include/experimental/internet +++ b/libstdc++-v3/include/experimental/internet @@ -1626,19 +1626,19 @@ namespace ip */ template - inline bool + constexpr bool operator==(const basic_endpoint<_InternetProtocol>& __a, const basic_endpoint<_InternetProtocol>& __b) { return __a.address() == __b.address() && __a.port() == __b.port(); } template - inline bool + constexpr bool operator!=(const basic_endpoint<_InternetProtocol>& __a, const basic_endpoint<_InternetProtocol>& __b) { return !(__a == __b); } template - inline bool + constexpr bool operator< (const basic_endpoint<_InternetProtocol>& __a, const basic_endpoint<_InternetProtocol>& __b) { @@ -1647,19 +1647,19 @@ namespace ip } template - inline bool + constexpr bool operator> (const basic_endpoint<_InternetProtocol>& __a, const basic_endpoint<_InternetProtocol>& __b) { return __b < __a; } template - inline bool + constexpr bool operator<=(const basic_endpoint<_InternetProtocol>& __a, const basic_endpoint<_InternetProtocol>& __b) { return !(__b < __a); } template - inline bool + constexpr bool operator>=(const basic_endpoint<_InternetProtocol>& __a, const basic_endpoint<_InternetProtocol>& __b) { return !(__a < __b); } diff --git a/libstdc++-v3/testsuite/experimental/net/internet/endpoint/cons.cc b/libstdc++-v3/testsuite/experimental/net/internet/endpoint/cons.cc new file mode 100644 index 00000000000..1b5c92c0b58 --- /dev/null +++ b/libstdc++-v3/testsuite/experimental/net/internet/endpoint/cons.cc @@ -0,0 +1,66 @@ +// { dg-do run { target c++14 } } +// { dg-require-effective-target net_ts_ip } +// { dg-add-options net_ts } + +#include +#include + +using namespace std::experimental::net; + +constexpr void +test_default() +{ + ip::tcp::endpoint t1; + VERIFY( t1.protocol() == ip::tcp::v4() ); + VERIFY( t1.address() == ip::address() ); + VERIFY( t1.port() == 0 ); + + ip::udp::endpoint t2; + VERIFY( t2.protocol() == ip::udp::v4() ); + VERIFY( t2.address() == ip::address() ); + VERIFY( t2.port() == 0 ); +} + +constexpr void +test_proto() +{ + ip::tcp::endpoint t1(ip::tcp::v4(), 22); + VERIFY( t1.protocol() == ip::tcp::v4() ); + VERIFY( t1.address() == ip::address_v4() ); + VERIFY( t1.port() == 22 ); + + ip::tcp::endpoint t2(ip::tcp::v6(), 80); + VERIFY( t2.protocol() == ip::tcp::v6() ); + VERIFY( t2.address() == ip::address_v6() ); + VERIFY( t2.port() == 80 ); +} + +constexpr void +test_addr() +{ + ip::address_v4 a1(ip::address_v4::bytes_type(1, 2, 3, 4)); + ip::tcp::endpoint t1(a1, 22); + VERIFY( t1.protocol() == ip::tcp::v4() ); + VERIFY( t1.address() == a1 ); + VERIFY( t1.port() == 22 ); + + ip::address_v6 a2(ip::address_v6::bytes_type(21,22,23,24,25,26,27,28,29)); + ip::tcp::endpoint t2(a2, 80); + VERIFY( t2.protocol() == ip::tcp::v6() ); + VERIFY( t2.address() == a2 ); + VERIFY( t2.port() == 80 ); +} + +int main() +{ + test_default(); + test_proto(); + test_addr(); + + constexpr bool c = [] { + test_default(); + test_proto(); + test_addr(); + return true; + }; +} From patchwork Fri Feb 24 14:28:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 61339 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp940146wrd; Fri, 24 Feb 2023 06:30:05 -0800 (PST) X-Google-Smtp-Source: AK7set/5WiUEAzjo75InqLIZpHMPlNybRI7r9RsTu1zVojwMMefe1V2+GVdA2sZMlFJGi3rBfBAY X-Received: by 2002:a05:6402:291:b0:4ad:7055:3908 with SMTP id l17-20020a056402029100b004ad70553908mr17110928edv.21.1677249004938; Fri, 24 Feb 2023 06:30:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677249004; cv=none; d=google.com; s=arc-20160816; b=rOa/dEr7z8VswR/+VrBlfPGhmoP/7usI7feYcJrChpoayd58JzHTZ3EqWg66IX/un+ lvDSndBVzs/xQCIXqmO9gNY3IkjaIpM2rhR2nUPlUPVLg6rju8OajP+FVGK7SjvmXWRq blxxcUGAW3cvkQc51K4czLOKdWL9OZ7lQ9y/QudC3WWUqYtWDR10s2bQv1cLaqB46d3w lGI5NHtv305N/zUHWazBrtKJuzGuhIhSGZM/gXea33scItDs/Rw7ay1oXYWbfdMA/ICo hBLCYYlx299CjzbDwFNQrz8s6MJ/yXQYttHWmZ84DTQPjo6wrlKvMjCWeQuniWsQG9Nu hFPQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:dmarc-filter:delivered-to:dkim-signature :dkim-filter; bh=xyJ1vAcfbpKyQY6fip3x6kKxhQYQPWMeaJkNwjhhztY=; b=owsDYKnsGw9Iy8X9bgVtrUyKDzxfagT4mX0GFn2ksVuy9G8JqUOXUw2u8LHtD3s2zy PeGnt+RLnc4isncGGPJx53ubWPtFZ3w7lp8ZTuKZWYLig3rgZt6uV6bTgmKWqfh2mqTp mAGMrGnhnSYjO2b2qoL+dFA38FimXe9K8RNf0X4lhuIexv3mV+hrw6UMvNBj5N+MVngS tIUTkDKNdErNE74NYBd5G6O02b9RUH+ixy7uOSwIOFoVD6GSlaK2HqDOhOHte9bFr28O 7Y6rcD91yuGqsqaEqoa737A1HrBPz/X4uixsCr3IPHVWPRu/tfz4LoPrzbHO9cyLVonl fGzA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=b1LtHWVK; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id 19-20020a170906005300b008dbc41587a8si13863942ejg.730.2023.02.24.06.30.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Feb 2023 06:30:04 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=b1LtHWVK; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6C813384F4B7 for ; Fri, 24 Feb 2023 14:29:03 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6C813384F4B7 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1677248943; bh=xyJ1vAcfbpKyQY6fip3x6kKxhQYQPWMeaJkNwjhhztY=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=b1LtHWVKyAw24BCdVjgzJVEol6X/kRYmpvITz9vjr/I8L07Sv983rIMaWs+Bv6wgX zPyaJI4ElozbUzo6/o94Z7eiPDQg1pL7FiW0XZ+N5qziQ8zvttcneuhohcxvXSFw1P CeG6qbB3E0a/tFLyuxlV90Rbg48/uLb8O/mPAA18= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 30B4C3857365 for ; Fri, 24 Feb 2023 14:28:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 30B4C3857365 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-577-jppRG5WkPYi64R702Ln7TA-1; Fri, 24 Feb 2023 09:28:13 -0500 X-MC-Unique: jppRG5WkPYi64R702Ln7TA-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A2BB618E0043; Fri, 24 Feb 2023 14:28:12 +0000 (UTC) Received: from localhost (unknown [10.33.36.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6A5B22166B29; Fri, 24 Feb 2023 14:28:12 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed 5/5] libstdc++: Constrain net::executor constructors Date: Fri, 24 Feb 2023 14:28:08 +0000 Message-Id: <20230224142808.714075-5-jwakely@redhat.com> In-Reply-To: <20230224142808.714075-1-jwakely@redhat.com> References: <20230224142808.714075-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1758723052542487610?= X-GMAIL-MSGID: =?utf-8?q?1758723052542487610?= Tested x86_64-linux. Pushed to trunk. -- >8 -- The TS says the arguments to these constructors shall meet the Executor requirements, so it's undefined if they don't. Constraining on a subset of those requirements won't affect valid cases, but prevents the majority of invalid cases from trying to instantiate the constructor. This prevents the non-explicit executor(Executor) constructor being a candidate anywhere that a net::executor could be constructed e.g. comparing ip::tcp::v4() == ip::udp::v4() would try to convert both operands to executor using that constructor, then compare then using operator==(const executor&, const executor&). libstdc++-v3/ChangeLog: * include/experimental/executor (executor): Constrain template constructors. --- libstdc++-v3/include/experimental/executor | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/libstdc++-v3/include/experimental/executor b/libstdc++-v3/include/experimental/executor index cd75d99ddb3..1dae8925916 100644 --- a/libstdc++-v3/include/experimental/executor +++ b/libstdc++-v3/include/experimental/executor @@ -1012,6 +1012,9 @@ inline namespace v1 class executor { + template + using _Context_t = decltype(std::declval<_Executor&>().context()); + public: // construct / copy / destroy: @@ -1021,12 +1024,14 @@ inline namespace v1 executor(const executor&) noexcept = default; executor(executor&&) noexcept = default; - template + template>>> executor(_Executor __e) : _M_target(make_shared<_Tgt1<_Executor>>(std::move(__e))) { } - template + template>>> executor(allocator_arg_t, const _ProtoAlloc& __a, _Executor __e) : _M_target(allocate_shared<_Tgt2<_Executor, _ProtoAlloc>>(__a, std::move(__e), __a))