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/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 5d8d5bfd73 libc/libfread: Use memcpy to copy read-ahead buffer to 
caller buffer.
5d8d5bfd73 is described below

commit 5d8d5bfd73c97610626dccbe40e74e007b2dcc9f
Author: Stuart Ianna <[email protected]>
AuthorDate: Wed Aug 23 14:49:22 2023 +1000

    libc/libfread: Use memcpy to copy read-ahead buffer to caller buffer.
    
    This change improves the performance when using larger 
CONFIG_STDIO_BUFFER_SIZE values as it allows architecture specific, or 
optimized memcpy algorithms to be utilized.
    
    Link: https://git.motec.com.au/id/I317dc4da266aed1ce0f1b5f9a609759e863b9005
---
 libs/libc/stdio/lib_libfread.c | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)

diff --git a/libs/libc/stdio/lib_libfread.c b/libs/libc/stdio/lib_libfread.c
index 44d703cfad..1633b3ad9c 100644
--- a/libs/libc/stdio/lib_libfread.c
+++ b/libs/libc/stdio/lib_libfread.c
@@ -50,6 +50,7 @@ ssize_t lib_fread(FAR void *ptr, size_t count, FAR FILE 
*stream)
   ssize_t bytes_read;
   size_t remaining = count;
 #ifndef CONFIG_STDIO_DISABLE_BUFFERING
+  size_t gulp_size;
   int ret;
 #endif
 
@@ -121,12 +122,25 @@ ssize_t lib_fread(FAR void *ptr, size_t count, FAR FILE 
*stream)
             {
               /* Is there readable data in the buffer? */
 
-              while (remaining > 0 && stream->fs_bufpos < stream->fs_bufread)
+              gulp_size = stream->fs_bufread - stream->fs_bufpos;
+
+              /* Avoid empty buffers or read requests greater than the size
+               *  buffer remaining
+               */
+
+              if (gulp_size > 0)
                 {
-                  /* Yes, copy a byte into the user buffer */
+                  if (gulp_size > count)
+                    {
+                      /* Clip the gulp size to the requested byte count */
+
+                      gulp_size = count;
+                    }
+
+                  memcpy(dest, stream->fs_bufpos, gulp_size);
 
-                  *dest++ = *stream->fs_bufpos++;
-                  remaining--;
+                  remaining -= gulp_size;
+                  stream->fs_bufpos += gulp_size;
                 }
 
               /* The buffer is empty OR we have already supplied the number

Reply via email to