git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Patrick Steinhardt <ps@pks.im>
Cc: "René Scharfe" <l.s.r@web.de>, "Git List" <git@vger.kernel.org>,
	"Han-Wen Nienhuys" <hanwenn@gmail.com>
Subject: Re: [PATCH] reftable: use xmalloc() and xrealloc()
Date: Mon, 08 Apr 2024 08:42:19 -0700	[thread overview]
Message-ID: <xmqq34rvg8tg.fsf@gitster.g> (raw)
In-Reply-To: <ZhOETox9FTIOAShN@tanuki> (Patrick Steinhardt's message of "Mon, 8 Apr 2024 07:44:46 +0200")

Patrick Steinhardt <ps@pks.im> writes:

> These are part of the library interfaces that should ideally not be tied
> to the Git code base at all so that they can theoretically be reused by
> another project like libgit2. So I think that instead of rewriting the
> generic fallbacks we should call `reftable_set_alloc()` somewhen early
> in Git's startup code.

It sounds like a sensible approach to me on the surface.

The reftable_subsystem_init() function, which would be the interface
into "reftable library" from Git side, can call such customization
functions supplied by the library.

> It does raise the question what to do about the generic fallbacks.

Generic fallbacks would be a plain vanilla malloc(), realloc(), and
friends, right?

> We could start calling `abort()` when we observe allocation
> failures. It's not exactly nice behaviour in a library though,
> where the caller may in fact want to handle this case.

But they would not be able to "handle" it, unless their substitute
alloc() function magically finds more memory and never runs out.  If
you want to allow them to "handle" the situation, the library itself
needs be prepared to see NULL returned from the allocator, and fail
the operation it was doing, and return an error.  If the caller asks
reftable_write_foo(), which may need to allocate some memory to
finish its work, it would see NULL and say "sorry, I cannot
continue", and return an error to its caller, I would imagine.

I think there are two levels of "handling" allocation and its
failure.  Substituting allocation functions would be a way to solve
only one of them (i.e. somehow allow the library client to specify a
way to supply you an unbounded amount of memory).  As long as the
library is not willing to check allocation failures and propagate
the error to the caller, you would have to "abort" the operation no
matter what before returning the control back to your client, and at
that point you would start wanting to make it customizable how to
"abort".  Their custom "abort" function might do longjmp() to try to
"recover", or simply call die() in our case where Git is the library
client, I guess.  So reftable_set_alloc() and reftable_set_abort() may
need to be there.  If you make it mandatory to call them, you can punt
and make it the responsibility of the library clients to worry about
error handling, I guess?

Thanks.





  reply	other threads:[~2024-04-08 15:42 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-06 20:37 [PATCH] reftable: use xmalloc() and xrealloc() René Scharfe
2024-04-08  5:44 ` Patrick Steinhardt
2024-04-08 15:42   ` Junio C Hamano [this message]
2024-04-08 16:33     ` Patrick Steinhardt
2024-04-08 17:50   ` Han-Wen Nienhuys
2024-04-08 20:36     ` Junio C Hamano
2024-04-09  3:24     ` Patrick Steinhardt

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=xmqq34rvg8tg.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=hanwenn@gmail.com \
    --cc=l.s.r@web.de \
    --cc=ps@pks.im \
    /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).