diff options
author | 2021-03-10 15:04:13 +0200 | |
---|---|---|
committer | 2021-03-10 15:33:58 +0200 | |
commit | 8788b9b33ec46e3f96170fb35a70a03addbf9671 (patch) | |
tree | 44b620061ca427f436e89c7beb80022febde1e58 /src/stream.c | |
parent | d7e2af2582660e3ed4ce824c33a21ffbf9ed4c6f (diff) | |
download | algos-ld1-8788b9b33ec46e3f96170fb35a70a03addbf9671.tar.gz algos-ld1-8788b9b33ec46e3f96170fb35a70a03addbf9671.tar.bz2 algos-ld1-8788b9b33ec46e3f96170fb35a70a03addbf9671.zip |
refactor && improve.
Make code more reusable and generic while preparing to add uncached ops.
Signed-off-by: Gediminas Jakutis <gediminas@varciai.lt>
Diffstat (limited to 'src/stream.c')
-rw-r--r-- | src/stream.c | 34 |
1 files changed, 31 insertions, 3 deletions
diff --git a/src/stream.c b/src/stream.c index 83235a5..b7bc39e 100644 --- a/src/stream.c +++ b/src/stream.c @@ -16,6 +16,7 @@ #include "defs.h" #include "datagen.h" #include "cache.h" +#include "util.h" static int stream_open_out(struct stream * const in); static int stream_open_lite(struct stream * const in); @@ -65,7 +66,9 @@ int stream_close(struct stream * const in) char path[PATH_MAX]; struct stat st; - try_s((ret = stream_flush(in)), err); + if (in->settings->access == cached) { + try_s((ret = stream_flush(in)), err); + } snprintf(path, PATH_MAX, "/proc/self/fd/%i", in->fd); @@ -96,6 +99,33 @@ err: return ret; } +/* generic, naïve and slow copy routine when an optimized one cannot be used */ +int stream_copy_range(struct stream * const restrict src, struct stream * const restrict dest) +{ + int ret = 0; + size_t ss; + struct entry_l tmp_store; + + try(src->n < dest->settings->to, err, EINVAL, "invalid copy size"); + + ss = dest->settings->ss; + + /* skip over to start position */ + while (ss--) { + src->get(src, &tmp_store); + } + + do { + dest->put(dest, src->get(src, &tmp_store)); + } while (dest->index < (dest->n)); + + stream_rewind(src); + stream_rewind(dest); + +err: + return ret; +} + int stream_shallow_copy(struct stream const * const restrict src, struct stream * const dest) { int ret = 0; @@ -106,8 +136,6 @@ int stream_shallow_copy(struct stream const * const restrict src, struct stream dest->get = src->get; dest->put = src->put; - dest->split = src->split; - dest->rewind = src->rewind; dest->copy = src->copy; dest->type = src->settings->access == cached ? stream_cache : stream_lite; |