summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Gediminas Jakutis <gediminas@varciai.lt> 2021-02-25 17:51:54 +0200
committerGravatar Gediminas Jakutis <gediminas@varciai.lt> 2021-02-25 17:51:54 +0200
commit984e9216da19dead3fe2c4669c2163c82687dbf5 (patch)
treed2d5b84f7440a3b5507a9c1fdec8c6b1b07d3a36
parent99c8e4b341d886595fedf0a7c03dfd95b16c01ad (diff)
downloadalgos-ld1-984e9216da19dead3fe2c4669c2163c82687dbf5.tar.gz
algos-ld1-984e9216da19dead3fe2c4669c2163c82687dbf5.tar.bz2
algos-ld1-984e9216da19dead3fe2c4669c2163c82687dbf5.zip
move readfile implementation to cache.
Reading and entire file in is only done for caching, uncached streams will never do that, so it makes sense to move it from stream function collection to cache functions, making it static along the way. Signed-off-by: Gediminas Jakutis <gediminas@varciai.lt>
-rw-r--r--src/cache.c38
-rw-r--r--src/stream.c34
-rw-r--r--src/stream.h3
3 files changed, 39 insertions, 36 deletions
diff --git a/src/cache.c b/src/cache.c
index e0852f4..f158e44 100644
--- a/src/cache.c
+++ b/src/cache.c
@@ -2,6 +2,7 @@
/* Copyright (C) 2020-2021 Gediminas Jakutis */
+#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
@@ -11,6 +12,8 @@
#include "stream.h"
#include "cache.h"
+static int cache_readfile(struct stream * const in);
+
int cache_create(struct stream * const in)
{
int ret = 0;
@@ -48,7 +51,7 @@ int cache_populate(struct stream * const in)
}
}
} else if (in->type == stream_in) {
- try_s((ret == stream_readfile(in)), err);
+ try_s((ret == cache_readfile(in)), err);
} else {
try(1, err, EINVAL, "cannot populate a non-reading stream cache");
}
@@ -247,3 +250,36 @@ int cache_rewind(struct stream * const in)
return ret;
}
+
+static int cache_readfile(struct stream * const in)
+{
+ ssize_t ret = 0;
+ size_t remaining = in->n * in->settings->stride;
+ ssize_t bytesread = 0;
+ size_t i;
+
+ try(in->fd < 3, err, EINVAL, "no file open for reading");
+
+ do {
+ ret = read(in->fd, in->cache + bytesread, remaining);
+ if (ret < 0) {
+ try(errno != EAGAIN, err, errno, "Writing to stream failed with %zi", ret);
+ } else {
+ bytesread += ret;
+ remaining -= ret;
+ }
+ } while (ret);
+
+ /* if this is a list, we need to adjust the link pointers from file offsets
+ * to buffer addresses. 'Cept for the last one, which needs to be NULL.
+ */
+ if (in->settings->format == list) {
+ for (i = 0; i < (in->n - 1); ++i) {
+ in->cache_l[i].nextaddr = in->cache + in->cache_l[i].offset;
+ }
+ in->cnode = in->cache_l;
+ }
+
+err:
+ return ret;
+}
diff --git a/src/stream.c b/src/stream.c
index 9de9e83..e29d17d 100644
--- a/src/stream.c
+++ b/src/stream.c
@@ -1,6 +1,6 @@
/* SPDX-License-Identifier: LGPL-2.1-only */
-/* Copyright (C) 2020 Gediminas Jakutis */
+/* Copyright (C) 2020-2021 Gediminas Jakutis */
#include <sys/types.h>
#include <sys/stat.h>
@@ -96,38 +96,6 @@ err:
return ret;
}
-int stream_readfile(struct stream * const in)
-{
- ssize_t ret = 0;
- size_t remaining = in->n * in->settings->stride;
- ssize_t bytesread = 0;
- size_t i;
-
- try(in->fd < 3, err, EINVAL, "no file open for reading");
-
- do {
- ret = read(in->fd, in->cache + bytesread, remaining);
- if (ret < 0) {
- try(errno != EAGAIN, err, errno, "Writing to stream failed with %zi", ret);
- } else {
- bytesread += ret;
- remaining -= ret;
- }
- } while (ret);
-
- /* if this is a list, we need to adjust the link pointers from file offsets
- * to buffer addresses. 'Cept for the last one, which needs to be NULL.
- */
- if (in->settings->format == list) {
- for (i = 0; i < (in->n - 1); ++i) {
- in->cache_l[i].offset = (char *) in->cache_l[i].next - in->cache;
- }
- }
-
-err:
- return ret;
-}
-
int stream_shallow_copy(struct stream const * const restrict src, struct stream * const dest)
{
int ret = 0;
diff --git a/src/stream.h b/src/stream.h
index 7a429a4..d208bdb 100644
--- a/src/stream.h
+++ b/src/stream.h
@@ -1,6 +1,6 @@
/* SPDX-License-Identifier: LGPL-2.1-only */
-/* Copyright (C) 2020 Gediminas Jakutis */
+/* Copyright (C) 2020-2021 Gediminas Jakutis */
#ifndef ALGOS_IO_H_INCLUDED
#define ALGOS_IO_H_INCLUDED
@@ -9,7 +9,6 @@
int stream_open(struct stream * const in);
int stream_close(struct stream * const in);
-int stream_readfile(struct stream * const in);
int direct_get_array(struct stream * const restrict in, ssize_t idx, struct entry_l * const data);
int direct_get_list(struct stream * const restrict in, ssize_t idx, struct entry_l * const data);