From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id CC744382D3E3 for ; Tue, 6 Dec 2022 22:33:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org CC744382D3E3 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1670366003; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to; bh=oqVEJOKaxEgJCzUIRmSKmAkNV92+rtK619zDyzYfj4Q=; b=XNIFtGnNbu2+yptR9Ml6jcs75chx9BFGfZQacl6YEXDvuRlGTuQNlAefn2UeFIwcoJhkas iZYU2E/TNCgzZLqxmv3jfNT+/2hbRjGLuzqljYyJ2b44aDLzhaU2fd8YET+qTczTiWEn7n I3jNlERtY+vROx2TZmv5tO9wLm7MF8s= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-365-9t-E7GXwO3eN2VJF-kriJw-1; Tue, 06 Dec 2022 17:33:20 -0500 X-MC-Unique: 9t-E7GXwO3eN2VJF-kriJw-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D819A3C0D199; Tue, 6 Dec 2022 22:33:19 +0000 (UTC) Received: from greed.delorie.com (unknown [10.22.8.183]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C3BD3C15BA4; Tue, 6 Dec 2022 22:33:19 +0000 (UTC) Received: from greed.delorie.com.redhat.com (localhost [127.0.0.1]) by greed.delorie.com (8.15.2/8.15.2) with ESMTP id 2B6MX98m1865956; Tue, 6 Dec 2022 17:33:09 -0500 From: DJ Delorie To: Siddhesh Poyarekar Cc: libc-alpha@sourceware.org Subject: Re: [PATCH v2] realloc: Return unchanged if request is within usable size In-Reply-To: <20221128172646.244742-1-siddhesh@sourceware.org> Date: Tue, 06 Dec 2022 17:33:09 -0500 Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain X-Spam-Status: No, score=-10.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Siddhesh Poyarekar via Libc-alpha writes: > Signed-off-by: Siddhesh Poyarekar LGTM Reviewed-by: DJ Delorie > 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 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; > }