From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 9E27C38582A7; Mon, 8 Jan 2024 06:13:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9E27C38582A7 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1704694417; bh=J3+NbDGwU7BLkvfIGWwVMUh0+XC7YPl8qEliMvDKsdU=; h=From:To:Subject:Date:From; b=AFXs5fGgOQGRGokfqoPP9kyaqkZRrXXz3XxT7T0oPmqJ56auLbuPSIplSrciN9lxR wWK5HaPqndyVURNirozV27lfKUVK4OSWfH/CP3rz/zUjHZcv9P5DuA1fFC+ShKxq4A lplpNk2F32Z87sdon6BqjQ3cHBqGqDjTi+8/wRz8= From: "hpa at zytor dot com" 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 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: glibc X-Bugzilla-Component: malloc X-Bugzilla-Version: unspecified X-Bugzilla-Keywords: X-Bugzilla-Severity: enhancement X-Bugzilla-Who: hpa at zytor dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P2 X-Bugzilla-Assigned-To: unassigned at sourceware dot org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://sourceware.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://sourceware.org/bugzilla/show_bug.cgi?id=3D31222 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 f= irst 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 programm= er. This is really useful in a loop of this form: void *buf =3D NULL; size_t bufsiz =3D 0; while (1) { size_t needbuf =3D bufsiz; function_writing_to_sized_buffer(buf, &needbuf, ....); if (needbuf <=3D bufsiz) break; /* Buffer big enough, operation successful */ buf =3D realloc(buf, needbuf); /* Need to handle realloc() failure */ bufsiz =3D 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. --=20 You are receiving this mail because: You are on the CC list for the bug.=