summaryrefslogtreecommitdiffstats
path: root/src/cache.c
blob: 4fe0c4659a0095eb0aab92b42d8e660ef8bee2f5 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* SPDX-License-Identifier: LGPL-2.1-only */

/* Copyright (C) 2020 Gediminas Jakutis */

#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <rin/definitions.h>
#include <rin/diagnostic.h>
#include "defs.h"

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

	try(!in->cached, err, EINVAL, "cannot create cache: stream is uncached");
	try(!(cache = calloc(in->n, s->stride)), err, ENOMEM, "out of memory");

	/* yeah... */
	in->cache = cache;
	in->cache_a = cache;
	in->cache_l = cache;
err:
	return ret;
}

int cache_populate(struct stream * const in)
{
	int ret = 0;
	size_t i;
	struct entry_l *tmp;

	try(!in->cached, err, EINVAL, "cannot populate cache: stream is uncached");

	for (i = 0; i < in->n && !ret; ++i) {
		errno = 0;
		tmp = in->get_next_element_direct(in);
		if (tmp) { /* non-cache reads CAN fail */
			put(in, tmp);
		} else {
			ret = errno;
			break;
		}
	}

err:
	return ret;
}

int cache_flush(struct stream * const in)
{
	int ret = 0;
	struct entry_l *tmp;

	try(!in->cached, err, EINVAL, "no cache to flush: stream is uncached");
	try(in->out != 1, err, EINVAL, "cannot flush a non-output cache");

	while((tmp = get(in))) {
		in->place_next_element_direct(in, tmp);
	}

err:
	return ret;
}

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

	try(!in->cached, err, EINVAL, "no cache to destroy: stream uncached");
	free(in->cache);
	in->cache_l = NULL;
	in->cache_a = NULL;
	in->cache = NULL;
err:
	return ret;
}

int cache_transfer(struct stream * const src, struct stream * const dest)
{
	int ret = 0;

	try(!src->cached || !dest->cached, err, EINVAL, "cannot transfer caches of uncached streams");
	try(!src->cache, err, EINVAL, "no cache to transfer");
	try(dest->cache, err, EINVAL, "cannot transfer cache: recipient cache already exists");

	dest->cache = src->cache;
	dest->cache_a = (struct entry *) src->cache;
	dest->cache_l = (struct entry_l *) src->cache;
	src->cache = NULL;
	src->cache_a = NULL;
	src->cache_l = NULL;
err:
	return ret;
}

int cache_block_copy(struct stream const * const src, struct stream * const dest, const struct settings * const s)
{
	int ret = 0;

	try(!src->cached || !dest->cached, err, EINVAL, "cannot cache-copy between uncached streams");
	try(!src->cache, err, EINVAL, "no cache to transfer");
	try(!(src->n < s->to), err, EINVAL, "invalid copy size");
	try(!dest->cache, err, EINVAL, "no cache to transfer to");

	memcpy(dest->cache, src->cache_a + s->ss, (s->to - s->ss) * s->stride);

err:
	return ret;
}

int cache_list_copy(struct stream const * const src, struct stream * const dest, const struct settings * const s)
{
	int ret = ENOSYS;

	(void) src;
	(void) dest;
	(void) s;

	rin_warn("stub!");

	return ret;
}

int cache_block_split(struct stream * const src, struct stream * const A, struct stream * const B)
{
	int ret = 0;

	try(src->n < 2, err, EINVAL, "cannot split single element stream.");

	/* copy everything, then change whatever's neccesary */
	*A = *B = *src;

	A->n = A->n / 2 + A->n % 2;
	B->n = B->n / 2;
	A->index = B->index = 0;
	B->cache_a = B->cache_a + A->n;
	B->cache_l = (struct entry_l *) B->cache_a;
	B->cache = (char *) B->cache_a;

err:
	return ret;
}

int cache_list_split(struct stream * const src, struct stream * const A, struct stream * const B)
{
	int ret = ENOSYS;

	(void) src;
	(void) A;
	(void) B;

	rin_warn("stub!");

	return ret;
}

struct entry_l *cached_get_array(struct stream * const in)
{
	struct entry_l *ret = NULL;

	if (in->index < in->n) {
		ret = (struct entry_l *)(in->cache_a + in->index);
		++in->index;
	}

	return ret;
}

struct entry_l *cached_get_list(struct stream * const in)
{
	struct entry_l *ret = NULL;

	if (in->index < in->n) {
		ret = in->cnode;
		in->cnode = (struct entry_l *) ret->next;
		++in->index;
	}

	return ret;
}

int cached_put_array(struct stream * const in, const struct entry_l * const data)
{
	int ret = EINVAL;

	if (in->index < in->n) {
		in->cache_a[in->index].val = data->val;
		++in->index;
	}

	return ret;
}

int cached_put_list(struct stream * const restrict in, const struct entry_l * const node)
{
	int ret = ENOSYS;

	(void) in;
	(void) node;

	rin_warn("stub!");

	return ret;
}