From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=lancelotsix.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=lancelotsix.com 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) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=lancelotsix.com; s=2021; t=1695032958; bh=U9aH874y+aKlMUp/oVbb28+dY/VIqdZgATtk4afyi3Y=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=tzAX+pqaQWlJDViWyI7TY2SwcpjCA1MQlL7hulU/T/CYjU2t8UK+UB0g3fIC8CS2y R1C151Zrj+uTAvk+ZzDBhk8pt5J2aUwuz4QmUYr9AZfNcXi9n8y7ZIfa8O28eGyBiO 8jUeqts3Sscy/IshHswjMLP0eho73LnPhF88Wv6Fr2BNSZbhCBOoQtXB29Ape4KpKn UhpYElSn+J9y/zPSRq6Hj0/LqbtlKzVICU3kIi+k3kA7ium2jOspTmSjXOmbd+Y+dA bl/JNM0G4gjLBc+n53MGhOxMA6+zxpxhADA15yEQEQqm19fhfNHWXauFW8155naRka zZfw8znbCYbSA== Date: Mon, 18 Sep 2023 11:29:13 +0100 From: Lancelot SIX 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 List-Id: 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 >