From patchwork Wed Jul 5 06:40: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: 115949 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9f45:0:b0:3ea:f831:8777 with SMTP id v5csp1671130vqx; Tue, 4 Jul 2023 23:41:59 -0700 (PDT) X-Google-Smtp-Source: APBJJlEd2amggL/0S1vPU5Qk4XMOJMpk1FF9hG2bhUDdGR/+pXlQ7DUQ28TTMBjw+HVZ6yH36bGm X-Received: by 2002:a50:ee05:0:b0:51d:d4dd:a0e1 with SMTP id g5-20020a50ee05000000b0051dd4dda0e1mr8049222eds.15.1688539319390; Tue, 04 Jul 2023 23:41:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688539319; cv=none; d=google.com; s=arc-20160816; b=vWkhjtJQAGQ0/hZGu6pdtgOP4msIj8G+XQ2JYXDhckHpUNdN8vpDlrM/Waf3ep4F5I O60POPrVTi/5mtbidGXIY8qBRh1SQFIJMQlMXOKMYwq8Q96KMUSmXFIqlm8STQb09P7k j29iAjxbquzqIURV7GXbJDTcw+emZONnxBvtqCeR/shdE63Z2lmHiCIdqYdvfCMppV1+ uul1UTh3y8tBTev11UNIJE1GPEQ/qU0TWUgCozVFKEODkn8kFu1mLWNn+kou+sh3/gES r4dLUHsk8Xcb0+KfbJx1HErGRCC05bcprO/hIp2xpJ6TMNQBEvoL+T6c6GS26dD6mhg+ /VxA== 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=nHgyTf3RR63FRhfxmcrAv3sre8HskKM1FauNzAczD0U=; fh=sJ+2/4g29YdyXkoRrFZSpsL2zxijepB7X/1rB0LDDh8=; b=rQuWxzJC0+i9m4RyEQYPJS7DDUgClvtjH/X+PIMJXwyz9wHw2dzGBKpaqefDyLTLZF qMGzodH2vxjCdZuH3LarzeB+BuxJTJqJhnA1fiAmeU8M75fQeOIloaAXKgJpOsKjyI1+ WmMkOOyp/1hQF2n5WDBBhCChek+nB5LUGOqPgfSRorOsNPB1rszqpjPsLZK+8oP8rXN+ TtRdZxqqO2YJL3LOIrB4nhrYj36Lp9RKVAalijwoS+UEh2onsVjnIi0Z/22Jhki9FPXK lq6wfcQiXVRmlapYgUUUwYxBnkLFAVXn+wC3Sai/xveDynwIhWwZB96iRpm4QIBDJQj9 xq5g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=hp22lbVN; 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 (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id h7-20020a50ed87000000b0051e26de2e9dsi593922edr.93.2023.07.04.23.41.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Jul 2023 23:41:59 -0700 (PDT) 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=hp22lbVN; 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 50D893865C20 for ; Wed, 5 Jul 2023 06:41:05 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 50D893865C20 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1688539265; bh=nHgyTf3RR63FRhfxmcrAv3sre8HskKM1FauNzAczD0U=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=hp22lbVNv3PJA2bLQVc+MyIIT6yMDT07ULK2M0FCdLm0lN+0qTuouNIOT+Jx68IJX ms9AYwi8Vegm/m/+j6FYsU2Qpnk4qwA/lG+GlgGhVBjZU0H5PjLuyv77+kuhDwnDTV wiQftKWGTeFrH2KQcPcoFhz2q6yK6yiBhsw//dJ8= 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 524E03856DDC for ; Wed, 5 Jul 2023 06:40:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 524E03856DDC 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-479-qrYcY0aDNWKpg-Y0oxxeYg-1; Wed, 05 Jul 2023 02:40:11 -0400 X-MC-Unique: qrYcY0aDNWKpg-Y0oxxeYg-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 5A8A93C01B8D; Wed, 5 Jul 2023 06:40:11 +0000 (UTC) Received: from localhost (unknown [10.42.28.54]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2708E40C2063; Wed, 5 Jul 2023 06:40:11 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Use RAII in std::vector::_M_default_append Date: Wed, 5 Jul 2023 07:40:05 +0100 Message-ID: <20230705064010.1143604-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=-12.1 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_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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?1770561805511439430?= X-GMAIL-MSGID: =?utf-8?q?1770561805511439430?= Tested x86_64-linux. Pushed to trunk. -- >8 -- Similar to r14-2052-gdd2eb972a5b063, replace the try-block with RAII types for deallocating storage and destroying elements. libstdc++-v3/ChangeLog: * include/bits/vector.tcc (_M_default_append): Replace try-block with RAII types. --- libstdc++-v3/include/bits/vector.tcc | 91 ++++++++++++++++++---------- 1 file changed, 59 insertions(+), 32 deletions(-) diff --git a/libstdc++-v3/include/bits/vector.tcc b/libstdc++-v3/include/bits/vector.tcc index cda52fbbc4a..f592c72dec2 100644 --- a/libstdc++-v3/include/bits/vector.tcc +++ b/libstdc++-v3/include/bits/vector.tcc @@ -704,47 +704,74 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER const size_type __len = _M_check_len(__n, "vector::_M_default_append"); pointer __new_start(this->_M_allocate(__len)); + + // RAII guard for allocated storage. + struct _Guard + { + pointer _M_storage; // Storage to deallocate + size_type _M_len; + _Tp_alloc_type& _M_alloc; + + _GLIBCXX20_CONSTEXPR + _Guard(pointer __s, size_type __l, _Tp_alloc_type& __a) + : _M_storage(__s), _M_len(__l), _M_alloc(__a) + { } + + _GLIBCXX20_CONSTEXPR + ~_Guard() + { + if (_M_storage) + __gnu_cxx::__alloc_traits<_Tp_alloc_type>:: + deallocate(_M_alloc, _M_storage, _M_len); + } + + private: + _Guard(const _Guard&); + }; + _Guard __guard(__new_start, __len, _M_impl); + + std::__uninitialized_default_n_a(__new_start + __size, __n, + _M_get_Tp_allocator()); + if _GLIBCXX17_CONSTEXPR (_S_use_relocate()) { - __try - { - std::__uninitialized_default_n_a(__new_start + __size, - __n, _M_get_Tp_allocator()); - } - __catch(...) - { - _M_deallocate(__new_start, __len); - __throw_exception_again; - } _S_relocate(__old_start, __old_finish, __new_start, _M_get_Tp_allocator()); } else { - pointer __destroy_from = pointer(); - __try - { - std::__uninitialized_default_n_a(__new_start + __size, - __n, _M_get_Tp_allocator()); - __destroy_from = __new_start + __size; - std::__uninitialized_move_if_noexcept_a( - __old_start, __old_finish, - __new_start, _M_get_Tp_allocator()); - } - __catch(...) - { - if (__destroy_from) - std::_Destroy(__destroy_from, __destroy_from + __n, - _M_get_Tp_allocator()); - _M_deallocate(__new_start, __len); - __throw_exception_again; - } - std::_Destroy(__old_start, __old_finish, - _M_get_Tp_allocator()); + // RAII type to destroy initialized elements. + struct _Guard_elts + { + pointer _M_first, _M_last; // Elements to destroy + _Tp_alloc_type& _M_alloc; + + _GLIBCXX20_CONSTEXPR + _Guard_elts(pointer __first, size_type __n, + _Tp_alloc_type& __a) + : _M_first(__first), _M_last(__first + __n), _M_alloc(__a) + { } + + _GLIBCXX20_CONSTEXPR + ~_Guard_elts() + { std::_Destroy(_M_first, _M_last, _M_alloc); } + + private: + _Guard_elts(const _Guard_elts&); + }; + _Guard_elts __guard_elts(__new_start + __size, __n, _M_impl); + + std::__uninitialized_move_if_noexcept_a( + __old_start, __old_finish, __new_start, + _M_get_Tp_allocator()); + + __guard_elts._M_first = __old_start; + __guard_elts._M_last = __old_finish; } _GLIBCXX_ASAN_ANNOTATE_REINIT; - _M_deallocate(__old_start, - this->_M_impl._M_end_of_storage - __old_start); + __guard._M_storage = __old_start; + __guard._M_len = this->_M_impl._M_end_of_storage - __old_start; + this->_M_impl._M_start = __new_start; this->_M_impl._M_finish = __new_start + __size + __n; this->_M_impl._M_end_of_storage = __new_start + __len;