public inbox for glibc-bugs@sourceware.org help / color / mirror / Atom feed
From: "hpa at zytor dot com" <sourceware-bugzilla@sourceware.org> To: glibc-bugs@sourceware.org Subject: [Bug malloc/31222] New: RFE: malloc_size() and/or a realloc() which returns the allocated size Date: Mon, 08 Jan 2024 06:13:37 +0000 [thread overview] Message-ID: <bug-31222-131@http.sourceware.org/bugzilla/> (raw) https://sourceware.org/bugzilla/show_bug.cgi?id=31222 Bug ID: 31222 Summary: RFE: malloc_size() and/or a realloc() which returns the allocated size Product: glibc Version: unspecified Status: UNCONFIRMED Severity: enhancement Priority: P2 Component: malloc Assignee: unassigned at sourceware dot org Reporter: hpa at zytor dot com Target Milestone: --- glibc currently provides malloc_usable_size(), which has the caveat: of which the programmer should rely on. This function is intended to only be used for diagnostics and statistics; writing to the excess memory without first calling realloc(3) to resize the allocation is not supported. The returned value is only valid at the time of the call. In contrast, BSD provides malloc_size(), which provides the permanently allocated size of the memory area, which can be relied upon by the programmer. This is really useful in a loop of this form: void *buf = NULL; size_t bufsiz = 0; while (1) { size_t needbuf = bufsiz; function_writing_to_sized_buffer(buf, &needbuf, ....); if (needbuf <= bufsiz) break; /* Buffer big enough, operation successful */ buf = realloc(buf, needbuf); /* Need to handle realloc() failure */ bufsiz = needbuf; } This form is safe against function_writing_to_sized_buffer() not producing the same output on a subsequent call. However, this also means that work is thrown away unnecessarily if the block actually returned by realloc() was rounded to a value big enough and the realloc() was unnecessary. -- You are receiving this mail because: You are on the CC list for the bug.
reply other threads:[~2024-01-08 6:13 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=bug-31222-131@http.sourceware.org/bugzilla/ \ --to=sourceware-bugzilla@sourceware.org \ --cc=glibc-bugs@sourceware.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: linkBe 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).