From patchwork Sun Oct 2 17:04:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 58278 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BA9DD3854143 for ; Sun, 2 Oct 2022 17:06:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BA9DD3854143 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1664730388; bh=mAPYZ+X5rIrlKYdncz0uIo1CsBp96kLuEoG3lFbw2qM=; 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=rijLPBQxTe3IB/fsGTjG49qPa2PIva0dZxAX/u0vS5a9vtTAS/peZ85FJ8VfRoJru G9iAxy2BLt7GnsKTj/nDbNcyzkVCIGF97orXwn10R3lySgEBmxHKGoBS2zOrCIq2nZ 2vTElYa+Z1xKFhDKEFE9cfIyFZxH9mcL7AkD+q8I= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.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 6288C3858D38 for ; Sun, 2 Oct 2022 17:04:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 6288C3858D38 Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-54-Dg9LyUVENquLZYeGGo5ccg-1; Sun, 02 Oct 2022 13:04:54 -0400 X-MC-Unique: Dg9LyUVENquLZYeGGo5ccg-1 Received: by mail-wr1-f70.google.com with SMTP id d22-20020adfa356000000b0022e224b21c0so850247wrb.9 for ; Sun, 02 Oct 2022 10:04:54 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=mAPYZ+X5rIrlKYdncz0uIo1CsBp96kLuEoG3lFbw2qM=; b=RyR0AWJ0xR/nibBfpBe/g5BI+bHHCkZ3hRRf8GqCyCeHYXVyg27pwjTkKSOAncJV3Z ySVY6Flk/3aeYsIBXP0Qg7AATGYRrdoeJPZoWk9YkxlxdrytGeJTIs9K3sEy9JHr6MOm hTo+Y82h9G8452wYsUABCs41MsYrYbNxdjzF4liVfeDAE6F34ny45hJ8aUMqYPozXN7j a4b9iOHjMfqASfizmjKMkZy5vfINdq50kUegoIQo1blMB9qp0d43EVElOoGMtIyW+b+4 LAhVzzSi50SByXyWULyrOcbmdpheZC78j2iWFxMiOjbR9v5qgZOPg6jcr417QPS9Tnme T4cw== X-Gm-Message-State: ACrzQf3q1+7MCMe4pMByaMNUHQjGjyU8QrVIo44nfywPuH8w9Tqj6gNh 3znz54/1EnkCbbUDGJJPYpqe907EhNNKDSyz5BL/kMPdL23NF1M6mkK7bGC1Jy9pZhCnnOXyvcJ POqc7PyEBUn8zbbZ7fYIGBJesrwh9HJoJTi3Q8exC+5wcuVRNTDukea6xtWW27jsQs1+IVc29lw == X-Received: by 2002:a05:600c:281:b0:3b5:a5:7c9c with SMTP id 1-20020a05600c028100b003b500a57c9cmr4502937wmk.202.1664730293035; Sun, 02 Oct 2022 10:04:53 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6hffIhZYyUnNBJEihmHSF0OySRyzOYLyMNHe14KU3O3ZrwHRreEfSMTGGcv4Vb+RdZAAzE6g== X-Received: by 2002:a05:600c:281:b0:3b5:a5:7c9c with SMTP id 1-20020a05600c028100b003b500a57c9cmr4502925wmk.202.1664730292732; Sun, 02 Oct 2022 10:04:52 -0700 (PDT) Received: from localhost (52.72.115.87.dyn.plus.net. [87.115.72.52]) by smtp.gmail.com with ESMTPSA id x7-20020adfffc7000000b0022cbf4cda62sm6739951wrs.27.2022.10.02.10.04.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 02 Oct 2022 10:04:52 -0700 (PDT) To: gdb-patches@sourceware.org Subject: [PATCHv2 1/7] gdb/remote: remove some manual reference count handling Date: Sun, 2 Oct 2022 18:04:42 +0100 Message-Id: <5c946ed5f149bc1a1491bf30750ae1d61e9c81ea.1664729721.git.aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: References: <20220921131200.3983844-1-aburgess@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.3 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, 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: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Andrew Burgess via Gdb-patches From: Andrew Burgess Reply-To: Andrew Burgess Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" While working on some other target_ops reference count related code, I spotted that in remote.c we do some manual reference count handling, i.e. we call target_ops::incref and decref_target (which wraps target_ops::decref). I think it would be better to make use of gdb::ref_ptr to automate the reference count management. So, this commit updates scoped_mark_target_starting in two ways, first, I use gdb::ref_ptr to handle the reference counts. Then, instead of using the scoped_mark_target_starting constructor and destructor to set and reset the starting_up flag, I now use a scoped_restore_tmpl object to set and restore the flag. The above changes mean that the scoped_mark_target_starting destructor can be completely removed, and the constructor body is now empty. I've also fixed a typo in the class comment. The only change in behaviour after this commit is that previously we didn't care what the value of starting_up was, we just set it to true in the constructor and false in the destructor. Now, I assert that the flag is initially false, then set the flag true when the scoped_mark_target_starting is created. As the starting_up flag is initialized to false then, for the assert to fire, we would need to recursively enter remote_target::start_remote_1, which I don't think is something we should be doing, so I think the new assert is an improvement. --- gdb/remote.c | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/gdb/remote.c b/gdb/remote.c index 2f6cb2d01ee..5a71c41d61e 100644 --- a/gdb/remote.c +++ b/gdb/remote.c @@ -4706,7 +4706,7 @@ remote_target::process_initial_stop_replies (int from_tty) } } -/* Mark a remote_target as marking (by setting the starting_up flag within +/* Mark a remote_target as starting (by setting the starting_up flag within its remote_state) for the lifetime of this object. The reference count on the remote target is temporarily incremented, to prevent the target being deleted under our feet. */ @@ -4716,26 +4716,32 @@ struct scoped_mark_target_starting /* Constructor, TARGET is the target to be marked as starting, its reference count will be incremented. */ scoped_mark_target_starting (remote_target *target) - : m_remote_target (target) - { - m_remote_target->incref (); - remote_state *rs = m_remote_target->get_remote_state (); - rs->starting_up = true; - } + : m_remote_target (remote_target_ref::new_reference (target)), + m_restore_starting_up (set_starting_up_flag (target)) + { /* Nothing. */ } + +private: - /* Destructor, mark the target being worked on as no longer starting, and - decrement the reference count. */ - ~scoped_mark_target_starting () + /* Helper function, set the starting_up flag on TARGET and return an + object which, when it goes out of scope, will restore the previous + value of the starting_up flag. */ + static scoped_restore_tmpl + set_starting_up_flag (remote_target *target) { - remote_state *rs = m_remote_target->get_remote_state (); - rs->starting_up = false; - decref_target (m_remote_target); + remote_state *rs = target->get_remote_state (); + gdb_assert (!rs->starting_up); + return make_scoped_restore (&rs->starting_up, true); } -private: + /* A gdb::ref_ptr pointer to a remote_target. */ + using remote_target_ref = gdb::ref_ptr; + + /* A reference to the target on which we are operating. */ + remote_target_ref m_remote_target; - /* The target on which we are operating. */ - remote_target *m_remote_target; + /* An object which restores the previous value of the starting_up flag + when it goes out of scope. */ + scoped_restore_tmpl m_restore_starting_up; }; /* Helper for remote_target::start_remote, start the remote connection and