git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Jacob Keller <jacob.keller@gmail.com>
To: Derrick Stolee <derrickstolee@github.com>
Cc: Elijah Newren via GitGitGadget <gitgitgadget@gmail.com>,
	git@vger.kernel.org, Elijah Newren <newren@gmail.com>
Subject: Re: [PATCH 00/11] Clarify API for dir.[ch] and unpack-trees.[ch] -- mark relevant fields as internal
Date: Thu, 23 Feb 2023 21:54:00 -0800	[thread overview]
Message-ID: <CA+P7+xo02dGkjb5DwJ1Af_hoQ5HiuxASheZxoFz+r6B-6cQMug@mail.gmail.com> (raw)
In-Reply-To: <16ff5069-0408-21cd-995c-8b47afb9810d@github.com>

On Thu, Feb 23, 2023 at 7:29 AM Derrick Stolee <derrickstolee@github.com> wrote:
>
> On 2/23/2023 4:14 AM, Elijah Newren via GitGitGadget wrote:
> > This patch is primarily about moving internal-only fields within these two
> > structs into an embedded internal struct. Patch breakdown:
> >
> >  * Patches 1-3: Restructuring dir_struct
> >    * Patch 1: Splitting off internal-use-only fields
> >    * Patch 2: Add important usage note to avoid accidentally using
> >      deprecated API
> >    * Patch 3: Mark output-only fields as such
> >  * Patches 4-11: Restructuring unpack_trees_options
> >    * Patches 4-6: Preparatory cleanup
> >    * Patches 7-10: Splitting off internal-use-only fields
> >    * Patch 11: Mark output-only field as such
>
> > And after the changes:
> >
> > struct dir_struct {
> >     enum [...] flags;
> >     int nr; /* output only */
> >     int ignored_nr; /* output only */
> >     struct dir_entry **entries; /* output only */
> >     struct dir_entry **ignored; /* output only */
> >     struct untracked_cache *untracked;
> >     const char *exclude_per_dir; /* deprecated */
> >     struct dir_struct_internal {
> >         int alloc;
> >         int ignored_alloc;
> > #define EXC_CMDL 0
> > #define EXC_DIRS 1
> > #define EXC_FILE 2
> >         struct exclude_list_group exclude_list_group[3];
> >         struct exclude_stack *exclude_stack;
> >         struct path_pattern *pattern;
> >         struct strbuf basebuf;
> >         struct oid_stat ss_info_exclude;
> >         struct oid_stat ss_excludes_file;
> >         unsigned unmanaged_exclude_files;
> >         unsigned visited_paths;
> >         unsigned visited_directories;
> >     } internal;
> > };
>
> This does present a very clear structure to avoid callers being
> confused when writing these changes. It doesn't, however, present
> any way to guarantee that callers can't mutate this state.
>
> ...here I go on a side track thinking of an alternative...
>
> One way to track this would be to anonymously declare 'struct
> dir_struct_internal' in the header file and let 'struct dir_struct'
> contain a _pointer_ to the internal struct. The dir_struct_internal
> can then be defined inside the .c file, limiting its scope. (It
> must be a pointer in dir_struct or else callers would not be able
> to create a dir_struct without using a pointer and an initializer
> method.
>
> The major downside to this pointer approach is that the internal
> struct needs to be initialized within API calls and somehow cleared
> by all callers. The internal data could be initialized by the common
> initializers read_directory() or fill_directory(). There is a
> dir_clear() that _should_ be called by all callers (but I notice we
> are leaking the struct in at least one place in add-interactive.c,
> and likely others).
>
> This alternative adds some complexity to the structure, but
> provides compiler-level guarantees that these internals are not used
> outside of dir.c. I thought it worth exploring, even if we decide
> that the complexity is not worth those guarantees.
>

Another approach, if you don't mind structure pointer math is to
create two structures:

a) the external public one in the public header file

struct dir_entry {
  <public stuff>
};

b) a private structure in the source file:

struct dir_entry_private {
  struct dir_entry entry;
  <private stuff>
};

In the source file you also define a macro/function that can take a
pointer to dir_entry and get a pointer to dir_entry_private:

struct dir_entry_private *dir_entry_to_private(struct dir_entry *entry)
{
  return (struct dir_entry_private *)(<calculate the offset that entry
inside dir_entry_private is, then subtract that from entry pointer>))
}

