summaryrefslogtreecommitdiffstats
path: root/src/stream.c
diff options
context:
space:
mode:
authorGravatar Gediminas Jakutis <gediminas@varciai.lt> 2021-03-10 15:04:13 +0200
committerGravatar Gediminas Jakutis <gediminas@varciai.lt> 2021-03-10 15:33:58 +0200
commit8788b9b33ec46e3f96170fb35a70a03addbf9671 (patch)
tree44b620061ca427f436e89c7beb80022febde1e58 /src/stream.c
parentd7e2af2582660e3ed4ce824c33a21ffbf9ed4c6f (diff)
downloadalgos-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.c34
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;