1/*
2 * nghttp3
3 *
4 * Copyright (c) 2019 nghttp3 contributors
5 * Copyright (c) 2017 ngtcp2 contributors
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26#ifndef NGHTTP3_GAPTR_H
27#define NGHTTP3_GAPTR_H
28
29#ifdef HAVE_CONFIG_H
30#  include <config.h>
31#endif /* HAVE_CONFIG_H */
32
33#include <nghttp3/nghttp3.h>
34
35#include "nghttp3_mem.h"
36#include "nghttp3_ksl.h"
37#include "nghttp3_range.h"
38
39/*
40 * nghttp3_gaptr maintains the gap in the range [0, UINT64_MAX).
41 */
42typedef struct nghttp3_gaptr {
43  /* gap maintains the range of offset which is not received
44     yet. Initially, its range is [0, UINT64_MAX). */
45  nghttp3_ksl gap;
46  /* mem is custom memory allocator */
47  const nghttp3_mem *mem;
48} nghttp3_gaptr;
49
50/*
51 * nghttp3_gaptr_init initializes |gaptr|.
52 */
53void nghttp3_gaptr_init(nghttp3_gaptr *gaptr, const nghttp3_mem *mem);
54
55/*
56 * nghttp3_gaptr_free frees resources allocated for |gaptr|.
57 */
58void nghttp3_gaptr_free(nghttp3_gaptr *gaptr);
59
60/*
61 * nghttp3_gaptr_push adds new data of length |datalen| at the stream
62 * offset |offset|.
63 *
64 * This function returns 0 if it succeeds, or one of the following
65 * negative error codes:
66 *
67 * NGHTTP3_ERR_NOMEM
68 *     Out of memory
69 */
70int nghttp3_gaptr_push(nghttp3_gaptr *gaptr, uint64_t offset, uint64_t datalen);
71
72/*
73 * nghttp3_gaptr_first_gap_offset returns the offset to the first gap.
74 * If there is no gap, it returns UINT64_MAX.
75 */
76uint64_t nghttp3_gaptr_first_gap_offset(nghttp3_gaptr *gaptr);
77
78/*
79 * nghttp3_gaptr_get_first_gap_after returns the first gap which
80 * overlaps or comes after |offset|.
81 */
82nghttp3_range nghttp3_gaptr_get_first_gap_after(nghttp3_gaptr *gaptr,
83                                                uint64_t offset);
84
85/*
86 * nghttp3_gaptr_is_pushed returns nonzero if range [offset, offset +
87 * datalen) is completely pushed into this object.
88 */
89int nghttp3_gaptr_is_pushed(nghttp3_gaptr *gaptr, uint64_t offset,
90                            uint64_t datalen);
91
92/*
93 * nghttp3_gaptr_drop_first_gap deletes the first gap entirely as if
94 * the range is pushed.  This function assumes that at least one gap
95 * exists.
96 */
97void nghttp3_gaptr_drop_first_gap(nghttp3_gaptr *gaptr);
98
99#endif /* NGHTTP3_GAPTR_H */
100