blob: ca90fbe4be9cd6f0f4a2fdd761b40ac5131373c0 [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 Tarreau2a4b5432014-11-24 11:39:34 +010043extern struct buffer buf_empty;
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +010044extern struct buffer buf_wanted;
Willy Tarreauc7e42382012-08-24 19:22:53 +020045
Willy Tarreau9b28e032012-10-12 23:49:43 +020046int init_buffer();
Willy Tarreauaf819352012-08-27 22:08:00 +020047int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len);
48int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len);
Willy Tarreauc7e42382012-08-24 19:22:53 +020049void buffer_dump(FILE *o, struct buffer *b, int from, int to);
50void buffer_slow_realign(struct buffer *buf);
51void buffer_bounce_realign(struct buffer *buf);
52
53/*****************************************************************/
54/* These functions are used to compute various buffer area sizes */
55/*****************************************************************/
56
57/* Returns an absolute pointer for a position relative to the current buffer's
58 * pointer. It is written so that it is optimal when <ofs> is a const. It is
59 * written as a macro instead of an inline function so that the compiler knows
60 * when it can optimize out the sign test on <ofs> when passed an unsigned int.
Willy Tarreauce39bfb2012-09-22 18:36:29 +020061 * Note that callers MUST cast <ofs> to int if they expect negative values.
Willy Tarreauc7e42382012-08-24 19:22:53 +020062 */
63#define b_ptr(b, ofs) \
64 ({ \
65 char *__ret = (b)->p + (ofs); \
66 if ((ofs) > 0 && __ret >= (b)->data + (b)->size) \
67 __ret -= (b)->size; \
68 else if ((ofs) < 0 && __ret < (b)->data) \
69 __ret += (b)->size; \
70 __ret; \
71 })
72
Willy Tarreaua75bcef2012-08-24 22:56:11 +020073/* Advances the buffer by <adv> bytes, which means that the buffer
74 * pointer advances, and that as many bytes from in are transferred
75 * to out. The caller is responsible for ensuring that adv is always
76 * smaller than or equal to b->i.
77 */
78static inline void b_adv(struct buffer *b, unsigned int adv)
79{
80 b->i -= adv;
81 b->o += adv;
82 b->p = b_ptr(b, adv);
83}
84
85/* Rewinds the buffer by <adv> bytes, which means that the buffer pointer goes
86 * backwards, and that as many bytes from out are moved to in. The caller is
87 * responsible for ensuring that adv is always smaller than or equal to b->o.
88 */
89static inline void b_rew(struct buffer *b, unsigned int adv)
90{
91 b->i += adv;
92 b->o -= adv;
93 b->p = b_ptr(b, (int)-adv);
94}
95
Willy Tarreauc7e42382012-08-24 19:22:53 +020096/* Returns the start of the input data in a buffer */
97static inline char *bi_ptr(const struct buffer *b)
98{
99 return b->p;
100}
101
102/* Returns the end of the input data in a buffer (pointer to next
103 * insertion point).
104 */
105static inline char *bi_end(const struct buffer *b)
106{
107 char *ret = b->p + b->i;
108
109 if (ret >= b->data + b->size)
110 ret -= b->size;
111 return ret;
112}
113
114/* Returns the amount of input data that can contiguously be read at once */
115static inline int bi_contig_data(const struct buffer *b)
116{
117 int data = b->data + b->size - b->p;
118
119 if (data > b->i)
120 data = b->i;
121 return data;
122}
123
124/* Returns the start of the output data in a buffer */
125static inline char *bo_ptr(const struct buffer *b)
126{
127 char *ret = b->p - b->o;
128
129 if (ret < b->data)
130 ret += b->size;
131 return ret;
132}
133
134/* Returns the end of the output data in a buffer */
135static inline char *bo_end(const struct buffer *b)
136{
137 return b->p;
138}
139
140/* Returns the amount of output data that can contiguously be read at once */
141static inline int bo_contig_data(const struct buffer *b)
142{
143 char *beg = b->p - b->o;
144
145 if (beg < b->data)
146 return b->data - beg;
147 return b->o;
148}
149
150/* Return the buffer's length in bytes by summing the input and the output */
151static inline int buffer_len(const struct buffer *buf)
152{
153 return buf->i + buf->o;
154}
155
156/* Return non-zero only if the buffer is not empty */
157static inline int buffer_not_empty(const struct buffer *buf)
158{
159 return buf->i | buf->o;
160}
161
162/* Return non-zero only if the buffer is empty */
163static inline int buffer_empty(const struct buffer *buf)
164{
165 return !buffer_not_empty(buf);
166}
167
Willy Tarreau42d06662012-08-27 19:51:36 +0200168/* Returns non-zero if the buffer's INPUT is considered full, which means that
169 * it holds at least as much INPUT data as (size - reserve). This also means
170 * that data that are scheduled for output are considered as potential free
171 * space, and that the reserved space is always considered as not usable. This
172 * information alone cannot be used as a general purpose free space indicator.
173 * However it accurately indicates that too many data were fed in the buffer
Willy Tarreau3889fff2015-01-13 20:20:10 +0100174 * for an analyzer for instance. See the channel_may_recv() function for a more
Willy Tarreau42d06662012-08-27 19:51:36 +0200175 * generic function taking everything into account.
176 */
177static inline int buffer_full(const struct buffer *b, unsigned int reserve)
178{
Willy Tarreau4428a292014-11-28 20:54:13 +0100179 if (b == &buf_empty)
180 return 0;
181
Willy Tarreau42d06662012-08-27 19:51:36 +0200182 return (b->i + reserve >= b->size);
183}
184
Willy Tarreauc7e42382012-08-24 19:22:53 +0200185/* Normalizes a pointer after a subtract */
186static inline char *buffer_wrap_sub(const struct buffer *buf, char *ptr)
187{
188 if (ptr < buf->data)
189 ptr += buf->size;
190 return ptr;
191}
192
193/* Normalizes a pointer after an addition */
194static inline char *buffer_wrap_add(const struct buffer *buf, char *ptr)
195{
196 if (ptr - buf->size >= buf->data)
197 ptr -= buf->size;
198 return ptr;
199}
200
201/* Return the maximum amount of bytes that can be written into the buffer,
202 * including reserved space which may be overwritten.
203 */
204static inline int buffer_total_space(const struct buffer *buf)
205{
206 return buf->size - buffer_len(buf);
207}
208
209/* Returns the number of contiguous bytes between <start> and <start>+<count>,
210 * and enforces a limit on buf->data + buf->size. <start> must be within the
211 * buffer.
212 */
213static inline int buffer_contig_area(const struct buffer *buf, const char *start, int count)
214{
215 if (count > buf->data - start + buf->size)
216 count = buf->data - start + buf->size;
217 return count;
218}
219
220/* Return the amount of bytes that can be written into the buffer at once,
221 * including reserved space which may be overwritten.
222 */
223static inline int buffer_contig_space(const struct buffer *buf)
224{
225 const char *left, *right;
226
227 if (buf->data + buf->o <= buf->p)
228 right = buf->data + buf->size;
229 else
230 right = buf->p + buf->size - buf->o;
231
232 left = buffer_wrap_add(buf, buf->p + buf->i);
233 return right - left;
234}
235
Thierry FOURNIERd2b597a2015-03-07 14:38:50 +0100236/* Returns the amount of byte that can be written starting from <p> into the
237 * input buffer at once, including reserved space which may be overwritten.
238 * This is used by Lua to insert data in the input side just before the other
239 * data using buffer_replace(). The goal is to transfer these new data in the
240 * output buffer.
241 */
242static inline int bi_space_for_replace(const struct buffer *buf)
243{
244 const char *end;
245
246 /* If the input side data overflows, we cannot insert data contiguously. */
247 if (buf->p + buf->i >= buf->data + buf->size)
248 return 0;
249
250 /* Check the last byte used in the buffer, it may be a byte of the output
251 * side if the buffer wraps, or its the end of the buffer.
252 */
253 end = buffer_wrap_sub(buf, buf->p - buf->o);
254 if (end <= buf->p)
255 end = buf->data + buf->size;
256
257 /* Compute the amount of bytes which can be written. */
258 return end - (buf->p + buf->i);
259}
260
261
Willy Tarreauc7e42382012-08-24 19:22:53 +0200262/* Normalizes a pointer which is supposed to be relative to the beginning of a
263 * buffer, so that wrapping is correctly handled. The intent is to use this
264 * when increasing a pointer. Note that the wrapping test is only performed
265 * once, so the original pointer must be between ->data-size and ->data+2*size-1,
266 * otherwise an invalid pointer might be returned.
267 */
268static inline const char *buffer_pointer(const struct buffer *buf, const char *ptr)
269{
270 if (ptr < buf->data)
271 ptr += buf->size;
272 else if (ptr - buf->size >= buf->data)
273 ptr -= buf->size;
274 return ptr;
275}
276
277/* Returns the distance between two pointers, taking into account the ability
278 * to wrap around the buffer's end.
279 */
280static inline int buffer_count(const struct buffer *buf, const char *from, const char *to)
281{
282 int count = to - from;
Willy Tarreaubf439272013-04-02 01:25:57 +0200283
284 count += count < 0 ? buf->size : 0;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200285 return count;
286}
287
288/* returns the amount of pending bytes in the buffer. It is the amount of bytes
289 * that is not scheduled to be sent.
290 */
291static inline int buffer_pending(const struct buffer *buf)
292{
293 return buf->i;
294}
295
296/* Returns the size of the working area which the caller knows ends at <end>.
297 * If <end> equals buf->r (modulo size), then it means that the free area which
298 * follows is part of the working area. Otherwise, the working area stops at
299 * <end>. It always starts at buf->p. The work area includes the
300 * reserved area.
301 */
302static inline int buffer_work_area(const struct buffer *buf, const char *end)
303{
304 end = buffer_pointer(buf, end);
305 if (end == buffer_wrap_add(buf, buf->p + buf->i))
306 /* pointer exactly at end, lets push forwards */
307 end = buffer_wrap_sub(buf, buf->p - buf->o);
308 return buffer_count(buf, buf->p, end);
309}
310
311/* Return 1 if the buffer has less than 1/4 of its capacity free, otherwise 0 */
312static inline int buffer_almost_full(const struct buffer *buf)
313{
Willy Tarreau4428a292014-11-28 20:54:13 +0100314 if (buf == &buf_empty)
315 return 0;
316
317 if (!buf->size || buffer_total_space(buf) < buf->size / 4)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200318 return 1;
319 return 0;
320}
321
322/* Cut the first <n> pending bytes in a contiguous buffer. It is illegal to
323 * call this function with remaining data waiting to be sent (o > 0). The
324 * caller must ensure that <n> is smaller than the actual buffer's length.
325 * This is mainly used to remove empty lines at the beginning of a request
326 * or a response.
327 */
328static inline void bi_fast_delete(struct buffer *buf, int n)
329{
330 buf->i -= n;
331 buf->p += n;
332}
333
334/*
335 * Tries to realign the given buffer, and returns how many bytes can be written
336 * there at once without overwriting anything.
337 */
338static inline int buffer_realign(struct buffer *buf)
339{
340 if (!(buf->i | buf->o)) {
341 /* let's realign the buffer to optimize I/O */
342 buf->p = buf->data;
343 }
344 return buffer_contig_space(buf);
345}
346
Willy Tarreaua75bcef2012-08-24 22:56:11 +0200347/* Schedule all remaining buffer data to be sent. ->o is not touched if it
348 * already covers those data. That permits doing a flush even after a forward,
349 * although not recommended.
350 */
351static inline void buffer_flush(struct buffer *buf)
352{
353 buf->p = buffer_wrap_add(buf, buf->p + buf->i);
354 buf->o += buf->i;
355 buf->i = 0;
356}
Willy Tarreauc7e42382012-08-24 19:22:53 +0200357
Willy Tarreauaf819352012-08-27 22:08:00 +0200358/* This function writes the string <str> at position <pos> which must be in
359 * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters
360 * (l, r, lr) are updated to be valid after the shift. the shift value
361 * (positive or negative) is returned. If there's no space left, the move is
362 * not done. The function does not adjust ->o because it does not make sense
363 * to use it on data scheduled to be sent.
364 */
365static inline int buffer_replace(struct buffer *b, char *pos, char *end, const char *str)
366{
367 return buffer_replace2(b, pos, end, str, strlen(str));
368}
369
Willy Tarreau8c89c202012-09-28 16:02:48 +0200370/* Tries to write char <c> into output data at buffer <b>. Supports wrapping.
371 * Data are truncated if buffer is full.
372 */
373static inline void bo_putchr(struct buffer *b, char c)
374{
375 if (buffer_len(b) == b->size)
376 return;
377 *b->p = c;
378 b->p = b_ptr(b, 1);
379 b->o++;
380}
381
382/* Tries to copy block <blk> into output data at buffer <b>. Supports wrapping.
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100383 * Data are truncated if buffer is too short. It returns the number of bytes
384 * copied.
Willy Tarreau8c89c202012-09-28 16:02:48 +0200385 */
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100386static inline int bo_putblk(struct buffer *b, const char *blk, int len)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200387{
388 int cur_len = buffer_len(b);
389 int half;
390
391 if (len > b->size - cur_len)
392 len = (b->size - cur_len);
393 if (!len)
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100394 return 0;
Willy Tarreau8c89c202012-09-28 16:02:48 +0200395
396 half = buffer_contig_space(b);
397 if (half > len)
398 half = len;
399
400 memcpy(b->p, blk, half);
401 b->p = b_ptr(b, half);
402 if (len > half) {
403 memcpy(b->p, blk, len - half);
404 b->p = b_ptr(b, half);
405 }
406 b->o += len;
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100407 return len;
Willy Tarreau8c89c202012-09-28 16:02:48 +0200408}
409
410/* Tries to copy string <str> into output data at buffer <b>. Supports wrapping.
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100411 * Data are truncated if buffer is too short. It returns the number of bytes
412 * copied.
Willy Tarreau8c89c202012-09-28 16:02:48 +0200413 */
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100414static inline int bo_putstr(struct buffer *b, const char *str)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200415{
416 return bo_putblk(b, str, strlen(str));
417}
418
419/* Tries to copy chunk <chk> into output data at buffer <b>. Supports wrapping.
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100420 * Data are truncated if buffer is too short. It returns the number of bytes
421 * copied.
Willy Tarreau8c89c202012-09-28 16:02:48 +0200422 */
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100423static inline int bo_putchk(struct buffer *b, const struct chunk *chk)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200424{
425 return bo_putblk(b, chk->str, chk->len);
426}
427
Willy Tarreau474cf542014-11-24 10:54:47 +0100428/* Resets a buffer. The size is not touched. */
429static inline void b_reset(struct buffer *buf)
430{
431 buf->o = 0;
432 buf->i = 0;
433 buf->p = buf->data;
434}
435
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100436/* Allocates a buffer and replaces *buf with this buffer. If no memory is
437 * available, &buf_wanted is used instead. No control is made to check if *buf
438 * already pointed to another buffer. The allocated buffer is returned, or
439 * NULL in case no memory is available.
Willy Tarreaue583ea52014-11-24 11:30:16 +0100440 */
441static inline struct buffer *b_alloc(struct buffer **buf)
442{
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100443 struct buffer *b;
444
445 *buf = &buf_wanted;
446 b = pool_alloc_dirty(pool2_buffer);
447 if (likely(b)) {
448 b->size = pool2_buffer->size - sizeof(struct buffer);
449 b_reset(b);
450 *buf = b;
Willy Tarreaue583ea52014-11-24 11:30:16 +0100451 }
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100452 return b;
Willy Tarreaue583ea52014-11-24 11:30:16 +0100453}
454
Willy Tarreau620bd6c2014-12-08 16:37:26 +0100455/* Allocates a buffer and replaces *buf with this buffer. If no memory is
456 * available, &buf_wanted is used instead. No control is made to check if *buf
457 * already pointed to another buffer. The allocated buffer is returned, or
458 * NULL in case no memory is available. The difference with b_alloc() is that
459 * this function only picks from the pool and never calls malloc(), so it can
460 * fail even if some memory is available.
461 */
462static inline struct buffer *b_alloc_fast(struct buffer **buf)
463{
464 struct buffer *b;
465
466 *buf = &buf_wanted;
467 b = pool_get_first(pool2_buffer);
468 if (likely(b)) {
469 b->size = pool2_buffer->size - sizeof(struct buffer);
470 b_reset(b);
471 *buf = b;
472 }
473 return b;
474}
475
Willy Tarreau2a4b5432014-11-24 11:39:34 +0100476/* Releases buffer *buf (no check of emptiness) */
477static inline void __b_drop(struct buffer **buf)
Willy Tarreau7dfca9d2014-11-25 19:45:11 +0100478{
479 pool_free2(pool2_buffer, *buf);
480}
481
Willy Tarreau2a4b5432014-11-24 11:39:34 +0100482/* Releases buffer *buf if allocated. */
483static inline void b_drop(struct buffer **buf)
484{
485 if (!(*buf)->size)
486 return;
487 __b_drop(buf);
488}
489
490/* Releases buffer *buf if allocated, and replaces it with &buf_empty. */
491static inline void b_free(struct buffer **buf)
492{
493 b_drop(buf);
494 *buf = &buf_empty;
495}
496
Willy Tarreauf4718e82014-12-02 13:54:01 +0100497/* Ensures that <buf> is allocated. If an allocation is needed, it ensures that
498 * there are still at least <margin> buffers available in the pool after this
499 * allocation so that we don't leave the pool in a condition where a session or
500 * a response buffer could not be allocated anymore, resulting in a deadlock.
501 * This means that we sometimes need to try to allocate extra entries even if
502 * only one buffer is needed.
503 */
504static inline struct buffer *b_alloc_margin(struct buffer **buf, int margin)
505{
506 struct buffer *next;
507
508 if ((*buf)->size)
509 return *buf;
510
511 /* fast path */
512 if ((pool2_buffer->allocated - pool2_buffer->used) > margin)
513 return b_alloc_fast(buf);
514
515 next = pool_refill_alloc(pool2_buffer, margin);
516 if (!next)
517 return next;
518
519 next->size = pool2_buffer->size - sizeof(struct buffer);
520 b_reset(next);
521 *buf = next;
522 return next;
523}
524
Willy Tarreauc7e42382012-08-24 19:22:53 +0200525#endif /* _COMMON_BUFFER_H */
526
527/*
528 * Local variables:
529 * c-indent-level: 8
530 * c-basic-offset: 8
531 * End:
532 */