From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS54825 147.75.48.0/24 X-Spam-Status: No, score=-3.8 required=3.0 tests=AWL,BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org [147.75.48.161]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id EA4361F406 for ; Wed, 25 Oct 2023 07:42:14 +0000 (UTC) Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 62CC6B2129B for ; Wed, 25 Oct 2023 07:42:13 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B8A191A5B7; Wed, 25 Oct 2023 07:42:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=none Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C18BB2D62C for ; Wed, 25 Oct 2023 07:42:02 +0000 (UTC) Received: from cloud.peff.net (cloud.peff.net [104.130.231.41]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8E45B3591 for ; Wed, 25 Oct 2023 00:37:38 -0700 (PDT) Received: (qmail 7259 invoked by uid 109); 25 Oct 2023 07:37:37 -0000 Received: from Unknown (HELO peff.net) (10.0.1.2) by cloud.peff.net (qpsmtpd/0.94) with ESMTP; Wed, 25 Oct 2023 07:37:37 +0000 Authentication-Results: cloud.peff.net; auth=none Received: (qmail 515 invoked by uid 111); 25 Oct 2023 07:37:41 -0000 Received: from coredump.intra.peff.net (HELO coredump.intra.peff.net) (10.0.0.2) by peff.net (qpsmtpd/0.94) with (TLS_AES_256_GCM_SHA384 encrypted) ESMTPS; Wed, 25 Oct 2023 03:37:41 -0400 Authentication-Results: peff.net; auth=none Date: Wed, 25 Oct 2023 03:37:36 -0400 From: Jeff King To: Taylor Blau Cc: git@vger.kernel.org, Elijah Newren , "Eric W. Biederman" , Junio C Hamano , Patrick Steinhardt Subject: Re: [PATCH v5 1/5] bulk-checkin: extract abstract `bulk_checkin_source` Message-ID: <20231025073736.GB2145145@coredump.intra.peff.net> References: <696aa027e46ddec310812fad2d4b12082447d925.1698101088.git.me@ttaylorr.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: , MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <696aa027e46ddec310812fad2d4b12082447d925.1698101088.git.me@ttaylorr.com> List-Unsubscribe-Post: List-Unsubscribe=One-Click On Mon, Oct 23, 2023 at 06:44:56PM -0400, Taylor Blau wrote: > +struct bulk_checkin_source { > + off_t (*read)(struct bulk_checkin_source *, void *, size_t); > + off_t (*seek)(struct bulk_checkin_source *, off_t); > + > + union { > + struct { > + int fd; > + } from_fd; > + } data; > + > + size_t size; > + const char *path; > +}; The virtual functions combined with the union are a funny mix of object-oriented and procedural code. The bulk_checkin_source has totally virtualized functions, but knows about all of the ancillary data each set of virtualized functions might want. ;) I think the more pure OO version would embed the parent, and have each concrete type define its own struct type, like: struct bulk_checkin_source_fd { struct bulk_checkin_source src; int fd; }; That works great if the code which constructs it knows which concrete type it wants, and can just do: struct bulk_checkin_source_fd src; init_bulk_checkin_source_from_fd(&src, ...); If even the construction is somewhat virtualized, then you are stuck with heap constructors like: struct bulk_checkin_source *bulk_checkin_source_from_fd(...); Not too bad, but you have to remember to free now. Alternatively, I think some of our other OO code just leaves room for a type-specific void pointer, like: struct bulk_checkin_source { ...the usual stuff... void *magic_type_data; }; and then the init_bulk_checkin_source_from_fd() function allocates its own heap struct for the magic_type_data field and sticks the int in there. That said, both of those are a lot more annoying to use in C (more boilerplate, more casting, and more opportunities to get something wrong, including leaks). So I don't mind this in-between state. It is a funny layering violating from an OO standpoint, but it's not like we expect an unbounded set of concrete types to "inherit" from the source struct. -Peff