diff options
author | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2023-06-04 14:36:25 +0200 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2023-06-04 15:09:05 +0200 |
commit | 723306c417b3b0f8e6a3904906d6c5612cace432 (patch) | |
tree | 34235c4e6e11a1702e61d81f4f58bc9f3da57c58 /lib/io/src | |
parent | 061fbc12fe49ff49088a644def3227d3800cd8a7 (diff) |
libio: Move istream_t precache logic into backend implementation
The end goal is to remove direct buffer access from the istream_t
interfaces and make that opaque. For the tar implementation, this
already safes us needless buffer copying, as we essentially allow
the user to read-through from the underlying stream.
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/io/src')
-rw-r--r-- | lib/io/src/istream.c | 20 | ||||
-rw-r--r-- | lib/io/src/mem.c | 15 | ||||
-rw-r--r-- | lib/io/src/unix/istream.c | 18 | ||||
-rw-r--r-- | lib/io/src/win32/istream.c | 15 | ||||
-rw-r--r-- | lib/io/src/xfrm/istream.c | 15 |
5 files changed, 58 insertions, 25 deletions
diff --git a/lib/io/src/istream.c b/lib/io/src/istream.c index 64fa478..051fac8 100644 --- a/lib/io/src/istream.c +++ b/lib/io/src/istream.c @@ -38,26 +38,6 @@ sqfs_s32 istream_read(istream_t *strm, void *data, size_t size) return total; } -int istream_precache(istream_t *strm) -{ - if (strm->buffer_offset >= strm->buffer_used) { - strm->buffer_offset = 0; - strm->buffer_used = 0; - } else if (strm->buffer_offset > 0) { - memmove(strm->buffer, - strm->buffer + strm->buffer_offset, - strm->buffer_used - strm->buffer_offset); - - strm->buffer_used -= strm->buffer_offset; - strm->buffer_offset = 0; - } - - if (strm->eof) - return 0; - - return strm->precache(strm); -} - int istream_skip(istream_t *strm, sqfs_u64 size) { size_t diff; diff --git a/lib/io/src/mem.c b/lib/io/src/mem.c index 4e9f9af..d305a83 100644 --- a/lib/io/src/mem.c +++ b/lib/io/src/mem.c @@ -26,8 +26,21 @@ typedef struct { static int mem_in_precache(istream_t *strm) { mem_istream_t *mem = (mem_istream_t *)strm; - size_t diff = mem->bufsz - strm->buffer_used; + size_t diff; + + if (strm->buffer_offset >= strm->buffer_used) { + strm->buffer_offset = 0; + strm->buffer_used = 0; + } else if (strm->buffer_offset > 0) { + memmove(strm->buffer, + strm->buffer + strm->buffer_offset, + strm->buffer_used - strm->buffer_offset); + + strm->buffer_used -= strm->buffer_offset; + strm->buffer_offset = 0; + } + diff = mem->bufsz - strm->buffer_used; if (diff > mem->size) diff = mem->size; diff --git a/lib/io/src/unix/istream.c b/lib/io/src/unix/istream.c index f8cffad..24b18d6 100644 --- a/lib/io/src/unix/istream.c +++ b/lib/io/src/unix/istream.c @@ -20,15 +20,25 @@ static int file_precache(istream_t *strm) ssize_t ret; size_t diff; - while (strm->buffer_used < sizeof(file->buffer)) { + if (strm->buffer_offset >= strm->buffer_used) { + strm->buffer_offset = 0; + strm->buffer_used = 0; + } else if (strm->buffer_offset > 0) { + memmove(strm->buffer, + strm->buffer + strm->buffer_offset, + strm->buffer_used - strm->buffer_offset); + + strm->buffer_used -= strm->buffer_offset; + strm->buffer_offset = 0; + } + + while (!strm->eof && strm->buffer_used < sizeof(file->buffer)) { diff = sizeof(file->buffer) - strm->buffer_used; ret = read(file->fd, strm->buffer + strm->buffer_used, diff); - if (ret == 0) { + if (ret == 0) strm->eof = true; - break; - } if (ret < 0) { if (errno == EINTR) diff --git a/lib/io/src/win32/istream.c b/lib/io/src/win32/istream.c index be3d829..918b868 100644 --- a/lib/io/src/win32/istream.c +++ b/lib/io/src/win32/istream.c @@ -23,6 +23,21 @@ static int file_precache(istream_t *strm) DWORD diff, actual; HANDLE hnd; + if (strm->buffer_offset >= strm->buffer_used) { + strm->buffer_offset = 0; + strm->buffer_used = 0; + } else if (strm->buffer_offset > 0) { + memmove(strm->buffer, + strm->buffer + strm->buffer_offset, + strm->buffer_used - strm->buffer_offset); + + strm->buffer_used -= strm->buffer_offset; + strm->buffer_offset = 0; + } + + if (strm->eof) + return 0; + hnd = file->path == NULL ? GetStdHandle(STD_INPUT_HANDLE) : file->hnd; while (strm->buffer_used < sizeof(file->buffer)) { diff --git a/lib/io/src/xfrm/istream.c b/lib/io/src/xfrm/istream.c index 4a1ad77..b301209 100644 --- a/lib/io/src/xfrm/istream.c +++ b/lib/io/src/xfrm/istream.c @@ -20,6 +20,21 @@ static int xfrm_precache(istream_t *base) istream_xfrm_t *xfrm = (istream_xfrm_t *)base; int ret; + if (base->buffer_offset >= base->buffer_used) { + base->buffer_offset = 0; + base->buffer_used = 0; + } else if (base->buffer_offset > 0) { + memmove(base->buffer, + base->buffer + base->buffer_offset, + base->buffer_used - base->buffer_offset); + + base->buffer_used -= base->buffer_offset; + base->buffer_offset = 0; + } + + if (base->eof) + return 0; + ret = istream_precache(xfrm->wrapped); if (ret != 0) return ret; |