libs/modlib: avoid seeking in each reading

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
This commit is contained in:
dongjiuzhu1 2023-09-08 21:06:33 +08:00 committed by Xiang Xiao
parent 13bdaaec99
commit 310ba09ed7

View File

@ -50,23 +50,7 @@
****************************************************************************/ ****************************************************************************/
#ifdef ELF_DUMP_READDATA #ifdef ELF_DUMP_READDATA
static inline void modlib_dumpreaddata(FAR char *buffer, size_t buflen) # define modlib_dumpreaddata(b,n) binfodumpbuffer("modlib_read",b,n)
{
FAR uint32_t *buf32 = (FAR uint32_t *)buffer;
size_t i;
size_t j;
for (i = 0; i < buflen; i += 32)
{
syslog(LOG_DEBUG, "%04zx:", i);
for (j = 0; j < 32; j += sizeof(uint32_t))
{
syslog(LOG_DEBUG, " %08" PRIx32, *buf32++);
}
syslog(LOG_DEBUG, "\n");
}
}
#else #else
# define modlib_dumpreaddata(b,n) # define modlib_dumpreaddata(b,n)
#endif #endif
@ -94,38 +78,39 @@ int modlib_read(FAR struct mod_loadinfo_s *loadinfo, FAR uint8_t *buffer,
size_t nsize = readsize; size_t nsize = readsize;
ssize_t nbytes; /* Number of bytes read */ ssize_t nbytes; /* Number of bytes read */
off_t rpos; /* Position returned by lseek */ off_t rpos; /* Position returned by lseek */
int errval;
binfo("Read %zu bytes from offset %" PRIdOFF "\n", readsize, offset); binfo("Read %zu bytes from offset %" PRIdOFF "\n", readsize, offset);
/* Loop until all of the requested data has been read. */ /* Loop until all of the requested data has been read. */
/* Seek to the read position */
rpos = _NX_SEEK(loadinfo->filfd, offset, SEEK_SET);
if (rpos != offset)
{
errval = _NX_GETERRNO(rpos);
berr("ERROR: Failed to seek to position %" PRIdOFF ": %d\n",
offset, errval);
return -errval;
}
while (readsize > 0) while (readsize > 0)
{ {
/* Seek to the next read position */
rpos = _NX_SEEK(loadinfo->filfd, offset, SEEK_SET);
if (rpos != offset)
{
int errval = _NX_GETERRNO(rpos);
berr("ERROR: Failed to seek to position %" PRIdOFF ": %d\n",
offset, errval);
return -errval;
}
/* Read the file data at offset into the user buffer */ /* Read the file data at offset into the user buffer */
nbytes = _NX_READ(loadinfo->filfd, nbytes = _NX_READ(loadinfo->filfd,
buffer + nsize - readsize, readsize); buffer + nsize - readsize, readsize);
if (nbytes < 0) if (nbytes < 0)
{ {
int errval = _NX_GETERRNO(nbytes); errval = _NX_GETERRNO(nbytes);
/* EINTR just means that we received a signal */ /* EINTR just means that we received a signal */
if (errval != EINTR) if (errval != EINTR)
{ {
berr("ERROR: Read from offset %" PRIdOFF " failed: %d\n", berr("ERROR: Read from offset %" PRIdOFF " failed: %d\n",
offset, errval); (off_t)(offset + nsize - readsize), errval);
return -errval; return -errval;
} }
} }
@ -137,7 +122,6 @@ int modlib_read(FAR struct mod_loadinfo_s *loadinfo, FAR uint8_t *buffer,
else else
{ {
readsize -= nbytes; readsize -= nbytes;
offset += nbytes;
} }
} }