blob: 346521a555f0bf07cf0ba8e091cb103c9d90d3c2 [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
Willy Tarreauc7e42382012-08-24 19:22:53 +0200236/* Normalizes a pointer which is supposed to be relative to the beginning of a
237 * buffer, so that wrapping is correctly handled. The intent is to use this
238 * when increasing a pointer. Note that the wrapping test is only performed
239 * once, so the original pointer must be between ->data-size and ->data+2*size-1,
240 * otherwise an invalid pointer might be returned.
241 */
242static inline const char *buffer_pointer(const struct buffer *buf, const char *ptr)
243{
244 if (ptr < buf->data)
245 ptr += buf->size;
246 else if (ptr - buf->size >= buf->data)
247 ptr -= buf->size;
248 return ptr;
249}
250
251/* Returns the distance between two pointers, taking into account the ability
252 * to wrap around the buffer's end.
253 */
254static inline int buffer_count(const struct buffer *buf, const char *from, const char *to)
255{
256 int count = to - from;
Willy Tarreaubf439272013-04-02 01:25:57 +0200257
258 count += count < 0 ? buf->size : 0;
Willy Tarreauc7e42382012-08-24 19:22:53 +0200259 return count;
260}
261
262/* returns the amount of pending bytes in the buffer. It is the amount of bytes
263 * that is not scheduled to be sent.
264 */
265static inline int buffer_pending(const struct buffer *buf)
266{
267 return buf->i;
268}
269
270/* Returns the size of the working area which the caller knows ends at <end>.
271 * If <end> equals buf->r (modulo size), then it means that the free area which
272 * follows is part of the working area. Otherwise, the working area stops at
273 * <end>. It always starts at buf->p. The work area includes the
274 * reserved area.
275 */
276static inline int buffer_work_area(const struct buffer *buf, const char *end)
277{
278 end = buffer_pointer(buf, end);
279 if (end == buffer_wrap_add(buf, buf->p + buf->i))
280 /* pointer exactly at end, lets push forwards */
281 end = buffer_wrap_sub(buf, buf->p - buf->o);
282 return buffer_count(buf, buf->p, end);
283}
284
285/* Return 1 if the buffer has less than 1/4 of its capacity free, otherwise 0 */
286static inline int buffer_almost_full(const struct buffer *buf)
287{
Willy Tarreau4428a292014-11-28 20:54:13 +0100288 if (buf == &buf_empty)
289 return 0;
290
291 if (!buf->size || buffer_total_space(buf) < buf->size / 4)
Willy Tarreauc7e42382012-08-24 19:22:53 +0200292 return 1;
293 return 0;
294}
295
296/* Cut the first <n> pending bytes in a contiguous buffer. It is illegal to
297 * call this function with remaining data waiting to be sent (o > 0). The
298 * caller must ensure that <n> is smaller than the actual buffer's length.
299 * This is mainly used to remove empty lines at the beginning of a request
300 * or a response.
301 */
302static inline void bi_fast_delete(struct buffer *buf, int n)
303{
304 buf->i -= n;
305 buf->p += n;
306}
307
308/*
309 * Tries to realign the given buffer, and returns how many bytes can be written
310 * there at once without overwriting anything.
311 */
312static inline int buffer_realign(struct buffer *buf)
313{
314 if (!(buf->i | buf->o)) {
315 /* let's realign the buffer to optimize I/O */
316 buf->p = buf->data;
317 }
318 return buffer_contig_space(buf);
319}
320
Willy Tarreaua75bcef2012-08-24 22:56:11 +0200321/* Schedule all remaining buffer data to be sent. ->o is not touched if it
322 * already covers those data. That permits doing a flush even after a forward,
323 * although not recommended.
324 */
325static inline void buffer_flush(struct buffer *buf)
326{
327 buf->p = buffer_wrap_add(buf, buf->p + buf->i);
328 buf->o += buf->i;
329 buf->i = 0;
330}
Willy Tarreauc7e42382012-08-24 19:22:53 +0200331
Willy Tarreauaf819352012-08-27 22:08:00 +0200332/* This function writes the string <str> at position <pos> which must be in
333 * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters
334 * (l, r, lr) are updated to be valid after the shift. the shift value
335 * (positive or negative) is returned. If there's no space left, the move is
336 * not done. The function does not adjust ->o because it does not make sense
337 * to use it on data scheduled to be sent.
338 */
339static inline int buffer_replace(struct buffer *b, char *pos, char *end, const char *str)
340{
341 return buffer_replace2(b, pos, end, str, strlen(str));
342}
343
Willy Tarreau8c89c202012-09-28 16:02:48 +0200344/* Tries to write char <c> into output data at buffer <b>. Supports wrapping.
345 * Data are truncated if buffer is full.
346 */
347static inline void bo_putchr(struct buffer *b, char c)
348{
349 if (buffer_len(b) == b->size)
350 return;
351 *b->p = c;
352 b->p = b_ptr(b, 1);
353 b->o++;
354}
355
356/* Tries to copy block <blk> into output data at buffer <b>. Supports wrapping.
357 * Data are truncated if buffer is too short.
358 */
359static inline void bo_putblk(struct buffer *b, const char *blk, int len)
360{
361 int cur_len = buffer_len(b);
362 int half;
363
364 if (len > b->size - cur_len)
365 len = (b->size - cur_len);
366 if (!len)
367 return;
368
369 half = buffer_contig_space(b);
370 if (half > len)
371 half = len;
372
373 memcpy(b->p, blk, half);
374 b->p = b_ptr(b, half);
375 if (len > half) {
376 memcpy(b->p, blk, len - half);
377 b->p = b_ptr(b, half);
378 }
379 b->o += len;
380}
381
382/* Tries to copy string <str> into output data at buffer <b>. Supports wrapping.
383 * Data are truncated if buffer is too short.
384 */
385static inline void bo_putstr(struct buffer *b, const char *str)
386{
387 return bo_putblk(b, str, strlen(str));
388}
389
390/* Tries to copy chunk <chk> into output data at buffer <b>. Supports wrapping.
391 * Data are truncated if buffer is too short.
392 */
393static inline void bo_putchk(struct buffer *b, const struct chunk *chk)
394{
395 return bo_putblk(b, chk->str, chk->len);
396}
397
Willy Tarreau474cf542014-11-24 10:54:47 +0100398/* Resets a buffer. The size is not touched. */
399static inline void b_reset(struct buffer *buf)
400{
401 buf->o = 0;
402 buf->i = 0;
403 buf->p = buf->data;
404}
405
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100406/* Allocates a buffer and replaces *buf with this buffer. If no memory is
407 * available, &buf_wanted is used instead. No control is made to check if *buf
408 * already pointed to another buffer. The allocated buffer is returned, or
409 * NULL in case no memory is available.
Willy Tarreaue583ea52014-11-24 11:30:16 +0100410 */
411static inline struct buffer *b_alloc(struct buffer **buf)
412{
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100413 struct buffer *b;
414
415 *buf = &buf_wanted;
416 b = pool_alloc_dirty(pool2_buffer);
417 if (likely(b)) {
418 b->size = pool2_buffer->size - sizeof(struct buffer);
419 b_reset(b);
420 *buf = b;
Willy Tarreaue583ea52014-11-24 11:30:16 +0100421 }
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +0100422 return b;
Willy Tarreaue583ea52014-11-24 11:30:16 +0100423}
424
Willy Tarreau620bd6c2014-12-08 16:37:26 +0100425/* Allocates a buffer and replaces *buf with this buffer. If no memory is
426 * available, &buf_wanted is used instead. No control is made to check if *buf
427 * already pointed to another buffer. The allocated buffer is returned, or
428 * NULL in case no memory is available. The difference with b_alloc() is that
429 * this function only picks from the pool and never calls malloc(), so it can
430 * fail even if some memory is available.
431 */
432static inline struct buffer *b_alloc_fast(struct buffer **buf)
433{
434 struct buffer *b;
435
436 *buf = &buf_wanted;
437 b = pool_get_first(pool2_buffer);
438 if (likely(b)) {
439 b->size = pool2_buffer->size - sizeof(struct buffer);
440 b_reset(b);
441 *buf = b;
442 }
443 return b;
444}
445
Willy Tarreau2a4b5432014-11-24 11:39:34 +0100446/* Releases buffer *buf (no check of emptiness) */
447static inline void __b_drop(struct buffer **buf)
Willy Tarreau7dfca9d2014-11-25 19:45:11 +0100448{
449 pool_free2(pool2_buffer, *buf);
450}
451
Willy Tarreau2a4b5432014-11-24 11:39:34 +0100452/* Releases buffer *buf if allocated. */
453static inline void b_drop(struct buffer **buf)
454{
455 if (!(*buf)->size)
456 return;
457 __b_drop(buf);
458}
459
460/* Releases buffer *buf if allocated, and replaces it with &buf_empty. */
461static inline void b_free(struct buffer **buf)
462{
463 b_drop(buf);
464 *buf = &buf_empty;
465}
466
Willy Tarreauf4718e82014-12-02 13:54:01 +0100467/* Ensures that <buf> is allocated. If an allocation is needed, it ensures that
468 * there are still at least <margin> buffers available in the pool after this
469 * allocation so that we don't leave the pool in a condition where a session or
470 * a response buffer could not be allocated anymore, resulting in a deadlock.
471 * This means that we sometimes need to try to allocate extra entries even if
472 * only one buffer is needed.
473 */
474static inline struct buffer *b_alloc_margin(struct buffer **buf, int margin)
475{
476 struct buffer *next;
477
478 if ((*buf)->size)
479 return *buf;
480
481 /* fast path */
482 if ((pool2_buffer->allocated - pool2_buffer->used) > margin)
483 return b_alloc_fast(buf);
484
485 next = pool_refill_alloc(pool2_buffer, margin);
486 if (!next)
487 return next;
488
489 next->size = pool2_buffer->size - sizeof(struct buffer);
490 b_reset(next);
491 *buf = next;
492 return next;
493}
494
Willy Tarreauc7e42382012-08-24 19:22:53 +0200495#endif /* _COMMON_BUFFER_H */
496
497/*
498 * Local variables:
499 * c-indent-level: 8
500 * c-basic-offset: 8
501 * End:
502 */