blob: 564e0ebad3dbd984d48e9e66c19977128a5b50da [file] [log] [blame]
Willy Tarreau41806d12018-07-11 09:39:05 +02001/*
2 * include/common/buf.h
3 * Simple buffer handling.
4 *
5 * Copyright (C) 2000-2018 Willy Tarreau - w@1wt.eu
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
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26 */
27
28#ifndef _COMMON_BUF_H
29#define _COMMON_BUF_H
30
Willy Tarreau506a29a2018-07-18 10:07:58 +020031#include <stdint.h>
32
Willy Tarreau41806d12018-07-11 09:39:05 +020033/* Structure defining a buffer's head */
34struct buffer {
35 char *p; /* buffer's start pointer, separates in and out data */
Willy Tarreau506a29a2018-07-18 10:07:58 +020036 size_t size; /* buffer size in bytes */
37 size_t i; /* number of input bytes pending for analysis in the buffer */
38 size_t o; /* number of out bytes the sender can consume from this buffer */
Willy Tarreau41806d12018-07-11 09:39:05 +020039 char data[0]; /* <size> bytes of stored data */
40};
41
Willy Tarreaubbc68df2018-06-06 14:30:50 +020042
43/***************************************************************************/
44/* Functions used to compute offsets and pointers. Most of them exist in */
45/* both wrapping-safe and unchecked ("__" prefix) variants. Some returning */
46/* a pointer are also provided with an "_ofs" suffix when they return an */
47/* offset relative to the storage area. */
48/***************************************************************************/
49
50/* b_orig() : returns the pointer to the origin of the storage, which is the
51 * location of byte at offset zero. This is mostly used by functions which
52 * handle the wrapping by themselves.
53 */
54static inline char *b_orig(const struct buffer *b)
55{
56 return (char *)b->data;
57}
58
59/* b_size() : returns the size of the buffer. */
60static inline size_t b_size(const struct buffer *b)
61{
62 return b->size;
63}
64
65/* b_wrap() : returns the pointer to the wrapping position of the buffer area,
66 * which is by definition the first byte not part of the buffer.
67 */
68static inline char *b_wrap(const struct buffer *b)
69{
70 return (char *)b->data + b->size;
71}
72
73/* b_data() : returns the number of bytes present in the buffer. */
74static inline size_t b_data(const struct buffer *b)
75{
76 return b->i + b->o;
77}
78
79/* b_room() : returns the amount of room left in the buffer */
80static inline size_t b_room(const struct buffer *b)
81{
82 return b->size - b_data(b);
83}
84
85/* b_full() : returns true if the buffer is full. */
86static inline size_t b_full(const struct buffer *b)
87{
88 return !b_room(b);
89}
90
91
92/* b_stop() : returns the pointer to the byte following the end of the buffer,
93 * which may be out of the buffer if the buffer ends on the last byte of the
94 * area.
95 */
96static inline size_t __b_stop_ofs(const struct buffer *b)
97{
98 return b->p - b->data + b->i;
99}
100
101static inline const char *__b_stop(const struct buffer *b)
102{
103 return b->p + b->i;
104}
105
106static inline size_t b_stop_ofs(const struct buffer *b)
107{
108 size_t stop = __b_stop_ofs(b);
109
110 if (stop > b->size)
111 stop -= b->size;
112 return stop;
113}
114
115static inline const char *b_stop(const struct buffer *b)
116{
117 return b->data + b_stop_ofs(b);
118}
119
120
121/* b_peek() : returns a pointer to the data at position <ofs> relative to the
122 * head of the buffer. Will typically point to input data if called with the
123 * amount of output data. The wrapped versions will only support wrapping once
124 * before the beginning or after the end.
125 */
126static inline size_t __b_peek_ofs(const struct buffer *b, size_t ofs)
127{
128 return b->p - b->data + ofs - b->o;
129}
130
131static inline char *__b_peek(const struct buffer *b, size_t ofs)
132{
133 return b->p - b->o + ofs;
134}
135
136static inline size_t b_peek_ofs(const struct buffer *b, size_t ofs)
137{
138 size_t ret = __b_peek_ofs(b, ofs);
139
140 if (ret >= b->size) {
141 /* wraps either up or down */
142 if ((ssize_t)ret < 0)
143 ret += b->size;
144 else
145 ret -= b->size;
146 }
147
148 return ret;
149}
150
151static inline char *b_peek(const struct buffer *b, size_t ofs)
152{
153 return (char *)b->data + b_peek_ofs(b, ofs);
154}
155
156
157/* b_head() : returns the pointer to the buffer's head, which is the location
158 * of the next byte to be dequeued. Note that for buffers of size zero, the
159 * returned pointer may be outside of the buffer or even invalid.
160 */
161static inline size_t __b_head_ofs(const struct buffer *b)
162{
163 return __b_peek_ofs(b, 0);
164}
165
166static inline char *__b_head(const struct buffer *b)
167{
168 return __b_peek(b, 0);
169}
170
171static inline size_t b_head_ofs(const struct buffer *b)
172{
173 return b_peek_ofs(b, 0);
174}
175
176static inline char *b_head(const struct buffer *b)
177{
178 return b_peek(b, 0);
179}
180
181
182/* b_tail() : returns the pointer to the tail of the buffer, which is the
183 * location of the first byte where it is possible to enqueue new data. Note
184 * that for buffers of size zero, the returned pointer may be outside of the
185 * buffer or even invalid.
186 */
187static inline size_t __b_tail_ofs(const struct buffer *b)
188{
189 return __b_peek_ofs(b, b_data(b));
190}
191
192static inline char *__b_tail(const struct buffer *b)
193{
194 return __b_peek(b, b_data(b));
195}
196
197static inline size_t b_tail_ofs(const struct buffer *b)
198{
199 return b_peek_ofs(b, b_data(b));
200}
201
202static inline char *b_tail(const struct buffer *b)
203{
204 return b_peek(b, b_data(b));
205}
206
207
208/* b_next() : for an absolute pointer <p> or a relative offset <o> pointing to
209 * a valid location within buffer <b>, returns either the absolute pointer or
210 * the relative offset pointing to the next byte, which usually is at (p + 1)
211 * unless p reaches the wrapping point and wrapping is needed.
212 */
213static inline size_t b_next_ofs(const struct buffer *b, size_t o)
214{
215 o++;
216 if (o == b->size)
217 o = 0;
218 return o;
219}
220
221static inline char *b_next(const struct buffer *b, const char *p)
222{
223 p++;
224 if (p == b_wrap(b))
225 p = b_orig(b);
226 return (char *)p;
227}
228
229/* b_dist() : returns the distance between two pointers, taking into account
230 * the ability to wrap around the buffer's end. The operation is not defined if
231 * either of the pointers does not belong to the buffer or if their distance is
232 * greater than the buffer's size.
233 */
234static inline size_t b_dist(const struct buffer *b, const char *from, const char *to)
235{
236 ssize_t dist = to - from;
237
238 dist += dist < 0 ? b_size(b) : 0;
239 return dist;
240}
241
242/* b_almost_full() : returns 1 if the buffer uses at least 3/4 of its capacity,
243 * otherwise zero. Buffers of size zero are considered full.
244 */
245static inline int b_almost_full(const struct buffer *b)
246{
247 return b_data(b) >= b_size(b) * 3 / 4;
248}
249
250/* b_space_wraps() : returns non-zero only if the buffer's free space wraps :
251 * [ |oooo| ] => yes
252 * [ |iiii| ] => yes
253 * [ |oooo|iiii| ] => yes
254 * [oooo| ] => no
255 * [ |oooo] => no
256 * [iiii| ] => no
257 * [ |iiii] => no
258 * [oooo|iiii| ] => no
259 * [ |oooo|iiii] => no
260 * [iiii| |oooo] => no
261 * [oo|iiii| |oo] => no
262 * [iiii| |oo|ii] => no
263 * [oooooooooo|iiiiiiiiiii] => no
264 * [iiiiiiiiiiiii|oooooooo] => no
265 *
266 * So the only case where the buffer does not wrap is when there's data either
267 * at the beginning or at the end of the buffer. Thus we have this :
268 * - if (head <= 0) ==> doesn't wrap
269 * - if (tail >= size) ==> doesn't wrap
270 * - otherwise wraps
271 */
272static inline int b_space_wraps(const struct buffer *b)
273{
274 if ((ssize_t)__b_head_ofs(b) <= 0)
275 return 0;
276 if (__b_tail_ofs(b) >= b_size(b))
277 return 0;
278 return 1;
279}
280
281
282/*********************************************/
283/* Functions used to modify the buffer state */
284/*********************************************/
285
286/* b_reset() : resets a buffer. The size is not touched. */
287static inline void b_reset(struct buffer *b)
288{
289 b->o = 0;
290 b->i = 0;
291 b->p = b_orig(b);
292}
Willy Tarreau41806d12018-07-11 09:39:05 +0200293
Olivier Houchard09138ec2018-06-28 19:17:38 +0200294/* b_sub() : decreases the buffer length by <count> */
295static inline void b_sub(struct buffer *b, size_t count)
296{
297 b->i -= count;
298}
299
300/* b_add() : increase the buffer length by <count> */
301static inline void b_add(struct buffer *b, size_t count)
302{
303 b->i += count;
304}
305
306/* bo_add() : increase the buffer output and length by <count>
307 * (LEGACY API)
308 */
309static inline void bo_add(struct buffer *b, size_t count)
310{
311 b->o += count;
312}
313
Olivier Houcharda04e40d2018-06-28 19:10:25 +0200314/* b_set_data() : sets the buffer's length */
315static inline void b_set_data(struct buffer *b, size_t len)
316{
317 if (len >= b->o)
318 b->i = len - b->o;
319 else {
320 b->o = len;
321 b->i = 0;
322 }
323}
324
Willy Tarreauf17f19f2018-06-15 17:50:15 +0200325/* b_realign_if_empty() : realigns a buffer if it's empty */
326static inline void b_realign_if_empty(struct buffer *b)
327{
328 if (!b_data(b))
329 b->p = b->data;
330}
331
Willy Tarreau41806d12018-07-11 09:39:05 +0200332
333#endif /* _COMMON_BUF_H */
334
335/*
336 * Local variables:
337 * c-indent-level: 8
338 * c-basic-offset: 8
339 * End:
340 */