Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 1 | /* |
| 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 Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 29 | #include <common/chunk.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 30 | #include <common/config.h> |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 31 | #include <common/memory.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 32 | |
| 33 | |
| 34 | struct 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 | |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 42 | /* an element of the <buffer_wq> list. It represents an object that need to |
| 43 | * acquire a buffer to continue its process. */ |
| 44 | struct buffer_wait { |
| 45 | void *target; /* The waiting object that should be woken up */ |
| 46 | int (*wakeup_cb)(void *); /* The function used to wake up the <target>, passed as argument */ |
| 47 | struct list list; /* Next element in the <buffer_wq> list */ |
| 48 | }; |
| 49 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 50 | extern struct pool_head *pool2_buffer; |
Willy Tarreau | 2a4b543 | 2014-11-24 11:39:34 +0100 | [diff] [blame] | 51 | extern struct buffer buf_empty; |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 52 | extern struct buffer buf_wanted; |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 53 | extern struct list buffer_wq; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 54 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 55 | int init_buffer(); |
Christopher Faulet | ad405f1 | 2017-08-29 15:30:11 +0200 | [diff] [blame] | 56 | void deinit_buffer(); |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 57 | int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len); |
| 58 | int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len); |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 59 | void buffer_dump(FILE *o, struct buffer *b, int from, int to); |
| 60 | void buffer_slow_realign(struct buffer *buf); |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 61 | |
| 62 | /*****************************************************************/ |
| 63 | /* These functions are used to compute various buffer area sizes */ |
| 64 | /*****************************************************************/ |
| 65 | |
| 66 | /* Returns an absolute pointer for a position relative to the current buffer's |
| 67 | * pointer. It is written so that it is optimal when <ofs> is a const. It is |
| 68 | * written as a macro instead of an inline function so that the compiler knows |
| 69 | * when it can optimize out the sign test on <ofs> when passed an unsigned int. |
Willy Tarreau | ce39bfb | 2012-09-22 18:36:29 +0200 | [diff] [blame] | 70 | * Note that callers MUST cast <ofs> to int if they expect negative values. |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 71 | */ |
| 72 | #define b_ptr(b, ofs) \ |
| 73 | ({ \ |
| 74 | char *__ret = (b)->p + (ofs); \ |
| 75 | if ((ofs) > 0 && __ret >= (b)->data + (b)->size) \ |
| 76 | __ret -= (b)->size; \ |
| 77 | else if ((ofs) < 0 && __ret < (b)->data) \ |
| 78 | __ret += (b)->size; \ |
| 79 | __ret; \ |
| 80 | }) |
| 81 | |
Willy Tarreau | 26488ad | 2017-09-19 21:14:08 +0200 | [diff] [blame] | 82 | /* Returns the pointer to the buffer's end (data+size) */ |
| 83 | static inline const char *b_end(const struct buffer *b) |
| 84 | { |
| 85 | return b->data + b->size; |
| 86 | } |
| 87 | |
| 88 | /* Returns the distance between <p> and the buffer's end (data+size) */ |
| 89 | static inline unsigned int b_to_end(const struct buffer *b) |
| 90 | { |
| 91 | return b->data + b->size - b->p; |
| 92 | } |
| 93 | |
Willy Tarreau | 4a6425d | 2017-09-19 14:18:46 +0200 | [diff] [blame] | 94 | /* Skips <del> bytes in a one-way buffer <b> : <p> advances by <del>, <i> |
| 95 | * shrinks by <del> as well, and <o> is left untouched (supposed to be zero). |
| 96 | * The caller is responsible for ensuring that <del> is always smaller than or |
| 97 | * equal to b->i. |
| 98 | */ |
| 99 | static inline void b_del(struct buffer *b, unsigned int del) |
| 100 | { |
| 101 | b->i -= del; |
| 102 | b->p = b_ptr(b, del); |
| 103 | } |
| 104 | |
Willy Tarreau | a75bcef | 2012-08-24 22:56:11 +0200 | [diff] [blame] | 105 | /* Advances the buffer by <adv> bytes, which means that the buffer |
| 106 | * pointer advances, and that as many bytes from in are transferred |
| 107 | * to out. The caller is responsible for ensuring that adv is always |
| 108 | * smaller than or equal to b->i. |
| 109 | */ |
| 110 | static inline void b_adv(struct buffer *b, unsigned int adv) |
| 111 | { |
| 112 | b->i -= adv; |
| 113 | b->o += adv; |
| 114 | b->p = b_ptr(b, adv); |
| 115 | } |
| 116 | |
| 117 | /* Rewinds the buffer by <adv> bytes, which means that the buffer pointer goes |
| 118 | * backwards, and that as many bytes from out are moved to in. The caller is |
| 119 | * responsible for ensuring that adv is always smaller than or equal to b->o. |
| 120 | */ |
| 121 | static inline void b_rew(struct buffer *b, unsigned int adv) |
| 122 | { |
| 123 | b->i += adv; |
| 124 | b->o -= adv; |
| 125 | b->p = b_ptr(b, (int)-adv); |
| 126 | } |
| 127 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 128 | /* Returns the start of the input data in a buffer */ |
| 129 | static inline char *bi_ptr(const struct buffer *b) |
| 130 | { |
| 131 | return b->p; |
| 132 | } |
| 133 | |
| 134 | /* Returns the end of the input data in a buffer (pointer to next |
| 135 | * insertion point). |
| 136 | */ |
| 137 | static inline char *bi_end(const struct buffer *b) |
| 138 | { |
| 139 | char *ret = b->p + b->i; |
| 140 | |
| 141 | if (ret >= b->data + b->size) |
| 142 | ret -= b->size; |
| 143 | return ret; |
| 144 | } |
| 145 | |
| 146 | /* Returns the amount of input data that can contiguously be read at once */ |
| 147 | static inline int bi_contig_data(const struct buffer *b) |
| 148 | { |
| 149 | int data = b->data + b->size - b->p; |
| 150 | |
| 151 | if (data > b->i) |
| 152 | data = b->i; |
| 153 | return data; |
| 154 | } |
| 155 | |
| 156 | /* Returns the start of the output data in a buffer */ |
| 157 | static inline char *bo_ptr(const struct buffer *b) |
| 158 | { |
| 159 | char *ret = b->p - b->o; |
| 160 | |
| 161 | if (ret < b->data) |
| 162 | ret += b->size; |
| 163 | return ret; |
| 164 | } |
| 165 | |
| 166 | /* Returns the end of the output data in a buffer */ |
| 167 | static inline char *bo_end(const struct buffer *b) |
| 168 | { |
| 169 | return b->p; |
| 170 | } |
| 171 | |
| 172 | /* Returns the amount of output data that can contiguously be read at once */ |
| 173 | static inline int bo_contig_data(const struct buffer *b) |
| 174 | { |
| 175 | char *beg = b->p - b->o; |
| 176 | |
| 177 | if (beg < b->data) |
| 178 | return b->data - beg; |
| 179 | return b->o; |
| 180 | } |
| 181 | |
Christopher Faulet | 637f8f2 | 2017-03-29 11:58:28 +0200 | [diff] [blame] | 182 | /* Return the amount of bytes that can be written into the input area at once |
| 183 | * including reserved space which may be overwritten (this is the caller |
| 184 | * responsibility to know if the reserved space is protected or not). |
| 185 | */ |
| 186 | static inline int bi_contig_space(const struct buffer *b) |
| 187 | { |
| 188 | const char *left, *right; |
| 189 | |
Christopher Faulet | a36b311 | 2017-06-13 22:00:22 +0200 | [diff] [blame] | 190 | left = b->p + b->i; |
| 191 | right = b->p - b->o; |
| 192 | if (left >= b->data + b->size) |
| 193 | left -= b->size; |
| 194 | else { |
| 195 | if (right < b->data) |
| 196 | right += b->size; |
| 197 | else |
| 198 | right = b->data + b->size; |
| 199 | } |
Christopher Faulet | 637f8f2 | 2017-03-29 11:58:28 +0200 | [diff] [blame] | 200 | return (right - left); |
| 201 | } |
| 202 | |
| 203 | /* Return the amount of bytes that can be written into the output area at once |
| 204 | * including reserved space which may be overwritten (this is the caller |
| 205 | * responsibility to know if the reserved space is protected or not). Input data |
| 206 | * are assumed to not exist. |
| 207 | */ |
| 208 | static inline int bo_contig_space(const struct buffer *b) |
| 209 | { |
| 210 | const char *left, *right; |
| 211 | |
Christopher Faulet | a36b311 | 2017-06-13 22:00:22 +0200 | [diff] [blame] | 212 | left = b->p; |
| 213 | right = b->p - b->o; |
| 214 | if (right < b->data) |
| 215 | right += b->size; |
| 216 | else |
Christopher Faulet | 637f8f2 | 2017-03-29 11:58:28 +0200 | [diff] [blame] | 217 | right = b->data + b->size; |
| 218 | |
| 219 | return (right - left); |
| 220 | } |
| 221 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 222 | /* Return the buffer's length in bytes by summing the input and the output */ |
| 223 | static inline int buffer_len(const struct buffer *buf) |
| 224 | { |
| 225 | return buf->i + buf->o; |
| 226 | } |
| 227 | |
| 228 | /* Return non-zero only if the buffer is not empty */ |
| 229 | static inline int buffer_not_empty(const struct buffer *buf) |
| 230 | { |
| 231 | return buf->i | buf->o; |
| 232 | } |
| 233 | |
| 234 | /* Return non-zero only if the buffer is empty */ |
| 235 | static inline int buffer_empty(const struct buffer *buf) |
| 236 | { |
| 237 | return !buffer_not_empty(buf); |
| 238 | } |
| 239 | |
Willy Tarreau | 42d0666 | 2012-08-27 19:51:36 +0200 | [diff] [blame] | 240 | /* Returns non-zero if the buffer's INPUT is considered full, which means that |
| 241 | * it holds at least as much INPUT data as (size - reserve). This also means |
| 242 | * that data that are scheduled for output are considered as potential free |
| 243 | * space, and that the reserved space is always considered as not usable. This |
| 244 | * information alone cannot be used as a general purpose free space indicator. |
| 245 | * However it accurately indicates that too many data were fed in the buffer |
Willy Tarreau | 3889fff | 2015-01-13 20:20:10 +0100 | [diff] [blame] | 246 | * for an analyzer for instance. See the channel_may_recv() function for a more |
Willy Tarreau | 42d0666 | 2012-08-27 19:51:36 +0200 | [diff] [blame] | 247 | * generic function taking everything into account. |
| 248 | */ |
| 249 | static inline int buffer_full(const struct buffer *b, unsigned int reserve) |
| 250 | { |
Willy Tarreau | 4428a29 | 2014-11-28 20:54:13 +0100 | [diff] [blame] | 251 | if (b == &buf_empty) |
| 252 | return 0; |
| 253 | |
Willy Tarreau | 42d0666 | 2012-08-27 19:51:36 +0200 | [diff] [blame] | 254 | return (b->i + reserve >= b->size); |
| 255 | } |
| 256 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 257 | /* Normalizes a pointer after a subtract */ |
| 258 | static inline char *buffer_wrap_sub(const struct buffer *buf, char *ptr) |
| 259 | { |
| 260 | if (ptr < buf->data) |
| 261 | ptr += buf->size; |
| 262 | return ptr; |
| 263 | } |
| 264 | |
| 265 | /* Normalizes a pointer after an addition */ |
| 266 | static inline char *buffer_wrap_add(const struct buffer *buf, char *ptr) |
| 267 | { |
| 268 | if (ptr - buf->size >= buf->data) |
| 269 | ptr -= buf->size; |
| 270 | return ptr; |
| 271 | } |
| 272 | |
| 273 | /* Return the maximum amount of bytes that can be written into the buffer, |
| 274 | * including reserved space which may be overwritten. |
| 275 | */ |
| 276 | static inline int buffer_total_space(const struct buffer *buf) |
| 277 | { |
| 278 | return buf->size - buffer_len(buf); |
| 279 | } |
| 280 | |
Thierry FOURNIER | d2b597a | 2015-03-07 14:38:50 +0100 | [diff] [blame] | 281 | /* Returns the amount of byte that can be written starting from <p> into the |
| 282 | * input buffer at once, including reserved space which may be overwritten. |
| 283 | * This is used by Lua to insert data in the input side just before the other |
| 284 | * data using buffer_replace(). The goal is to transfer these new data in the |
| 285 | * output buffer. |
| 286 | */ |
| 287 | static inline int bi_space_for_replace(const struct buffer *buf) |
| 288 | { |
| 289 | const char *end; |
| 290 | |
| 291 | /* If the input side data overflows, we cannot insert data contiguously. */ |
| 292 | if (buf->p + buf->i >= buf->data + buf->size) |
| 293 | return 0; |
| 294 | |
| 295 | /* Check the last byte used in the buffer, it may be a byte of the output |
| 296 | * side if the buffer wraps, or its the end of the buffer. |
| 297 | */ |
| 298 | end = buffer_wrap_sub(buf, buf->p - buf->o); |
| 299 | if (end <= buf->p) |
| 300 | end = buf->data + buf->size; |
| 301 | |
| 302 | /* Compute the amount of bytes which can be written. */ |
| 303 | return end - (buf->p + buf->i); |
| 304 | } |
| 305 | |
| 306 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 307 | /* Normalizes a pointer which is supposed to be relative to the beginning of a |
| 308 | * buffer, so that wrapping is correctly handled. The intent is to use this |
| 309 | * when increasing a pointer. Note that the wrapping test is only performed |
| 310 | * once, so the original pointer must be between ->data-size and ->data+2*size-1, |
| 311 | * otherwise an invalid pointer might be returned. |
| 312 | */ |
| 313 | static inline const char *buffer_pointer(const struct buffer *buf, const char *ptr) |
| 314 | { |
| 315 | if (ptr < buf->data) |
| 316 | ptr += buf->size; |
| 317 | else if (ptr - buf->size >= buf->data) |
| 318 | ptr -= buf->size; |
| 319 | return ptr; |
| 320 | } |
| 321 | |
| 322 | /* Returns the distance between two pointers, taking into account the ability |
| 323 | * to wrap around the buffer's end. |
| 324 | */ |
| 325 | static inline int buffer_count(const struct buffer *buf, const char *from, const char *to) |
| 326 | { |
| 327 | int count = to - from; |
Willy Tarreau | bf43927 | 2013-04-02 01:25:57 +0200 | [diff] [blame] | 328 | |
| 329 | count += count < 0 ? buf->size : 0; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 330 | return count; |
| 331 | } |
| 332 | |
| 333 | /* returns the amount of pending bytes in the buffer. It is the amount of bytes |
| 334 | * that is not scheduled to be sent. |
| 335 | */ |
| 336 | static inline int buffer_pending(const struct buffer *buf) |
| 337 | { |
| 338 | return buf->i; |
| 339 | } |
| 340 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 341 | /* Return 1 if the buffer has less than 1/4 of its capacity free, otherwise 0 */ |
| 342 | static inline int buffer_almost_full(const struct buffer *buf) |
| 343 | { |
Willy Tarreau | 4428a29 | 2014-11-28 20:54:13 +0100 | [diff] [blame] | 344 | if (buf == &buf_empty) |
| 345 | return 0; |
| 346 | |
| 347 | if (!buf->size || buffer_total_space(buf) < buf->size / 4) |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 348 | return 1; |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | /* Cut the first <n> pending bytes in a contiguous buffer. It is illegal to |
| 353 | * call this function with remaining data waiting to be sent (o > 0). The |
| 354 | * caller must ensure that <n> is smaller than the actual buffer's length. |
| 355 | * This is mainly used to remove empty lines at the beginning of a request |
| 356 | * or a response. |
| 357 | */ |
| 358 | static inline void bi_fast_delete(struct buffer *buf, int n) |
| 359 | { |
| 360 | buf->i -= n; |
| 361 | buf->p += n; |
| 362 | } |
| 363 | |
Christopher Faulet | 637f8f2 | 2017-03-29 11:58:28 +0200 | [diff] [blame] | 364 | /* Tries to realign the given buffer. */ |
| 365 | static inline void buffer_realign(struct buffer *buf) |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 366 | { |
| 367 | if (!(buf->i | buf->o)) { |
| 368 | /* let's realign the buffer to optimize I/O */ |
| 369 | buf->p = buf->data; |
| 370 | } |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 371 | } |
| 372 | |
Willy Tarreau | a75bcef | 2012-08-24 22:56:11 +0200 | [diff] [blame] | 373 | /* Schedule all remaining buffer data to be sent. ->o is not touched if it |
| 374 | * already covers those data. That permits doing a flush even after a forward, |
| 375 | * although not recommended. |
| 376 | */ |
| 377 | static inline void buffer_flush(struct buffer *buf) |
| 378 | { |
| 379 | buf->p = buffer_wrap_add(buf, buf->p + buf->i); |
| 380 | buf->o += buf->i; |
| 381 | buf->i = 0; |
| 382 | } |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 383 | |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 384 | /* This function writes the string <str> at position <pos> which must be in |
| 385 | * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters |
| 386 | * (l, r, lr) are updated to be valid after the shift. the shift value |
| 387 | * (positive or negative) is returned. If there's no space left, the move is |
| 388 | * not done. The function does not adjust ->o because it does not make sense |
| 389 | * to use it on data scheduled to be sent. |
| 390 | */ |
| 391 | static inline int buffer_replace(struct buffer *b, char *pos, char *end, const char *str) |
| 392 | { |
| 393 | return buffer_replace2(b, pos, end, str, strlen(str)); |
| 394 | } |
| 395 | |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 396 | /* Tries to write char <c> into output data at buffer <b>. Supports wrapping. |
| 397 | * Data are truncated if buffer is full. |
| 398 | */ |
| 399 | static inline void bo_putchr(struct buffer *b, char c) |
| 400 | { |
| 401 | if (buffer_len(b) == b->size) |
| 402 | return; |
| 403 | *b->p = c; |
| 404 | b->p = b_ptr(b, 1); |
| 405 | b->o++; |
| 406 | } |
| 407 | |
| 408 | /* Tries to copy block <blk> into output data at buffer <b>. Supports wrapping. |
Thierry FOURNIER | 549aac8 | 2015-02-06 18:40:20 +0100 | [diff] [blame] | 409 | * Data are truncated if buffer is too short. It returns the number of bytes |
| 410 | * copied. |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 411 | */ |
Thierry FOURNIER | 549aac8 | 2015-02-06 18:40:20 +0100 | [diff] [blame] | 412 | static inline int bo_putblk(struct buffer *b, const char *blk, int len) |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 413 | { |
| 414 | int cur_len = buffer_len(b); |
| 415 | int half; |
| 416 | |
| 417 | if (len > b->size - cur_len) |
| 418 | len = (b->size - cur_len); |
| 419 | if (!len) |
Thierry FOURNIER | 549aac8 | 2015-02-06 18:40:20 +0100 | [diff] [blame] | 420 | return 0; |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 421 | |
Christopher Faulet | 637f8f2 | 2017-03-29 11:58:28 +0200 | [diff] [blame] | 422 | half = bo_contig_space(b); |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 423 | if (half > len) |
| 424 | half = len; |
| 425 | |
| 426 | memcpy(b->p, blk, half); |
| 427 | b->p = b_ptr(b, half); |
| 428 | if (len > half) { |
| 429 | memcpy(b->p, blk, len - half); |
| 430 | b->p = b_ptr(b, half); |
| 431 | } |
| 432 | b->o += len; |
Thierry FOURNIER | 549aac8 | 2015-02-06 18:40:20 +0100 | [diff] [blame] | 433 | return len; |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | /* Tries to copy string <str> into output data at buffer <b>. Supports wrapping. |
Thierry FOURNIER | 549aac8 | 2015-02-06 18:40:20 +0100 | [diff] [blame] | 437 | * Data are truncated if buffer is too short. It returns the number of bytes |
| 438 | * copied. |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 439 | */ |
Thierry FOURNIER | 549aac8 | 2015-02-06 18:40:20 +0100 | [diff] [blame] | 440 | static inline int bo_putstr(struct buffer *b, const char *str) |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 441 | { |
| 442 | return bo_putblk(b, str, strlen(str)); |
| 443 | } |
| 444 | |
| 445 | /* Tries to copy chunk <chk> into output data at buffer <b>. Supports wrapping. |
Thierry FOURNIER | 549aac8 | 2015-02-06 18:40:20 +0100 | [diff] [blame] | 446 | * Data are truncated if buffer is too short. It returns the number of bytes |
| 447 | * copied. |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 448 | */ |
Thierry FOURNIER | 549aac8 | 2015-02-06 18:40:20 +0100 | [diff] [blame] | 449 | static inline int bo_putchk(struct buffer *b, const struct chunk *chk) |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 450 | { |
| 451 | return bo_putblk(b, chk->str, chk->len); |
| 452 | } |
| 453 | |
Willy Tarreau | 474cf54 | 2014-11-24 10:54:47 +0100 | [diff] [blame] | 454 | /* Resets a buffer. The size is not touched. */ |
| 455 | static inline void b_reset(struct buffer *buf) |
| 456 | { |
| 457 | buf->o = 0; |
| 458 | buf->i = 0; |
| 459 | buf->p = buf->data; |
| 460 | } |
| 461 | |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 462 | /* Allocates a buffer and replaces *buf with this buffer. If no memory is |
| 463 | * available, &buf_wanted is used instead. No control is made to check if *buf |
| 464 | * already pointed to another buffer. The allocated buffer is returned, or |
| 465 | * NULL in case no memory is available. |
Willy Tarreau | e583ea5 | 2014-11-24 11:30:16 +0100 | [diff] [blame] | 466 | */ |
| 467 | static inline struct buffer *b_alloc(struct buffer **buf) |
| 468 | { |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 469 | struct buffer *b; |
| 470 | |
| 471 | *buf = &buf_wanted; |
| 472 | b = pool_alloc_dirty(pool2_buffer); |
| 473 | if (likely(b)) { |
| 474 | b->size = pool2_buffer->size - sizeof(struct buffer); |
| 475 | b_reset(b); |
| 476 | *buf = b; |
Willy Tarreau | e583ea5 | 2014-11-24 11:30:16 +0100 | [diff] [blame] | 477 | } |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 478 | return b; |
Willy Tarreau | e583ea5 | 2014-11-24 11:30:16 +0100 | [diff] [blame] | 479 | } |
| 480 | |
Willy Tarreau | 620bd6c | 2014-12-08 16:37:26 +0100 | [diff] [blame] | 481 | /* Allocates a buffer and replaces *buf with this buffer. If no memory is |
| 482 | * available, &buf_wanted is used instead. No control is made to check if *buf |
| 483 | * already pointed to another buffer. The allocated buffer is returned, or |
| 484 | * NULL in case no memory is available. The difference with b_alloc() is that |
| 485 | * this function only picks from the pool and never calls malloc(), so it can |
| 486 | * fail even if some memory is available. |
| 487 | */ |
| 488 | static inline struct buffer *b_alloc_fast(struct buffer **buf) |
| 489 | { |
| 490 | struct buffer *b; |
| 491 | |
| 492 | *buf = &buf_wanted; |
| 493 | b = pool_get_first(pool2_buffer); |
| 494 | if (likely(b)) { |
| 495 | b->size = pool2_buffer->size - sizeof(struct buffer); |
| 496 | b_reset(b); |
| 497 | *buf = b; |
| 498 | } |
| 499 | return b; |
| 500 | } |
| 501 | |
Willy Tarreau | 2a4b543 | 2014-11-24 11:39:34 +0100 | [diff] [blame] | 502 | /* Releases buffer *buf (no check of emptiness) */ |
| 503 | static inline void __b_drop(struct buffer **buf) |
Willy Tarreau | 7dfca9d | 2014-11-25 19:45:11 +0100 | [diff] [blame] | 504 | { |
| 505 | pool_free2(pool2_buffer, *buf); |
| 506 | } |
| 507 | |
Willy Tarreau | 2a4b543 | 2014-11-24 11:39:34 +0100 | [diff] [blame] | 508 | /* Releases buffer *buf if allocated. */ |
| 509 | static inline void b_drop(struct buffer **buf) |
| 510 | { |
| 511 | if (!(*buf)->size) |
| 512 | return; |
| 513 | __b_drop(buf); |
| 514 | } |
| 515 | |
| 516 | /* Releases buffer *buf if allocated, and replaces it with &buf_empty. */ |
| 517 | static inline void b_free(struct buffer **buf) |
| 518 | { |
| 519 | b_drop(buf); |
| 520 | *buf = &buf_empty; |
| 521 | } |
| 522 | |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 523 | /* Ensures that <buf> is allocated. If an allocation is needed, it ensures that |
| 524 | * there are still at least <margin> buffers available in the pool after this |
| 525 | * allocation so that we don't leave the pool in a condition where a session or |
| 526 | * a response buffer could not be allocated anymore, resulting in a deadlock. |
| 527 | * This means that we sometimes need to try to allocate extra entries even if |
| 528 | * only one buffer is needed. |
| 529 | */ |
| 530 | static inline struct buffer *b_alloc_margin(struct buffer **buf, int margin) |
| 531 | { |
| 532 | struct buffer *next; |
| 533 | |
| 534 | if ((*buf)->size) |
| 535 | return *buf; |
| 536 | |
| 537 | /* fast path */ |
| 538 | if ((pool2_buffer->allocated - pool2_buffer->used) > margin) |
| 539 | return b_alloc_fast(buf); |
| 540 | |
| 541 | next = pool_refill_alloc(pool2_buffer, margin); |
| 542 | if (!next) |
| 543 | return next; |
| 544 | |
| 545 | next->size = pool2_buffer->size - sizeof(struct buffer); |
| 546 | b_reset(next); |
| 547 | *buf = next; |
| 548 | return next; |
| 549 | } |
| 550 | |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 551 | |
| 552 | void __offer_buffer(void *from, unsigned int threshold); |
| 553 | |
| 554 | static inline void offer_buffers(void *from, unsigned int threshold) |
| 555 | { |
| 556 | if (LIST_ISEMPTY(&buffer_wq)) |
| 557 | return; |
| 558 | __offer_buffer(from, threshold); |
| 559 | } |
| 560 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 561 | #endif /* _COMMON_BUFFER_H */ |
| 562 | |
| 563 | /* |
| 564 | * Local variables: |
| 565 | * c-indent-level: 8 |
| 566 | * c-basic-offset: 8 |
| 567 | * End: |
| 568 | */ |