From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-Status: No, score=-3.9 required=3.0 tests=AWL,BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS shortcircuit=no autolearn=unavailable autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id A75A21F4B4 for ; Mon, 28 Dec 2020 14:00:17 +0000 (UTC) Received: from localhost ([::1]:48572 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ktt4K-0001le-MK for normalperson@yhbt.net; Mon, 28 Dec 2020 09:00:16 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:34184) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ktt3z-0001kZ-U0 for bug-gnulib@gnu.org; Mon, 28 Dec 2020 08:59:56 -0500 Received: from mail-qv1-xf2e.google.com ([2607:f8b0:4864:20::f2e]:32837) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ktt3y-0003BA-3Q for bug-gnulib@gnu.org; Mon, 28 Dec 2020 08:59:55 -0500 Received: by mail-qv1-xf2e.google.com with SMTP id a13so4966194qvv.0 for ; Mon, 28 Dec 2020 05:59:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=u8hGdF6dpmXVclcmm3bEdMe3tgFRvcQtcTiW2ZTROXw=; b=yRWNGC7wUAVajqrPnpxohFfrVpU7hIMBMsAC7sbtwqbnYOiClMd/QkNbXUF1jMmjBp /6Qku7qWozq0JEwcWoFgJKGEzWIf9z4Sv88J5y1Drgclo5zRer7GmtRjTCJK8iJNkoMb iNY3KnN2VK/Z78ax81JMn3dKSg8GXjvPEAw1aL6XvGCl4rktRpfE0lcG6Ca7ebhy/chr cWaxpyHUR0V1T9pTTFlQMIIKmZ9QPrWsEYhA5Wc4JNdxVGK4Ewi+jtLWB9pnYqGOOHou hGMtelKi0ejxxscMgqFi1IroBKF/rL+BPcwACWEfqKHb1tKmatlVfyFbNhsR3OeHbJxc uZmA== 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=u8hGdF6dpmXVclcmm3bEdMe3tgFRvcQtcTiW2ZTROXw=; b=itDq723u3KEYLA/IRzQfN72bsE2wNMgLkYQlChBSTvNHMucru1qdi9JkBqY3d6MnR7 RR6/6mP8rJmYYKBtgEU1LnJowzCZw/eWTGhfYmiP9oT2R9ohbeu4V8bQ4GBP5vOG14j4 A63z5MVk2NcxhDOIW8p5245y/NAdjXF6s54k36DOHFilDwU+ychUydfQpI/rWKtVc0GV zFz+l2ROoLJZ5h6eVL7CK21osQsdbI9emuD7m3vrMQhH3SKws6AIbBA0cCyggoejv66d lRaKI+X7sFI0wFbh6yoK9NMbEXJSJtCLWXadYpZC+x/tfX2Dl6mr4GZW7QJ3hBtlQHAo KaxA== X-Gm-Message-State: AOAM531WsEjnDr1AWpIgrrAjdKd8lbQLQdENrd7cWbGPuhbY2DQDhZ9z n/UASKGm6zyWp+92E73Ujrvj+Q== X-Google-Smtp-Source: ABdhPJz2+i0Z6+VBv0pv+F7fwJvQB18eU/UdsTNbWiS1jV3q6lfGRBr0pc1a38IsEIqtt5YQHD33Iw== X-Received: by 2002:a05:6214:a14:: with SMTP id dw20mr46558075qvb.43.1609163993063; Mon, 28 Dec 2020 05:59:53 -0800 (PST) Received: from localhost.localdomain ([177.194.48.209]) by smtp.googlemail.com with ESMTPSA id i17sm24243744qtg.77.2020.12.28.05.59.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Dec 2020 05:59:52 -0800 (PST) From: Adhemerval Zanella To: libc-alpha@sourceware.org, Paul Eggert Subject: [PATCH v2 3/6] malloc: Add scratch_buffer_dupfree Date: Mon, 28 Dec 2020 10:59:41 -0300 Message-Id: <20201228135944.1018303-3-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201228135944.1018303-1-adhemerval.zanella@linaro.org> References: <20201228135944.1018303-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2607:f8b0:4864:20::f2e; envelope-from=adhemerval.zanella@linaro.org; helo=mail-qv1-xf2e.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: bug-gnulib@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Gnulib discussion list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: bug-gnulib@gnu.org Errors-To: bug-gnulib-bounces+normalperson=yhbt.net@gnu.org Sender: "bug-gnulib" 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 ab64dcfd73..f0ec3dae6b 100644 --- a/malloc/Makefile +++ b/malloc/Makefile @@ -63,6 +63,7 @@ tests += $(tests-static) test-srcs = tst-mtrace 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 -- 2.25.1