The definition of malloc_usable_size()
H. Peter Anvin
hpa@zytor.com
Mon May 4 17:47:02 GMT 2026
So the definition of malloc_usable_size() states:
CAVEATS
The value returned by malloc_usable_size() may be greater than the re‐
quested size of the allocation because of various internal implementa‐
tion details, none of which the programmer should rely on. This func‐
tion is intended to only be used for diagnostics and statistics; writ‐
ing 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.
Has *any* version of glibc actually behaved this way (meaning returned a value
larger than what would actually be safe to use?)
The very name malloc_usable_size() strongly implies that this amount of memory
is, well, usable, and as such this function name *strongly* violate the
principle of least surprise. As this is highly useful information for at
least some types of applications, I would be *highly* surprised if there
aren't applications out there which take advantage of it, regardless of this
statement, unless there are versions of glibc for which this is explicitly not
the case.
If there *are*, I would strongly suggest deprecating this function with an
explicit warning, and rename it something like __malloc_current_size() with
the __ prefix to indicate that it is an implementation-specific function, and
removing the word "usable" in the name.
-hpa
More information about the Libc-alpha
mailing list