Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Kitware
GitHub Repository: Kitware/CMake
Path: blob/master/Utilities/cmcurl/lib/bufq.h
5017 views
1
#ifndef HEADER_CURL_BUFQ_H
2
#define HEADER_CURL_BUFQ_H
3
/***************************************************************************
4
* _ _ ____ _
5
* Project ___| | | | _ \| |
6
* / __| | | | |_) | |
7
* | (__| |_| | _ <| |___
8
* \___|\___/|_| \_\_____|
9
*
10
* Copyright (C) Daniel Stenberg, <[email protected]>, et al.
11
*
12
* This software is licensed as described in the file COPYING, which
13
* you should have received as part of this distribution. The terms
14
* are also available at https://curl.se/docs/copyright.html.
15
*
16
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
17
* copies of the Software, and permit persons to whom the Software is
18
* furnished to do so, under the terms of the COPYING file.
19
*
20
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21
* KIND, either express or implied.
22
*
23
* SPDX-License-Identifier: curl
24
*
25
***************************************************************************/
26
#include "curl_setup.h"
27
28
/**
29
* A chunk of bytes for reading and writing.
30
* The size is fixed a creation with read and write offset
31
* for where unread content is.
32
*/
33
struct buf_chunk {
34
struct buf_chunk *next; /* to keep it in a list */
35
size_t dlen; /* the amount of allocated x.data[] */
36
size_t r_offset; /* first unread bytes */
37
size_t w_offset; /* one after last written byte */
38
union {
39
uint8_t data[1]; /* the buffer for `dlen` bytes */
40
void *dummy; /* alignment */
41
} x;
42
};
43
44
/**
45
* A pool for providing/keeping a number of chunks of the same size
46
*
47
* The same pool can be shared by many `bufq` instances. However, a pool
48
* is not thread safe. All bufqs using it are supposed to operate in the
49
* same thread.
50
*/
51
struct bufc_pool {
52
struct buf_chunk *spare; /* list of available spare chunks */
53
size_t chunk_size; /* the size of chunks in this pool */
54
size_t spare_count; /* current number of spare chunks in list */
55
size_t spare_max; /* max number of spares to keep */
56
};
57
58
void Curl_bufcp_init(struct bufc_pool *pool,
59
size_t chunk_size, size_t spare_max);
60
61
void Curl_bufcp_free(struct bufc_pool *pool);
62
63
/**
64
* A queue of byte chunks for reading and writing.
65
* Reading is done from `head`, writing is done to `tail`.
66
*
67
* `bufq`s can be empty or full or neither. Its `len` is the number
68
* of bytes that can be read. For an empty bufq, `len` will be 0.
69
*
70
* By default, a bufq can hold up to `max_chunks * chunk_size` number
71
* of bytes. When `max_chunks` are used (in the `head` list) and the
72
* `tail` chunk is full, the bufq will report that it is full.
73
*
74
* On a full bufq, `len` may be less than the maximum number of bytes,
75
* e.g. when the head chunk is partially read. `len` may also become
76
* larger than the max when option `BUFQ_OPT_SOFT_LIMIT` is used.
77
*
78
* By default, writing to a full bufq will return (-1, CURLE_AGAIN). Same
79
* as reading from an empty bufq.
80
* With `BUFQ_OPT_SOFT_LIMIT` set, a bufq will allow writing becond this
81
* limit and use more than `max_chunks`. However it will report that it
82
* is full nevertheless. This is provided for situation where writes
83
* preferably never fail (except for memory exhaustion).
84
*
85
* By default and without a pool, a bufq will keep chunks that read
86
* empty in its `spare` list. Option `BUFQ_OPT_NO_SPARES` will
87
* disable that and free chunks once they become empty.
88
*
89
* When providing a pool to a bufq, all chunk creation and spare handling
90
* will be delegated to that pool.
91
*/
92
struct bufq {
93
struct buf_chunk *head; /* chunk with bytes to read from */
94
struct buf_chunk *tail; /* chunk to write to */
95
struct buf_chunk *spare; /* list of free chunks, unless `pool` */
96
struct bufc_pool *pool; /* optional pool for free chunks */
97
size_t chunk_count; /* current number of chunks in `head+spare` */
98
size_t max_chunks; /* max `head` chunks to use */
99
size_t chunk_size; /* size of chunks to manage */
100
int opts; /* options for handling queue, see below */
101
};
102
103
/**
104
* Default behaviour: chunk limit is "hard", meaning attempts to write
105
* more bytes than can be hold in `max_chunks` is refused and will return
106
* -1, CURLE_AGAIN. */
107
#define BUFQ_OPT_NONE (0)
108
/**
109
* Make `max_chunks` a "soft" limit. A bufq will report that it is "full"
110
* when `max_chunks` are used, but allows writing beyond this limit.
111
*/
112
#define BUFQ_OPT_SOFT_LIMIT (1 << 0)
113
/**
114
* Do not keep spare chunks.
115
*/
116
#define BUFQ_OPT_NO_SPARES (1 << 1)
117
118
/**
119
* Initialize a buffer queue that can hold up to `max_chunks` buffers
120
* each of size `chunk_size`. The bufq will not allow writing of
121
* more bytes than can be held in `max_chunks`.
122
*/
123
void Curl_bufq_init(struct bufq *q, size_t chunk_size, size_t max_chunks);
124
125
/**
126
* Initialize a buffer queue that can hold up to `max_chunks` buffers
127
* each of size `chunk_size` with the given options. See `BUFQ_OPT_*`.
128
*/
129
void Curl_bufq_init2(struct bufq *q, size_t chunk_size,
130
size_t max_chunks, int opts);
131
132
void Curl_bufq_initp(struct bufq *q, struct bufc_pool *pool,
133
size_t max_chunks, int opts);
134
135
/**
136
* Reset the buffer queue to be empty. Will keep any allocated buffer
137
* chunks around.
138
*/
139
void Curl_bufq_reset(struct bufq *q);
140
141
/**
142
* Free all resources held by the buffer queue.
143
*/
144
void Curl_bufq_free(struct bufq *q);
145
146
/**
147
* Return the total amount of data in the queue.
148
*/
149
size_t Curl_bufq_len(const struct bufq *q);
150
151
/**
152
* Returns TRUE iff there is no data in the buffer queue.
153
*/
154
bool Curl_bufq_is_empty(const struct bufq *q);
155
156
/**
157
* Returns TRUE iff there is no space left in the buffer queue.
158
*/
159
bool Curl_bufq_is_full(const struct bufq *q);
160
161
/**
162
* Write buf to the end of the buffer queue. The buf is copied
163
* and the amount of copied bytes is returned.
164
* CURLE_AGAIN is returned if the buffer queue is full.
165
*/
166
CURLcode Curl_bufq_write(struct bufq *q,
167
const uint8_t *buf, size_t len,
168
size_t *pnwritten);
169
170
CURLcode Curl_bufq_cwrite(struct bufq *q,
171
const char *buf, size_t len,
172
size_t *pnwritten);
173
174
/**
175
* Read buf from the start of the buffer queue. The buf is copied
176
* and the amount of copied bytes is returned.
177
*/
178
CURLcode Curl_bufq_read(struct bufq *q, uint8_t *buf, size_t len,
179
size_t *pnread);
180
181
CURLcode Curl_bufq_cread(struct bufq *q, char *buf, size_t len,
182
size_t *pnread);
183
184
/**
185
* Peek at the head chunk in the buffer queue. Returns a pointer to
186
* the chunk buf (at the current offset) and its length. Does not
187
* modify the buffer queue.
188
* Returns TRUE iff bytes are available. Sets `pbuf` to NULL and `plen`
189
* to 0 when no bytes are available.
190
* Repeated calls return the same information until the buffer queue
191
* is modified, see `Curl_bufq_skip()``
192
*/
193
bool Curl_bufq_peek(struct bufq *q,
194
const uint8_t **pbuf, size_t *plen);
195
196
bool Curl_bufq_peek_at(struct bufq *q, size_t offset,
197
const uint8_t **pbuf, size_t *plen);
198
199
/**
200
* Tell the buffer queue to discard `amount` buf bytes at the head
201
* of the queue. Skipping more buf than is currently buffered will
202
* just empty the queue.
203
*/
204
void Curl_bufq_skip(struct bufq *q, size_t amount);
205
206
typedef CURLcode Curl_bufq_writer(void *writer_ctx,
207
const uint8_t *buf, size_t len,
208
size_t *pwritten);
209
/**
210
* Passes the chunks in the buffer queue to the writer and returns
211
* the amount of buf written. A writer may return -1 and CURLE_AGAIN
212
* to indicate blocking at which point the queue will stop and return
213
* the amount of buf passed so far.
214
* -1 is returned on any other errors reported by the writer.
215
* Note that in case of a -1 chunks may have been written and
216
* the buffer queue will have different length than before.
217
*/
218
CURLcode Curl_bufq_pass(struct bufq *q, Curl_bufq_writer *writer,
219
void *writer_ctx, size_t *pwritten);
220
221
typedef CURLcode Curl_bufq_reader(void *reader_ctx,
222
uint8_t *buf, size_t len,
223
size_t *pnread);
224
225
/**
226
* Read bytes and append them to the end of the buffer queue until the
227
* reader returns blocking or the queue is full. A reader returns
228
* CURLE_AGAIN to indicate blocking.
229
* Returns the total amount of buf read (may be 0) in `pnread` on success.
230
* Note that in case of an error chunks may have been read and
231
* the buffer queue will have different length than before.
232
*/
233
CURLcode Curl_bufq_slurp(struct bufq *q, Curl_bufq_reader *reader,
234
void *reader_ctx, size_t *pnread);
235
236
/**
237
* Read *once* up to `max_len` bytes and append it to the buffer.
238
* if `max_len` is 0, no limit is imposed besides the chunk space.
239
* Returns the total amount of buf read (may be 0) or -1 on other
240
* reader errors.
241
*/
242
CURLcode Curl_bufq_sipn(struct bufq *q, size_t max_len,
243
Curl_bufq_reader *reader, void *reader_ctx,
244
size_t *pnread);
245
246
/**
247
* Write buf to the end of the buffer queue.
248
* Will write bufq content or passed `buf` directly using the `writer`
249
* callback when it sees fit. 'buf' might get passed directly
250
* on or is placed into the buffer, depending on `len` and current
251
* amount buffered, chunk size, etc.
252
*/
253
CURLcode Curl_bufq_write_pass(struct bufq *q,
254
const uint8_t *buf, size_t len,
255
Curl_bufq_writer *writer, void *writer_ctx,
256
size_t *pwritten);
257
258
#endif /* HEADER_CURL_BUFQ_H */
259
260