blob: c9868f0a8847d8ac87e78bd10b08f9b1edc839d6 [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 Tarreau41806d12018-07-11 09:39:05 +020029#include <common/buf.h>
Willy Tarreau8c89c202012-09-28 16:02:48 +020030#include <common/chunk.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020031#include <common/config.h>
Willy Tarreau6634b632017-09-22 15:02:54 +020032#include <common/ist.h>
Willy Tarreau9b28e032012-10-12 23:49:43 +020033#include <common/memory.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020034
35
Christopher Fauleta73e59b2016-12-09 17:30:18 +010036/* an element of the <buffer_wq> list. It represents an object that need to
37 * acquire a buffer to continue its process. */
38struct buffer_wait {
39 void *target; /* The waiting object that should be woken up */
40 int (*wakeup_cb)(void *); /* The function used to wake up the <target>, passed as argument */
41 struct list list; /* Next element in the <buffer_wq> list */
42};
43
Willy Tarreaubafbe012017-11-24 17:34:44 +010044extern struct pool_head *pool_head_buffer;
Willy Tarreau2a4b5432014-11-24 11:39:34 +010045extern struct buffer buf_empty;
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +010046extern struct buffer buf_wanted;
Christopher Fauleta73e59b2016-12-09 17:30:18 +010047extern struct list buffer_wq;
Willy Tarreau53bae852017-11-26 11:00:37 +010048__decl_hathreads(extern HA_SPINLOCK_T buffer_wq_lock);
Willy Tarreauc7e42382012-08-24 19:22:53 +020049
Willy Tarreau9b28e032012-10-12 23:49:43 +020050int init_buffer();
Christopher Fauletad405f12017-08-29 15:30:11 +020051void deinit_buffer();
Willy Tarreauaf819352012-08-27 22:08:00 +020052int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len);
53int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len);
Willy Tarreauc7e42382012-08-24 19:22:53 +020054void buffer_dump(FILE *o, struct buffer *b, int from, int to);
Willy Tarreauc7e42382012-08-24 19:22:53 +020055
56/*****************************************************************/
57/* These functions are used to compute various buffer area sizes */
58/*****************************************************************/
59
60/* Returns an absolute pointer for a position relative to the current buffer's
61 * pointer. It is written so that it is optimal when <ofs> is a const. It is
62 * written as a macro instead of an inline function so that the compiler knows
63 * when it can optimize out the sign test on <ofs> when passed an unsigned int.
Willy Tarreauce39bfb2012-09-22 18:36:29 +020064 * Note that callers MUST cast <ofs> to int if they expect negative values.
Willy Tarreauc7e42382012-08-24 19:22:53 +020065 */
66#define b_ptr(b, ofs) \
67 ({ \
68 char *__ret = (b)->p + (ofs); \
69 if ((ofs) > 0 && __ret >= (b)->data + (b)->size) \
70 __ret -= (b)->size; \
71 else if ((ofs) < 0 && __ret < (b)->data) \
72 __ret += (b)->size; \
73 __ret; \
74 })
75
Willy Tarreau26488ad2017-09-19 21:14:08 +020076/* Returns the pointer to the buffer's end (data+size) */
77static inline const char *b_end(const struct buffer *b)
78{
79 return b->data + b->size;
80}
81
82/* Returns the distance between <p> and the buffer's end (data+size) */
83static inline unsigned int b_to_end(const struct buffer *b)
84{
85 return b->data + b->size - b->p;
86}
87
Willy Tarreauc7e42382012-08-24 19:22:53 +020088/* Return the buffer's length in bytes by summing the input and the output */
89static inline int buffer_len(const struct buffer *buf)
90{
91 return buf->i + buf->o;
92}
93
94/* Return non-zero only if the buffer is not empty */
95static inline int buffer_not_empty(const struct buffer *buf)
96{
97 return buf->i | buf->o;
98}
99
100/* Return non-zero only if the buffer is empty */
101static inline int buffer_empty(const struct buffer *buf)
102{
103 return !buffer_not_empty(buf);
104}
105
Willy Tarreau42d06662012-08-27 19:51:36 +0200106/* Returns non-zero if the buffer's INPUT is considered full, which means that
107 * it holds at least as much INPUT data as (size - reserve). This also means
108 * that data that are scheduled for output are considered as potential free
109 * space, and that the reserved space is always considered as not usable. This
110 * information alone cannot be used as a general purpose free space indicator.
111 * However it accurately indicates that too many data were fed in the buffer
Willy Tarreau3889fff2015-01-13 20:20:10 +0100112 * for an analyzer for instance. See the channel_may_recv() function for a more
Willy Tarreau42d06662012-08-27 19:51:36 +0200113 * generic function taking everything into account.
114 */
115static inline int buffer_full(const struct buffer *b, unsigned int reserve)
116{
Willy Tarreau4428a292014-11-28 20:54:13 +0100117 if (b == &buf_empty)
118 return 0;
119
Willy Tarreau42d06662012-08-27 19:51:36 +0200120 return (b->i + reserve >= b->size);
121}
122
Willy Tarreauc7e42382012-08-24 19:22:53 +0200123/* Normalizes a pointer after a subtract */
124static inline char *buffer_wrap_sub(const struct buffer *buf, char *ptr)
125{
126 if (ptr < buf->data)
127 ptr += buf->size;
128 return ptr;
129}
130
131/* Normalizes a pointer after an addition */
132static inline char *buffer_wrap_add(const struct buffer *buf, char *ptr)
133{
134 if (ptr - buf->size >= buf->data)
135 ptr -= buf->size;
136 return ptr;
137}
138
139/* Return the maximum amount of bytes that can be written into the buffer,
140 * including reserved space which may be overwritten.
141 */
142static inline int buffer_total_space(const struct buffer *buf)
143{
144 return buf->size - buffer_len(buf);
145}
146
Thierry FOURNIERd2b597a2015-03-07 14:38:50 +0100147/* Returns the amount of byte that can be written starting from <p> into the
148 * input buffer at once, including reserved space which may be overwritten.
149 * This is used by Lua to insert data in the input side just before the other
150 * data using buffer_replace(). The goal is to transfer these new data in the
151 * output buffer.
152 */
153static inline int bi_space_for_replace(const struct buffer *buf)
154{
155 const char *end;
156
157 /* If the input side data overflows, we cannot insert data contiguously. */
158 if (buf->p + buf->i >= buf->data + buf->size)
159 return 0;
160
161 /* Check the last byte used in the buffer, it may be a byte of the output
162 * side if the buffer wraps, or its the end of the buffer.
163 */
164 end = buffer_wrap_sub(buf, buf->p - buf->o);
165 if (end <= buf->p)
166 end = buf->data + buf->size;
167
168 /* Compute the amount of bytes which can be written. */
169 return end - (buf->p + buf->i);
170}
171
172
Willy Tarreauc7e42382012-08-24 19:22:53 +0200173/* Normalizes a pointer which is supposed to be relative to the beginning of a
174 * buffer, so that wrapping is correctly handled. The intent is to use this
175 * when increasing a pointer. Note that the wrapping test is only performed
176 * once, so the original pointer must be between ->data-size and ->data+2*size-1,
177 * otherwise an invalid pointer might be returned.
178 */
179static inline const char *buffer_pointer(const struct buffer *buf, const char *ptr)
180{
181 if (ptr < buf->data)
182 ptr += buf->size;
183 else if (ptr - buf->size >= buf->data)
184 ptr -= buf->size;
185 return ptr;
186}
187
188/* Returns the distance between two pointers, taking into account the ability
189 * to wrap around the buffer's end.
190 */
191static inline int buffer_count(const struct buffer *buf, const char *from, const char *to)
192{
193 int count = to - from;
Willy Tarreaubf439272013-04-02 01:25:57 +0200194
195 count += count < 0 ? buf->size : 0;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200196 return count;
197}
198
199/* returns the amount of pending bytes in the buffer. It is the amount of bytes
200 * that is not scheduled to be sent.
201 */
202static inline int buffer_pending(const struct buffer *buf)
203{
204 return buf->i;
205}
206
Willy Tarreauc7e42382012-08-24 19:22:53 +0200207/* Return 1 if the buffer has less than 1/4 of its capacity free, otherwise 0 */
208static inline int buffer_almost_full(const struct buffer *buf)
209{
Willy Tarreau4428a292014-11-28 20:54:13 +0100210 if (buf == &buf_empty)
211 return 0;
212
Willy Tarreaubbc68df2018-06-06 14:30:50 +0200213 return b_almost_full(buf);
Willy Tarreauc7e42382012-08-24 19:22:53 +0200214}
215
216/* Cut the first <n> pending bytes in a contiguous buffer. It is illegal to
217 * call this function with remaining data waiting to be sent (o > 0). The
218 * caller must ensure that <n> is smaller than the actual buffer's length.
219 * This is mainly used to remove empty lines at the beginning of a request
220 * or a response.
221 */
222static inline void bi_fast_delete(struct buffer *buf, int n)
223{
224 buf->i -= n;
225 buf->p += n;
226}
227
Willy Tarreaua75bcef2012-08-24 22:56:11 +0200228/* Schedule all remaining buffer data to be sent. ->o is not touched if it
229 * already covers those data. That permits doing a flush even after a forward,
230 * although not recommended.
231 */
232static inline void buffer_flush(struct buffer *buf)
233{
234 buf->p = buffer_wrap_add(buf, buf->p + buf->i);
235 buf->o += buf->i;
236 buf->i = 0;
237}
Willy Tarreauc7e42382012-08-24 19:22:53 +0200238
Willy Tarreauaf819352012-08-27 22:08:00 +0200239/* This function writes the string <str> at position <pos> which must be in
240 * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters
241 * (l, r, lr) are updated to be valid after the shift. the shift value
242 * (positive or negative) is returned. If there's no space left, the move is
243 * not done. The function does not adjust ->o because it does not make sense
244 * to use it on data scheduled to be sent.
245 */
246static inline int buffer_replace(struct buffer *b, char *pos, char *end, const char *str)
247{
248 return buffer_replace2(b, pos, end, str, strlen(str));
249}
250
Willy Tarreau8c89c202012-09-28 16:02:48 +0200251/* Tries to write char <c> into output data at buffer <b>. Supports wrapping.
252 * Data are truncated if buffer is full.
253 */
254static inline void bo_putchr(struct buffer *b, char c)
255{
256 if (buffer_len(b) == b->size)
257 return;
258 *b->p = c;
259 b->p = b_ptr(b, 1);
260 b->o++;
261}
262
263/* Tries to copy block <blk> into output data at buffer <b>. Supports wrapping.
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100264 * Data are truncated if buffer is too short. It returns the number of bytes
265 * copied.
Willy Tarreau8c89c202012-09-28 16:02:48 +0200266 */
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100267static inline int bo_putblk(struct buffer *b, const char *blk, int len)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200268{
269 int cur_len = buffer_len(b);
270 int half;
271
272 if (len > b->size - cur_len)
273 len = (b->size - cur_len);
274 if (!len)
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100275 return 0;
Willy Tarreau8c89c202012-09-28 16:02:48 +0200276
Willy Tarreaue4d5a032018-06-07 18:58:07 +0200277 half = b_contig_space(b);
Willy Tarreau8c89c202012-09-28 16:02:48 +0200278 if (half > len)
279 half = len;
280
281 memcpy(b->p, blk, half);
282 b->p = b_ptr(b, half);
283 if (len > half) {
Christopher Fauletb2b27942018-02-26 10:47:03 +0100284 memcpy(b->p, blk + half, len - half);
Willy Tarreau8c89c202012-09-28 16:02:48 +0200285 b->p = b_ptr(b, half);
286 }
287 b->o += len;
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100288 return len;
Willy Tarreau8c89c202012-09-28 16:02:48 +0200289}
290
291/* Tries to copy string <str> into output data at buffer <b>. Supports wrapping.
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100292 * Data are truncated if buffer is too short. It returns the number of bytes
293 * copied.
Willy Tarreau8c89c202012-09-28 16:02:48 +0200294 */
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100295static inline int bo_putstr(struct buffer *b, const char *str)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200296{
297 return bo_putblk(b, str, strlen(str));
298}
299
300/* Tries to copy chunk <chk> into output data at buffer <b>. Supports wrapping.
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100301 * Data are truncated if buffer is too short. It returns the number of bytes
302 * copied.
Willy Tarreau8c89c202012-09-28 16:02:48 +0200303 */
Thierry FOURNIER549aac82015-02-06 18:40:20 +0100304static inline int bo_putchk(struct buffer *b, const struct chunk *chk)
Willy Tarreau8c89c202012-09-28 16:02:48 +0200305{
306 return bo_putblk(b, chk->str, chk->len);
307}
308
Willy Tarreau145746c2017-10-26 15:26:17 +0200309/* Tries to write char <c> into input data at buffer <b>. Supports wrapping.
310 * Data are truncated if buffer is full.
311 */
312static inline void bi_putchr(struct buffer *b, char c)
313{
314 if (buffer_len(b) == b->size)
315 return;
Willy Tarreau8f9c72d2018-06-07 18:46:28 +0200316 *b_tail(b) = c;
Willy Tarreau145746c2017-10-26 15:26:17 +0200317 b->i++;
318}
319
320/* Tries to copy block <blk> into input data at buffer <b>. Supports wrapping.
321 * Data are truncated if buffer is too short. It returns the number of bytes
322 * copied.
323 */
324static inline int bi_putblk(struct buffer *b, const char *blk, int len)
325{
326 int cur_len = buffer_len(b);
327 int half;
328
329 if (len > b->size - cur_len)
330 len = (b->size - cur_len);
331 if (!len)
332 return 0;
333
Willy Tarreaue4d5a032018-06-07 18:58:07 +0200334 half = b_contig_space(b);
Willy Tarreau145746c2017-10-26 15:26:17 +0200335 if (half > len)
336 half = len;
337
Willy Tarreau8f9c72d2018-06-07 18:46:28 +0200338 memcpy(b_tail(b), blk, half);
Willy Tarreau145746c2017-10-26 15:26:17 +0200339 if (len > half)
Christopher Fauletca6ef502018-02-26 10:51:28 +0100340 memcpy(b_ptr(b, b->i + half), blk + half, len - half);
Willy Tarreau145746c2017-10-26 15:26:17 +0200341 b->i += len;
342 return len;
343}
344
345/* Tries to copy string <str> into input data at buffer <b>. Supports wrapping.
346 * Data are truncated if buffer is too short. It returns the number of bytes
347 * copied.
348 */
349static inline int bi_putstr(struct buffer *b, const char *str)
350{
351 return bi_putblk(b, str, strlen(str));
352}
353
354/* Tries to copy chunk <chk> into input data at buffer <b>. Supports wrapping.
355 * Data are truncated if buffer is too short. It returns the number of bytes
356 * copied.
357 */
358static inline int bi_putchk(struct buffer *b, const struct chunk *chk)
359{
360 return bi_putblk(b, chk->str, chk->len);
361}
362
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100363/* Allocates a buffer and replaces *buf with this buffer. If no memory is
364 * available, &buf_wanted is used instead. No control is made to check if *buf
365 * already pointed to another buffer. The allocated buffer is returned, or
366 * NULL in case no memory is available.
Willy Tarreaue583ea52014-11-24 11:30:16 +0100367 */
368static inline struct buffer *b_alloc(struct buffer **buf)
369{
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100370 struct buffer *b;
371
372 *buf = &buf_wanted;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100373 b = pool_alloc_dirty(pool_head_buffer);
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100374 if (likely(b)) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100375 b->size = pool_head_buffer->size - sizeof(struct buffer);
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100376 b_reset(b);
377 *buf = b;
Willy Tarreaue583ea52014-11-24 11:30:16 +0100378 }
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100379 return b;
Willy Tarreaue583ea52014-11-24 11:30:16 +0100380}
381
Willy Tarreau620bd6c2014-12-08 16:37:26 +0100382/* Allocates a buffer and replaces *buf with this buffer. If no memory is
383 * available, &buf_wanted is used instead. No control is made to check if *buf
384 * already pointed to another buffer. The allocated buffer is returned, or
385 * NULL in case no memory is available. The difference with b_alloc() is that
386 * this function only picks from the pool and never calls malloc(), so it can
387 * fail even if some memory is available.
388 */
389static inline struct buffer *b_alloc_fast(struct buffer **buf)
390{
391 struct buffer *b;
392
393 *buf = &buf_wanted;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100394 b = pool_get_first(pool_head_buffer);
Willy Tarreau620bd6c2014-12-08 16:37:26 +0100395 if (likely(b)) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100396 b->size = pool_head_buffer->size - sizeof(struct buffer);
Willy Tarreau620bd6c2014-12-08 16:37:26 +0100397 b_reset(b);
398 *buf = b;
399 }
400 return b;
401}
402
Willy Tarreau2a4b5432014-11-24 11:39:34 +0100403/* Releases buffer *buf (no check of emptiness) */
404static inline void __b_drop(struct buffer **buf)
Willy Tarreau7dfca9d2014-11-25 19:45:11 +0100405{
Willy Tarreaubafbe012017-11-24 17:34:44 +0100406 pool_free(pool_head_buffer, *buf);
Willy Tarreau7dfca9d2014-11-25 19:45:11 +0100407}
408
Willy Tarreau2a4b5432014-11-24 11:39:34 +0100409/* Releases buffer *buf if allocated. */
410static inline void b_drop(struct buffer **buf)
411{
412 if (!(*buf)->size)
413 return;
414 __b_drop(buf);
415}
416
417/* Releases buffer *buf if allocated, and replaces it with &buf_empty. */
418static inline void b_free(struct buffer **buf)
419{
420 b_drop(buf);
421 *buf = &buf_empty;
422}
423
Willy Tarreauf4718e82014-12-02 13:54:01 +0100424/* Ensures that <buf> is allocated. If an allocation is needed, it ensures that
425 * there are still at least <margin> buffers available in the pool after this
426 * allocation so that we don't leave the pool in a condition where a session or
427 * a response buffer could not be allocated anymore, resulting in a deadlock.
428 * This means that we sometimes need to try to allocate extra entries even if
429 * only one buffer is needed.
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100430 *
431 * We need to lock the pool here to be sure to have <margin> buffers available
432 * after the allocation, regardless how many threads that doing it in the same
433 * time. So, we use internal and lockless memory functions (prefixed with '__').
Willy Tarreauf4718e82014-12-02 13:54:01 +0100434 */
435static inline struct buffer *b_alloc_margin(struct buffer **buf, int margin)
436{
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100437 struct buffer *b;
Willy Tarreauf4718e82014-12-02 13:54:01 +0100438
439 if ((*buf)->size)
440 return *buf;
441
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100442 *buf = &buf_wanted;
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100443#ifndef CONFIG_HAP_LOCKLESS_POOLS
Willy Tarreaubafbe012017-11-24 17:34:44 +0100444 HA_SPIN_LOCK(POOL_LOCK, &pool_head_buffer->lock);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100445#endif
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100446
Willy Tarreauf4718e82014-12-02 13:54:01 +0100447 /* fast path */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100448 if ((pool_head_buffer->allocated - pool_head_buffer->used) > margin) {
449 b = __pool_get_first(pool_head_buffer);
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100450 if (likely(b)) {
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100451#ifndef CONFIG_HAP_LOCKLESS_POOLS
Willy Tarreaubafbe012017-11-24 17:34:44 +0100452 HA_SPIN_UNLOCK(POOL_LOCK, &pool_head_buffer->lock);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100453#endif
Willy Tarreaubafbe012017-11-24 17:34:44 +0100454 b->size = pool_head_buffer->size - sizeof(struct buffer);
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100455 b_reset(b);
456 *buf = b;
457 return b;
458 }
459 }
Willy Tarreauf4718e82014-12-02 13:54:01 +0100460
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100461 /* slow path, uses malloc() */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100462 b = __pool_refill_alloc(pool_head_buffer, margin);
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100463
Willy Tarreauf161d0f2018-02-22 14:05:55 +0100464#ifndef CONFIG_HAP_LOCKLESS_POOLS
Willy Tarreaubafbe012017-11-24 17:34:44 +0100465 HA_SPIN_UNLOCK(POOL_LOCK, &pool_head_buffer->lock);
Olivier Houchardcf975d42018-01-24 18:38:31 +0100466#endif
Willy Tarreauf4718e82014-12-02 13:54:01 +0100467
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100468 if (b) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100469 b->size = pool_head_buffer->size - sizeof(struct buffer);
Christopher Fauletfa5c8122017-11-10 10:39:16 +0100470 b_reset(b);
471 *buf = b;
472 }
473 return b;
Willy Tarreauf4718e82014-12-02 13:54:01 +0100474}
475
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100476
Willy Tarreauc41b3e82018-03-02 10:27:12 +0100477/* Offer a buffer currently belonging to target <from> to whoever needs one.
478 * Any pointer is valid for <from>, including NULL. Its purpose is to avoid
479 * passing a buffer to oneself in case of failed allocations (e.g. need two
480 * buffers, get one, fail, release it and wake up self again). In case of
481 * normal buffer release where it is expected that the caller is not waiting
482 * for a buffer, NULL is fine.
483 */
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100484void __offer_buffer(void *from, unsigned int threshold);
485
486static inline void offer_buffers(void *from, unsigned int threshold)
487{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100488 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Emeric Bruna1dd2432017-06-21 15:42:52 +0200489 if (LIST_ISEMPTY(&buffer_wq)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100490 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100491 return;
Emeric Bruna1dd2432017-06-21 15:42:52 +0200492 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100493 __offer_buffer(from, threshold);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100494 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100495}
496
Willy Tarreau6634b632017-09-22 15:02:54 +0200497/*************************************************************************/
498/* functions used to manipulate strings and blocks with wrapping buffers */
499/*************************************************************************/
500
501/* returns > 0 if the first <n> characters of buffer <b> starting at
502 * offset <o> relative to b->p match <ist>. (empty strings do match). It is
503 * designed to be use with reasonably small strings (ie matches a single byte
504 * per iteration). This function is usable both with input and output data. To
505 * be used like this depending on what to match :
506 * - input contents : b_isteq(b, 0, b->i, ist);
507 * - output contents : b_isteq(b, -b->o, b->o, ist);
508 * Return value :
509 * >0 : the number of matching bytes
510 * =0 : not enough bytes (or matching of empty string)
511 * <0 : non-matching byte found
512 */
513static inline int b_isteq(const struct buffer *b, unsigned int o, size_t n, const struct ist ist)
514{
515 struct ist r = ist;
516 const char *p;
517 const char *end = b->data + b->size;
518
519 if (n < r.len)
520 return 0;
521
522 p = b_ptr(b, o);
523 while (r.len--) {
524 if (*p++ != *r.ptr++)
525 return -1;
526 if (unlikely(p == end))
527 p = b->data;
528 }
529 return ist.len;
530}
531
532/* "eats" string <ist> from the input region of buffer <b>. Wrapping data is
533 * explicitly supported. It matches a single byte per iteration so strings
534 * should remain reasonably small. Returns :
535 * > 0 : number of bytes matched and eaten
536 * = 0 : not enough bytes (or matching an empty string)
537 * < 0 : non-matching byte found
538 */
539static inline int bi_eat(struct buffer *b, const struct ist ist)
540{
541 int ret = b_isteq(b, 0, b->i, ist);
542 if (ret > 0)
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200543 b_del(b, ret);
Willy Tarreau6634b632017-09-22 15:02:54 +0200544 return ret;
545}
546
Willy Tarreaue5676e72017-09-22 15:47:51 +0200547/* injects string <ist> into the input region of buffer <b> provided that it
548 * fits. Wrapping is supported. It's designed for small strings as it only
549 * writes a single byte per iteration. Returns the number of characters copied
550 * (ist.len), 0 if it temporarily does not fit or -1 if it will never fit. It
551 * will only modify the buffer upon success. In all cases, the contents are
552 * copied prior to reporting an error, so that the destination at least
553 * contains a valid but truncated string.
554 */
555static inline int bi_istput(struct buffer *b, const struct ist ist)
556{
557 const char *end = b->data + b->size;
558 struct ist r = ist;
559 char *p;
560
561 if (r.len > (size_t)(b->size - b->i - b->o))
562 return r.len < b->size ? 0 : -1;
563
564 p = b_ptr(b, b->i);
565 b->i += r.len;
566 while (r.len--) {
567 *p++ = *r.ptr++;
568 if (unlikely(p == end))
569 p = b->data;
570 }
571 return ist.len;
572}
573
574
575/* injects string <ist> into the output region of buffer <b> provided that it
576 * fits. Input data is assumed not to exist and will silently be overwritten.
577 * Wrapping is supported. It's designed for small strings as it only writes a
578 * single byte per iteration. Returns the number of characters copied (ist.len),
579 * 0 if it temporarily does not fit or -1 if it will never fit. It will only
580 * modify the buffer upon success. In all cases, the contents are copied prior
581 * to reporting an error, so that the destination at least contains a valid
582 * but truncated string.
583 */
584static inline int bo_istput(struct buffer *b, const struct ist ist)
585{
586 const char *end = b->data + b->size;
587 struct ist r = ist;
588 char *p;
589
590 if (r.len > (size_t)(b->size - b->o))
591 return r.len < b->size ? 0 : -1;
592
593 p = b->p;
594 b->o += r.len;
595 b->p = b_ptr(b, r.len);
596 while (r.len--) {
597 *p++ = *r.ptr++;
598 if (unlikely(p == end))
599 p = b->data;
600 }
601 return ist.len;
602}
603
604
Willy Tarreauc7e42382012-08-24 19:22:53 +0200605#endif /* _COMMON_BUFFER_H */
606
607/*
608 * Local variables:
609 * c-indent-level: 8
610 * c-basic-offset: 8
611 * End:
612 */