summaryrefslogtreecommitdiffstats
path: root/src/cache.c
blob: ebe1047c26aba44d7e79e0f1b427552ed84a8126 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/* SPDX-License-Identifier: LGPL-2.1-only */

/* Copyright (C) 2020 Gediminas Jakutis */

#include <errno.h>
#include <stdlib.h>
#include "defs.h"

int cache_create(struct stream * const restrict in, const struct settings * const restrict s)
{
	int ret;
	void *cache;

	try(!(cache = calloc(in->n, s->stride)), err, ENOMEM, "out of memory");

	in->cache = cache;
err:
	return ret;
}

int cache_populate(struct stream * const restrict in)
{
	int ret = 0;
	ssize_t i;

	for (i = 0; i < in->n && !ret; ++i) {
		ret = in->get_element(in, i, in->cache + i);
	}

err:
	return ret;
}

int cache_flush(struct stream * const in)
{
	int ret;
	ssize_t i;

	for (i = 0; i < in->n && !ret; ++i) {
		ret = in->put_element(in, i, in->cache + i);
	}

err:
	return ret;
}

int cache_destroy(struct stream * const in)
{
	int ret;

	try(!in->cache, err, EINVAL, "trying to destroy cache of uncached streadm");
	free(in->cache);
	in->cache = NULL;
err:
	return ret;
}