From patchwork Fri Nov 18 16:42:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 60841 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 BEB1C384F483 for ; Fri, 18 Nov 2022 16:44:04 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BEB1C384F483 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1668789844; bh=uXsGKR1gf7ka4OweDqwOxB35/McxZZL5xLas572AGQw=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=D1NLymk86BPNoHtzHZGgbiyvrd8/fhQ0d8cO9hPHRQOz9nk3jalfF87lNlBYSw1jP vcves7iSzdsq7e/yL/IThagfhMwuoA8D5ddwAPhcr4LO8xnXgc2nPhcillKLkbDrKA +2XGl1d0HTBPd6Qr4HfHpe4RZQ7sg40yJGnIl2iw= 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 29EC13852200 for ; Fri, 18 Nov 2022 16:43:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 29EC13852200 Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-314-oe38ygGJOvezJOx6cJjaZQ-1; Fri, 18 Nov 2022 11:43:04 -0500 X-MC-Unique: oe38ygGJOvezJOx6cJjaZQ-1 Received: by mail-wm1-f71.google.com with SMTP id j2-20020a05600c1c0200b003cf7397fc9bso2493596wms.5 for ; Fri, 18 Nov 2022 08:43:04 -0800 (PST) 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:message-id:reply-to; bh=uXsGKR1gf7ka4OweDqwOxB35/McxZZL5xLas572AGQw=; b=srnAO4tMGVVwxDM85+sMaMk1mWzBNH3/eALNZlO+4HrNEda9UEuoqoH53KtsNDbQ1u l5/zDB0LBnB6em8ssAwEAFeCV70NPt4PUi7/2GW8hdurJyShOUKQxcxPrYjx9NXckU2/ xE0VfeG9EeHrBBZA7C+R6pya/KVUqerAf3XcCX/+6MDnr+Ssqn7YDg+dyXYOWO4286TU bPsyzh4Oy7KV/q2aJFRbSBkujAvUASUZu8wlYFVAiuIXvrYlB9BTEL7jdCs1XKouDGwO 3fmg8VE7HvkTnVqVrRs3Hb2cyTD5HyT+x12fVRMF9zJM14bhxAeYr5X23wKt8boBpufb 6aFQ== X-Gm-Message-State: ANoB5pnoeaeZD0/pFs9uOVgMKVBovNP0RuURvl2U6HvE3sNi7A941x60 vJuVwO69fKu4O9r2TONGlQAyOKX+lOCsLtAdBTtlle+9tEFjBxEvImuabh5UABXkM9jxn4dY31a LszC5aCoIT1ePdJUb3u94ErUr0RtHomfQvUhY4m7eqs6bBhPjcejhO7PUXcy4Lys7XznNXEM26A == X-Received: by 2002:a5d:4f09:0:b0:236:587a:61b2 with SMTP id c9-20020a5d4f09000000b00236587a61b2mr4837813wru.703.1668789783288; Fri, 18 Nov 2022 08:43:03 -0800 (PST) X-Google-Smtp-Source: AA0mqf6KiI4q5+z0TlyVCflD9NzXlW/ByBwHBI6YoLmo15RI+UnQ9Y/Qq+oLJZ8+BX/w/0EPBH/I2w== X-Received: by 2002:a5d:4f09:0:b0:236:587a:61b2 with SMTP id c9-20020a5d4f09000000b00236587a61b2mr4837802wru.703.1668789782914; Fri, 18 Nov 2022 08:43:02 -0800 (PST) Received: from localhost ([31.111.84.238]) by smtp.gmail.com with ESMTPSA id i6-20020a05600c354600b003cf894c05e4sm9859750wmq.22.2022.11.18.08.43.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Nov 2022 08:43:02 -0800 (PST) To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCHv3 1/7] gdb/remote: remove some manual reference count handling Date: Fri, 18 Nov 2022 16:42:52 +0000 Message-Id: X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.7 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: 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 5118ecd0a31..24da3d544d5 100644 --- a/gdb/remote.c +++ b/gdb/remote.c @@ -4702,7 +4702,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. */ @@ -4712,26 +4712,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