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 | 41806d1 | 2018-07-11 09:39:05 +0200 | [diff] [blame] | 29 | #include <common/buf.h> |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 30 | #include <common/chunk.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 31 | #include <common/config.h> |
Willy Tarreau | 6634b63 | 2017-09-22 15:02:54 +0200 | [diff] [blame] | 32 | #include <common/ist.h> |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 33 | #include <common/memory.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 34 | |
| 35 | |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 36 | /* an element of the <buffer_wq> list. It represents an object that need to |
| 37 | * acquire a buffer to continue its process. */ |
| 38 | struct 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 Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 44 | extern struct pool_head *pool_head_buffer; |
Willy Tarreau | 2a4b543 | 2014-11-24 11:39:34 +0100 | [diff] [blame] | 45 | extern struct buffer buf_empty; |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 46 | extern struct buffer buf_wanted; |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 47 | extern struct list buffer_wq; |
Willy Tarreau | 53bae85 | 2017-11-26 11:00:37 +0100 | [diff] [blame] | 48 | __decl_hathreads(extern HA_SPINLOCK_T buffer_wq_lock); |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 49 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 50 | int init_buffer(); |
Christopher Faulet | ad405f1 | 2017-08-29 15:30:11 +0200 | [diff] [blame] | 51 | void deinit_buffer(); |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 52 | int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len); |
| 53 | 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] | 54 | void buffer_dump(FILE *o, struct buffer *b, int from, int to); |
Willy Tarreau | 4cf1300 | 2018-06-06 06:53:15 +0200 | [diff] [blame] | 55 | void buffer_slow_realign(struct buffer *buf, size_t output); |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 56 | |
| 57 | /*****************************************************************/ |
| 58 | /* These functions are used to compute various buffer area sizes */ |
| 59 | /*****************************************************************/ |
| 60 | |
| 61 | /* Returns an absolute pointer for a position relative to the current buffer's |
| 62 | * pointer. It is written so that it is optimal when <ofs> is a const. It is |
| 63 | * written as a macro instead of an inline function so that the compiler knows |
| 64 | * 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] | 65 | * 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] | 66 | */ |
| 67 | #define b_ptr(b, ofs) \ |
| 68 | ({ \ |
| 69 | char *__ret = (b)->p + (ofs); \ |
| 70 | if ((ofs) > 0 && __ret >= (b)->data + (b)->size) \ |
| 71 | __ret -= (b)->size; \ |
| 72 | else if ((ofs) < 0 && __ret < (b)->data) \ |
| 73 | __ret += (b)->size; \ |
| 74 | __ret; \ |
| 75 | }) |
| 76 | |
Willy Tarreau | 26488ad | 2017-09-19 21:14:08 +0200 | [diff] [blame] | 77 | /* Returns the pointer to the buffer's end (data+size) */ |
| 78 | static inline const char *b_end(const struct buffer *b) |
| 79 | { |
| 80 | return b->data + b->size; |
| 81 | } |
| 82 | |
| 83 | /* Returns the distance between <p> and the buffer's end (data+size) */ |
| 84 | static inline unsigned int b_to_end(const struct buffer *b) |
| 85 | { |
| 86 | return b->data + b->size - b->p; |
| 87 | } |
| 88 | |
Willy Tarreau | 4a6425d | 2017-09-19 14:18:46 +0200 | [diff] [blame] | 89 | /* Skips <del> bytes in a one-way buffer <b> : <p> advances by <del>, <i> |
| 90 | * shrinks by <del> as well, and <o> is left untouched (supposed to be zero). |
| 91 | * The caller is responsible for ensuring that <del> is always smaller than or |
| 92 | * equal to b->i. |
| 93 | */ |
Willy Tarreau | 7f564d2 | 2017-10-18 08:32:12 +0200 | [diff] [blame] | 94 | static inline void bi_del(struct buffer *b, unsigned int del) |
Willy Tarreau | 4a6425d | 2017-09-19 14:18:46 +0200 | [diff] [blame] | 95 | { |
| 96 | b->i -= del; |
| 97 | b->p = b_ptr(b, del); |
| 98 | } |
| 99 | |
Willy Tarreau | 7f564d2 | 2017-10-18 08:32:12 +0200 | [diff] [blame] | 100 | /* Skips <del> bytes from the output of buffer <b> by simply shrinking <o>. |
| 101 | * The caller is responsible for ensuring that <del> is always smaller than or |
| 102 | * equal to b->o. |
| 103 | */ |
| 104 | static inline void bo_del(struct buffer *b, unsigned int del) |
| 105 | { |
| 106 | b->o -= del; |
| 107 | } |
| 108 | |
Willy Tarreau | a75bcef | 2012-08-24 22:56:11 +0200 | [diff] [blame] | 109 | /* Advances the buffer by <adv> bytes, which means that the buffer |
| 110 | * pointer advances, and that as many bytes from in are transferred |
| 111 | * to out. The caller is responsible for ensuring that adv is always |
| 112 | * smaller than or equal to b->i. |
| 113 | */ |
| 114 | static inline void b_adv(struct buffer *b, unsigned int adv) |
| 115 | { |
| 116 | b->i -= adv; |
| 117 | b->o += adv; |
| 118 | b->p = b_ptr(b, adv); |
| 119 | } |
| 120 | |
| 121 | /* Rewinds the buffer by <adv> bytes, which means that the buffer pointer goes |
| 122 | * backwards, and that as many bytes from out are moved to in. The caller is |
| 123 | * responsible for ensuring that adv is always smaller than or equal to b->o. |
| 124 | */ |
| 125 | static inline void b_rew(struct buffer *b, unsigned int adv) |
| 126 | { |
| 127 | b->i += adv; |
| 128 | b->o -= adv; |
| 129 | b->p = b_ptr(b, (int)-adv); |
| 130 | } |
| 131 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 132 | /* Returns the start of the input data in a buffer */ |
| 133 | static inline char *bi_ptr(const struct buffer *b) |
| 134 | { |
| 135 | return b->p; |
| 136 | } |
| 137 | |
| 138 | /* Returns the end of the input data in a buffer (pointer to next |
| 139 | * insertion point). |
| 140 | */ |
| 141 | static inline char *bi_end(const struct buffer *b) |
| 142 | { |
| 143 | char *ret = b->p + b->i; |
| 144 | |
| 145 | if (ret >= b->data + b->size) |
| 146 | ret -= b->size; |
| 147 | return ret; |
| 148 | } |
| 149 | |
| 150 | /* Returns the amount of input data that can contiguously be read at once */ |
| 151 | static inline int bi_contig_data(const struct buffer *b) |
| 152 | { |
| 153 | int data = b->data + b->size - b->p; |
| 154 | |
| 155 | if (data > b->i) |
| 156 | data = b->i; |
| 157 | return data; |
| 158 | } |
| 159 | |
| 160 | /* Returns the start of the output data in a buffer */ |
| 161 | static inline char *bo_ptr(const struct buffer *b) |
| 162 | { |
| 163 | char *ret = b->p - b->o; |
| 164 | |
| 165 | if (ret < b->data) |
| 166 | ret += b->size; |
| 167 | return ret; |
| 168 | } |
| 169 | |
| 170 | /* Returns the end of the output data in a buffer */ |
| 171 | static inline char *bo_end(const struct buffer *b) |
| 172 | { |
| 173 | return b->p; |
| 174 | } |
| 175 | |
| 176 | /* Returns the amount of output data that can contiguously be read at once */ |
| 177 | static inline int bo_contig_data(const struct buffer *b) |
| 178 | { |
| 179 | char *beg = b->p - b->o; |
| 180 | |
| 181 | if (beg < b->data) |
| 182 | return b->data - beg; |
| 183 | return b->o; |
| 184 | } |
| 185 | |
Christopher Faulet | 637f8f2 | 2017-03-29 11:58:28 +0200 | [diff] [blame] | 186 | /* Return the amount of bytes that can be written into the input area at once |
| 187 | * including reserved space which may be overwritten (this is the caller |
| 188 | * responsibility to know if the reserved space is protected or not). |
| 189 | */ |
| 190 | static inline int bi_contig_space(const struct buffer *b) |
| 191 | { |
| 192 | const char *left, *right; |
| 193 | |
Christopher Faulet | a36b311 | 2017-06-13 22:00:22 +0200 | [diff] [blame] | 194 | left = b->p + b->i; |
| 195 | right = b->p - b->o; |
| 196 | if (left >= b->data + b->size) |
| 197 | left -= b->size; |
| 198 | else { |
| 199 | if (right < b->data) |
| 200 | right += b->size; |
| 201 | else |
| 202 | right = b->data + b->size; |
| 203 | } |
Christopher Faulet | 637f8f2 | 2017-03-29 11:58:28 +0200 | [diff] [blame] | 204 | return (right - left); |
| 205 | } |
| 206 | |
| 207 | /* Return the amount of bytes that can be written into the output area at once |
| 208 | * including reserved space which may be overwritten (this is the caller |
| 209 | * responsibility to know if the reserved space is protected or not). Input data |
| 210 | * are assumed to not exist. |
| 211 | */ |
| 212 | static inline int bo_contig_space(const struct buffer *b) |
| 213 | { |
| 214 | const char *left, *right; |
| 215 | |
Christopher Faulet | a36b311 | 2017-06-13 22:00:22 +0200 | [diff] [blame] | 216 | left = b->p; |
| 217 | right = b->p - b->o; |
| 218 | if (right < b->data) |
| 219 | right += b->size; |
| 220 | else |
Christopher Faulet | 637f8f2 | 2017-03-29 11:58:28 +0200 | [diff] [blame] | 221 | right = b->data + b->size; |
| 222 | |
| 223 | return (right - left); |
| 224 | } |
| 225 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 226 | /* Return the buffer's length in bytes by summing the input and the output */ |
| 227 | static inline int buffer_len(const struct buffer *buf) |
| 228 | { |
| 229 | return buf->i + buf->o; |
| 230 | } |
| 231 | |
| 232 | /* Return non-zero only if the buffer is not empty */ |
| 233 | static inline int buffer_not_empty(const struct buffer *buf) |
| 234 | { |
| 235 | return buf->i | buf->o; |
| 236 | } |
| 237 | |
| 238 | /* Return non-zero only if the buffer is empty */ |
| 239 | static inline int buffer_empty(const struct buffer *buf) |
| 240 | { |
| 241 | return !buffer_not_empty(buf); |
| 242 | } |
| 243 | |
Willy Tarreau | 5b9834f | 2017-10-16 14:01:18 +0200 | [diff] [blame] | 244 | /* Return non-zero only if the buffer's free space wraps : |
| 245 | * [ |oooo| ] => yes |
| 246 | * [ |iiii| ] => yes |
| 247 | * [ |oooo|iiii| ] => yes |
| 248 | * [oooo| ] => no |
| 249 | * [ |oooo] => no |
| 250 | * [iiii| ] => no |
| 251 | * [ |iiii] => no |
| 252 | * [oooo|iiii| ] => no |
| 253 | * [ |oooo|iiii] => no |
| 254 | * [iiii| |oooo] => no |
| 255 | * [oo|iiii| |oo] => no |
| 256 | * [iiii| |oo|ii] => no |
| 257 | * [oooooooooo|iiiiiiiiiii] => no |
| 258 | * [iiiiiiiiiiiii|oooooooo] => no |
| 259 | * |
| 260 | * So the only case where the buffer does not wrap is when there's data either |
| 261 | * at the beginning or at the end of the buffer. Thus we have this : |
| 262 | * - if (p+i >= size) ==> doesn't wrap |
| 263 | * - if (p-data <= o) ==> doesn't wrap |
| 264 | * - otherwise wraps |
| 265 | */ |
| 266 | static inline int buffer_space_wraps(const struct buffer *buf) |
| 267 | { |
| 268 | if (buf->p + buf->i >= buf->data + buf->size) |
| 269 | return 0; |
| 270 | if (buf->p <= buf->data + buf->o) |
| 271 | return 0; |
| 272 | return 1; |
| 273 | } |
| 274 | |
Willy Tarreau | 42d0666 | 2012-08-27 19:51:36 +0200 | [diff] [blame] | 275 | /* Returns non-zero if the buffer's INPUT is considered full, which means that |
| 276 | * it holds at least as much INPUT data as (size - reserve). This also means |
| 277 | * that data that are scheduled for output are considered as potential free |
| 278 | * space, and that the reserved space is always considered as not usable. This |
| 279 | * information alone cannot be used as a general purpose free space indicator. |
| 280 | * 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] | 281 | * 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] | 282 | * generic function taking everything into account. |
| 283 | */ |
| 284 | static inline int buffer_full(const struct buffer *b, unsigned int reserve) |
| 285 | { |
Willy Tarreau | 4428a29 | 2014-11-28 20:54:13 +0100 | [diff] [blame] | 286 | if (b == &buf_empty) |
| 287 | return 0; |
| 288 | |
Willy Tarreau | 42d0666 | 2012-08-27 19:51:36 +0200 | [diff] [blame] | 289 | return (b->i + reserve >= b->size); |
| 290 | } |
| 291 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 292 | /* Normalizes a pointer after a subtract */ |
| 293 | static inline char *buffer_wrap_sub(const struct buffer *buf, char *ptr) |
| 294 | { |
| 295 | if (ptr < buf->data) |
| 296 | ptr += buf->size; |
| 297 | return ptr; |
| 298 | } |
| 299 | |
| 300 | /* Normalizes a pointer after an addition */ |
| 301 | static inline char *buffer_wrap_add(const struct buffer *buf, char *ptr) |
| 302 | { |
| 303 | if (ptr - buf->size >= buf->data) |
| 304 | ptr -= buf->size; |
| 305 | return ptr; |
| 306 | } |
| 307 | |
| 308 | /* Return the maximum amount of bytes that can be written into the buffer, |
| 309 | * including reserved space which may be overwritten. |
| 310 | */ |
| 311 | static inline int buffer_total_space(const struct buffer *buf) |
| 312 | { |
| 313 | return buf->size - buffer_len(buf); |
| 314 | } |
| 315 | |
Thierry FOURNIER | d2b597a | 2015-03-07 14:38:50 +0100 | [diff] [blame] | 316 | /* Returns the amount of byte that can be written starting from <p> into the |
| 317 | * input buffer at once, including reserved space which may be overwritten. |
| 318 | * This is used by Lua to insert data in the input side just before the other |
| 319 | * data using buffer_replace(). The goal is to transfer these new data in the |
| 320 | * output buffer. |
| 321 | */ |
| 322 | static inline int bi_space_for_replace(const struct buffer *buf) |
| 323 | { |
| 324 | const char *end; |
| 325 | |
| 326 | /* If the input side data overflows, we cannot insert data contiguously. */ |
| 327 | if (buf->p + buf->i >= buf->data + buf->size) |
| 328 | return 0; |
| 329 | |
| 330 | /* Check the last byte used in the buffer, it may be a byte of the output |
| 331 | * side if the buffer wraps, or its the end of the buffer. |
| 332 | */ |
| 333 | end = buffer_wrap_sub(buf, buf->p - buf->o); |
| 334 | if (end <= buf->p) |
| 335 | end = buf->data + buf->size; |
| 336 | |
| 337 | /* Compute the amount of bytes which can be written. */ |
| 338 | return end - (buf->p + buf->i); |
| 339 | } |
| 340 | |
| 341 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 342 | /* Normalizes a pointer which is supposed to be relative to the beginning of a |
| 343 | * buffer, so that wrapping is correctly handled. The intent is to use this |
| 344 | * when increasing a pointer. Note that the wrapping test is only performed |
| 345 | * once, so the original pointer must be between ->data-size and ->data+2*size-1, |
| 346 | * otherwise an invalid pointer might be returned. |
| 347 | */ |
| 348 | static inline const char *buffer_pointer(const struct buffer *buf, const char *ptr) |
| 349 | { |
| 350 | if (ptr < buf->data) |
| 351 | ptr += buf->size; |
| 352 | else if (ptr - buf->size >= buf->data) |
| 353 | ptr -= buf->size; |
| 354 | return ptr; |
| 355 | } |
| 356 | |
| 357 | /* Returns the distance between two pointers, taking into account the ability |
| 358 | * to wrap around the buffer's end. |
| 359 | */ |
| 360 | static inline int buffer_count(const struct buffer *buf, const char *from, const char *to) |
| 361 | { |
| 362 | int count = to - from; |
Willy Tarreau | bf43927 | 2013-04-02 01:25:57 +0200 | [diff] [blame] | 363 | |
| 364 | count += count < 0 ? buf->size : 0; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 365 | return count; |
| 366 | } |
| 367 | |
| 368 | /* returns the amount of pending bytes in the buffer. It is the amount of bytes |
| 369 | * that is not scheduled to be sent. |
| 370 | */ |
| 371 | static inline int buffer_pending(const struct buffer *buf) |
| 372 | { |
| 373 | return buf->i; |
| 374 | } |
| 375 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 376 | /* Return 1 if the buffer has less than 1/4 of its capacity free, otherwise 0 */ |
| 377 | static inline int buffer_almost_full(const struct buffer *buf) |
| 378 | { |
Willy Tarreau | 4428a29 | 2014-11-28 20:54:13 +0100 | [diff] [blame] | 379 | if (buf == &buf_empty) |
| 380 | return 0; |
| 381 | |
Willy Tarreau | bbc68df | 2018-06-06 14:30:50 +0200 | [diff] [blame] | 382 | return b_almost_full(buf); |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | /* Cut the first <n> pending bytes in a contiguous buffer. It is illegal to |
| 386 | * call this function with remaining data waiting to be sent (o > 0). The |
| 387 | * caller must ensure that <n> is smaller than the actual buffer's length. |
| 388 | * This is mainly used to remove empty lines at the beginning of a request |
| 389 | * or a response. |
| 390 | */ |
| 391 | static inline void bi_fast_delete(struct buffer *buf, int n) |
| 392 | { |
| 393 | buf->i -= n; |
| 394 | buf->p += n; |
| 395 | } |
| 396 | |
Willy Tarreau | a75bcef | 2012-08-24 22:56:11 +0200 | [diff] [blame] | 397 | /* Schedule all remaining buffer data to be sent. ->o is not touched if it |
| 398 | * already covers those data. That permits doing a flush even after a forward, |
| 399 | * although not recommended. |
| 400 | */ |
| 401 | static inline void buffer_flush(struct buffer *buf) |
| 402 | { |
| 403 | buf->p = buffer_wrap_add(buf, buf->p + buf->i); |
| 404 | buf->o += buf->i; |
| 405 | buf->i = 0; |
| 406 | } |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 407 | |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 408 | /* This function writes the string <str> at position <pos> which must be in |
| 409 | * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters |
| 410 | * (l, r, lr) are updated to be valid after the shift. the shift value |
| 411 | * (positive or negative) is returned. If there's no space left, the move is |
| 412 | * not done. The function does not adjust ->o because it does not make sense |
| 413 | * to use it on data scheduled to be sent. |
| 414 | */ |
| 415 | static inline int buffer_replace(struct buffer *b, char *pos, char *end, const char *str) |
| 416 | { |
| 417 | return buffer_replace2(b, pos, end, str, strlen(str)); |
| 418 | } |
| 419 | |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 420 | /* Tries to write char <c> into output data at buffer <b>. Supports wrapping. |
| 421 | * Data are truncated if buffer is full. |
| 422 | */ |
| 423 | static inline void bo_putchr(struct buffer *b, char c) |
| 424 | { |
| 425 | if (buffer_len(b) == b->size) |
| 426 | return; |
| 427 | *b->p = c; |
| 428 | b->p = b_ptr(b, 1); |
| 429 | b->o++; |
| 430 | } |
| 431 | |
| 432 | /* 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] | 433 | * Data are truncated if buffer is too short. It returns the number of bytes |
| 434 | * copied. |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 435 | */ |
Thierry FOURNIER | 549aac8 | 2015-02-06 18:40:20 +0100 | [diff] [blame] | 436 | 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] | 437 | { |
| 438 | int cur_len = buffer_len(b); |
| 439 | int half; |
| 440 | |
| 441 | if (len > b->size - cur_len) |
| 442 | len = (b->size - cur_len); |
| 443 | if (!len) |
Thierry FOURNIER | 549aac8 | 2015-02-06 18:40:20 +0100 | [diff] [blame] | 444 | return 0; |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 445 | |
Christopher Faulet | 637f8f2 | 2017-03-29 11:58:28 +0200 | [diff] [blame] | 446 | half = bo_contig_space(b); |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 447 | if (half > len) |
| 448 | half = len; |
| 449 | |
| 450 | memcpy(b->p, blk, half); |
| 451 | b->p = b_ptr(b, half); |
| 452 | if (len > half) { |
Christopher Faulet | b2b2794 | 2018-02-26 10:47:03 +0100 | [diff] [blame] | 453 | memcpy(b->p, blk + half, len - half); |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 454 | b->p = b_ptr(b, half); |
| 455 | } |
| 456 | b->o += len; |
Thierry FOURNIER | 549aac8 | 2015-02-06 18:40:20 +0100 | [diff] [blame] | 457 | return len; |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | /* 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] | 461 | * Data are truncated if buffer is too short. It returns the number of bytes |
| 462 | * copied. |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 463 | */ |
Thierry FOURNIER | 549aac8 | 2015-02-06 18:40:20 +0100 | [diff] [blame] | 464 | static inline int bo_putstr(struct buffer *b, const char *str) |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 465 | { |
| 466 | return bo_putblk(b, str, strlen(str)); |
| 467 | } |
| 468 | |
| 469 | /* 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] | 470 | * Data are truncated if buffer is too short. It returns the number of bytes |
| 471 | * copied. |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 472 | */ |
Thierry FOURNIER | 549aac8 | 2015-02-06 18:40:20 +0100 | [diff] [blame] | 473 | static inline int bo_putchk(struct buffer *b, const struct chunk *chk) |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 474 | { |
| 475 | return bo_putblk(b, chk->str, chk->len); |
| 476 | } |
| 477 | |
Willy Tarreau | e0e734c | 2017-10-19 14:56:49 +0200 | [diff] [blame] | 478 | /* Gets one full block of data at once from a buffer's output, optionally |
| 479 | * starting at a specific offset. Return values : |
| 480 | * >0 : number of bytes read, equal to requested size. |
| 481 | * =0 : not enough data available. <blk> is left undefined. |
| 482 | * The buffer is left unaffected. |
| 483 | */ |
| 484 | static inline int bo_getblk(const struct buffer *buf, char *blk, int len, int offset) |
| 485 | { |
| 486 | int firstblock; |
| 487 | |
| 488 | if (len + offset > buf->o) |
| 489 | return 0; |
| 490 | |
| 491 | firstblock = buf->data + buf->size - bo_ptr(buf); |
| 492 | if (firstblock > offset) { |
| 493 | if (firstblock >= len + offset) { |
| 494 | memcpy(blk, bo_ptr(buf) + offset, len); |
| 495 | return len; |
| 496 | } |
| 497 | |
| 498 | memcpy(blk, bo_ptr(buf) + offset, firstblock - offset); |
| 499 | memcpy(blk + firstblock - offset, buf->data, len - firstblock + offset); |
| 500 | return len; |
| 501 | } |
| 502 | |
| 503 | memcpy(blk, buf->data + offset - firstblock, len); |
| 504 | return len; |
| 505 | } |
| 506 | |
| 507 | /* Gets one or two blocks of data at once from a buffer's output. |
| 508 | * Return values : |
| 509 | * >0 : number of blocks filled (1 or 2). blk1 is always filled before blk2. |
| 510 | * =0 : not enough data available. <blk*> are left undefined. |
| 511 | * The buffer is left unaffected. Unused buffers are left in an undefined state. |
| 512 | */ |
| 513 | static inline int bo_getblk_nc(struct buffer *buf, char **blk1, int *len1, char **blk2, int *len2) |
| 514 | { |
| 515 | if (unlikely(buf->o == 0)) |
| 516 | return 0; |
| 517 | |
Willy Tarreau | 0621da5 | 2017-10-20 18:21:49 +0200 | [diff] [blame] | 518 | if (unlikely(buf->p != buf->data && buf->p - buf->o < buf->data)) { |
Willy Tarreau | e0e734c | 2017-10-19 14:56:49 +0200 | [diff] [blame] | 519 | *blk1 = buf->p - buf->o + buf->size; |
| 520 | *len1 = buf->data + buf->size - *blk1; |
| 521 | *blk2 = buf->data; |
| 522 | *len2 = buf->p - buf->data; |
| 523 | return 2; |
| 524 | } |
| 525 | |
Willy Tarreau | 4b75fff | 2017-11-02 17:16:07 +0100 | [diff] [blame] | 526 | *blk1 = bo_ptr(buf); |
Willy Tarreau | e0e734c | 2017-10-19 14:56:49 +0200 | [diff] [blame] | 527 | *len1 = buf->o; |
| 528 | return 1; |
| 529 | } |
| 530 | |
Willy Tarreau | 145746c | 2017-10-26 15:26:17 +0200 | [diff] [blame] | 531 | /* Tries to write char <c> into input data at buffer <b>. Supports wrapping. |
| 532 | * Data are truncated if buffer is full. |
| 533 | */ |
| 534 | static inline void bi_putchr(struct buffer *b, char c) |
| 535 | { |
| 536 | if (buffer_len(b) == b->size) |
| 537 | return; |
| 538 | *bi_end(b) = c; |
| 539 | b->i++; |
| 540 | } |
| 541 | |
| 542 | /* Tries to copy block <blk> into input data at buffer <b>. Supports wrapping. |
| 543 | * Data are truncated if buffer is too short. It returns the number of bytes |
| 544 | * copied. |
| 545 | */ |
| 546 | static inline int bi_putblk(struct buffer *b, const char *blk, int len) |
| 547 | { |
| 548 | int cur_len = buffer_len(b); |
| 549 | int half; |
| 550 | |
| 551 | if (len > b->size - cur_len) |
| 552 | len = (b->size - cur_len); |
| 553 | if (!len) |
| 554 | return 0; |
| 555 | |
| 556 | half = bi_contig_space(b); |
| 557 | if (half > len) |
| 558 | half = len; |
| 559 | |
| 560 | memcpy(bi_end(b), blk, half); |
| 561 | if (len > half) |
Christopher Faulet | ca6ef50 | 2018-02-26 10:51:28 +0100 | [diff] [blame] | 562 | memcpy(b_ptr(b, b->i + half), blk + half, len - half); |
Willy Tarreau | 145746c | 2017-10-26 15:26:17 +0200 | [diff] [blame] | 563 | b->i += len; |
| 564 | return len; |
| 565 | } |
| 566 | |
| 567 | /* Tries to copy string <str> into input data at buffer <b>. Supports wrapping. |
| 568 | * Data are truncated if buffer is too short. It returns the number of bytes |
| 569 | * copied. |
| 570 | */ |
| 571 | static inline int bi_putstr(struct buffer *b, const char *str) |
| 572 | { |
| 573 | return bi_putblk(b, str, strlen(str)); |
| 574 | } |
| 575 | |
| 576 | /* Tries to copy chunk <chk> into input data at buffer <b>. Supports wrapping. |
| 577 | * Data are truncated if buffer is too short. It returns the number of bytes |
| 578 | * copied. |
| 579 | */ |
| 580 | static inline int bi_putchk(struct buffer *b, const struct chunk *chk) |
| 581 | { |
| 582 | return bi_putblk(b, chk->str, chk->len); |
| 583 | } |
| 584 | |
| 585 | /* Gets one full block of data at once from a buffer's input. Return values : |
| 586 | * >0 : number of bytes read, equal to requested size. |
| 587 | * =0 : not enough data available. <blk> is left undefined. |
| 588 | * The buffer is left unaffected. |
| 589 | */ |
| 590 | static inline int bi_getblk(const struct buffer *buf, char *blk, int len) |
| 591 | { |
| 592 | int firstblock; |
| 593 | |
| 594 | if (len > buf->i) |
| 595 | return 0; |
| 596 | |
| 597 | firstblock = bi_contig_data(buf); |
| 598 | if (firstblock > len) |
| 599 | firstblock = len; |
| 600 | |
| 601 | memcpy(blk, bi_ptr(buf), firstblock); |
| 602 | if (len > firstblock) |
| 603 | memcpy(blk + firstblock, buf->data, len - firstblock); |
| 604 | return len; |
| 605 | } |
| 606 | |
| 607 | /* Gets one or two blocks of data at once from a buffer's input. |
| 608 | * Return values : |
| 609 | * >0 : number of blocks filled (1 or 2). blk1 is always filled before blk2. |
| 610 | * =0 : not enough data available. <blk*> are left undefined. |
| 611 | * The buffer is left unaffected. Unused buffers are left in an undefined state. |
| 612 | */ |
| 613 | static inline int bi_getblk_nc(struct buffer *buf, char **blk1, int *len1, char **blk2, int *len2) |
| 614 | { |
| 615 | if (unlikely(buf->i == 0)) |
| 616 | return 0; |
| 617 | |
| 618 | if (unlikely(buf->p + buf->i > buf->data + buf->size)) { |
| 619 | *blk1 = buf->p; |
| 620 | *len1 = buf->data + buf->size - buf->p; |
| 621 | *blk2 = buf->data; |
| 622 | *len2 = buf->i - *len1; |
| 623 | return 2; |
| 624 | } |
| 625 | |
| 626 | *blk1 = buf->p; |
| 627 | *len1 = buf->i; |
| 628 | return 1; |
| 629 | } |
Willy Tarreau | e0e734c | 2017-10-19 14:56:49 +0200 | [diff] [blame] | 630 | |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 631 | /* Allocates a buffer and replaces *buf with this buffer. If no memory is |
| 632 | * available, &buf_wanted is used instead. No control is made to check if *buf |
| 633 | * already pointed to another buffer. The allocated buffer is returned, or |
| 634 | * NULL in case no memory is available. |
Willy Tarreau | e583ea5 | 2014-11-24 11:30:16 +0100 | [diff] [blame] | 635 | */ |
| 636 | static inline struct buffer *b_alloc(struct buffer **buf) |
| 637 | { |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 638 | struct buffer *b; |
| 639 | |
| 640 | *buf = &buf_wanted; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 641 | b = pool_alloc_dirty(pool_head_buffer); |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 642 | if (likely(b)) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 643 | b->size = pool_head_buffer->size - sizeof(struct buffer); |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 644 | b_reset(b); |
| 645 | *buf = b; |
Willy Tarreau | e583ea5 | 2014-11-24 11:30:16 +0100 | [diff] [blame] | 646 | } |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 647 | return b; |
Willy Tarreau | e583ea5 | 2014-11-24 11:30:16 +0100 | [diff] [blame] | 648 | } |
| 649 | |
Willy Tarreau | 620bd6c | 2014-12-08 16:37:26 +0100 | [diff] [blame] | 650 | /* Allocates a buffer and replaces *buf with this buffer. If no memory is |
| 651 | * available, &buf_wanted is used instead. No control is made to check if *buf |
| 652 | * already pointed to another buffer. The allocated buffer is returned, or |
| 653 | * NULL in case no memory is available. The difference with b_alloc() is that |
| 654 | * this function only picks from the pool and never calls malloc(), so it can |
| 655 | * fail even if some memory is available. |
| 656 | */ |
| 657 | static inline struct buffer *b_alloc_fast(struct buffer **buf) |
| 658 | { |
| 659 | struct buffer *b; |
| 660 | |
| 661 | *buf = &buf_wanted; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 662 | b = pool_get_first(pool_head_buffer); |
Willy Tarreau | 620bd6c | 2014-12-08 16:37:26 +0100 | [diff] [blame] | 663 | if (likely(b)) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 664 | b->size = pool_head_buffer->size - sizeof(struct buffer); |
Willy Tarreau | 620bd6c | 2014-12-08 16:37:26 +0100 | [diff] [blame] | 665 | b_reset(b); |
| 666 | *buf = b; |
| 667 | } |
| 668 | return b; |
| 669 | } |
| 670 | |
Willy Tarreau | 2a4b543 | 2014-11-24 11:39:34 +0100 | [diff] [blame] | 671 | /* Releases buffer *buf (no check of emptiness) */ |
| 672 | static inline void __b_drop(struct buffer **buf) |
Willy Tarreau | 7dfca9d | 2014-11-25 19:45:11 +0100 | [diff] [blame] | 673 | { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 674 | pool_free(pool_head_buffer, *buf); |
Willy Tarreau | 7dfca9d | 2014-11-25 19:45:11 +0100 | [diff] [blame] | 675 | } |
| 676 | |
Willy Tarreau | 2a4b543 | 2014-11-24 11:39:34 +0100 | [diff] [blame] | 677 | /* Releases buffer *buf if allocated. */ |
| 678 | static inline void b_drop(struct buffer **buf) |
| 679 | { |
| 680 | if (!(*buf)->size) |
| 681 | return; |
| 682 | __b_drop(buf); |
| 683 | } |
| 684 | |
| 685 | /* Releases buffer *buf if allocated, and replaces it with &buf_empty. */ |
| 686 | static inline void b_free(struct buffer **buf) |
| 687 | { |
| 688 | b_drop(buf); |
| 689 | *buf = &buf_empty; |
| 690 | } |
| 691 | |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 692 | /* Ensures that <buf> is allocated. If an allocation is needed, it ensures that |
| 693 | * there are still at least <margin> buffers available in the pool after this |
| 694 | * allocation so that we don't leave the pool in a condition where a session or |
| 695 | * a response buffer could not be allocated anymore, resulting in a deadlock. |
| 696 | * This means that we sometimes need to try to allocate extra entries even if |
| 697 | * only one buffer is needed. |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 698 | * |
| 699 | * We need to lock the pool here to be sure to have <margin> buffers available |
| 700 | * after the allocation, regardless how many threads that doing it in the same |
| 701 | * time. So, we use internal and lockless memory functions (prefixed with '__'). |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 702 | */ |
| 703 | static inline struct buffer *b_alloc_margin(struct buffer **buf, int margin) |
| 704 | { |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 705 | struct buffer *b; |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 706 | |
| 707 | if ((*buf)->size) |
| 708 | return *buf; |
| 709 | |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 710 | *buf = &buf_wanted; |
Willy Tarreau | f161d0f | 2018-02-22 14:05:55 +0100 | [diff] [blame] | 711 | #ifndef CONFIG_HAP_LOCKLESS_POOLS |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 712 | HA_SPIN_LOCK(POOL_LOCK, &pool_head_buffer->lock); |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 713 | #endif |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 714 | |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 715 | /* fast path */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 716 | if ((pool_head_buffer->allocated - pool_head_buffer->used) > margin) { |
| 717 | b = __pool_get_first(pool_head_buffer); |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 718 | if (likely(b)) { |
Willy Tarreau | f161d0f | 2018-02-22 14:05:55 +0100 | [diff] [blame] | 719 | #ifndef CONFIG_HAP_LOCKLESS_POOLS |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 720 | HA_SPIN_UNLOCK(POOL_LOCK, &pool_head_buffer->lock); |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 721 | #endif |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 722 | b->size = pool_head_buffer->size - sizeof(struct buffer); |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 723 | b_reset(b); |
| 724 | *buf = b; |
| 725 | return b; |
| 726 | } |
| 727 | } |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 728 | |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 729 | /* slow path, uses malloc() */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 730 | b = __pool_refill_alloc(pool_head_buffer, margin); |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 731 | |
Willy Tarreau | f161d0f | 2018-02-22 14:05:55 +0100 | [diff] [blame] | 732 | #ifndef CONFIG_HAP_LOCKLESS_POOLS |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 733 | HA_SPIN_UNLOCK(POOL_LOCK, &pool_head_buffer->lock); |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 734 | #endif |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 735 | |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 736 | if (b) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 737 | b->size = pool_head_buffer->size - sizeof(struct buffer); |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 738 | b_reset(b); |
| 739 | *buf = b; |
| 740 | } |
| 741 | return b; |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 742 | } |
| 743 | |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 744 | |
Willy Tarreau | c41b3e8 | 2018-03-02 10:27:12 +0100 | [diff] [blame] | 745 | /* Offer a buffer currently belonging to target <from> to whoever needs one. |
| 746 | * Any pointer is valid for <from>, including NULL. Its purpose is to avoid |
| 747 | * passing a buffer to oneself in case of failed allocations (e.g. need two |
| 748 | * buffers, get one, fail, release it and wake up self again). In case of |
| 749 | * normal buffer release where it is expected that the caller is not waiting |
| 750 | * for a buffer, NULL is fine. |
| 751 | */ |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 752 | void __offer_buffer(void *from, unsigned int threshold); |
| 753 | |
| 754 | static inline void offer_buffers(void *from, unsigned int threshold) |
| 755 | { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 756 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Emeric Brun | a1dd243 | 2017-06-21 15:42:52 +0200 | [diff] [blame] | 757 | if (LIST_ISEMPTY(&buffer_wq)) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 758 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 759 | return; |
Emeric Brun | a1dd243 | 2017-06-21 15:42:52 +0200 | [diff] [blame] | 760 | } |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 761 | __offer_buffer(from, threshold); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 762 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 763 | } |
| 764 | |
Willy Tarreau | 6634b63 | 2017-09-22 15:02:54 +0200 | [diff] [blame] | 765 | /*************************************************************************/ |
| 766 | /* functions used to manipulate strings and blocks with wrapping buffers */ |
| 767 | /*************************************************************************/ |
| 768 | |
| 769 | /* returns > 0 if the first <n> characters of buffer <b> starting at |
| 770 | * offset <o> relative to b->p match <ist>. (empty strings do match). It is |
| 771 | * designed to be use with reasonably small strings (ie matches a single byte |
| 772 | * per iteration). This function is usable both with input and output data. To |
| 773 | * be used like this depending on what to match : |
| 774 | * - input contents : b_isteq(b, 0, b->i, ist); |
| 775 | * - output contents : b_isteq(b, -b->o, b->o, ist); |
| 776 | * Return value : |
| 777 | * >0 : the number of matching bytes |
| 778 | * =0 : not enough bytes (or matching of empty string) |
| 779 | * <0 : non-matching byte found |
| 780 | */ |
| 781 | static inline int b_isteq(const struct buffer *b, unsigned int o, size_t n, const struct ist ist) |
| 782 | { |
| 783 | struct ist r = ist; |
| 784 | const char *p; |
| 785 | const char *end = b->data + b->size; |
| 786 | |
| 787 | if (n < r.len) |
| 788 | return 0; |
| 789 | |
| 790 | p = b_ptr(b, o); |
| 791 | while (r.len--) { |
| 792 | if (*p++ != *r.ptr++) |
| 793 | return -1; |
| 794 | if (unlikely(p == end)) |
| 795 | p = b->data; |
| 796 | } |
| 797 | return ist.len; |
| 798 | } |
| 799 | |
| 800 | /* "eats" string <ist> from the input region of buffer <b>. Wrapping data is |
| 801 | * explicitly supported. It matches a single byte per iteration so strings |
| 802 | * should remain reasonably small. Returns : |
| 803 | * > 0 : number of bytes matched and eaten |
| 804 | * = 0 : not enough bytes (or matching an empty string) |
| 805 | * < 0 : non-matching byte found |
| 806 | */ |
| 807 | static inline int bi_eat(struct buffer *b, const struct ist ist) |
| 808 | { |
| 809 | int ret = b_isteq(b, 0, b->i, ist); |
| 810 | if (ret > 0) |
| 811 | bi_del(b, ret); |
| 812 | return ret; |
| 813 | } |
| 814 | |
Willy Tarreau | e5676e7 | 2017-09-22 15:47:51 +0200 | [diff] [blame] | 815 | /* injects string <ist> into the input region of buffer <b> provided that it |
| 816 | * fits. Wrapping is supported. It's designed for small strings as it only |
| 817 | * writes a single byte per iteration. Returns the number of characters copied |
| 818 | * (ist.len), 0 if it temporarily does not fit or -1 if it will never fit. It |
| 819 | * will only modify the buffer upon success. In all cases, the contents are |
| 820 | * copied prior to reporting an error, so that the destination at least |
| 821 | * contains a valid but truncated string. |
| 822 | */ |
| 823 | static inline int bi_istput(struct buffer *b, const struct ist ist) |
| 824 | { |
| 825 | const char *end = b->data + b->size; |
| 826 | struct ist r = ist; |
| 827 | char *p; |
| 828 | |
| 829 | if (r.len > (size_t)(b->size - b->i - b->o)) |
| 830 | return r.len < b->size ? 0 : -1; |
| 831 | |
| 832 | p = b_ptr(b, b->i); |
| 833 | b->i += r.len; |
| 834 | while (r.len--) { |
| 835 | *p++ = *r.ptr++; |
| 836 | if (unlikely(p == end)) |
| 837 | p = b->data; |
| 838 | } |
| 839 | return ist.len; |
| 840 | } |
| 841 | |
| 842 | |
| 843 | /* injects string <ist> into the output region of buffer <b> provided that it |
| 844 | * fits. Input data is assumed not to exist and will silently be overwritten. |
| 845 | * Wrapping is supported. It's designed for small strings as it only writes a |
| 846 | * single byte per iteration. Returns the number of characters copied (ist.len), |
| 847 | * 0 if it temporarily does not fit or -1 if it will never fit. It will only |
| 848 | * modify the buffer upon success. In all cases, the contents are copied prior |
| 849 | * to reporting an error, so that the destination at least contains a valid |
| 850 | * but truncated string. |
| 851 | */ |
| 852 | static inline int bo_istput(struct buffer *b, const struct ist ist) |
| 853 | { |
| 854 | const char *end = b->data + b->size; |
| 855 | struct ist r = ist; |
| 856 | char *p; |
| 857 | |
| 858 | if (r.len > (size_t)(b->size - b->o)) |
| 859 | return r.len < b->size ? 0 : -1; |
| 860 | |
| 861 | p = b->p; |
| 862 | b->o += r.len; |
| 863 | b->p = b_ptr(b, r.len); |
| 864 | while (r.len--) { |
| 865 | *p++ = *r.ptr++; |
| 866 | if (unlikely(p == end)) |
| 867 | p = b->data; |
| 868 | } |
| 869 | return ist.len; |
| 870 | } |
| 871 | |
| 872 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 873 | #endif /* _COMMON_BUFFER_H */ |
| 874 | |
| 875 | /* |
| 876 | * Local variables: |
| 877 | * c-indent-level: 8 |
| 878 | * c-basic-offset: 8 |
| 879 | * End: |
| 880 | */ |