You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2022/06/07 14:28:06 UTC
[incubator-nuttx] branch master updated: libc:Optimize the behavior of fwrite
This is an automated email from the ASF dual-hosted git repository.
xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new e0c3bf64a0 libc:Optimize the behavior of fwrite
e0c3bf64a0 is described below
commit e0c3bf64a004c556207ca674904c62535ba9924b
Author: anjiahao <an...@xiaomi.com>
AuthorDate: Thu Jun 2 23:53:14 2022 +0800
libc:Optimize the behavior of fwrite
Signed-off-by: anjiahao <an...@xiaomi.com>
---
libs/libc/stdio/lib_libfwrite.c | 47 +++++++++++++++++++++++------------------
1 file changed, 27 insertions(+), 20 deletions(-)
diff --git a/libs/libc/stdio/lib_libfwrite.c b/libs/libc/stdio/lib_libfwrite.c
index 8e4a872250..1619396488 100644
--- a/libs/libc/stdio/lib_libfwrite.c
+++ b/libs/libc/stdio/lib_libfwrite.c
@@ -49,7 +49,7 @@ ssize_t lib_fwrite(FAR const void *ptr, size_t count, FAR FILE *stream)
FAR const unsigned char *start = ptr;
FAR const unsigned char *src = ptr;
ssize_t ret = ERROR;
- unsigned char *dest;
+ size_t gulp_size;
/* Make sure that writing to this stream is allowed */
@@ -72,13 +72,11 @@ ssize_t lib_fwrite(FAR const void *ptr, size_t count, FAR FILE *stream)
if (stream->fs_bufstart == NULL)
{
ret = _NX_WRITE(stream->fs_fd, ptr, count);
-#if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__)
if (ret < 0)
{
_NX_SETERRNO(ret);
ret = ERROR;
}
-#endif
goto errout;
}
@@ -97,18 +95,11 @@ ssize_t lib_fwrite(FAR const void *ptr, size_t count, FAR FILE *stream)
goto errout_with_semaphore;
}
- /* Loop until all of the bytes have been buffered */
+ /* Determine the number of bytes left in the buffer */
- while (count > 0)
+ gulp_size = stream->fs_bufend - stream->fs_bufpos;
+ if (gulp_size != CONFIG_STDIO_BUFFER_SIZE || count < gulp_size)
{
- /* Determine the number of bytes left in the buffer */
-
- size_t gulp_size = stream->fs_bufend - stream->fs_bufpos;
-
- /* Will the user data fit into the amount of buffer space
- * that we have left?
- */
-
if (gulp_size > count)
{
/* Yes, clip the gulp to the size of the user data */
@@ -124,16 +115,13 @@ ssize_t lib_fwrite(FAR const void *ptr, size_t count, FAR FILE *stream)
/* Transfer the data into the buffer */
- for (dest = stream->fs_bufpos; gulp_size > 0; gulp_size--)
- {
- *dest++ = *src++;
- }
-
- stream->fs_bufpos = dest;
+ memcpy(stream->fs_bufpos, src, gulp_size);
+ stream->fs_bufpos += gulp_size;
+ src += gulp_size;
/* Is the buffer full? */
- if (dest >= stream->fs_bufend)
+ if (stream->fs_bufpos >= stream->fs_bufend)
{
/* Flush the buffered data to the IO stream */
@@ -145,6 +133,25 @@ ssize_t lib_fwrite(FAR const void *ptr, size_t count, FAR FILE *stream)
}
}
+ if (count >= CONFIG_STDIO_BUFFER_SIZE)
+ {
+ ret = _NX_WRITE(stream->fs_fd, src, count);
+ if (ret < 0)
+ {
+ _NX_SETERRNO(ret);
+ ret = ERROR;
+ goto errout_with_semaphore;
+ }
+
+ src += ret;
+ }
+ else if (count > 0)
+ {
+ memcpy(stream->fs_bufpos, src, count);
+ stream->fs_bufpos += count;
+ src += count;
+ }
+
/* Return the number of bytes written */
ret = (uintptr_t)src - (uintptr_t)start;