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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
/* SPDX-License-Identifier: LGPL-2.1-only */
/* Copyright (C) 2020-2021 Gediminas Jakutis */
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <rin/definitions.h>
#include <rin/diagnostic.h>
#include "defs.h"
#include "stream.h"
#include "cache.h"
static int cache_readfile(struct stream * const in);
int cache_create(struct stream * const in)
{
int ret = 0;
void *cache;
try(in->settings->access != cached, err, EINVAL, "cannot create cache: stream is uncached");
try(!(cache = calloc(in->n, in->settings->stride)), err, ENOMEM, "out of memory");
in->cache = cache;
in->cnode = NULL;
err:
return ret;
}
int cache_populate(struct stream * const in)
{
int ret = 0;
size_t i;
struct entry_l *tmp;
try(in->settings->access != cached, err, EINVAL, "cannot populate cache: stream is uncached");
try(!in->cache, err, EINVAL, "stream has no cache allocated");
/* if reading a a randstream, fall back to the one-element-at-a-time mode */
if (in->type == stream_randread) {
for (i = 0; i < in->n && !ret; ++i) {
errno = 0;
tmp = in->get(in);
if (tmp) { /* non-cache reads CAN fail */
in->put(in, tmp);
} else {
ret = errno;
break;
}
}
} else if (in->type == stream_in) {
try_s((ret == cache_readfile(in)), err);
} else {
try(1, err, EINVAL, "cannot populate a non-reading stream cache");
}
err:
return ret;
}
int cache_destroy(struct stream * const in)
{
int ret = 0;
try(in->settings->access != cached, err, EINVAL, "cannot destroy cache: stream is uncached");
free(in->cache);
in->cache = NULL;
err:
return ret;
}
int cache_transfer(struct stream * const src, struct stream * const dest)
{
int ret = 0;
try(src->settings->access != cached || dest->settings->access != 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;
src->cache = NULL;
err:
return ret;
}
int cache_block_copy(struct stream * const restrict src, struct stream * const restrict dest)
{
int ret = 0;
try(src->settings->access != cached || dest->settings->access != cached, err, EINVAL, "cannot cache-copy between uncached streams");
try(!src->cache, err, EINVAL, "no cache to transfer");
try(src->n < dest->settings->to, err, EINVAL, "invalid copy size");
try(!dest->cache, err, EINVAL, "no cache to transfer to");
memcpy(dest->cache, src->cache_a + dest->settings->ss, (dest->settings->to - dest->settings->ss) * dest->settings->stride);
dest->n = dest->settings->to - dest->settings->ss;
err:
return ret;
}
int cache_list_copy(struct stream * const restrict src, struct stream * const restrict dest)
{
int ret = 0;
size_t ss;
try(src->settings->access != cached || dest->settings->access != cached, err, EINVAL, "cannot cache-copy between uncached streams");
try(!src->cache, err, EINVAL, "no cache to transfer");
try(src->n < dest->settings->to, err, EINVAL, "invalid copy size");
try(!dest->cache, err, EINVAL, "no cache to transfer to");
ss = dest->settings->ss;
/* skip over to start position */
while (ss--) {
src->get(src);
}
do {
dest->put(dest, src->get(src));
} while (dest->index < (dest->n - 1));
src->rewind(src);
dest->rewind(dest);
err:
return ret;
}
int cache_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 = 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;
/* setting up A */
tmp_settings.ss = 0;
tmp_settings.to = A->n;
try_s((ret = cache_create(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 = src->copy(src, B)), err);
A->settings = B->settings = src->settings;
err:
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 = ret->next ? ret->next : in->cnode;
++in->index;
}
return ret;
}
int cached_put_array(struct stream * const in, const struct entry_l * const data)
{
int ret = 0;
if (in->index < in->n) {
in->cache_a[in->index].val = data->val;
++in->index;
}
return ret;
}
/* This is were fun with the fact mergesort is NOT an in-place algorightm begins.
* Since we only ever need to "put" on a) generating data and b) merging lists,
* we basically have to generate the list anew each and every time. For generating,
* it's a no-brainer, but for lists, while reusing existing nodes COULD be done in
* a chached variant, file variant cannot do this, as cross-file links are not
* something that could be handled without going into pretty insane (and laughably
* inefficient) lenghts either way.
*
* And thus alas, we have no option but to just make a list anew
*/
int cached_put_list(struct stream * const restrict in, const struct entry_l * const node)
{
int ret = 0;
try(in->index >= in->n, err, EINVAL, "can't add element: out of cache bounds");
if (!in->cnode) { /* if this is the very first one */
in->cnode = in->cache_l;
in->cnode->val = node->val;
in->cnode->next = NULL;
in->index = 0;
} else {
in->cnode->next = in->cnode + 1; /* lol says librin, lmao */
in->cnode = in->cnode->next;
in->cnode->val = node->val;
in->cnode->next = NULL;
++in->index;
}
err:
return ret;
}
int cache_rewind(struct stream * const restrict in)
{
int ret = 0;
in->cnode = in->cnode ? in->cache_l : NULL;
in->index = 0;
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;
}
|