From patchwork Tue Dec 29 19:34:51 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 1421240 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=libc-alpha-bounces@sourceware.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=sourceware.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=UPtMu/pE; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4D54Q2459kz9sVb for ; Wed, 30 Dec 2020 06:35:22 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 827E4388A43E; Tue, 29 Dec 2020 19:35:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 827E4388A43E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1609270508; bh=BRTobmsW2yTX1HG5dDOjq/RbHRadsEpcXdG/dendWt4=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=UPtMu/pE/Aez+eQktmM6D+eaQb7dQNcn/rZvkhhSOI/Szn8M1lcWrL+ZgnRPJDBbT FD7UXjWKpa+lu6uK5IijhgOesR5wQtmljO5Ry6mTGTKpHRdP0qoIZxHq9O+AQnLo5N imVdOBchNRa2ewh7pu6qjxEyH7p+RIKnzgFiVOcs= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qv1-xf34.google.com (mail-qv1-xf34.google.com [IPv6:2607:f8b0:4864:20::f34]) by sourceware.org (Postfix) with ESMTPS id BB2CA388A426 for ; Tue, 29 Dec 2020 19:35:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org BB2CA388A426 Received: by mail-qv1-xf34.google.com with SMTP id d11so6779092qvo.11 for ; Tue, 29 Dec 2020 11:35:05 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=BRTobmsW2yTX1HG5dDOjq/RbHRadsEpcXdG/dendWt4=; b=EdL1K7NfoJyccttUWcoWcbIhhY3F0pmF+J1mblUI2dwma6xCo01ax4jqH7lWIs/tLA o5NnUC+arOkVIrLEt8MTOa9gl8QDrfZqca345c6UUO48I4Uz5I3MAAd0oy7SYC7DBFpd EwmGuXGgssBGD0c9fhY1nPo5gvH0eMVXvdTN0hPVBHqsDWe6jviZkrk2+AuTWpBWShBh Qp18s1HvhoNNbrrgHXTaQ1QO+blPOyApgPyqz/akw1y5v0KMYtWXsiKLwkry6XW4vv+8 Dd1eyEd+U3awVaWGGo/j17Pq3bZEYdHbXMWcoIzqiwN0gSKMYCcTaDwnfNgcgQ9YRqb2 Re5g== X-Gm-Message-State: AOAM531xibS90KRJ+gKHzhKUlW2Pf8sCPIap499KQSl8rThlFa1zbPYi WVbw3zfQ6LS7LVo1mJ1QtwBVcKQ/JG94bg== X-Google-Smtp-Source: ABdhPJyw9A0WyRe3pNhD+9jDcn6ZS18XOUjIdK9egbgAgtboFpAXawLZp6p9CgyroywDy/BExB4Imw== X-Received: by 2002:a0c:efc9:: with SMTP id a9mr52921474qvt.31.1609270505075; Tue, 29 Dec 2020 11:35:05 -0800 (PST) Received: from localhost.localdomain ([177.194.48.209]) by smtp.googlemail.com with ESMTPSA id b14sm25383428qtx.36.2020.12.29.11.35.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Dec 2020 11:35:04 -0800 (PST) To: libc-alpha@sourceware.org, Paul Eggert Subject: [PATCH v3 3/6] malloc: Add scratch_buffer_dupfree Date: Tue, 29 Dec 2020 16:34:51 -0300 Message-Id: <20201229193454.34558-4-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201229193454.34558-1-adhemerval.zanella@linaro.org> References: <20201229193454.34558-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-13.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Cc: bug-gnulib@gnu.org Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" It returns a copy of the buffer up to a defined size. It will be used on realpath sync with gnulib. --- include/scratch_buffer.h | 16 +++++++++++++ malloc/Makefile | 1 + malloc/Versions | 1 + malloc/scratch_buffer_dupfree.c | 41 +++++++++++++++++++++++++++++++++ malloc/tst-scratch_buffer.c | 26 +++++++++++++++++++-- 5 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 malloc/scratch_buffer_dupfree.c diff --git a/include/scratch_buffer.h b/include/scratch_buffer.h index c39da78629..48d651b41a 100644 --- a/include/scratch_buffer.h +++ b/include/scratch_buffer.h @@ -132,4 +132,20 @@ scratch_buffer_set_array_size (struct scratch_buffer *buffer, (buffer, nelem, size)); } +/* Return a copy of *BUFFER's first SIZE bytes as a heap-allocated block, + deallocating *BUFFER if it was heap-allocated. SIZE must be at + most *BUFFER's size. Return NULL (setting errno) on memory + exhaustion. */ +void *__libc_scratch_buffer_dupfree (struct scratch_buffer *buffer, + size_t size); +libc_hidden_proto (__libc_scratch_buffer_dupfree) + +/* Alias for __libc_scratch_dupfree. */ +static __always_inline void * +scratch_buffer_dupfree (struct scratch_buffer *buffer, size_t size) +{ + void *r = __libc_scratch_buffer_dupfree (buffer, size); + return __glibc_likely (r != NULL) ? r : NULL; +} + #endif /* _SCRATCH_BUFFER_H */ diff --git a/malloc/Makefile b/malloc/Makefile index 39ffaa7161..ef70b547f9 100644 --- a/malloc/Makefile +++ b/malloc/Makefile @@ -74,6 +74,7 @@ tests-exclude-mcheck = tst-mcheck tst-malloc-usable \ tests-mcheck = $(filter-out $(tests-exclude-mcheck),$(tests)) routines = malloc morecore mcheck mtrace obstack reallocarray \ + scratch_buffer_dupfree \ scratch_buffer_grow scratch_buffer_grow_preserve \ scratch_buffer_set_array_size \ dynarray_at_failure \ diff --git a/malloc/Versions b/malloc/Versions index 94c8ba8040..6693c46ee2 100644 --- a/malloc/Versions +++ b/malloc/Versions @@ -75,6 +75,7 @@ libc { __libc_thread_freeres; # struct scratch_buffer support + __libc_scratch_buffer_dupfree; __libc_scratch_buffer_grow; __libc_scratch_buffer_grow_preserve; __libc_scratch_buffer_set_array_size; diff --git a/malloc/scratch_buffer_dupfree.c b/malloc/scratch_buffer_dupfree.c new file mode 100644 index 0000000000..5561e99b0a --- /dev/null +++ b/malloc/scratch_buffer_dupfree.c @@ -0,0 +1,41 @@ +/* Variable-sized buffer with on-stack default allocation. + Copyright (C) 2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#ifndef _LIBC +# include +#endif + +#include +#include + +void * +__libc_scratch_buffer_dupfree (struct scratch_buffer *buffer, size_t size) +{ + void *data = buffer->data; + if (data == buffer->__space.__c) + { + void *copy = malloc (size); + return copy != NULL ? memcpy (copy, data, size) : NULL; + } + else + { + void *copy = realloc (data, size); + return copy != NULL ? copy : data; + } +} +libc_hidden_def (__libc_scratch_buffer_dupfree) diff --git a/malloc/tst-scratch_buffer.c b/malloc/tst-scratch_buffer.c index ef5fb0a8eb..6008113174 100644 --- a/malloc/tst-scratch_buffer.c +++ b/malloc/tst-scratch_buffer.c @@ -16,7 +16,10 @@ License along with the GNU C Library; if not, see . */ +#include #include +#include +#include #include #include #include @@ -148,8 +151,27 @@ do_test (void) && array_size_must_fail (4, ((size_t)-1) / 4))) return 1; } + { + struct scratch_buffer buf; + scratch_buffer_init (&buf); + memset (buf.data, '@', buf.length); + + size_t sizes[] = { 16, buf.length, buf.length + 16 }; + for (int i = 0; i < array_length (sizes); i++) + { + /* The extra size is unitialized through realloc. */ + size_t l = sizes[i] > buf.length ? sizes[i] : buf.length; + void *r = scratch_buffer_dupfree (&buf, l); + void *c = xmalloc (l); + memset (c, '@', l); + TEST_COMPARE_BLOB (r, l, buf.data, l); + free (r); + free (c); + } + + scratch_buffer_free (&buf); + } return 0; } -#define TEST_FUNCTION do_test () -#include "../test-skeleton.c" +#include