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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
/*
* ngtcp2
*
* Copyright (c) 2017 ngtcp2 contributors
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "ngtcp2_rob.h"
#include <string.h>
#include <assert.h>
#include "ngtcp2_macro.h"
int ngtcp2_rob_gap_new(ngtcp2_rob_gap **pg, uint64_t begin, uint64_t end,
const ngtcp2_mem *mem) {
*pg = ngtcp2_mem_malloc(mem, sizeof(ngtcp2_rob_gap));
if (*pg == NULL) {
return NGTCP2_ERR_NOMEM;
}
(*pg)->range.begin = begin;
(*pg)->range.end = end;
return 0;
}
void ngtcp2_rob_gap_del(ngtcp2_rob_gap *g, const ngtcp2_mem *mem) {
ngtcp2_mem_free(mem, g);
}
int ngtcp2_rob_data_new(ngtcp2_rob_data **pd, uint64_t offset, size_t chunk,
const ngtcp2_mem *mem) {
*pd = ngtcp2_mem_malloc(mem, sizeof(ngtcp2_rob_data) + chunk);
if (*pd == NULL) {
return NGTCP2_ERR_NOMEM;
}
(*pd)->range.begin = offset;
(*pd)->range.end = offset + chunk;
(*pd)->begin = (uint8_t *)(*pd) + sizeof(ngtcp2_rob_data);
return 0;
}
void ngtcp2_rob_data_del(ngtcp2_rob_data *d, const ngtcp2_mem *mem) {
ngtcp2_mem_free(mem, d);
}
int ngtcp2_rob_init(ngtcp2_rob *rob, size_t chunk, const ngtcp2_mem *mem) {
int rv;
ngtcp2_rob_gap *g;
ngtcp2_ksl_init(&rob->gapksl, ngtcp2_ksl_range_compar, sizeof(ngtcp2_range),
mem);
rv = ngtcp2_rob_gap_new(&g, 0, UINT64_MAX, mem);
if (rv != 0) {
goto fail_rob_gap_new;
}
rv = ngtcp2_ksl_insert(&rob->gapksl, NULL, &g->range, g);
if (rv != 0) {
goto fail_gapksl_ksl_insert;
}
ngtcp2_ksl_init(&rob->dataksl, ngtcp2_ksl_range_compar, sizeof(ngtcp2_range),
mem);
rob->chunk = chunk;
rob->mem = mem;
return 0;
fail_gapksl_ksl_insert:
ngtcp2_rob_gap_del(g, mem);
fail_rob_gap_new:
ngtcp2_ksl_free(&rob->gapksl);
return rv;
}
void ngtcp2_rob_free(ngtcp2_rob *rob) {
ngtcp2_ksl_it it;
if (rob == NULL) {
return;
}
for (it = ngtcp2_ksl_begin(&rob->dataksl); !ngtcp2_ksl_it_end(&it);
ngtcp2_ksl_it_next(&it)) {
ngtcp2_rob_data_del(ngtcp2_ksl_it_get(&it), rob->mem);
}
for (it = ngtcp2_ksl_begin(&rob->gapksl); !ngtcp2_ksl_it_end(&it);
ngtcp2_ksl_it_next(&it)) {
ngtcp2_rob_gap_del(ngtcp2_ksl_it_get(&it), rob->mem);
}
ngtcp2_ksl_free(&rob->dataksl);
ngtcp2_ksl_free(&rob->gapksl);
}
static int rob_write_data(ngtcp2_rob *rob, uint64_t offset, const uint8_t *data,
size_t len) {
size_t n;
int rv;
ngtcp2_rob_data *d;
ngtcp2_range range = {offset, offset + len};
ngtcp2_ksl_it it;
for (it = ngtcp2_ksl_lower_bound_compar(&rob->dataksl, &range,
ngtcp2_ksl_range_exclusive_compar);
len; ngtcp2_ksl_it_next(&it)) {
if (ngtcp2_ksl_it_end(&it)) {
d = NULL;
} else {
d = ngtcp2_ksl_it_get(&it);
}
if (d == NULL || offset < d->range.begin) {
rv = ngtcp2_rob_data_new(&d, (offset / rob->chunk) * rob->chunk,
rob->chunk, rob->mem);
if (rv != 0) {
return rv;
}
rv = ngtcp2_ksl_insert(&rob->dataksl, &it, &d->range, d);
if (rv != 0) {
ngtcp2_rob_data_del(d, rob->mem);
return rv;
}
}
n = (size_t)ngtcp2_min_uint64((uint64_t)len,
d->range.begin + rob->chunk - offset);
memcpy(d->begin + (offset - d->range.begin), data, n);
offset += n;
data += n;
len -= n;
}
return 0;
}
int ngtcp2_rob_push(ngtcp2_rob *rob, uint64_t offset, const uint8_t *data,
size_t datalen) {
int rv;
ngtcp2_rob_gap *g;
ngtcp2_range m, l, r, q = {offset, offset + datalen};
ngtcp2_ksl_it it;
it = ngtcp2_ksl_lower_bound_compar(&rob->gapksl, &q,
ngtcp2_ksl_range_exclusive_compar);
for (; !ngtcp2_ksl_it_end(&it);) {
g = ngtcp2_ksl_it_get(&it);
m = ngtcp2_range_intersect(&q, &g->range);
if (!ngtcp2_range_len(&m)) {
break;
}
if (ngtcp2_range_eq(&g->range, &m)) {
ngtcp2_ksl_remove_hint(&rob->gapksl, &it, &it, &g->range);
ngtcp2_rob_gap_del(g, rob->mem);
rv = rob_write_data(rob, m.begin, data + (m.begin - offset),
(size_t)ngtcp2_range_len(&m));
if (rv != 0) {
return rv;
}
continue;
}
ngtcp2_range_cut(&l, &r, &g->range, &m);
if (ngtcp2_range_len(&l)) {
ngtcp2_ksl_update_key(&rob->gapksl, &g->range, &l);
g->range = l;
if (ngtcp2_range_len(&r)) {
ngtcp2_rob_gap *ng;
rv = ngtcp2_rob_gap_new(&ng, r.begin, r.end, rob->mem);
if (rv != 0) {
return rv;
}
rv = ngtcp2_ksl_insert(&rob->gapksl, &it, &ng->range, ng);
if (rv != 0) {
ngtcp2_rob_gap_del(ng, rob->mem);
return rv;
}
}
} else if (ngtcp2_range_len(&r)) {
ngtcp2_ksl_update_key(&rob->gapksl, &g->range, &r);
g->range = r;
}
rv = rob_write_data(rob, m.begin, data + (m.begin - offset),
(size_t)ngtcp2_range_len(&m));
if (rv != 0) {
return rv;
}
ngtcp2_ksl_it_next(&it);
}
return 0;
}
void ngtcp2_rob_remove_prefix(ngtcp2_rob *rob, uint64_t offset) {
ngtcp2_rob_gap *g;
ngtcp2_rob_data *d;
ngtcp2_ksl_it it;
it = ngtcp2_ksl_begin(&rob->gapksl);
for (; !ngtcp2_ksl_it_end(&it);) {
g = ngtcp2_ksl_it_get(&it);
if (offset <= g->range.begin) {
break;
}
if (offset < g->range.end) {
ngtcp2_range r = {offset, g->range.end};
ngtcp2_ksl_update_key(&rob->gapksl, &g->range, &r);
g->range.begin = offset;
break;
}
ngtcp2_ksl_remove_hint(&rob->gapksl, &it, &it, &g->range);
ngtcp2_rob_gap_del(g, rob->mem);
}
it = ngtcp2_ksl_begin(&rob->dataksl);
for (; !ngtcp2_ksl_it_end(&it);) {
d = ngtcp2_ksl_it_get(&it);
if (offset < d->range.begin + rob->chunk) {
return;
}
ngtcp2_ksl_remove_hint(&rob->dataksl, &it, &it, &d->range);
ngtcp2_rob_data_del(d, rob->mem);
}
}
size_t ngtcp2_rob_data_at(const ngtcp2_rob *rob, const uint8_t **pdest,
uint64_t offset) {
ngtcp2_rob_gap *g;
ngtcp2_rob_data *d;
ngtcp2_ksl_it it;
it = ngtcp2_ksl_begin(&rob->gapksl);
if (ngtcp2_ksl_it_end(&it)) {
return 0;
}
g = ngtcp2_ksl_it_get(&it);
if (g->range.begin <= offset) {
return 0;
}
it = ngtcp2_ksl_begin(&rob->dataksl);
d = ngtcp2_ksl_it_get(&it);
assert(d);
assert(d->range.begin <= offset);
assert(offset < d->range.begin + rob->chunk);
*pdest = d->begin + (offset - d->range.begin);
return (
size_t)(ngtcp2_min_uint64(g->range.begin, d->range.begin + rob->chunk) -
offset);
}
void ngtcp2_rob_pop(ngtcp2_rob *rob, uint64_t offset, size_t len) {
ngtcp2_ksl_it it;
ngtcp2_rob_data *d;
it = ngtcp2_ksl_begin(&rob->dataksl);
d = ngtcp2_ksl_it_get(&it);
assert(d);
if (offset + len < d->range.begin + rob->chunk) {
return;
}
ngtcp2_ksl_remove_hint(&rob->dataksl, NULL, &it, &d->range);
ngtcp2_rob_data_del(d, rob->mem);
}
uint64_t ngtcp2_rob_first_gap_offset(const ngtcp2_rob *rob) {
ngtcp2_ksl_it it = ngtcp2_ksl_begin(&rob->gapksl);
ngtcp2_rob_gap *g;
if (ngtcp2_ksl_it_end(&it)) {
return UINT64_MAX;
}
g = ngtcp2_ksl_it_get(&it);
return g->range.begin;
}
int ngtcp2_rob_data_buffered(const ngtcp2_rob *rob) {
return ngtcp2_ksl_len(&rob->dataksl) != 0;
}
|