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 [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id ACFA03851C26 for ; Tue, 15 Dec 2020 18:43:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org ACFA03851C26 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-425-xQDHNMvNObu5RC3bo9YYdQ-1; Tue, 15 Dec 2020 13:43:24 -0500 X-MC-Unique: xQDHNMvNObu5RC3bo9YYdQ-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id DE3B66D520; Tue, 15 Dec 2020 18:43:22 +0000 (UTC) Received: from localhost (unknown [10.33.36.115]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7BC585D6A1; Tue, 15 Dec 2020 18:43:22 +0000 (UTC) Date: Tue, 15 Dec 2020 18:43:21 +0000 From: Jonathan Wakely To: Keith Packard Cc: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: Re: [PATCH 1/2] libstdc++: Add --enable-stdio=stdio_pure option [v2] Message-ID: <20201215184321.GN2309743@redhat.com> References: <20201209101700.GS2309743@redhat.com> <20201210024657.3395687-1-keithp@keithp.com> <20201210024657.3395687-2-keithp@keithp.com> <20201210202306.GD2309743@redhat.com> <87tust9y80.fsf@keithp.com> MIME-Version: 1.0 In-Reply-To: <87tust9y80.fsf@keithp.com> X-Clacks-Overhead: GNU Terry Pratchett X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: multipart/mixed; boundary="eQyCKlb8USywWNtC" Content-Disposition: inline X-Spam-Status: No, score=-13.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_H3, RCVD_IN_MSPIKE_WL, 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: libstdc++@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++ mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 15 Dec 2020 18:43:28 -0000 --eQyCKlb8USywWNtC Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline On 10/12/20 12:56 -0800, Keith Packard via Libstdc++ wrote: >Jonathan Wakely writes: > >> I'll do a bit more testing and push it next week. > >That's awesome news. Thanks so much for you help; I'm looking forward to >having real C++ support for my embedded customers! Pushed to master. --eQyCKlb8USywWNtC Content-Type: text/x-patch; charset=us-ascii Content-Disposition: attachment; filename="patch.txt" commit 75aee072696a711f3e5b3bd1ab1f2b10fef4c7dd Author: Keith Packard Date: Tue Dec 15 17:39:24 2020 libstdc++: Support libc with stdio-only I/O in libstdc++ The current libstdc++ basic_file_stdio.cc code assumes a POSIX API underneath the stdio implementation provided by the host libc. This means that the host must provide a fairly broad POSIX file API, including read, write, open, close, lseek and ioctl. This patch changes basic_file_stdio.cc to only use basic ANSI-C stdio functions, allowing it to be used with libc implementations like picolibc which may not have a POSIX operating system underneath. This is enabled by a new --enable-cstdio=stdio_pure configure option. Aided-by: Jonathan Wakely Signed-off-by: Keith Packard libstdc++-v3/ChangeLog: * acinclude.m4 (GLIBCXX_ENABLE_CSTDIO): Allow "stdio_pure" option and define _GLIBCXX_USE_PURE_STDIO when it is used. Also add "stdio_posix" option as an alias for "stdio". * config/io/basic_file_stdio.cc [_GLIBCXX_USE_PURE_STDIO]: Only use defined stdio entry points for all I/O operations, without direct calls to underlying POSIX functions. * config.h.in: Regenerate. * configure: Regenerate. diff --git a/libstdc++-v3/acinclude.m4 b/libstdc++-v3/acinclude.m4 index 61191812c92..df8be3bf805 100644 --- a/libstdc++-v3/acinclude.m4 +++ b/libstdc++-v3/acinclude.m4 @@ -2868,24 +2868,30 @@ AC_DEFUN([GLIBCXX_ENABLE_PARALLEL], [ dnl -dnl Check for which I/O library to use: stdio, or something specific. +dnl Check for which I/O library to use: stdio and POSIX, or pure stdio. dnl -dnl Default is stdio. +dnl Default is stdio_posix. dnl AC_DEFUN([GLIBCXX_ENABLE_CSTDIO], [ AC_MSG_CHECKING([for underlying I/O to use]) GLIBCXX_ENABLE(cstdio,stdio,[[[=PACKAGE]]], - [use target-specific I/O package], [permit stdio]) + [use target-specific I/O package], [permit stdio|stdio_posix|stdio_pure]) - # Now that libio has been removed, you can have any color you want as long - # as it's black. This is one big no-op until other packages are added, but - # showing the framework never hurts. + # The only available I/O model is based on stdio, via basic_file_stdio. + # The default "stdio" is actually "stdio + POSIX" because it uses fdopen(3) + # to get a file descriptor and then uses read(3) and write(3) with it. + # The "stdio_pure" model doesn't use fdopen and only uses FILE* for I/O. case ${enable_cstdio} in - stdio) + stdio*) CSTDIO_H=config/io/c_io_stdio.h BASIC_FILE_H=config/io/basic_file_stdio.h BASIC_FILE_CC=config/io/basic_file_stdio.cc AC_MSG_RESULT(stdio) + + if test "x$enable_cstdio" = "xstdio_pure" ; then + AC_DEFINE(_GLIBCXX_USE_STDIO_PURE, 1, + [Define to restrict std::__basic_file<> to stdio APIs.]) + fi ;; esac diff --git a/libstdc++-v3/config/io/basic_file_stdio.cc b/libstdc++-v3/config/io/basic_file_stdio.cc index ba830fb9e97..eedffb017b6 100644 --- a/libstdc++-v3/config/io/basic_file_stdio.cc +++ b/libstdc++-v3/config/io/basic_file_stdio.cc @@ -111,13 +111,21 @@ namespace // Wrapper handling partial write. static std::streamsize +#ifdef _GLIBCXX_USE_STDIO_PURE + xwrite(FILE *__file, const char* __s, std::streamsize __n) +#else xwrite(int __fd, const char* __s, std::streamsize __n) +#endif { std::streamsize __nleft = __n; for (;;) { +#ifdef _GLIBCXX_USE_STDIO_PURE + const std::streamsize __ret = fwrite(__file, 1, __nleft, __file); +#else const std::streamsize __ret = write(__fd, __s, __nleft); +#endif if (__ret == -1L && errno == EINTR) continue; if (__ret == -1L) @@ -133,7 +141,7 @@ namespace return __n - __nleft; } -#ifdef _GLIBCXX_HAVE_WRITEV +#if defined(_GLIBCXX_HAVE_WRITEV) && !defined(_GLIBCXX_USE_STDIO_PURE) // Wrapper handling partial writev. static std::streamsize xwritev(int __fd, const char* __s1, std::streamsize __n1, @@ -286,9 +294,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __basic_file::is_open() const throw () { return _M_cfile != 0; } +#ifndef _GLIBCCXX_USE_STDIO_PURE int __basic_file::fd() throw () { return fileno(_M_cfile); } +#endif __c_file* __basic_file::file() throw () @@ -315,28 +325,46 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { streamsize __ret; do +#ifdef _GLIBCXX_USE_STDIO_PURE + __ret = fread(__s, 1, __n, this->file()); +#else __ret = read(this->fd(), __s, __n); +#endif while (__ret == -1L && errno == EINTR); return __ret; } streamsize __basic_file::xsputn(const char* __s, streamsize __n) - { return xwrite(this->fd(), __s, __n); } + { +#ifdef _GLIBCXX_USE_STDIO_PURE + return xwrite(this->file(), __s, __n); +#else + return xwrite(this->fd(), __s, __n); +#endif + } streamsize __basic_file::xsputn_2(const char* __s1, streamsize __n1, const char* __s2, streamsize __n2) { streamsize __ret = 0; -#ifdef _GLIBCXX_HAVE_WRITEV +#if defined(_GLIBCXX_HAVE_WRITEV) && !defined(_GLIBCXX_USE_STDIO_PURE) __ret = xwritev(this->fd(), __s1, __n1, __s2, __n2); #else if (__n1) +#ifdef _GLIBCXX_USE_STDIO_PURE + __ret = xwrite(this->file(), __s1, __n1); +#else __ret = xwrite(this->fd(), __s1, __n1); +#endif if (__ret == __n1) +#ifdef _GLIBCXX_USE_STDIO_PURE + __ret += xwrite(this->file(), __s2, __n2); +#else __ret += xwrite(this->fd(), __s2, __n2); +#endif #endif return __ret; } @@ -350,7 +378,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION if (__off > numeric_limits::max() || __off < numeric_limits::min()) return -1L; +#ifdef _GLIBCXX_USE_STDIO_PURE + return fseek(this->file(), __off, __way); +#else return lseek(this->fd(), __off, __way); +#endif #endif } @@ -361,7 +393,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION streamsize __basic_file::showmanyc() { -#ifndef _GLIBCXX_NO_IOCTL +#if !defined(_GLIBCXX_NO_IOCTL) && !defined(_GLIBCXX_USE_STDIO_PURE) #ifdef FIONREAD // Pipes and sockets. int __num = 0; @@ -371,7 +403,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION #endif #endif -#ifdef _GLIBCXX_HAVE_POLL +#if defined(_GLIBCXX_HAVE_POLL) && !defined(_GLIBCXX_USE_STDIO_PURE) // Cheap test. struct pollfd __pfd[1]; __pfd[0].fd = this->fd(); @@ -395,8 +427,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION struct stat __buffer; const int __err = fstat(this->fd(), &__buffer); if (!__err && _GLIBCXX_ISREG(__buffer.st_mode)) +#ifdef _GLIBCXX_USE_STDIO_PURE + return __buffer.st_size - fseek(this->file(), 0, ios_base::cur); +#else return __buffer.st_size - lseek(this->fd(), 0, ios_base::cur); #endif +#endif #endif return 0; } --eQyCKlb8USywWNtC--