From 8788b9b33ec46e3f96170fb35a70a03addbf9671 Mon Sep 17 00:00:00 2001 From: Gediminas Jakutis Date: Wed, 10 Mar 2021 15:04:13 +0200 Subject: refactor && improve. Make code more reusable and generic while preparing to add uncached ops. Signed-off-by: Gediminas Jakutis --- src/stream.c | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) (limited to 'src/stream.c') 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; -- cgit v1.2.3