In Linux kernel this is container_of, not sure if git has this already
defined and its a common pattern.

Then you can set the code up such that the only way to allocate a
dir_entry is to call some function in the dir code. If a new entry
needs to be allocated, implement an alloc function that has the full
private structure definition.

This way you don't need an extra field in the dir_entry struct at all,
but at the cost of requiring special allocations. It works great for
code where the only way to get a dir_entry is already some other
functions that would ensure the private version is setup correctly.

> The best news is that your existing series makes it easier to flip
> to the internal pointer method in the future, since we can shift
> the 'd->internal.member" uses into "d->internal->member" in a
> mechanical way. Thus, the change you are proposing does not lock us
> into this approach if we change our minds later.
>

I think either approach is good. I like container_of because I'm quite
used to it in low level kernel code and its a good way to provide
private/public split abstractions there. The private pointer variation
is also a common approach to this problem and I think it sounds like
we already use it in a few places. Its perhaps better for those
reasons.

> Thanks,
> -Stolee

  parent reply	other threads:[~2023-02-24  5:54 UTC|newest]

Thread overview: 57+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-23  9:14 [PATCH 00/11] Clarify API for dir.[ch] and unpack-trees.[ch] -- mark relevant fields as internal Elijah Newren via GitGitGadget
2023-02-23  9:14 ` [PATCH 01/11] dir: separate public from internal portion of dir_struct Elijah Newren via GitGitGadget
2023-02-23  9:14 ` [PATCH 02/11] dir: add a usage note to exclude_per_dir Elijah Newren via GitGitGadget
2023-02-24 22:31   ` Jonathan Tan
2023-02-25  0:23     ` Elijah Newren
2023-02-25  1:54       ` Jonathan Tan
2023-02-25  3:23         ` Elijah Newren
2023-02-23  9:14 ` [PATCH 03/11] dir: mark output only fields of dir_struct as such Elijah Newren via GitGitGadget
2023-02-23  9:14 ` [PATCH 04/11] unpack-trees: clean up some flow control Elijah Newren via GitGitGadget
2023-02-24 22:33   ` Jonathan Tan
2023-02-23  9:14 ` [PATCH 05/11] sparse-checkout: avoid using internal API of unpack-trees Elijah Newren via GitGitGadget
2023-02-24 22:37   ` Jonathan Tan
2023-02-25  0:33     ` Elijah Newren
2023-02-23  9:14 ` [PATCH 06/11] sparse-checkout: avoid using internal API of unpack-trees, take 2 Elijah Newren via GitGitGadget
2023-02-24 23:22   ` Jonathan Tan
2023-02-25  0:40     ` Elijah Newren
2023-02-23  9:14 ` [PATCH 07/11] unpack_trees: start splitting internal fields from public API Elijah Newren via GitGitGadget
2023-02-23  9:14 ` [PATCH 08/11] unpack-trees: mark fields only used internally as internal Elijah Newren via GitGitGadget
2023-02-23  9:14 ` [PATCH 09/11] unpack-trees: rewrap a few overlong lines from previous patch Elijah Newren via GitGitGadget
2023-02-23  9:14 ` [PATCH 10/11] unpack-trees: special case read-tree debugging as internal usage Elijah Newren via GitGitGadget
2023-02-23  9:15 ` [PATCH 11/11] unpack-trees: add usage notices around df_conflict_entry Elijah Newren via GitGitGadget
2023-02-23 15:18 ` [PATCH 00/11] Clarify API for dir.[ch] and unpack-trees.[ch] -- mark relevant fields as internal Derrick Stolee
2023-02-23 15:26   ` Derrick Stolee
2023-02-23 20:35     ` Elijah Newren
2023-02-23 20:31   ` Elijah Newren
2023-02-24  1:24   ` Junio C Hamano
2023-02-24  5:54   ` Jacob Keller [this message]
2023-02-24 23:36 ` Jonathan Tan
2023-02-25  2:25 ` [PATCH v2 " Elijah Newren via GitGitGadget
2023-02-25  2:25   ` [PATCH v2 01/11] dir: separate public from internal portion of dir_struct Elijah Newren via GitGitGadget
2023-02-25  2:25   ` [PATCH v2 02/11] dir: add a usage note to exclude_per_dir Elijah Newren via GitGitGadget
2023-02-25  2:25   ` [PATCH v2 03/11] dir: mark output only fields of dir_struct as such Elijah Newren via GitGitGadget
2023-02-25  2:25   ` [PATCH v2 04/11] unpack-trees: clean up some flow control Elijah Newren via GitGitGadget
2023-02-25  2:25   ` [PATCH v2 05/11] sparse-checkout: avoid using internal API of unpack-trees Elijah Newren via GitGitGadget
2023-02-25  2:25   ` [PATCH v2 06/11] sparse-checkout: avoid using internal API of unpack-trees, take 2 Elijah Newren via GitGitGadget
2023-02-25  2:25   ` [PATCH v2 07/11] unpack_trees: start splitting internal fields from public API Elijah Newren via GitGitGadget
2023-02-25  2:25   ` [PATCH v2 08/11] unpack-trees: mark fields only used internally as internal Elijah Newren via GitGitGadget
2023-02-25  2:25   ` [PATCH v2 09/11] unpack-trees: rewrap a few overlong lines from previous patch Elijah Newren via GitGitGadget
2023-02-25  2:25   ` [PATCH v2 10/11] unpack-trees: special case read-tree debugging as internal usage Elijah Newren via GitGitGadget
2023-02-25  2:26   ` [PATCH v2 11/11] unpack-trees: add usage notices around df_conflict_entry Elijah Newren via GitGitGadget
2023-02-25 23:30   ` [PATCH v2 00/11] Clarify API for dir.[ch] and unpack-trees.[ch] -- mark relevant fields as internal Junio C Hamano
2023-02-27 15:28   ` [PATCH v3 00/13] " Elijah Newren via GitGitGadget
2023-02-27 15:28     ` [PATCH v3 01/13] t2021: fix platform-specific leftover cruft Elijah Newren via GitGitGadget
2023-02-27 19:11       ` Derrick Stolee
2023-02-27 15:28     ` [PATCH v3 02/13] unpack-trees: heed requests to overwrite ignored files Elijah Newren via GitGitGadget
2023-02-27 23:20       ` Jonathan Tan
2023-02-27 15:28     ` [PATCH v3 03/13] dir: separate public from internal portion of dir_struct Elijah Newren via GitGitGadget
2023-02-27 15:28     ` [PATCH v3 04/13] dir: add a usage note to exclude_per_dir Elijah Newren via GitGitGadget
2023-02-27 15:28     ` [PATCH v3 05/13] dir: mark output only fields of dir_struct as such Elijah Newren via GitGitGadget
2023-02-27 15:28     ` [PATCH v3 06/13] unpack-trees: clean up some flow control Elijah Newren via GitGitGadget
2023-02-27 15:28     ` [PATCH v3 07/13] sparse-checkout: avoid using internal API of unpack-trees Elijah Newren via GitGitGadget
2023-02-27 15:28     ` [PATCH v3 08/13] sparse-checkout: avoid using internal API of unpack-trees, take 2 Elijah Newren via GitGitGadget
2023-02-27 15:28     ` [PATCH v3 09/13] unpack_trees: start splitting internal fields from public API Elijah Newren via GitGitGadget
2023-02-27 15:28     ` [PATCH v3 10/13] unpack-trees: mark fields only used internally as internal Elijah Newren via GitGitGadget
2023-02-27 15:28     ` [PATCH v3 11/13] unpack-trees: rewrap a few overlong lines from previous patch Elijah Newren via GitGitGadget
2023-02-27 15:28     ` [PATCH v3 12/13] unpack-trees: special case read-tree debugging as internal usage Elijah Newren via GitGitGadget
2023-02-27 15:28     ` [PATCH v3 13/13] unpack-trees: add usage notices around df_conflict_entry Elijah Newren via GitGitGadget

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CA+P7+xo02dGkjb5DwJ1Af_hoQ5HiuxASheZxoFz+r6B-6cQMug@mail.gmail.com \
    --to=jacob.keller@gmail.com \
    --cc=derrickstolee@github.com \
    --cc=git@vger.kernel.org \
    --cc=gitgitgadget@gmail.com \
    --cc=newren@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).