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
This commit is contained in:
Stuart Ianna 2023-08-23 14:49:22 +10:00 committed by Xiang Xiao
parent 24e45d071e
commit 5d8d5bfd73
1 changed files with 19 additions and 5 deletions

View File

@ -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)
{
/* Yes, copy a byte into the user buffer */
gulp_size = stream->fs_bufread - stream->fs_bufpos;
*dest++ = *stream->fs_bufpos++;
remaining--;
/* Avoid empty buffers or read requests greater than the size
* buffer remaining
*/
if (gulp_size > 0)
{
if (gulp_size > count)
{
/* Clip the gulp size to the requested byte count */
gulp_size = count;
}
memcpy(dest, stream->fs_bufpos, gulp_size);
remaining -= gulp_size;
stream->fs_bufpos += gulp_size;
}
/* The buffer is empty OR we have already supplied the number