public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: DJ Delorie <dj@redhat.com>
To: Siddhesh Poyarekar <siddhesh@gotplt.org>
Cc: libc-alpha@sourceware.org
Subject: Re: [RFC] Supporting malloc_usable_size
Date: Thu, 01 Dec 2022 23:42:57 -0500	[thread overview]
Message-ID: <xnedti76we.fsf@greed.delorie.com> (raw)
In-Reply-To: <20221124213258.305192-1-siddhesh@gotplt.org>

Siddhesh Poyarekar <siddhesh@gotplt.org> writes:
> At that time, I thought this use was to avoid reallocating[1] when

You can't do an optimal realloc if you don't know what the usable size
is, but once you know the optimal size, calling realloc() should be a
valid way of telling the compiler what you're doing.  We just need to
ensure that our realloc() does the sane thing in such cases and I see no
problem with users doing it.

> that maybe providing a fast path for cases where the new
> size was within the current chunk size was the solution to the problem
> systemd was trying to solve with malloc_usable_size.

I think a fast path check would be good for performance anyway, but
let's not call it a solution, because it won't change the functionality,
just the speed.

> it is used as a way to query sizes of objects and hence eliminating
> the need to pass their corresponding sizes.

Since malloc_usable_size is nonstandard and intended for debugging, this
is a bad programming practice (as noted in the man page).  As such, I
see no need to spend effort on it, either to block it or to optimize it.
Caveat programmer.  As long as the function returns the correct value, I
have no desire to worry about it further ;-)

> I have an idea to support this abuse and at the same time, satisfy the
> compiler by giving it a hint of the new size.

That's a lot of work to avoid a call to realloc().  And if they're
calling into libc to get a record size, they already don't care about
optimal performance.

> The larger consequence of this patch though is that we further support
> the usage of malloc_usable_size for cases beyond diagnostics.  Do we
> want to do that?

To what extend do we support it *at all*, other than "it returns the
right number at the time it was called" ?

> If we do, should we also then make clear what kind of usage we support
> as a library, say, in the manual?

Our man page already says "The main use of this function is for
debugging and introspection".  The BSD manual says "The
malloc_usable_size() function is not a mechanism for in-place realloc();
rather it is provided solely as a tool for introspection purposes."

So clarifying the manual to be more in line with BSD is probably OK but
anything further than that is IMHO an API change.

We may want to add a caveat that the returned value is only valid until
the next call to any malloc family function, though, although I don't
see any way our code could make the usable size of an allocated chunk
*smaller*.  I don't think we've ever guaranteed that the heap metadata
is constant.

Maybe as a lark we could make 1% of the calls return an obviously wrong
value?  ;-)


  reply	other threads:[~2022-12-02  4:43 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-24 21:32 Siddhesh Poyarekar
2022-12-02  4:42 ` DJ Delorie [this message]
2022-12-02  5:00   ` Sam James
2022-12-02  5:28     ` DJ Delorie
2022-12-02 12:36       ` Siddhesh Poyarekar
2022-12-02 19:16         ` DJ Delorie
2022-12-02 19:49           ` Siddhesh Poyarekar
2022-12-02 19:57             ` DJ Delorie
2022-12-02 12:03 ` Andreas Schwab
2022-12-02 12:22   ` Siddhesh Poyarekar
2022-12-02 12:34     ` Andreas Schwab
2022-12-02 12:39       ` Florian Weimer
2022-12-05 18:46         ` Zack Weinberg
2022-12-05 19:04           ` Siddhesh Poyarekar
2022-12-05 20:35           ` Florian Weimer
2022-12-06 19:25             ` Siddhesh Poyarekar
2022-12-07 10:01               ` Florian Weimer
2022-12-07 16:34                 ` Siddhesh Poyarekar
2022-12-07 16:54                   ` Adhemerval Zanella Netto
2022-12-07 16:57                     ` Sam James
2022-12-07 17:39                     ` Florian Weimer
2022-12-09 15:42                     ` Siddhesh Poyarekar
2022-12-07 18:45                 ` DJ Delorie
2022-12-02 12:54     ` Florian Weimer
2022-12-02 13:54 Wilco Dijkstra

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

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

  git send-email \
    --in-reply-to=xnedti76we.fsf@greed.delorie.com \
    --to=dj@redhat.com \
    --cc=libc-alpha@sourceware.org \
    --cc=siddhesh@gotplt.org \
    /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.
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).