From 068e3e6c5a74702c3e7db0e37b243f522c433a7f Mon Sep 17 00:00:00 2001 From: Gediminas Jakutis Date: Wed, 3 Mar 2021 14:57:28 +0200 Subject: straight up code refactor. Signed-off-by: Gediminas Jakutis --- src/cache.c | 70 +++++++++++++------------------------------------------------ 1 file changed, 15 insertions(+), 55 deletions(-) (limited to 'src/cache.c') diff --git a/src/cache.c b/src/cache.c index 86e3161..af33f30 100644 --- a/src/cache.c +++ b/src/cache.c @@ -41,9 +41,9 @@ int cache_populate(struct stream * const in) if (in->type == stream_randread) { for (i = 0; i < in->n && !ret; ++i) { errno = 0; - tmp = in->get_next_element_direct(in); + tmp = in->get(in); if (tmp) { /* non-cache reads CAN fail */ - put(in, tmp); + in->put(in, tmp); } else { ret = errno; break; @@ -84,7 +84,7 @@ err: return ret; } -int cache_block_copy(struct stream const * const src, struct stream * const dest) +int cache_block_copy(struct stream * const restrict src, struct stream * const restrict dest) { int ret = 0; @@ -100,7 +100,7 @@ err: return ret; } -int cache_list_copy(struct stream * const src, struct stream * const dest) +int cache_list_copy(struct stream * const restrict src, struct stream * const restrict dest) { int ret = 0; size_t ss; @@ -114,21 +114,21 @@ int cache_list_copy(struct stream * const src, struct stream * const dest) /* skip over to start position */ while (ss--) { - get(src); + src->get(src); } do { - put(dest, get(src)); + dest->put(dest, src->get(src)); } while (dest->index < (dest->n - 1)); - cache_rewind(src); - cache_rewind(dest); + src->rewind(src); + dest->rewind(dest); err: return ret; } -int cache_block_split(struct stream * const src, struct stream * const A, struct stream * const B) +int cache_split(struct stream * const src, struct stream * const A, struct stream * const B) { int ret = 0; struct settings tmp_settings; @@ -142,10 +142,11 @@ int cache_block_split(struct stream * const src, struct stream * const A, struct A->type = B->type = stream_cache; /* disallow any stream operations other than split/merge on children */ A->fd = B->fd = -1; /* if we're splitting, these are for holding cache only */ /* we only care about these three functions for these temporary streams */ - A->get_next_element_cache = B->get_next_element_cache = src->get_next_element_cache; - A->place_next_element_cache = B->place_next_element_cache = src->place_next_element_cache; + A->get = B->get = src->get; + A->put = B->put = src->put; A->split = B->split = src->split; A->rewind = B->rewind = src->rewind; + A->copy = B->copy = src->copy; tmp_settings = *src->settings; A->settings = B->settings = &tmp_settings; @@ -154,54 +155,13 @@ int cache_block_split(struct stream * const src, struct stream * const A, struct tmp_settings.ss = 0; tmp_settings.to = A->n; try_s((ret = cache_create(A)), err); - try_s((ret = cache_block_copy(src, A)), err); + try_s((ret = src->copy(src, A)), err); /* setting up B */ tmp_settings.ss = A->n; tmp_settings.to = src->n; try_s((ret = cache_create(B)), err); - try_s((ret = cache_block_copy(src, B)), err); - - A->settings = B->settings = src->settings; - -err: - return ret; -} - -int cache_list_split(struct stream * const src, struct stream * const A, struct stream * const B) -{ - int ret = 0; - struct settings tmp_settings; - - - try(src->n < 2, err, EINVAL, "cannot split single element stream."); - - /* setting up minimal stream basics */ - A->n = src->n / 2; - B->n = src->n / 2 + (src->n & 1ul); - A->index = B->index = 0; - A->type = B->type = stream_cache; /* disallow any stream operations other than split/merge on children */ - A->fd = B->fd = -1; /* if we're splitting, these are for holding cache only */ - /* we only care about these three functions for these temporary streams */ - A->get_next_element_cache = B->get_next_element_cache = src->get_next_element_cache; - A->place_next_element_cache = B->place_next_element_cache = src->place_next_element_cache; - A->split = B->split = src->split; - A->rewind = B->rewind = src->rewind; - - tmp_settings = *src->settings; - A->settings = B->settings = &tmp_settings; - - /* setting up A */ - tmp_settings.ss = 0; - tmp_settings.to = A->n; - try_s((ret = cache_create(A)), err); - try_s((ret = cache_list_copy(src, A)), err); - - /* setting up B */ - tmp_settings.ss = A->n; - tmp_settings.to = src->n; - try_s((ret = cache_create(B)), err); - try_s((ret = cache_list_copy(src, B)), err); + try_s((ret = src->copy(src, B)), err); A->settings = B->settings = src->settings; @@ -279,7 +239,7 @@ err: return ret; } -int cache_rewind(struct stream * const in) +int cache_rewind(struct stream * const restrict in) { int ret = 0; -- cgit v1.2.3