From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id OBXtFcvl/WPLeAAAWB0awg (envelope-from ) for ; Tue, 28 Feb 2023 06:30:19 -0500 Received: by simark.ca (Postfix, from userid 112) id 567F21E222; Tue, 28 Feb 2023 06:30:19 -0500 (EST) Authentication-Results: simark.ca; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=UACbOAAy; dkim-atps=neutral X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-7.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI, RDNS_DYNAMIC,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id E95D51E128 for ; Tue, 28 Feb 2023 06:30:18 -0500 (EST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9CC64384F494 for ; Tue, 28 Feb 2023 11:30:18 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9CC64384F494 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1677583818; bh=N8xIraSJ8U6NuBJDgTxc+jEImvFkSppVXF7xQ2gE2jU=; 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=UACbOAAy162TpMxLwq3GJ59NO3zRRlZEvv7XJxJyxfkdfj5ZlTF4Nv0A0eAFRPfu3 b19igPNpew/QVpkEDhViMPJCFjBWEJfIrquOBN4f/f4P/UKdhKUJo/c432BDigus2O lsOdLpghZLu/lteIipSysAfUm43LyOa4youvaLwQ= Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by sourceware.org (Postfix) with ESMTPS id 063FE3858C27 for ; Tue, 28 Feb 2023 11:29:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 063FE3858C27 X-IronPort-AV: E=McAfee;i="6500,9779,10634"; a="396679481" X-IronPort-AV: E=Sophos;i="5.98,221,1673942400"; d="scan'208";a="396679481" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Feb 2023 03:29:29 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10634"; a="919748205" X-IronPort-AV: E=Sophos;i="5.98,221,1673942400"; d="scan'208";a="919748205" Received: from ultl2604.iul.intel.com (HELO localhost) ([172.28.48.47]) by fmsmga006-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Feb 2023 03:29:29 -0800 To: gdb-patches@sourceware.org Subject: [PATCH 08/26] gdbserver: convert free_register_cache into a destructor of regcache Date: Tue, 28 Feb 2023 12:28:06 +0100 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit 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: , From: Tankut Baris Aktemur via Gdb-patches Reply-To: Tankut Baris Aktemur Errors-To: gdb-patches-bounces+public-inbox=simark.ca@sourceware.org Sender: "Gdb-patches" Convert the `free_register_cache` function into a destructor of the regcache struct. In one place, we completely remove the call to free the regcache object by stack-allocating the object. We also delete the copy constructor explicitly to prevent the risk of copying regcaches and then double-freeing buffers when they are destructed. --- gdbserver/gdbthread.h | 2 +- gdbserver/regcache.cc | 15 +++++---------- gdbserver/regcache.h | 9 +++++---- gdbserver/server.cc | 8 +++----- 4 files changed, 14 insertions(+), 20 deletions(-) diff --git a/gdbserver/gdbthread.h b/gdbserver/gdbthread.h index 493e1dbf6cb..cfd81870af9 100644 --- a/gdbserver/gdbthread.h +++ b/gdbserver/gdbthread.h @@ -35,7 +35,7 @@ struct thread_info ~thread_info () { - free_register_cache (this->regcache_data); + delete this->regcache_data; } /* The id of this thread. */ diff --git a/gdbserver/regcache.cc b/gdbserver/regcache.cc index be01df342bb..7b6337166ad 100644 --- a/gdbserver/regcache.cc +++ b/gdbserver/regcache.cc @@ -165,16 +165,11 @@ regcache::regcache (const target_desc *tdesc) initialize (tdesc, nullptr); } -void -free_register_cache (struct regcache *regcache) +regcache::~regcache () { - if (regcache) - { - if (regcache->registers_owned) - free (regcache->registers); - free (regcache->register_status); - delete regcache; - } + if (registers_owned) + free (registers); + free (register_status); } #endif @@ -280,7 +275,7 @@ free_register_cache_thread (struct thread_info *thread) if (regcache != NULL) { regcache_invalidate_thread (thread); - free_register_cache (regcache); + delete regcache; set_thread_regcache_data (thread, NULL); } } diff --git a/gdbserver/regcache.h b/gdbserver/regcache.h index 32b3a8dccfc..614d5a2561f 100644 --- a/gdbserver/regcache.h +++ b/gdbserver/regcache.h @@ -51,6 +51,11 @@ struct regcache : public reg_buffer_common /* Constructors. */ regcache () = default; regcache (const target_desc *tdesc); + regcache (const regcache &rhs) = delete; + regcache &operator= (const regcache &rhs) = delete; + + /* Deconstructor. */ + virtual ~regcache (); #endif /* Init the regcache data. */ @@ -77,10 +82,6 @@ struct regcache : public reg_buffer_common regcache *get_thread_regcache (thread_info *thread, bool fetch = true); -/* Release all memory associated with the register cache for INFERIOR. */ - -void free_register_cache (struct regcache *regcache); - /* Invalidate cached registers for one thread. */ void regcache_invalidate_thread (struct thread_info *); diff --git a/gdbserver/server.cc b/gdbserver/server.cc index 4be12ec1c66..521f13c07d7 100644 --- a/gdbserver/server.cc +++ b/gdbserver/server.cc @@ -4237,15 +4237,13 @@ process_serial_event (void) require_running_or_break (cs.own_buf); if (cs.current_traceframe >= 0) { - struct regcache *regcache - = new struct regcache (current_target_desc ()); + regcache a_regcache (current_target_desc ()); if (fetch_traceframe_registers (cs.current_traceframe, - regcache, -1) == 0) - registers_to_string (regcache, cs.own_buf); + &a_regcache, -1) == 0) + registers_to_string (&a_regcache, cs.own_buf); else write_enn (cs.own_buf); - free_register_cache (regcache); } else { -- 2.25.1 Intel Deutschland GmbH Registered Address: Am Campeon 10, 85579 Neubiberg, Germany Tel: +49 89 99 8853-0, www.intel.de Managing Directors: Christin Eisenschmid, Sharon Heck, Tiffany Doon Silva Chairperson of the Supervisory Board: Nicole Lau Registered Office: Munich Commercial Register: Amtsgericht Muenchen HRB 186928