From patchwork Fri Feb 9 15:25:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 85532 X-Patchwork-Delegate: azanella@linux.vnet.ibm.com Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5E461385802E for ; Fri, 9 Feb 2024 15:27:07 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org 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 4EC9D385829E for ; Fri, 9 Feb 2024 15:25:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4EC9D385829E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 4EC9D385829E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707492345; cv=none; b=vadn76mSfPoINLRoO13DCrY0gvsWYYeEpxEeue99i/H/hFzTBINyaT7xcK7ziEVseJX9djlkJ9nFW5qCkcQipzDyPP4YayunpTWJZRpJyD2kiK7JQPbrV6jvPA7BmXO3B0fgkqvFkEvYm2/fW0kyq0Nffz5ivkyoRqJFuiv/LuI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707492345; c=relaxed/simple; bh=nFLvjHXYiS8ceIj0M1yUMgFIosQ78kkjB6Q5KAAmWGY=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=aAdpAn7WGgCWSvfXjJnyC4tUguSpLTj3Wf9F/0wPCIGsRFP3c8O3+WiKObKGdT8EqcFTeK5W69VNc/BES7OV3RlEI5fCKI/sxhUZ+mhXtSx7+hcpbVTaH4PcJ19rw2As0jHsXum0Gj0fvSP7K8XY2YgCw7H/8awmYrHPf8sVHSs= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707492342; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=AKOfd7Roz9dgnDRT4eNNSjdepnfuGDPoh6MHXAjgMK0=; b=GQGFcHvTnj/x4I8d/0rRK4j7F0hvPrJOmgDJVP1+7Gp2kq6MN+7ZAEymM6RbFKEnb7FimA IVyLTkf5kpQhElHPbsCDy1O2ttK2zkN4dBaIFvZInQnETY0AJ+DcXclPO5Jesj/tXN4z1P z5oS7JreiIb+jdUoI519Mg05j4PzZrg= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-265-G3UYzYEGPa6rlwS-RvKatA-1; Fri, 09 Feb 2024 10:25:40 -0500 X-MC-Unique: G3UYzYEGPa6rlwS-RvKatA-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 6CD4F85A58C for ; Fri, 9 Feb 2024 15:25:40 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.39.192.58]) by smtp.corp.redhat.com (Postfix) with ESMTPS id DDBAD1103A for ; Fri, 9 Feb 2024 15:25:39 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH 07/11] libio: Extract __printf_buffer_asprintf_init from asprintf implementation In-Reply-To: Message-ID: <345c3f94450a38be9408f158a211c82a657034b1.1707491940.git.fweimer@redhat.com> References: X-From-Line: 345c3f94450a38be9408f158a211c82a657034b1 Mon Sep 17 00:00:00 2001 Date: Fri, 09 Feb 2024 16:25:38 +0100 User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.3 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.7 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, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org And __printf_buffer_asprintf_free as well. This will allow to reuse the asprintf implementation for more general buffer handling, similar to open_memstream, but without the mandatory heap allocation. Reviewed-by: Adhemerval Zanella --- include/printf_buffer.h | 30 +++++++++++++++++++++++++----- libio/vasprintf.c | 40 +++++++++++++++++----------------------- 2 files changed, 42 insertions(+), 28 deletions(-) diff --git a/include/printf_buffer.h b/include/printf_buffer.h index fb0b42178e..cee5afb581 100644 --- a/include/printf_buffer.h +++ b/include/printf_buffer.h @@ -317,6 +317,31 @@ void __printf_buffer_snprintf_init (struct __printf_buffer_snprintf *, int __printf_buffer_snprintf_done (struct __printf_buffer_snprintf *) attribute_hidden; +/* Size of the initial on-stack buffer for asprintf. It should be + large enough to copy almost all asprintf usages with just a single + (final, correctly sized) heap allocation. */ +#define PRINTF_BUFFER_SIZE_ASPRINTF 200 + +struct __printf_buffer_asprintf +{ + /* base.write_base points either to a heap-allocated buffer, or to + the direct array below. */ + struct __printf_buffer base; + + /* Initial allocation. */ + char direct[PRINTF_BUFFER_SIZE_ASPRINTF]; +}; + +/* Sets up BUF for writing via __printf_buffer. */ +void __printf_buffer_asprintf_init (struct __printf_buffer_asprintf *buf) + attribute_hidden; + +/* Deallocates any allocated memory in *BUF. (This is not the usual + done routine for asprintf because that has to preserve allocation.) + Always returns -1 to indicate an error. */ +int __printf_buffer_asprintf_free (struct __printf_buffer_asprintf *buf) + attribute_hidden; + /* Flush function implementations follow. They are called from __printf_buffer_flush. Generic code should not call these flush functions directly. Some modes have inline implementations. */ @@ -363,11 +388,6 @@ void __wprintf_buffer_flush_to_file (struct __wprintf_buffer_to_file *) /* Temporary buffer used during floating point digit translation. */ #define PRINTF_BUFFER_SIZE_DIGITS 64 -/* Size of the initial on-stack buffer for asprintf. It should be - large enough to copy almost all asprintf usages with just a single - (final, correctly sized) heap allocation. */ -#define PRINTF_BUFFER_SIZE_ASPRINTF 200 - /* This should cover most of the packet-oriented file descriptors, where boundaries between writes could be visible to readers. But it is still small enough not to cause too many stack overflow issues. */ diff --git a/libio/vasprintf.c b/libio/vasprintf.c index edcfab2323..f8413eedfe 100644 --- a/libio/vasprintf.c +++ b/libio/vasprintf.c @@ -34,18 +34,6 @@ #include #include -struct __printf_buffer_asprintf -{ - /* base.write_base points either to a heap-allocated buffer, or to - the direct array below. */ - struct __printf_buffer base; - - /* Initial allocation. 200 should be large enough to copy almost - all asprintf usages with just a single (final, correctly sized) - heap allocation. */ - char direct[PRINTF_BUFFER_SIZE_ASPRINTF]; -}; - void __printf_buffer_flush_asprintf (struct __printf_buffer_asprintf *buf) { @@ -90,23 +78,32 @@ __printf_buffer_flush_asprintf (struct __printf_buffer_asprintf *buf) buf->base.write_end = new_buffer + new_size; } +void +__printf_buffer_asprintf_init (struct __printf_buffer_asprintf *buf) +{ + __printf_buffer_init (&buf->base, buf->direct, array_length (buf->direct), + __printf_buffer_mode_asprintf); +} + +int +__printf_buffer_asprintf_free (struct __printf_buffer_asprintf *buf) +{ + if (buf->base.write_base != buf->direct) + free (buf->base.write_base); + return -1; +} int __vasprintf_internal (char **result_ptr, const char *format, va_list args, unsigned int mode_flags) { struct __printf_buffer_asprintf buf; - __printf_buffer_init (&buf.base, buf.direct, array_length (buf.direct), - __printf_buffer_mode_asprintf); + __printf_buffer_asprintf_init (&buf); __vprintf_buffer (&buf.base, format, args, mode_flags); int done = __printf_buffer_done (&buf.base); if (done < 0) - { - if (buf.base.write_base != buf.direct) - free (buf.base.write_base); - return done; - } + return __printf_buffer_asprintf_free (&buf); /* Transfer to the final buffer. */ char *result; @@ -122,10 +119,7 @@ __vasprintf_internal (char **result_ptr, const char *format, va_list args, { result = realloc (buf.base.write_base, size + 1); if (result == NULL) - { - free (buf.base.write_base); - return -1; - } + return __printf_buffer_asprintf_free (&buf); } /* Add NUL termination. */