summaryrefslogtreecommitdiffstats
path: root/src/cache.c
blob: c88cc78ffb7d4c885c2c1ca1773cfa8a552ba552 (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);

	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; ++i) {
		try_s((ret = in->get_element(in, i, in->cache + i)), err);
	}

err:
	return ret;
}

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

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

err:
	return ret;
}

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

	try(!in->cache, err, EINVAL);
	free(in->cache);
	in->cache = NULL;
err:
	return ret;
}