From: DJ Delorie <dj@redhat.com>
To: Siddhesh Poyarekar <siddhesh@sourceware.org>
Cc: libc-alpha@sourceware.org
Subject: Re: [PATCH v2] realloc: Return unchanged if request is within usable size
Date: Tue, 06 Dec 2022 17:33:09 -0500 [thread overview]
Message-ID: <xn7cz45fiy.fsf@greed.delorie.com> (raw)
In-Reply-To: <20221128172646.244742-1-siddhesh@sourceware.org>
Siddhesh Poyarekar via Libc-alpha <libc-alpha@sourceware.org> writes:
> Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
LGTM
Reviewed-by: DJ Delorie <dj@redhat.com>
> diff --git a/malloc/malloc.c b/malloc/malloc.c
> index 2a61c8b5ee..ef8c794fb7 100644
> --- a/malloc/malloc.c
> +++ b/malloc/malloc.c
> @@ -1100,6 +1100,8 @@ static void munmap_chunk(mchunkptr p);
> static mchunkptr mremap_chunk(mchunkptr p, size_t new_size);
> #endif
>
> +static size_t musable (void *mem);
Matches function which occurs later on
> + /* Return the chunk as is whenever possible, i.e. there's enough usable space
> + but not so much that we end up fragmenting the block. We use the trim
> + threshold as the heuristic to decide the latter. */
> + size_t usable = musable (oldmem);
> + if (bytes <= usable
> + && (unsigned long) (usable - bytes) <= mp_.trim_threshold)
> + return oldmem;
Ok.
> diff --git a/malloc/tst-realloc.c b/malloc/tst-realloc.c
> +#include <stdint.h>
Ok.
>
> + /* Smoke test to make sure that allocations do not move if they have enough
> + space to expand in the chunk. */
> + for (size_t sz = 3; sz < 256 * 1024; sz += 2048)
> + {
> + p = realloc (NULL, sz);
size 3, 2051, 4099... always 3 bytes more than a 2048-boundary
> + if (p == NULL)
> + FAIL_EXIT1 ("realloc (NULL, %zu) returned NULL.", sz);
> + size_t newsz = malloc_usable_size (p);
Ok.
> + printf ("size: %zu, usable size: %zu, extra: %zu\n",
> + sz, newsz, newsz - sz);
> + uintptr_t oldp = (uintptr_t) p;
> + void *new_p = realloc (p, newsz);
Should always work; either we're within a few words, or within a page
(mmap). Ok.
> + if ((uintptr_t) new_p != oldp)
> + FAIL_EXIT1 ("Expanding (%zu bytes) to usable size (%zu) moved block",
> + sz, newsz);
> + free (new_p);
> +
> + /* We encountered a large enough extra size at least once. */
> + if (newsz - sz > 1024)
> + break;
Ok.
> +
> return 0;
> }
prev parent reply other threads:[~2022-12-06 22:33 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-11-25 20:09 [PATCH] " Siddhesh Poyarekar
2022-11-25 20:40 ` Andreas Schwab
2022-11-28 14:23 ` Siddhesh Poyarekar
2022-11-28 14:29 ` Andreas Schwab
2022-11-28 17:26 ` [PATCH v2] " Siddhesh Poyarekar
2022-12-06 22:33 ` DJ Delorie [this message]
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=xn7cz45fiy.fsf@greed.delorie.com \
--to=dj@redhat.com \
--cc=libc-alpha@sourceware.org \
--cc=siddhesh@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: 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).