blob: e49316efe10c2ee7a7dca2ef930080eff32d9dba [file] [log] [blame]
Willy Tarreauc7e42382012-08-24 19:22:53 +02001/*
2 * include/common/buffer.h
3 * Buffer management definitions, macros and inline functions.
4 *
5 * Copyright (C) 2000-2012 Willy Tarreau - w@1wt.eu
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation, version 2.1
10 * exclusively.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#ifndef _COMMON_BUFFER_H
23#define _COMMON_BUFFER_H
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28
29#include <common/config.h>
30
31
32struct buffer {
33 char *p; /* buffer's start pointer, separates in and out data */
34 unsigned int size; /* buffer size in bytes */
35 unsigned int i; /* number of input bytes pending for analysis in the buffer */
36 unsigned int o; /* number of out bytes the sender can consume from this buffer */
37 char data[0]; /* <size> bytes */
38};
39
40
Willy Tarreauaf819352012-08-27 22:08:00 +020041int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len);
42int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len);
Willy Tarreauc7e42382012-08-24 19:22:53 +020043void buffer_dump(FILE *o, struct buffer *b, int from, int to);
44void buffer_slow_realign(struct buffer *buf);
45void buffer_bounce_realign(struct buffer *buf);
46
47/*****************************************************************/
48/* These functions are used to compute various buffer area sizes */
49/*****************************************************************/
50
51/* Returns an absolute pointer for a position relative to the current buffer's
52 * pointer. It is written so that it is optimal when <ofs> is a const. It is
53 * written as a macro instead of an inline function so that the compiler knows
54 * when it can optimize out the sign test on <ofs> when passed an unsigned int.
55 */
56#define b_ptr(b, ofs) \
57 ({ \
58 char *__ret = (b)->p + (ofs); \
59 if ((ofs) > 0 && __ret >= (b)->data + (b)->size) \
60 __ret -= (b)->size; \
61 else if ((ofs) < 0 && __ret < (b)->data) \
62 __ret += (b)->size; \
63 __ret; \
64 })
65
Willy Tarreaua75bcef2012-08-24 22:56:11 +020066/* Advances the buffer by <adv> bytes, which means that the buffer
67 * pointer advances, and that as many bytes from in are transferred
68 * to out. The caller is responsible for ensuring that adv is always
69 * smaller than or equal to b->i.
70 */
71static inline void b_adv(struct buffer *b, unsigned int adv)
72{
73 b->i -= adv;
74 b->o += adv;
75 b->p = b_ptr(b, adv);
76}
77
78/* Rewinds the buffer by <adv> bytes, which means that the buffer pointer goes
79 * backwards, and that as many bytes from out are moved to in. The caller is
80 * responsible for ensuring that adv is always smaller than or equal to b->o.
81 */
82static inline void b_rew(struct buffer *b, unsigned int adv)
83{
84 b->i += adv;
85 b->o -= adv;
86 b->p = b_ptr(b, (int)-adv);
87}
88
Willy Tarreauc7e42382012-08-24 19:22:53 +020089/* Returns the start of the input data in a buffer */
90static inline char *bi_ptr(const struct buffer *b)
91{
92 return b->p;
93}
94
95/* Returns the end of the input data in a buffer (pointer to next
96 * insertion point).
97 */
98static inline char *bi_end(const struct buffer *b)
99{
100 char *ret = b->p + b->i;
101
102 if (ret >= b->data + b->size)
103 ret -= b->size;
104 return ret;
105}
106
107/* Returns the amount of input data that can contiguously be read at once */
108static inline int bi_contig_data(const struct buffer *b)
109{
110 int data = b->data + b->size - b->p;
111
112 if (data > b->i)
113 data = b->i;
114 return data;
115}
116
117/* Returns the start of the output data in a buffer */
118static inline char *bo_ptr(const struct buffer *b)
119{
120 char *ret = b->p - b->o;
121
122 if (ret < b->data)
123 ret += b->size;
124 return ret;
125}
126
127/* Returns the end of the output data in a buffer */
128static inline char *bo_end(const struct buffer *b)
129{
130 return b->p;
131}
132
133/* Returns the amount of output data that can contiguously be read at once */
134static inline int bo_contig_data(const struct buffer *b)
135{
136 char *beg = b->p - b->o;
137
138 if (beg < b->data)
139 return b->data - beg;
140 return b->o;
141}
142
143/* Return the buffer's length in bytes by summing the input and the output */
144static inline int buffer_len(const struct buffer *buf)
145{
146 return buf->i + buf->o;
147}
148
149/* Return non-zero only if the buffer is not empty */
150static inline int buffer_not_empty(const struct buffer *buf)
151{
152 return buf->i | buf->o;
153}
154
155/* Return non-zero only if the buffer is empty */
156static inline int buffer_empty(const struct buffer *buf)
157{
158 return !buffer_not_empty(buf);
159}
160
Willy Tarreau42d06662012-08-27 19:51:36 +0200161/* Returns non-zero if the buffer's INPUT is considered full, which means that
162 * it holds at least as much INPUT data as (size - reserve). This also means
163 * that data that are scheduled for output are considered as potential free
164 * space, and that the reserved space is always considered as not usable. This
165 * information alone cannot be used as a general purpose free space indicator.
166 * However it accurately indicates that too many data were fed in the buffer
167 * for an analyzer for instance. See the channel_full() function for a more
168 * generic function taking everything into account.
169 */
170static inline int buffer_full(const struct buffer *b, unsigned int reserve)
171{
172 return (b->i + reserve >= b->size);
173}
174
Willy Tarreauc7e42382012-08-24 19:22:53 +0200175/* Normalizes a pointer after a subtract */
176static inline char *buffer_wrap_sub(const struct buffer *buf, char *ptr)
177{
178 if (ptr < buf->data)
179 ptr += buf->size;
180 return ptr;
181}
182
183/* Normalizes a pointer after an addition */
184static inline char *buffer_wrap_add(const struct buffer *buf, char *ptr)
185{
186 if (ptr - buf->size >= buf->data)
187 ptr -= buf->size;
188 return ptr;
189}
190
191/* Return the maximum amount of bytes that can be written into the buffer,
192 * including reserved space which may be overwritten.
193 */
194static inline int buffer_total_space(const struct buffer *buf)
195{
196 return buf->size - buffer_len(buf);
197}
198
199/* Returns the number of contiguous bytes between <start> and <start>+<count>,
200 * and enforces a limit on buf->data + buf->size. <start> must be within the
201 * buffer.
202 */
203static inline int buffer_contig_area(const struct buffer *buf, const char *start, int count)
204{
205 if (count > buf->data - start + buf->size)
206 count = buf->data - start + buf->size;
207 return count;
208}
209
210/* Return the amount of bytes that can be written into the buffer at once,
211 * including reserved space which may be overwritten.
212 */
213static inline int buffer_contig_space(const struct buffer *buf)
214{
215 const char *left, *right;
216
217 if (buf->data + buf->o <= buf->p)
218 right = buf->data + buf->size;
219 else
220 right = buf->p + buf->size - buf->o;
221
222 left = buffer_wrap_add(buf, buf->p + buf->i);
223 return right - left;
224}
225
226/* Return the amount of bytes that can be written into the buffer at once,
227 * excluding the amount of reserved space passed in <res>, which is
228 * preserved.
229 */
230static inline int buffer_contig_space_with_res(const struct buffer *buf, int res)
231{
232 /* Proceed differently if the buffer is full, partially used or empty.
233 * The hard situation is when it's partially used and either data or
234 * reserved space wraps at the end.
235 */
236 int spare = buf->size - res;
237
238 if (buffer_len(buf) >= spare)
239 spare = 0;
240 else if (buffer_len(buf)) {
241 spare = buffer_contig_space(buf) - res;
242 if (spare < 0)
243 spare = 0;
244 }
245 return spare;
246}
247
248
249/* Normalizes a pointer which is supposed to be relative to the beginning of a
250 * buffer, so that wrapping is correctly handled. The intent is to use this
251 * when increasing a pointer. Note that the wrapping test is only performed
252 * once, so the original pointer must be between ->data-size and ->data+2*size-1,
253 * otherwise an invalid pointer might be returned.
254 */
255static inline const char *buffer_pointer(const struct buffer *buf, const char *ptr)
256{
257 if (ptr < buf->data)
258 ptr += buf->size;
259 else if (ptr - buf->size >= buf->data)
260 ptr -= buf->size;
261 return ptr;
262}
263
264/* Returns the distance between two pointers, taking into account the ability
265 * to wrap around the buffer's end.
266 */
267static inline int buffer_count(const struct buffer *buf, const char *from, const char *to)
268{
269 int count = to - from;
270 if (count < 0)
271 count += buf->size;
272 return count;
273}
274
275/* returns the amount of pending bytes in the buffer. It is the amount of bytes
276 * that is not scheduled to be sent.
277 */
278static inline int buffer_pending(const struct buffer *buf)
279{
280 return buf->i;
281}
282
283/* Returns the size of the working area which the caller knows ends at <end>.
284 * If <end> equals buf->r (modulo size), then it means that the free area which
285 * follows is part of the working area. Otherwise, the working area stops at
286 * <end>. It always starts at buf->p. The work area includes the
287 * reserved area.
288 */
289static inline int buffer_work_area(const struct buffer *buf, const char *end)
290{
291 end = buffer_pointer(buf, end);
292 if (end == buffer_wrap_add(buf, buf->p + buf->i))
293 /* pointer exactly at end, lets push forwards */
294 end = buffer_wrap_sub(buf, buf->p - buf->o);
295 return buffer_count(buf, buf->p, end);
296}
297
298/* Return 1 if the buffer has less than 1/4 of its capacity free, otherwise 0 */
299static inline int buffer_almost_full(const struct buffer *buf)
300{
301 if (buffer_total_space(buf) < buf->size / 4)
302 return 1;
303 return 0;
304}
305
306/* Cut the first <n> pending bytes in a contiguous buffer. It is illegal to
307 * call this function with remaining data waiting to be sent (o > 0). The
308 * caller must ensure that <n> is smaller than the actual buffer's length.
309 * This is mainly used to remove empty lines at the beginning of a request
310 * or a response.
311 */
312static inline void bi_fast_delete(struct buffer *buf, int n)
313{
314 buf->i -= n;
315 buf->p += n;
316}
317
318/*
319 * Tries to realign the given buffer, and returns how many bytes can be written
320 * there at once without overwriting anything.
321 */
322static inline int buffer_realign(struct buffer *buf)
323{
324 if (!(buf->i | buf->o)) {
325 /* let's realign the buffer to optimize I/O */
326 buf->p = buf->data;
327 }
328 return buffer_contig_space(buf);
329}
330
Willy Tarreaua75bcef2012-08-24 22:56:11 +0200331/* Schedule all remaining buffer data to be sent. ->o is not touched if it
332 * already covers those data. That permits doing a flush even after a forward,
333 * although not recommended.
334 */
335static inline void buffer_flush(struct buffer *buf)
336{
337 buf->p = buffer_wrap_add(buf, buf->p + buf->i);
338 buf->o += buf->i;
339 buf->i = 0;
340}
Willy Tarreauc7e42382012-08-24 19:22:53 +0200341
Willy Tarreauaf819352012-08-27 22:08:00 +0200342/* This function writes the string <str> at position <pos> which must be in
343 * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters
344 * (l, r, lr) are updated to be valid after the shift. the shift value
345 * (positive or negative) is returned. If there's no space left, the move is
346 * not done. The function does not adjust ->o because it does not make sense
347 * to use it on data scheduled to be sent.
348 */
349static inline int buffer_replace(struct buffer *b, char *pos, char *end, const char *str)
350{
351 return buffer_replace2(b, pos, end, str, strlen(str));
352}
353
Willy Tarreauc7e42382012-08-24 19:22:53 +0200354#endif /* _COMMON_BUFFER_H */
355
356/*
357 * Local variables:
358 * c-indent-level: 8
359 * c-basic-offset: 8
360 * End:
361 */