blob: 9006c0095350184813b6d3ba4608ca3aa50f0dc6 [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 Tarreau6634b632017-09-22 15:02:54 +020031#include <common/ist.h>
Willy Tarreau9b28e032012-10-12 23:49:43 +020032#include <common/memory.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020033
34
35struct buffer {
36 char *p; /* buffer's start pointer, separates in and out data */
37 unsigned int size; /* buffer size in bytes */
38 unsigned int i; /* number of input bytes pending for analysis in the buffer */
39 unsigned int o; /* number of out bytes the sender can consume from this buffer */
40 char data[0]; /* <size> bytes */
41};
42
Christopher Fauleta73e59b2016-12-09 17:30:18 +010043/* an element of the <buffer_wq> list. It represents an object that need to
44 * acquire a buffer to continue its process. */
45struct buffer_wait {
46 void *target; /* The waiting object that should be woken up */
47 int (*wakeup_cb)(void *); /* The function used to wake up the <target>, passed as argument */
48 struct list list; /* Next element in the <buffer_wq> list */
49};
50
Willy Tarreau9b28e032012-10-12 23:49:43 +020051extern struct pool_head *pool2_buffer;
Willy Tarreau2a4b5432014-11-24 11:39:34 +010052extern struct buffer buf_empty;
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +010053extern struct buffer buf_wanted;
Christopher Fauleta73e59b2016-12-09 17:30:18 +010054extern struct list buffer_wq;
Willy Tarreauc7e42382012-08-24 19:22:53 +020055
Willy Tarreau9b28e032012-10-12 23:49:43 +020056int init_buffer();
Christopher Fauletad405f12017-08-29 15:30:11 +020057void deinit_buffer();
Willy Tarreauaf819352012-08-27 22:08:00 +020058int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len);
59int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len);
Willy Tarreauc7e42382012-08-24 19:22:53 +020060void buffer_dump(FILE *o, struct buffer *b, int from, int to);
61void buffer_slow_realign(struct buffer *buf);
Willy Tarreauc7e42382012-08-24 19:22:53 +020062
63/*****************************************************************/
64/* These functions are used to compute various buffer area sizes */
65/*****************************************************************/
66
67/* Returns an absolute pointer for a position relative to the current buffer's
68 * pointer. It is written so that it is optimal when <ofs> is a const. It is
69 * written as a macro instead of an inline function so that the compiler knows
70 * when it can optimize out the sign test on <ofs> when passed an unsigned int.
Willy Tarreauce39bfb2012-09-22 18:36:29 +020071 * Note that callers MUST cast <ofs> to int if they expect negative values.
Willy Tarreauc7e42382012-08-24 19:22:53 +020072 */
73#define b_ptr(b, ofs) \
74 ({ \
75 char *__ret = (b)->p + (ofs); \
76 if ((ofs) > 0 && __ret >= (b)->data + (b)->size) \
77 __ret -= (b)->size; \
78 else if ((ofs) < 0 && __ret < (b)->data) \
79 __ret += (b)->size; \
80 __ret; \
81 })
82
Willy Tarreau26488ad2017-09-19 21:14:08 +020083/* Returns the pointer to the buffer's end (data+size) */
84static inline const char *b_end(const struct buffer *b)
85{
86 return b->data + b->size;
87}
88
89/* Returns the distance between <p> and the buffer's end (data+size) */
90static inline unsigned int b_to_end(const struct buffer *b)
91{
92 return b->data + b->size - b->p;
93}
94
Willy Tarreau4a6425d2017-09-19 14:18:46 +020095/* Skips <del> bytes in a one-way buffer <b> : <p> advances by <del>, <i>
96 * shrinks by <del> as well, and <o> is left untouched (supposed to be zero).
97 * The caller is responsible for ensuring that <del> is always smaller than or
98 * equal to b->i.
99 */
Willy Tarreau7f564d22017-10-18 08:32:12 +0200100static inline void bi_del(struct buffer *b, unsigned int del)
Willy Tarreau4a6425d2017-09-19 14:18:46 +0200101{
102 b->i -= del;
103 b->p = b_ptr(b, del);
104}
105
Willy Tarreau7f564d22017-10-18 08:32:12 +0200106/* Skips <del> bytes from the output of buffer <b> by simply shrinking <o>.
107 * The caller is responsible for ensuring that <del> is always smaller than or
108 * equal to b->o.
109 */
110static inline void bo_del(struct buffer *b, unsigned int del)
111{
112 b->o -= del;
113}
114
Willy Tarreaua75bcef2012-08-24 22:56:11 +0200115/* Advances the buffer by <adv> bytes, which means that the buffer
116 * pointer advances, and that as many bytes from in are transferred
117 * to out. The caller is responsible for ensuring that adv is always
118 * smaller than or equal to b->i.
119 */
120static inline void b_adv(struct buffer *b, unsigned int adv)
121{
122 b->i -= adv;
123 b->o += adv;
124 b->p = b_ptr(b, adv);
125}
126
127/* Rewinds the buffer by <adv> bytes, which means that the buffer pointer goes
128 * backwards, and that as many bytes from out are moved to in. The caller is
129 * responsible for ensuring that adv is always smaller than or equal to b->o.
130 */
131static inline void b_rew(struct buffer *b, unsigned int adv)
132{
133 b->i += adv;
134 b->o -= adv;
135 b->p = b_ptr(b, (int)-adv);
136}
137
Willy Tarreauc7e42382012-08-24 19:22:53 +0200138/* Returns the start of the input data in a buffer */
139static inline char *bi_ptr(const struct buffer *b)
140{
141 return b->p;
142}
143
144/* Returns the end of the input data in a buffer (pointer to next
145 * insertion point).
146 */
147static inline char *bi_end(const struct buffer *b)
148{
149 char *ret = b->p + b->i;
150
151 if (ret >= b->data + b->size)
152 ret -= b->size;
153 return ret;
154}
155
156/* Returns the amount of input data that can contiguously be read at once */
157static inline int bi_contig_data(const struct buffer *b)
158{
159 int data = b->data + b->size - b->p;
160
161 if (data > b->i)
162 data = b->i;
163 return data;
164}
165
166/* Returns the start of the output data in a buffer */
167static inline char *bo_ptr(const struct buffer *b)
168{
169 char *ret = b->p - b->o;
170
171 if (ret < b->data)
172 ret += b->size;
173 return ret;
174}
175
176/* Returns the end of the output data in a buffer */
177static inline char *bo_end(const struct buffer *b)
178{
179 return b->p;
180}
181
182/* Returns the amount of output data that can contiguously be read at once */
183static inline int bo_contig_data(const struct buffer *b)
184{
185 char *beg = b->p - b->o;
186
187 if (beg < b->data)
188 return b->data - beg;
189 return b->o;
190}
191
Christopher Faulet637f8f22017-03-29 11:58:28 +0200192/* Return the amount of bytes that can be written into the input area at once
193 * including reserved space which may be overwritten (this is the caller
194 * responsibility to know if the reserved space is protected or not).
195*/
196static inline int bi_contig_space(const struct buffer *b)
197{
198 const char *left, *right;
199
Christopher Fauleta36b3112017-06-13 22:00:22 +0200200 left = b->p + b->i;
201 right = b->p - b->o;
202 if (left >= b->data + b->size)
203 left -= b->size;
204 else {
205 if (right < b->data)
206 right += b->size;
207 else
208 right = b->data + b->size;
209 }
Christopher Faulet637f8f22017-03-29 11:58:28 +0200210 return (right - left);
211}
212
213/* Return the amount of bytes that can be written into the output area at once
214 * including reserved space which may be overwritten (this is the caller
215 * responsibility to know if the reserved space is protected or not). Input data
216 * are assumed to not exist.
217*/
218static inline int bo_contig_space(const struct buffer *b)
219{
220 const char *left, *right;
221
Christopher Fauleta36b3112017-06-13 22:00:22 +0200222 left = b->p;
223 right = b->p - b->o;
224 if (right < b->data)
225 right += b->size;
226 else
Christopher Faulet637f8f22017-03-29 11:58:28 +0200227 right = b->data + b->size;
228
229 return (right - left);
230}
231
Willy Tarreauc7e42382012-08-24 19:22:53 +0200232/* Return the buffer's length in bytes by summing the input and the output */
233static inline int buffer_len(const struct buffer *buf)
234{
235 return buf->i + buf->o;
236}
237
238/* Return non-zero only if the buffer is not empty */
239static inline int buffer_not_empty(const struct buffer *buf)
240{
241 return buf->i | buf->o;
242}
243
244/* Return non-zero only if the buffer is empty */
245static inline int buffer_empty(const struct buffer *buf)
246{
247 return !buffer_not_empty(buf);
248}
249
Willy Tarreau5b9834f2017-10-16 14:01:18 +0200250/* Return 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 (p+i >= size) ==> doesn't wrap
269 * - if (p-data <= o) ==> doesn't wrap
270 * - otherwise wraps
271 */
272static inline int buffer_space_wraps(const struct buffer *buf)
273{
274 if (buf->p + buf->i >= buf->data + buf->size)
275 return 0;
276 if (buf->p <= buf->data + buf->o)
277 return 0;
278 return 1;
279}
280
Willy Tarreau42d06662012-08-27 19:51:36 +0200281/* Returns non-zero if the buffer's INPUT is considered full, which means that
282 * it holds at least as much INPUT data as (size - reserve). This also means
283 * that data that are scheduled for output are considered as potential free
284 * space, and that the reserved space is always considered as not usable. This
285 * information alone cannot be used as a general purpose free space indicator.
286 * However it accurately indicates that too many data were fed in the buffer
Willy Tarreau3889fff2015-01-13 20:20:10 +0100287 * for an analyzer for instance. See the channel_may_recv() function for a more
Willy Tarreau42d06662012-08-27 19:51:36 +0200288 * generic function taking everything into account.
289 */
290static inline int buffer_full(const struct buffer *b, unsigned int reserve)
291{
Willy Tarreau4428a292014-11-28 20:54:13 +0100292 if (b == &buf_empty)
293 return 0;
294
Willy Tarreau42d06662012-08-27 19:51:36 +0200295 return (b->i + reserve >= b->size);
296}
297
Willy Tarreauc7e42382012-08-24 19:22:53 +0200298/* Normalizes a pointer after a subtract */
299static inline char *buffer_wrap_sub(const struct buffer *buf, char *ptr)
300{
301 if (ptr < buf->data)
302 ptr += buf->size;
303 return ptr;
304}
305
306/* Normalizes a pointer after an addition */
307static inline char *buffer_wrap_add(const struct buffer *buf, char *ptr)
308{
309 if (ptr - buf->size >= buf->data)
310 ptr -= buf->size;
311 return ptr;
312}
313
314/* Return the maximum amount of bytes that can be written into the buffer,
315 * including reserved space which may be overwritten.
316 */
317static inline int buffer_total_space(const struct buffer *buf)
318{
319 return buf->size - buffer_len(buf);
320}
321
Thierry FOURNIERd2b597a2015-03-07 14:38:50 +0100322/* Returns the amount of byte that can be written starting from <p> into the
323 * input buffer at once, including reserved space which may be overwritten.
324 * This is used by Lua to insert data in the input side just before the other
325 * data using buffer_replace(). The goal is to transfer these new data in the
326 * output buffer.
327 */
328static inline int bi_space_for_replace(const struct buffer *buf)
329{
330 const char *end;
331
332 /* If the input side data overflows, we cannot insert data contiguously. */
333 if (buf->p + buf->i >= buf->data + buf->size)
334 return 0;
335
336 /* Check the last byte used in the buffer, it may be a byte of the output
337 * side if the buffer wraps, or its the end of the buffer.
338 */
339 end = buffer_wrap_sub(buf, buf->p - buf->o);
340 if (end <= buf->p)
341 end = buf->data + buf->size;
342
343 /* Compute the amount of bytes which can be written. */
344 return end - (buf->p + buf->i);
345}
346
347
Willy Tarreauc7e42382012-08-24 19:22:53 +0200348/* Normalizes a pointer which is supposed to be relative to the beginning of a
349 * buffer, so that wrapping is correctly handled. The intent is to use this
350 * when increasing a pointer. Note that the wrapping test is only performed
351 * once, so the original pointer must be between ->data-size and ->data+2*size-1,
352 * otherwise an invalid pointer might be returned.
353 */
354static inline const char *buffer_pointer(const struct buffer *buf, const char *ptr)
355{
356 if (ptr < buf->data)
357 ptr += buf->size;
358 else if (ptr - buf->size >= buf->data)
359 ptr -= buf->size;
360 return ptr;
361}
362
363/* Returns the distance between two pointers, taking into account the ability
364 * to wrap around the buffer's end.
365 */
366static inline int buffer_count(const struct buffer *buf, const char *from, const char *to)
367{
368 int count = to - from;
Willy Tarreaubf439272013-04-02 01:25:57 +0200369
370 count += count < 0 ? buf->size : 0;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200371 return count;
372}
373
374/* returns the amount of pending bytes in the buffer. It is the amount of bytes
375 * that is not scheduled to be sent.
376 */
377static inline int buffer_pending(const struct buffer *buf)
378{
379 return buf->i;
380}
381
Willy Tarreauc7e42382012-08-24 19:22:53 +0200382/* Return 1 if the buffer has less than 1/4 of its capacity free, otherwise 0 */
383static inline int buffer_almost_full(const struct buffer *buf)
384{
Willy Tarreau4428a292014-11-28 20:54:13 +0100385 if (buf == &buf_empty)
386 return 0;
387
388 if (!buf->size || buffer_total_space(buf) < buf->size / 4)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200389 return 1;
390 return 0;
391}
392
393/* Cut the first <n> pending bytes in a contiguous buffer. It is illegal to
394 * call this function with remaining data waiting to be sent (o > 0). The
395 * caller must ensure that <n> is smaller than the actual buffer's length.
396 * This is mainly used to remove empty lines at the beginning of a request
397 * or a response.
398 */
399static inline void bi_fast_delete(struct buffer *buf, int n)
400{
401 buf->i -= n;
402 buf->p += n;
403}
404
Christopher Faulet637f8f22017-03-29 11:58:28 +0200405/* Tries to realign the given buffer. */
406static inline void buffer_realign(struct buffer *buf)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200407{
408 if (!(buf->i | buf->o)) {
409 /* let's realign the buffer to optimize I/O */
410 buf->p = buf->data;
411 }
Willy Tarreauc7e42382012-08-24 19:22:53 +0200412}
413
Willy Tarreaua75bcef2012-08-24 22:56:11 +0200414/* Schedule all remaining buffer data to be sent. ->o is not touched if it
415 * already covers those data. That permits doing a flush even after a forward,
416 * although not recommended.
417 */
418static inline void buffer_flush(struct buffer *buf)
419{
420 buf->p = buffer_wrap_add(buf, buf->p + buf->i);
421 buf->o += buf->i;
422 buf->i = 0;
423}
Willy Tarreauc7e42382012-08-24 19:22:53 +0200424
Willy Tarreauaf819352012-08-27 22:08:00 +0200425/* This function writes the string <str> at position <pos> which must be in
426 * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters
427 * (l, r, lr) are updated to be valid after the shift. the shift value
428 * (positive or negative) is returned. If there's no space left, the move is
429 * not done. The function does not adjust ->o because it does not make sense
430 * to use it on data scheduled to be sent.
431 */
432static inline int buffer_replace(struct buffer *b, char *pos, char *end, const char *str)
433{
434 return buffer_replace2(b, pos, end, str, strlen(str));
435}
436
Willy Tarreau8c89c202012-09-28 16:02:48 +0200437/* Tries to write char <c> into output data at buffer <b>. Supports wrapping.
438 * Data are truncated if buffer is full.
439 */
440static inline void bo_putchr(struct buffer *b, char c)
441{
442 if (buffer_len(b) == b->size)
443 return;
444 *b->p = c;
445 b->p = b_ptr(b, 1);
446 b->o++;
447}
448
449/* Tries to copy block <blk> into output data at buffer <b>. Supports wrapping.
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100450 * Data are truncated if buffer is too short. It returns the number of bytes
451 * copied.
Willy Tarreau8c89c202012-09-28 16:02:48 +0200452 */
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100453static inline int bo_putblk(struct buffer *b, const char *blk, int len)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200454{
455 int cur_len = buffer_len(b);
456 int half;
457
458 if (len > b->size - cur_len)
459 len = (b->size - cur_len);
460 if (!len)
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100461 return 0;
Willy Tarreau8c89c202012-09-28 16:02:48 +0200462
Christopher Faulet637f8f22017-03-29 11:58:28 +0200463 half = bo_contig_space(b);
Willy Tarreau8c89c202012-09-28 16:02:48 +0200464 if (half > len)
465 half = len;
466
467 memcpy(b->p, blk, half);
468 b->p = b_ptr(b, half);
469 if (len > half) {
470 memcpy(b->p, blk, len - half);
471 b->p = b_ptr(b, half);
472 }
473 b->o += len;
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100474 return len;
Willy Tarreau8c89c202012-09-28 16:02:48 +0200475}
476
477/* Tries to copy string <str> into output data at buffer <b>. Supports wrapping.
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100478 * Data are truncated if buffer is too short. It returns the number of bytes
479 * copied.
Willy Tarreau8c89c202012-09-28 16:02:48 +0200480 */
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100481static inline int bo_putstr(struct buffer *b, const char *str)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200482{
483 return bo_putblk(b, str, strlen(str));
484}
485
486/* Tries to copy chunk <chk> into output data at buffer <b>. Supports wrapping.
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100487 * Data are truncated if buffer is too short. It returns the number of bytes
488 * copied.
Willy Tarreau8c89c202012-09-28 16:02:48 +0200489 */
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100490static inline int bo_putchk(struct buffer *b, const struct chunk *chk)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200491{
492 return bo_putblk(b, chk->str, chk->len);
493}
494
Willy Tarreaue0e734c2017-10-19 14:56:49 +0200495/* Gets one full block of data at once from a buffer's output, optionally
496 * starting at a specific offset. Return values :
497 * >0 : number of bytes read, equal to requested size.
498 * =0 : not enough data available. <blk> is left undefined.
499 * The buffer is left unaffected.
500 */
501static inline int bo_getblk(const struct buffer *buf, char *blk, int len, int offset)
502{
503 int firstblock;
504
505 if (len + offset > buf->o)
506 return 0;
507
508 firstblock = buf->data + buf->size - bo_ptr(buf);
509 if (firstblock > offset) {
510 if (firstblock >= len + offset) {
511 memcpy(blk, bo_ptr(buf) + offset, len);
512 return len;
513 }
514
515 memcpy(blk, bo_ptr(buf) + offset, firstblock - offset);
516 memcpy(blk + firstblock - offset, buf->data, len - firstblock + offset);
517 return len;
518 }
519
520 memcpy(blk, buf->data + offset - firstblock, len);
521 return len;
522}
523
524/* Gets one or two blocks of data at once from a buffer's output.
525 * Return values :
526 * >0 : number of blocks filled (1 or 2). blk1 is always filled before blk2.
527 * =0 : not enough data available. <blk*> are left undefined.
528 * The buffer is left unaffected. Unused buffers are left in an undefined state.
529 */
530static inline int bo_getblk_nc(struct buffer *buf, char **blk1, int *len1, char **blk2, int *len2)
531{
532 if (unlikely(buf->o == 0))
533 return 0;
534
Willy Tarreau0621da52017-10-20 18:21:49 +0200535 if (unlikely(buf->p != buf->data && buf->p - buf->o < buf->data)) {
Willy Tarreaue0e734c2017-10-19 14:56:49 +0200536 *blk1 = buf->p - buf->o + buf->size;
537 *len1 = buf->data + buf->size - *blk1;
538 *blk2 = buf->data;
539 *len2 = buf->p - buf->data;
540 return 2;
541 }
542
543 *blk1 = buf->p - buf->o;
544 *len1 = buf->o;
545 return 1;
546}
547
548
Willy Tarreau474cf542014-11-24 10:54:47 +0100549/* Resets a buffer. The size is not touched. */
550static inline void b_reset(struct buffer *buf)
551{
552 buf->o = 0;
553 buf->i = 0;
554 buf->p = buf->data;
555}
556
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100557/* Allocates a buffer and replaces *buf with this buffer. If no memory is
558 * available, &buf_wanted is used instead. No control is made to check if *buf
559 * already pointed to another buffer. The allocated buffer is returned, or
560 * NULL in case no memory is available.
Willy Tarreaue583ea52014-11-24 11:30:16 +0100561 */
562static inline struct buffer *b_alloc(struct buffer **buf)
563{
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100564 struct buffer *b;
565
566 *buf = &buf_wanted;
567 b = pool_alloc_dirty(pool2_buffer);
568 if (likely(b)) {
569 b->size = pool2_buffer->size - sizeof(struct buffer);
570 b_reset(b);
571 *buf = b;
Willy Tarreaue583ea52014-11-24 11:30:16 +0100572 }
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100573 return b;
Willy Tarreaue583ea52014-11-24 11:30:16 +0100574}
575
Willy Tarreau620bd6c2014-12-08 16:37:26 +0100576/* Allocates a buffer and replaces *buf with this buffer. If no memory is
577 * available, &buf_wanted is used instead. No control is made to check if *buf
578 * already pointed to another buffer. The allocated buffer is returned, or
579 * NULL in case no memory is available. The difference with b_alloc() is that
580 * this function only picks from the pool and never calls malloc(), so it can
581 * fail even if some memory is available.
582 */
583static inline struct buffer *b_alloc_fast(struct buffer **buf)
584{
585 struct buffer *b;
586
587 *buf = &buf_wanted;
588 b = pool_get_first(pool2_buffer);
589 if (likely(b)) {
590 b->size = pool2_buffer->size - sizeof(struct buffer);
591 b_reset(b);
592 *buf = b;
593 }
594 return b;
595}
596
Willy Tarreau2a4b5432014-11-24 11:39:34 +0100597/* Releases buffer *buf (no check of emptiness) */
598static inline void __b_drop(struct buffer **buf)
Willy Tarreau7dfca9d2014-11-25 19:45:11 +0100599{
600 pool_free2(pool2_buffer, *buf);
601}
602
Willy Tarreau2a4b5432014-11-24 11:39:34 +0100603/* Releases buffer *buf if allocated. */
604static inline void b_drop(struct buffer **buf)
605{
606 if (!(*buf)->size)
607 return;
608 __b_drop(buf);
609}
610
611/* Releases buffer *buf if allocated, and replaces it with &buf_empty. */
612static inline void b_free(struct buffer **buf)
613{
614 b_drop(buf);
615 *buf = &buf_empty;
616}
617
Willy Tarreauf4718e82014-12-02 13:54:01 +0100618/* Ensures that <buf> is allocated. If an allocation is needed, it ensures that
619 * there are still at least <margin> buffers available in the pool after this
620 * allocation so that we don't leave the pool in a condition where a session or
621 * a response buffer could not be allocated anymore, resulting in a deadlock.
622 * This means that we sometimes need to try to allocate extra entries even if
623 * only one buffer is needed.
624 */
625static inline struct buffer *b_alloc_margin(struct buffer **buf, int margin)
626{
627 struct buffer *next;
628
629 if ((*buf)->size)
630 return *buf;
631
632 /* fast path */
633 if ((pool2_buffer->allocated - pool2_buffer->used) > margin)
634 return b_alloc_fast(buf);
635
636 next = pool_refill_alloc(pool2_buffer, margin);
637 if (!next)
638 return next;
639
640 next->size = pool2_buffer->size - sizeof(struct buffer);
641 b_reset(next);
642 *buf = next;
643 return next;
644}
645
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100646
647void __offer_buffer(void *from, unsigned int threshold);
648
649static inline void offer_buffers(void *from, unsigned int threshold)
650{
651 if (LIST_ISEMPTY(&buffer_wq))
652 return;
653 __offer_buffer(from, threshold);
654}
655
Willy Tarreau6634b632017-09-22 15:02:54 +0200656/*************************************************************************/
657/* functions used to manipulate strings and blocks with wrapping buffers */
658/*************************************************************************/
659
660/* returns > 0 if the first <n> characters of buffer <b> starting at
661 * offset <o> relative to b->p match <ist>. (empty strings do match). It is
662 * designed to be use with reasonably small strings (ie matches a single byte
663 * per iteration). This function is usable both with input and output data. To
664 * be used like this depending on what to match :
665 * - input contents : b_isteq(b, 0, b->i, ist);
666 * - output contents : b_isteq(b, -b->o, b->o, ist);
667 * Return value :
668 * >0 : the number of matching bytes
669 * =0 : not enough bytes (or matching of empty string)
670 * <0 : non-matching byte found
671 */
672static inline int b_isteq(const struct buffer *b, unsigned int o, size_t n, const struct ist ist)
673{
674 struct ist r = ist;
675 const char *p;
676 const char *end = b->data + b->size;
677
678 if (n < r.len)
679 return 0;
680
681 p = b_ptr(b, o);
682 while (r.len--) {
683 if (*p++ != *r.ptr++)
684 return -1;
685 if (unlikely(p == end))
686 p = b->data;
687 }
688 return ist.len;
689}
690
691/* "eats" string <ist> from the input region of buffer <b>. Wrapping data is
692 * explicitly supported. It matches a single byte per iteration so strings
693 * should remain reasonably small. Returns :
694 * > 0 : number of bytes matched and eaten
695 * = 0 : not enough bytes (or matching an empty string)
696 * < 0 : non-matching byte found
697 */
698static inline int bi_eat(struct buffer *b, const struct ist ist)
699{
700 int ret = b_isteq(b, 0, b->i, ist);
701 if (ret > 0)
702 bi_del(b, ret);
703 return ret;
704}
705
Willy Tarreaue5676e72017-09-22 15:47:51 +0200706/* injects string <ist> into the input region of buffer <b> provided that it
707 * fits. Wrapping is supported. It's designed for small strings as it only
708 * writes a single byte per iteration. Returns the number of characters copied
709 * (ist.len), 0 if it temporarily does not fit or -1 if it will never fit. It
710 * will only modify the buffer upon success. In all cases, the contents are
711 * copied prior to reporting an error, so that the destination at least
712 * contains a valid but truncated string.
713 */
714static inline int bi_istput(struct buffer *b, const struct ist ist)
715{
716 const char *end = b->data + b->size;
717 struct ist r = ist;
718 char *p;
719
720 if (r.len > (size_t)(b->size - b->i - b->o))
721 return r.len < b->size ? 0 : -1;
722
723 p = b_ptr(b, b->i);
724 b->i += r.len;
725 while (r.len--) {
726 *p++ = *r.ptr++;
727 if (unlikely(p == end))
728 p = b->data;
729 }
730 return ist.len;
731}
732
733
734/* injects string <ist> into the output region of buffer <b> provided that it
735 * fits. Input data is assumed not to exist and will silently be overwritten.
736 * Wrapping is supported. It's designed for small strings as it only writes a
737 * single byte per iteration. Returns the number of characters copied (ist.len),
738 * 0 if it temporarily does not fit or -1 if it will never fit. It will only
739 * modify the buffer upon success. In all cases, the contents are copied prior
740 * to reporting an error, so that the destination at least contains a valid
741 * but truncated string.
742 */
743static inline int bo_istput(struct buffer *b, const struct ist ist)
744{
745 const char *end = b->data + b->size;
746 struct ist r = ist;
747 char *p;
748
749 if (r.len > (size_t)(b->size - b->o))
750 return r.len < b->size ? 0 : -1;
751
752 p = b->p;
753 b->o += r.len;
754 b->p = b_ptr(b, r.len);
755 while (r.len--) {
756 *p++ = *r.ptr++;
757 if (unlikely(p == end))
758 p = b->data;
759 }
760 return ist.len;
761}
762
763
Willy Tarreauc7e42382012-08-24 19:22:53 +0200764#endif /* _COMMON_BUFFER_H */
765
766/*
767 * Local variables:
768 * c-indent-level: 8
769 * c-basic-offset: 8
770 * End:
771 */