blob: 18ced917b2849c48de1a3a1ea76ba0f237e66399 [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
Willy Tarreau8c89c202012-09-28 16:02:48 +020029#include <common/chunk.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020030#include <common/config.h>
Willy Tarreau9b28e032012-10-12 23:49:43 +020031#include <common/memory.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020032
33
34struct buffer {
35 char *p; /* buffer's start pointer, separates in and out data */
36 unsigned int size; /* buffer size in bytes */
37 unsigned int i; /* number of input bytes pending for analysis in the buffer */
38 unsigned int o; /* number of out bytes the sender can consume from this buffer */
39 char data[0]; /* <size> bytes */
40};
41
Willy Tarreau9b28e032012-10-12 23:49:43 +020042extern struct pool_head *pool2_buffer;
Willy Tarreauc7e42382012-08-24 19:22:53 +020043
Willy Tarreau9b28e032012-10-12 23:49:43 +020044int init_buffer();
Willy Tarreauaf819352012-08-27 22:08:00 +020045int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len);
46int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len);
Willy Tarreauc7e42382012-08-24 19:22:53 +020047void buffer_dump(FILE *o, struct buffer *b, int from, int to);
48void buffer_slow_realign(struct buffer *buf);
49void buffer_bounce_realign(struct buffer *buf);
50
51/*****************************************************************/
52/* These functions are used to compute various buffer area sizes */
53/*****************************************************************/
54
55/* Returns an absolute pointer for a position relative to the current buffer's
56 * pointer. It is written so that it is optimal when <ofs> is a const. It is
57 * written as a macro instead of an inline function so that the compiler knows
58 * when it can optimize out the sign test on <ofs> when passed an unsigned int.
Willy Tarreauce39bfb2012-09-22 18:36:29 +020059 * Note that callers MUST cast <ofs> to int if they expect negative values.
Willy Tarreauc7e42382012-08-24 19:22:53 +020060 */
61#define b_ptr(b, ofs) \
62 ({ \
63 char *__ret = (b)->p + (ofs); \
64 if ((ofs) > 0 && __ret >= (b)->data + (b)->size) \
65 __ret -= (b)->size; \
66 else if ((ofs) < 0 && __ret < (b)->data) \
67 __ret += (b)->size; \
68 __ret; \
69 })
70
Willy Tarreaua75bcef2012-08-24 22:56:11 +020071/* Advances the buffer by <adv> bytes, which means that the buffer
72 * pointer advances, and that as many bytes from in are transferred
73 * to out. The caller is responsible for ensuring that adv is always
74 * smaller than or equal to b->i.
75 */
76static inline void b_adv(struct buffer *b, unsigned int adv)
77{
78 b->i -= adv;
79 b->o += adv;
80 b->p = b_ptr(b, adv);
81}
82
83/* Rewinds the buffer by <adv> bytes, which means that the buffer pointer goes
84 * backwards, and that as many bytes from out are moved to in. The caller is
85 * responsible for ensuring that adv is always smaller than or equal to b->o.
86 */
87static inline void b_rew(struct buffer *b, unsigned int adv)
88{
89 b->i += adv;
90 b->o -= adv;
91 b->p = b_ptr(b, (int)-adv);
92}
93
Willy Tarreauc7e42382012-08-24 19:22:53 +020094/* Returns the start of the input data in a buffer */
95static inline char *bi_ptr(const struct buffer *b)
96{
97 return b->p;
98}
99
100/* Returns the end of the input data in a buffer (pointer to next
101 * insertion point).
102 */
103static inline char *bi_end(const struct buffer *b)
104{
105 char *ret = b->p + b->i;
106
107 if (ret >= b->data + b->size)
108 ret -= b->size;
109 return ret;
110}
111
112/* Returns the amount of input data that can contiguously be read at once */
113static inline int bi_contig_data(const struct buffer *b)
114{
115 int data = b->data + b->size - b->p;
116
117 if (data > b->i)
118 data = b->i;
119 return data;
120}
121
122/* Returns the start of the output data in a buffer */
123static inline char *bo_ptr(const struct buffer *b)
124{
125 char *ret = b->p - b->o;
126
127 if (ret < b->data)
128 ret += b->size;
129 return ret;
130}
131
132/* Returns the end of the output data in a buffer */
133static inline char *bo_end(const struct buffer *b)
134{
135 return b->p;
136}
137
138/* Returns the amount of output data that can contiguously be read at once */
139static inline int bo_contig_data(const struct buffer *b)
140{
141 char *beg = b->p - b->o;
142
143 if (beg < b->data)
144 return b->data - beg;
145 return b->o;
146}
147
148/* Return the buffer's length in bytes by summing the input and the output */
149static inline int buffer_len(const struct buffer *buf)
150{
151 return buf->i + buf->o;
152}
153
154/* Return non-zero only if the buffer is not empty */
155static inline int buffer_not_empty(const struct buffer *buf)
156{
157 return buf->i | buf->o;
158}
159
160/* Return non-zero only if the buffer is empty */
161static inline int buffer_empty(const struct buffer *buf)
162{
163 return !buffer_not_empty(buf);
164}
165
Willy Tarreau42d06662012-08-27 19:51:36 +0200166/* Returns non-zero if the buffer's INPUT is considered full, which means that
167 * it holds at least as much INPUT data as (size - reserve). This also means
168 * that data that are scheduled for output are considered as potential free
169 * space, and that the reserved space is always considered as not usable. This
170 * information alone cannot be used as a general purpose free space indicator.
171 * However it accurately indicates that too many data were fed in the buffer
172 * for an analyzer for instance. See the channel_full() function for a more
173 * generic function taking everything into account.
174 */
175static inline int buffer_full(const struct buffer *b, unsigned int reserve)
176{
177 return (b->i + reserve >= b->size);
178}
179
Willy Tarreauc7e42382012-08-24 19:22:53 +0200180/* Normalizes a pointer after a subtract */
181static inline char *buffer_wrap_sub(const struct buffer *buf, char *ptr)
182{
183 if (ptr < buf->data)
184 ptr += buf->size;
185 return ptr;
186}
187
188/* Normalizes a pointer after an addition */
189static inline char *buffer_wrap_add(const struct buffer *buf, char *ptr)
190{
191 if (ptr - buf->size >= buf->data)
192 ptr -= buf->size;
193 return ptr;
194}
195
196/* Return the maximum amount of bytes that can be written into the buffer,
197 * including reserved space which may be overwritten.
198 */
199static inline int buffer_total_space(const struct buffer *buf)
200{
201 return buf->size - buffer_len(buf);
202}
203
204/* Returns the number of contiguous bytes between <start> and <start>+<count>,
205 * and enforces a limit on buf->data + buf->size. <start> must be within the
206 * buffer.
207 */
208static inline int buffer_contig_area(const struct buffer *buf, const char *start, int count)
209{
210 if (count > buf->data - start + buf->size)
211 count = buf->data - start + buf->size;
212 return count;
213}
214
215/* Return the amount of bytes that can be written into the buffer at once,
216 * including reserved space which may be overwritten.
217 */
218static inline int buffer_contig_space(const struct buffer *buf)
219{
220 const char *left, *right;
221
222 if (buf->data + buf->o <= buf->p)
223 right = buf->data + buf->size;
224 else
225 right = buf->p + buf->size - buf->o;
226
227 left = buffer_wrap_add(buf, buf->p + buf->i);
228 return right - left;
229}
230
231/* Return the amount of bytes that can be written into the buffer at once,
232 * excluding the amount of reserved space passed in <res>, which is
233 * preserved.
234 */
235static inline int buffer_contig_space_with_res(const struct buffer *buf, int res)
236{
237 /* Proceed differently if the buffer is full, partially used or empty.
238 * The hard situation is when it's partially used and either data or
239 * reserved space wraps at the end.
240 */
241 int spare = buf->size - res;
242
243 if (buffer_len(buf) >= spare)
244 spare = 0;
245 else if (buffer_len(buf)) {
246 spare = buffer_contig_space(buf) - res;
247 if (spare < 0)
248 spare = 0;
249 }
250 return spare;
251}
252
253
254/* Normalizes a pointer which is supposed to be relative to the beginning of a
255 * buffer, so that wrapping is correctly handled. The intent is to use this
256 * when increasing a pointer. Note that the wrapping test is only performed
257 * once, so the original pointer must be between ->data-size and ->data+2*size-1,
258 * otherwise an invalid pointer might be returned.
259 */
260static inline const char *buffer_pointer(const struct buffer *buf, const char *ptr)
261{
262 if (ptr < buf->data)
263 ptr += buf->size;
264 else if (ptr - buf->size >= buf->data)
265 ptr -= buf->size;
266 return ptr;
267}
268
269/* Returns the distance between two pointers, taking into account the ability
270 * to wrap around the buffer's end.
271 */
272static inline int buffer_count(const struct buffer *buf, const char *from, const char *to)
273{
274 int count = to - from;
Willy Tarreaubf439272013-04-02 01:25:57 +0200275
276 count += count < 0 ? buf->size : 0;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200277 return count;
278}
279
280/* returns the amount of pending bytes in the buffer. It is the amount of bytes
281 * that is not scheduled to be sent.
282 */
283static inline int buffer_pending(const struct buffer *buf)
284{
285 return buf->i;
286}
287
288/* Returns the size of the working area which the caller knows ends at <end>.
289 * If <end> equals buf->r (modulo size), then it means that the free area which
290 * follows is part of the working area. Otherwise, the working area stops at
291 * <end>. It always starts at buf->p. The work area includes the
292 * reserved area.
293 */
294static inline int buffer_work_area(const struct buffer *buf, const char *end)
295{
296 end = buffer_pointer(buf, end);
297 if (end == buffer_wrap_add(buf, buf->p + buf->i))
298 /* pointer exactly at end, lets push forwards */
299 end = buffer_wrap_sub(buf, buf->p - buf->o);
300 return buffer_count(buf, buf->p, end);
301}
302
303/* Return 1 if the buffer has less than 1/4 of its capacity free, otherwise 0 */
304static inline int buffer_almost_full(const struct buffer *buf)
305{
306 if (buffer_total_space(buf) < buf->size / 4)
307 return 1;
308 return 0;
309}
310
311/* Cut the first <n> pending bytes in a contiguous buffer. It is illegal to
312 * call this function with remaining data waiting to be sent (o > 0). The
313 * caller must ensure that <n> is smaller than the actual buffer's length.
314 * This is mainly used to remove empty lines at the beginning of a request
315 * or a response.
316 */
317static inline void bi_fast_delete(struct buffer *buf, int n)
318{
319 buf->i -= n;
320 buf->p += n;
321}
322
323/*
324 * Tries to realign the given buffer, and returns how many bytes can be written
325 * there at once without overwriting anything.
326 */
327static inline int buffer_realign(struct buffer *buf)
328{
329 if (!(buf->i | buf->o)) {
330 /* let's realign the buffer to optimize I/O */
331 buf->p = buf->data;
332 }
333 return buffer_contig_space(buf);
334}
335
Willy Tarreaua75bcef2012-08-24 22:56:11 +0200336/* Schedule all remaining buffer data to be sent. ->o is not touched if it
337 * already covers those data. That permits doing a flush even after a forward,
338 * although not recommended.
339 */
340static inline void buffer_flush(struct buffer *buf)
341{
342 buf->p = buffer_wrap_add(buf, buf->p + buf->i);
343 buf->o += buf->i;
344 buf->i = 0;
345}
Willy Tarreauc7e42382012-08-24 19:22:53 +0200346
Willy Tarreauaf819352012-08-27 22:08:00 +0200347/* This function writes the string <str> at position <pos> which must be in
348 * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters
349 * (l, r, lr) are updated to be valid after the shift. the shift value
350 * (positive or negative) is returned. If there's no space left, the move is
351 * not done. The function does not adjust ->o because it does not make sense
352 * to use it on data scheduled to be sent.
353 */
354static inline int buffer_replace(struct buffer *b, char *pos, char *end, const char *str)
355{
356 return buffer_replace2(b, pos, end, str, strlen(str));
357}
358
Willy Tarreau8c89c202012-09-28 16:02:48 +0200359/* Tries to write char <c> into output data at buffer <b>. Supports wrapping.
360 * Data are truncated if buffer is full.
361 */
362static inline void bo_putchr(struct buffer *b, char c)
363{
364 if (buffer_len(b) == b->size)
365 return;
366 *b->p = c;
367 b->p = b_ptr(b, 1);
368 b->o++;
369}
370
371/* Tries to copy block <blk> into output data at buffer <b>. Supports wrapping.
372 * Data are truncated if buffer is too short.
373 */
374static inline void bo_putblk(struct buffer *b, const char *blk, int len)
375{
376 int cur_len = buffer_len(b);
377 int half;
378
379 if (len > b->size - cur_len)
380 len = (b->size - cur_len);
381 if (!len)
382 return;
383
384 half = buffer_contig_space(b);
385 if (half > len)
386 half = len;
387
388 memcpy(b->p, blk, half);
389 b->p = b_ptr(b, half);
390 if (len > half) {
391 memcpy(b->p, blk, len - half);
392 b->p = b_ptr(b, half);
393 }
394 b->o += len;
395}
396
397/* Tries to copy string <str> into output data at buffer <b>. Supports wrapping.
398 * Data are truncated if buffer is too short.
399 */
400static inline void bo_putstr(struct buffer *b, const char *str)
401{
402 return bo_putblk(b, str, strlen(str));
403}
404
405/* Tries to copy chunk <chk> into output data at buffer <b>. Supports wrapping.
406 * Data are truncated if buffer is too short.
407 */
408static inline void bo_putchk(struct buffer *b, const struct chunk *chk)
409{
410 return bo_putblk(b, chk->str, chk->len);
411}
412
Willy Tarreauc7e42382012-08-24 19:22:53 +0200413#endif /* _COMMON_BUFFER_H */
414
415/*
416 * Local variables:
417 * c-indent-level: 8
418 * c-basic-offset: 8
419 * End:
420 */