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 [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 513FE3858002 for ; Tue, 8 Feb 2022 21:04:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 513FE3858002 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-470-CYvlojuUOQWFB3GxVGqkfA-1; Tue, 08 Feb 2022 16:04:49 -0500 X-MC-Unique: CYvlojuUOQWFB3GxVGqkfA-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D5D7519251A4; Tue, 8 Feb 2022 21:04:48 +0000 (UTC) Received: from localhost (unknown [10.33.37.88]) by smtp.corp.redhat.com (Postfix) with ESMTP id 868751017CE5; Tue, 8 Feb 2022 21:04:48 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Simplify resource management in directory iterators Date: Tue, 8 Feb 2022 21:04:47 +0000 Message-Id: <20220208210447.3104779-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII" X-Spam-Status: No, score=-13.5 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_LOW, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: Tue, 08 Feb 2022 21:04:53 -0000 Tested powerpc64le-linux, pushed to trunk. This replaces the _Dir constructor that takes ownership of an existing DIR* resource with one that takes a _Dir_base rvalue instead. This means a raw DIR* is never passed around, but is always owned by a _Dir_base object. libstdc++-v3/ChangeLog: * src/c++17/fs_dir.cc (_Dir(DIR*, const path&)): Change first parameter to _Dir_base&&. * src/filesystem/dir-common.h (_Dir_base(DIR*)): Remove. * src/filesystem/dir.cc (_Dir(DIR*, const path&)): Change first parameter to _Dir_base&&. --- libstdc++-v3/src/c++17/fs_dir.cc | 4 ++-- libstdc++-v3/src/filesystem/dir-common.h | 2 -- libstdc++-v3/src/filesystem/dir.cc | 4 ++-- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/libstdc++-v3/src/c++17/fs_dir.cc b/libstdc++-v3/src/c++17/fs_dir.cc index 54f135d2baf..c67fe76bc14 100644 --- a/libstdc++-v3/src/c++17/fs_dir.cc +++ b/libstdc++-v3/src/c++17/fs_dir.cc @@ -57,7 +57,7 @@ struct fs::_Dir : _Dir_base path = p; } - _Dir(posix::DIR* dirp, const path& p) : _Dir_base(dirp), path(p) { } + _Dir(_Dir_base&& d, const path& p) : _Dir_base(std::move(d)), path(p) { } _Dir(_Dir&&) = default; @@ -140,7 +140,7 @@ struct fs::_Dir : _Dir_base _Dir_base d(dirfd, pathname, skip_permission_denied, nofollow, ec); // If this->path is empty, the new _Dir should have an empty path too. const fs::path& p = this->path.empty() ? this->path : this->entry.path(); - return _Dir(std::exchange(d.dirp, nullptr), p); + return _Dir(std::move(d), p); } bool diff --git a/libstdc++-v3/src/filesystem/dir-common.h b/libstdc++-v3/src/filesystem/dir-common.h index 0b7665a3f70..511b988f1c7 100644 --- a/libstdc++-v3/src/filesystem/dir-common.h +++ b/libstdc++-v3/src/filesystem/dir-common.h @@ -89,8 +89,6 @@ is_permission_denied_error(int e) struct _Dir_base { - _Dir_base(posix::DIR* dirp = nullptr) : dirp(dirp) { } - // If no error occurs then dirp is non-null, // otherwise null (even if a permission denied error is ignored). _Dir_base(int fd, const posix::char_type* pathname, diff --git a/libstdc++-v3/src/filesystem/dir.cc b/libstdc++-v3/src/filesystem/dir.cc index e838b4bc6bf..b451902c4a1 100644 --- a/libstdc++-v3/src/filesystem/dir.cc +++ b/libstdc++-v3/src/filesystem/dir.cc @@ -59,7 +59,7 @@ struct fs::_Dir : std::filesystem::_Dir_base path = p; } - _Dir(posix::DIR* dirp, const path& p) : _Dir_base(dirp), path(p) { } + _Dir(_Dir_base&& d, const path& p) : _Dir_base(std::move(d)), path(p) { } _Dir(_Dir&&) = default; @@ -133,7 +133,7 @@ struct fs::_Dir : std::filesystem::_Dir_base { auto [dirfd, pathname] = dir_and_pathname(); _Dir_base d(dirfd, pathname, skip_permission_denied, nofollow, ec); - return _Dir(std::exchange(d.dirp, nullptr), entry.path()); + return _Dir(std::move(d), entry.path()); } fs::path path; -- 2.34.1