From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id WK3MK5YmCGXNZBgAWB0awg (envelope-from ) for ; Mon, 18 Sep 2023 06:29:42 -0400 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=HD0Z2Ovg; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id A6B2B1E0C3; Mon, 18 Sep 2023 06:29:42 -0400 (EDT) Received: from server2.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 ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 940E01E028 for ; Mon, 18 Sep 2023 06:29:40 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 76DDA3858C1F for ; Mon, 18 Sep 2023 10:29:39 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 76DDA3858C1F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1695032979; bh=/ctUcISJ6ecM9b8Pz/FjXfVkKIYnjxfOXuvDfJjBsMA=; h=Date:To:Cc:Subject:References:In-Reply-To:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=HD0Z2OvgDT35nerBVdl5TXc1FyscbH+INIzDj29PN4+8Gh6XnMLGwGK1Umw9Pw+nU 7M8KY0vyCSiR5MlscukukzuaQyqOFUFktl/T31j1F3gjxGEdjVLqJTAe6vAt6hEqCh s1y4p13fmSydOFNVysyehIiOmQueoFcnff86leHo= Received: from lndn.lancelotsix.com (lndn.lancelotsix.com [51.195.220.111]) by sourceware.org (Postfix) with ESMTPS id 8DF6A3858D32 for ; Mon, 18 Sep 2023 10:29:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8DF6A3858D32 Received: from octopus (cust120-dsl54.idnet.net [212.69.54.120]) by lndn.lancelotsix.com (Postfix) with ESMTPSA id 1B7CF83B8B; Mon, 18 Sep 2023 10:29:18 +0000 (UTC) Date: Mon, 18 Sep 2023 11:29:13 +0100 To: Tom Tromey Cc: gdb-patches@sourceware.org Subject: Re: [PATCH 1/7] Introduce type-safe variant of gdb_bfd_openr_iovec Message-ID: <20230918102849.ksgb4c33dwsqpcjc@octopus> References: <20230824-gdb-bfd-vec-v1-0-850e4e907ed1@adacore.com> <20230824-gdb-bfd-vec-v1-1-850e4e907ed1@adacore.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20230824-gdb-bfd-vec-v1-1-850e4e907ed1@adacore.com> X-Greylist: Sender succeeded SMTP AUTH, not delayed by milter-greylist-4.6.2 (lndn.lancelotsix.com [0.0.0.0]); Mon, 18 Sep 2023 10:29:18 +0000 (UTC) X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, 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.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Lancelot SIX via Gdb-patches Reply-To: Lancelot SIX Errors-To: gdb-patches-bounces+public-inbox=simark.ca@sourceware.org Sender: "Gdb-patches" Hi Tom, On Thu, Aug 24, 2023 at 11:12:18AM -0600, Tom Tromey via Gdb-patches wrote: > This patch adds a new, type-safe variant of gdb_bfd_openr_iovec. In > this approach, the underlying user data is simply an object, the > callbacks are methods, and the "open" function is a function view. > Nothing uses this new code yet. > --- > gdb/gdb_bfd.c | 42 ++++++++++++++++++++++++++++++++++++++++++ > gdb/gdb_bfd.h | 31 +++++++++++++++++++++++++++++++ > 2 files changed, 73 insertions(+) > > diff --git a/gdb/gdb_bfd.c b/gdb/gdb_bfd.c > index 3765561cbe9..0bb97460898 100644 > --- a/gdb/gdb_bfd.c > +++ b/gdb/gdb_bfd.c > @@ -907,6 +907,48 @@ gdb_bfd_openw (const char *filename, const char *target) > > /* See gdb_bfd.h. */ > > +gdb_bfd_ref_ptr > +gdb_bfd_openr_iovec (const char *filename, const char *target, > + gdb_iovec_opener_ftype open_fn) > +{ > + auto do_open = [] (bfd *nbfd, void *closure) -> void * > + { > + auto real_opener = (gdb_iovec_opener_ftype *) closure; I think I would have a personal preference toward using static_cast here, but in this case the C-style cast and static cast are equivalent. Other than this remark, this looks good to me. Reviewed-By: Lancelot Six Best, Lancelot. > + return (*real_opener) (nbfd); > + }; > + > + auto read_trampoline = [] (bfd *nbfd, void *stream, void *buf, > + file_ptr nbytes, file_ptr offset) -> file_ptr > + { > + gdb_bfd_iovec_base *obj = (gdb_bfd_iovec_base *) stream; > + return obj->read (nbfd, buf, nbytes, offset); > + }; > + > + auto stat_trampoline = [] (struct bfd *abfd, void *stream, > + struct stat *sb) -> int > + { > + gdb_bfd_iovec_base *obj = (gdb_bfd_iovec_base *) stream; > + return obj->stat (abfd, sb); > + }; > + > + auto close_trampoline = [] (struct bfd *nbfd, void *stream) -> int > + { > + gdb_bfd_iovec_base *obj = (gdb_bfd_iovec_base *) stream; > + delete obj; > + /* Success. */ > + return 0; > + }; > + > + bfd *result = bfd_openr_iovec (filename, target, > + do_open, &open_fn, > + read_trampoline, close_trampoline, > + stat_trampoline); > + > + return gdb_bfd_ref_ptr::new_reference (result); > +} > + > +/* See gdb_bfd.h. */ > + > gdb_bfd_ref_ptr > gdb_bfd_openr_iovec (const char *filename, const char *target, > void *(*open_func) (struct bfd *nbfd, > diff --git a/gdb/gdb_bfd.h b/gdb/gdb_bfd.h > index d15b1106d9a..ae374f5d7ae 100644 > --- a/gdb/gdb_bfd.h > +++ b/gdb/gdb_bfd.h > @@ -22,6 +22,7 @@ > > #include "registry.h" > #include "gdbsupport/byte-vector.h" > +#include "gdbsupport/function-view.h" > #include "gdbsupport/gdb_ref_ptr.h" > #include "gdbsupport/iterator-range.h" > #include "gdbsupport/next-iterator.h" > @@ -150,6 +151,36 @@ gdb_bfd_ref_ptr gdb_bfd_openr (const char *, const char *); > > gdb_bfd_ref_ptr gdb_bfd_openw (const char *, const char *); > > +/* The base class for BFD "iovec" implementations. This is used by > + gdb_bfd_openr_iovec and enables better type safety. */ > + > +class gdb_bfd_iovec_base > +{ > +protected: > + > + gdb_bfd_iovec_base () = default; > + > +public: > + > + virtual ~gdb_bfd_iovec_base () = default; > + > + /* The "read" callback. */ > + virtual file_ptr read (bfd *abfd, void *buffer, file_ptr nbytes, > + file_ptr offset) = 0; > + > + /* The "stat" callback. */ > + virtual int stat (struct bfd *abfd, struct stat *sb) = 0; > +}; > + > +/* The type of the function used to open a new iovec-based BFD. */ > +using gdb_iovec_opener_ftype > + = gdb::function_view; > + > +/* A type-safe wrapper for bfd_openr_iovec. */ > + > +gdb_bfd_ref_ptr gdb_bfd_openr_iovec (const char *filename, const char *target, > + gdb_iovec_opener_ftype open_fn); > + > /* A wrapper for bfd_openr_iovec that initializes the gdb-specific > reference count. */ > > > -- > 2.40.1 >