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); |
| 55 | void buffer_slow_realign(struct buffer *buf); |
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 | |
Christopher Faulet | 637f8f2 | 2017-03-29 11:58:28 +0200 | [diff] [blame] | 397 | /* Tries to realign the given buffer. */ |
| 398 | static inline void buffer_realign(struct buffer *buf) |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 399 | { |
| 400 | if (!(buf->i | buf->o)) { |
| 401 | /* let's realign the buffer to optimize I/O */ |
| 402 | buf->p = buf->data; |
| 403 | } |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 404 | } |
| 405 | |
Willy Tarreau | a75bcef | 2012-08-24 22:56:11 +0200 | [diff] [blame] | 406 | /* Schedule all remaining buffer data to be sent. ->o is not touched if it |
| 407 | * already covers those data. That permits doing a flush even after a forward, |
| 408 | * although not recommended. |
| 409 | */ |
| 410 | static inline void buffer_flush(struct buffer *buf) |
| 411 | { |
| 412 | buf->p = buffer_wrap_add(buf, buf->p + buf->i); |
| 413 | buf->o += buf->i; |
| 414 | buf->i = 0; |
| 415 | } |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 416 | |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 417 | /* This function writes the string <str> at position <pos> which must be in |
| 418 | * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters |
| 419 | * (l, r, lr) are updated to be valid after the shift. the shift value |
| 420 | * (positive or negative) is returned. If there's no space left, the move is |
| 421 | * not done. The function does not adjust ->o because it does not make sense |
| 422 | * to use it on data scheduled to be sent. |
| 423 | */ |
| 424 | static inline int buffer_replace(struct buffer *b, char *pos, char *end, const char *str) |
| 425 | { |
| 426 | return buffer_replace2(b, pos, end, str, strlen(str)); |
| 427 | } |
| 428 | |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 429 | /* Tries to write char <c> into output data at buffer <b>. Supports wrapping. |
| 430 | * Data are truncated if buffer is full. |
| 431 | */ |
| 432 | static inline void bo_putchr(struct buffer *b, char c) |
| 433 | { |
| 434 | if (buffer_len(b) == b->size) |
| 435 | return; |
| 436 | *b->p = c; |
| 437 | b->p = b_ptr(b, 1); |
| 438 | b->o++; |
| 439 | } |
| 440 | |
| 441 | /* 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] | 442 | * Data are truncated if buffer is too short. It returns the number of bytes |
| 443 | * copied. |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 444 | */ |
Thierry FOURNIER | 549aac8 | 2015-02-06 18:40:20 +0100 | [diff] [blame] | 445 | 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] | 446 | { |
| 447 | int cur_len = buffer_len(b); |
| 448 | int half; |
| 449 | |
| 450 | if (len > b->size - cur_len) |
| 451 | len = (b->size - cur_len); |
| 452 | if (!len) |
Thierry FOURNIER | 549aac8 | 2015-02-06 18:40:20 +0100 | [diff] [blame] | 453 | return 0; |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 454 | |
Christopher Faulet | 637f8f2 | 2017-03-29 11:58:28 +0200 | [diff] [blame] | 455 | half = bo_contig_space(b); |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 456 | if (half > len) |
| 457 | half = len; |
| 458 | |
| 459 | memcpy(b->p, blk, half); |
| 460 | b->p = b_ptr(b, half); |
| 461 | if (len > half) { |
Christopher Faulet | b2b2794 | 2018-02-26 10:47:03 +0100 | [diff] [blame] | 462 | memcpy(b->p, blk + half, len - half); |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 463 | b->p = b_ptr(b, half); |
| 464 | } |
| 465 | b->o += len; |
Thierry FOURNIER | 549aac8 | 2015-02-06 18:40:20 +0100 | [diff] [blame] | 466 | return len; |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | /* 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] | 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_putstr(struct buffer *b, const char *str) |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 474 | { |
| 475 | return bo_putblk(b, str, strlen(str)); |
| 476 | } |
| 477 | |
| 478 | /* 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] | 479 | * Data are truncated if buffer is too short. It returns the number of bytes |
| 480 | * copied. |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 481 | */ |
Thierry FOURNIER | 549aac8 | 2015-02-06 18:40:20 +0100 | [diff] [blame] | 482 | static inline int bo_putchk(struct buffer *b, const struct chunk *chk) |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 483 | { |
| 484 | return bo_putblk(b, chk->str, chk->len); |
| 485 | } |
| 486 | |
Willy Tarreau | e0e734c | 2017-10-19 14:56:49 +0200 | [diff] [blame] | 487 | /* Gets one full block of data at once from a buffer's output, optionally |
| 488 | * starting at a specific offset. Return values : |
| 489 | * >0 : number of bytes read, equal to requested size. |
| 490 | * =0 : not enough data available. <blk> is left undefined. |
| 491 | * The buffer is left unaffected. |
| 492 | */ |
| 493 | static inline int bo_getblk(const struct buffer *buf, char *blk, int len, int offset) |
| 494 | { |
| 495 | int firstblock; |
| 496 | |
| 497 | if (len + offset > buf->o) |
| 498 | return 0; |
| 499 | |
| 500 | firstblock = buf->data + buf->size - bo_ptr(buf); |
| 501 | if (firstblock > offset) { |
| 502 | if (firstblock >= len + offset) { |
| 503 | memcpy(blk, bo_ptr(buf) + offset, len); |
| 504 | return len; |
| 505 | } |
| 506 | |
| 507 | memcpy(blk, bo_ptr(buf) + offset, firstblock - offset); |
| 508 | memcpy(blk + firstblock - offset, buf->data, len - firstblock + offset); |
| 509 | return len; |
| 510 | } |
| 511 | |
| 512 | memcpy(blk, buf->data + offset - firstblock, len); |
| 513 | return len; |
| 514 | } |
| 515 | |
| 516 | /* Gets one or two blocks of data at once from a buffer's output. |
| 517 | * Return values : |
| 518 | * >0 : number of blocks filled (1 or 2). blk1 is always filled before blk2. |
| 519 | * =0 : not enough data available. <blk*> are left undefined. |
| 520 | * The buffer is left unaffected. Unused buffers are left in an undefined state. |
| 521 | */ |
| 522 | static inline int bo_getblk_nc(struct buffer *buf, char **blk1, int *len1, char **blk2, int *len2) |
| 523 | { |
| 524 | if (unlikely(buf->o == 0)) |
| 525 | return 0; |
| 526 | |
Willy Tarreau | 0621da5 | 2017-10-20 18:21:49 +0200 | [diff] [blame] | 527 | if (unlikely(buf->p != buf->data && buf->p - buf->o < buf->data)) { |
Willy Tarreau | e0e734c | 2017-10-19 14:56:49 +0200 | [diff] [blame] | 528 | *blk1 = buf->p - buf->o + buf->size; |
| 529 | *len1 = buf->data + buf->size - *blk1; |
| 530 | *blk2 = buf->data; |
| 531 | *len2 = buf->p - buf->data; |
| 532 | return 2; |
| 533 | } |
| 534 | |
Willy Tarreau | 4b75fff | 2017-11-02 17:16:07 +0100 | [diff] [blame] | 535 | *blk1 = bo_ptr(buf); |
Willy Tarreau | e0e734c | 2017-10-19 14:56:49 +0200 | [diff] [blame] | 536 | *len1 = buf->o; |
| 537 | return 1; |
| 538 | } |
| 539 | |
Willy Tarreau | 145746c | 2017-10-26 15:26:17 +0200 | [diff] [blame] | 540 | /* Tries to write char <c> into input data at buffer <b>. Supports wrapping. |
| 541 | * Data are truncated if buffer is full. |
| 542 | */ |
| 543 | static inline void bi_putchr(struct buffer *b, char c) |
| 544 | { |
| 545 | if (buffer_len(b) == b->size) |
| 546 | return; |
| 547 | *bi_end(b) = c; |
| 548 | b->i++; |
| 549 | } |
| 550 | |
| 551 | /* Tries to copy block <blk> into input data at buffer <b>. Supports wrapping. |
| 552 | * Data are truncated if buffer is too short. It returns the number of bytes |
| 553 | * copied. |
| 554 | */ |
| 555 | static inline int bi_putblk(struct buffer *b, const char *blk, int len) |
| 556 | { |
| 557 | int cur_len = buffer_len(b); |
| 558 | int half; |
| 559 | |
| 560 | if (len > b->size - cur_len) |
| 561 | len = (b->size - cur_len); |
| 562 | if (!len) |
| 563 | return 0; |
| 564 | |
| 565 | half = bi_contig_space(b); |
| 566 | if (half > len) |
| 567 | half = len; |
| 568 | |
| 569 | memcpy(bi_end(b), blk, half); |
| 570 | if (len > half) |
Christopher Faulet | ca6ef50 | 2018-02-26 10:51:28 +0100 | [diff] [blame] | 571 | memcpy(b_ptr(b, b->i + half), blk + half, len - half); |
Willy Tarreau | 145746c | 2017-10-26 15:26:17 +0200 | [diff] [blame] | 572 | b->i += len; |
| 573 | return len; |
| 574 | } |
| 575 | |
| 576 | /* Tries to copy string <str> 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_putstr(struct buffer *b, const char *str) |
| 581 | { |
| 582 | return bi_putblk(b, str, strlen(str)); |
| 583 | } |
| 584 | |
| 585 | /* Tries to copy chunk <chk> into input data at buffer <b>. Supports wrapping. |
| 586 | * Data are truncated if buffer is too short. It returns the number of bytes |
| 587 | * copied. |
| 588 | */ |
| 589 | static inline int bi_putchk(struct buffer *b, const struct chunk *chk) |
| 590 | { |
| 591 | return bi_putblk(b, chk->str, chk->len); |
| 592 | } |
| 593 | |
| 594 | /* Gets one full block of data at once from a buffer's input. Return values : |
| 595 | * >0 : number of bytes read, equal to requested size. |
| 596 | * =0 : not enough data available. <blk> is left undefined. |
| 597 | * The buffer is left unaffected. |
| 598 | */ |
| 599 | static inline int bi_getblk(const struct buffer *buf, char *blk, int len) |
| 600 | { |
| 601 | int firstblock; |
| 602 | |
| 603 | if (len > buf->i) |
| 604 | return 0; |
| 605 | |
| 606 | firstblock = bi_contig_data(buf); |
| 607 | if (firstblock > len) |
| 608 | firstblock = len; |
| 609 | |
| 610 | memcpy(blk, bi_ptr(buf), firstblock); |
| 611 | if (len > firstblock) |
| 612 | memcpy(blk + firstblock, buf->data, len - firstblock); |
| 613 | return len; |
| 614 | } |
| 615 | |
| 616 | /* Gets one or two blocks of data at once from a buffer's input. |
| 617 | * Return values : |
| 618 | * >0 : number of blocks filled (1 or 2). blk1 is always filled before blk2. |
| 619 | * =0 : not enough data available. <blk*> are left undefined. |
| 620 | * The buffer is left unaffected. Unused buffers are left in an undefined state. |
| 621 | */ |
| 622 | static inline int bi_getblk_nc(struct buffer *buf, char **blk1, int *len1, char **blk2, int *len2) |
| 623 | { |
| 624 | if (unlikely(buf->i == 0)) |
| 625 | return 0; |
| 626 | |
| 627 | if (unlikely(buf->p + buf->i > buf->data + buf->size)) { |
| 628 | *blk1 = buf->p; |
| 629 | *len1 = buf->data + buf->size - buf->p; |
| 630 | *blk2 = buf->data; |
| 631 | *len2 = buf->i - *len1; |
| 632 | return 2; |
| 633 | } |
| 634 | |
| 635 | *blk1 = buf->p; |
| 636 | *len1 = buf->i; |
| 637 | return 1; |
| 638 | } |
Willy Tarreau | e0e734c | 2017-10-19 14:56:49 +0200 | [diff] [blame] | 639 | |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 640 | /* Allocates a buffer and replaces *buf with this buffer. If no memory is |
| 641 | * available, &buf_wanted is used instead. No control is made to check if *buf |
| 642 | * already pointed to another buffer. The allocated buffer is returned, or |
| 643 | * NULL in case no memory is available. |
Willy Tarreau | e583ea5 | 2014-11-24 11:30:16 +0100 | [diff] [blame] | 644 | */ |
| 645 | static inline struct buffer *b_alloc(struct buffer **buf) |
| 646 | { |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 647 | struct buffer *b; |
| 648 | |
| 649 | *buf = &buf_wanted; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 650 | b = pool_alloc_dirty(pool_head_buffer); |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 651 | if (likely(b)) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 652 | b->size = pool_head_buffer->size - sizeof(struct buffer); |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 653 | b_reset(b); |
| 654 | *buf = b; |
Willy Tarreau | e583ea5 | 2014-11-24 11:30:16 +0100 | [diff] [blame] | 655 | } |
Willy Tarreau | f2f7d6b | 2014-11-24 11:55:08 +0100 | [diff] [blame] | 656 | return b; |
Willy Tarreau | e583ea5 | 2014-11-24 11:30:16 +0100 | [diff] [blame] | 657 | } |
| 658 | |
Willy Tarreau | 620bd6c | 2014-12-08 16:37:26 +0100 | [diff] [blame] | 659 | /* Allocates a buffer and replaces *buf with this buffer. If no memory is |
| 660 | * available, &buf_wanted is used instead. No control is made to check if *buf |
| 661 | * already pointed to another buffer. The allocated buffer is returned, or |
| 662 | * NULL in case no memory is available. The difference with b_alloc() is that |
| 663 | * this function only picks from the pool and never calls malloc(), so it can |
| 664 | * fail even if some memory is available. |
| 665 | */ |
| 666 | static inline struct buffer *b_alloc_fast(struct buffer **buf) |
| 667 | { |
| 668 | struct buffer *b; |
| 669 | |
| 670 | *buf = &buf_wanted; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 671 | b = pool_get_first(pool_head_buffer); |
Willy Tarreau | 620bd6c | 2014-12-08 16:37:26 +0100 | [diff] [blame] | 672 | if (likely(b)) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 673 | b->size = pool_head_buffer->size - sizeof(struct buffer); |
Willy Tarreau | 620bd6c | 2014-12-08 16:37:26 +0100 | [diff] [blame] | 674 | b_reset(b); |
| 675 | *buf = b; |
| 676 | } |
| 677 | return b; |
| 678 | } |
| 679 | |
Willy Tarreau | 2a4b543 | 2014-11-24 11:39:34 +0100 | [diff] [blame] | 680 | /* Releases buffer *buf (no check of emptiness) */ |
| 681 | static inline void __b_drop(struct buffer **buf) |
Willy Tarreau | 7dfca9d | 2014-11-25 19:45:11 +0100 | [diff] [blame] | 682 | { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 683 | pool_free(pool_head_buffer, *buf); |
Willy Tarreau | 7dfca9d | 2014-11-25 19:45:11 +0100 | [diff] [blame] | 684 | } |
| 685 | |
Willy Tarreau | 2a4b543 | 2014-11-24 11:39:34 +0100 | [diff] [blame] | 686 | /* Releases buffer *buf if allocated. */ |
| 687 | static inline void b_drop(struct buffer **buf) |
| 688 | { |
| 689 | if (!(*buf)->size) |
| 690 | return; |
| 691 | __b_drop(buf); |
| 692 | } |
| 693 | |
| 694 | /* Releases buffer *buf if allocated, and replaces it with &buf_empty. */ |
| 695 | static inline void b_free(struct buffer **buf) |
| 696 | { |
| 697 | b_drop(buf); |
| 698 | *buf = &buf_empty; |
| 699 | } |
| 700 | |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 701 | /* Ensures that <buf> is allocated. If an allocation is needed, it ensures that |
| 702 | * there are still at least <margin> buffers available in the pool after this |
| 703 | * allocation so that we don't leave the pool in a condition where a session or |
| 704 | * a response buffer could not be allocated anymore, resulting in a deadlock. |
| 705 | * This means that we sometimes need to try to allocate extra entries even if |
| 706 | * only one buffer is needed. |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 707 | * |
| 708 | * We need to lock the pool here to be sure to have <margin> buffers available |
| 709 | * after the allocation, regardless how many threads that doing it in the same |
| 710 | * time. So, we use internal and lockless memory functions (prefixed with '__'). |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 711 | */ |
| 712 | static inline struct buffer *b_alloc_margin(struct buffer **buf, int margin) |
| 713 | { |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 714 | struct buffer *b; |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 715 | |
| 716 | if ((*buf)->size) |
| 717 | return *buf; |
| 718 | |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 719 | *buf = &buf_wanted; |
Willy Tarreau | f161d0f | 2018-02-22 14:05:55 +0100 | [diff] [blame] | 720 | #ifndef CONFIG_HAP_LOCKLESS_POOLS |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 721 | HA_SPIN_LOCK(POOL_LOCK, &pool_head_buffer->lock); |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 722 | #endif |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 723 | |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 724 | /* fast path */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 725 | if ((pool_head_buffer->allocated - pool_head_buffer->used) > margin) { |
| 726 | b = __pool_get_first(pool_head_buffer); |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 727 | if (likely(b)) { |
Willy Tarreau | f161d0f | 2018-02-22 14:05:55 +0100 | [diff] [blame] | 728 | #ifndef CONFIG_HAP_LOCKLESS_POOLS |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 729 | HA_SPIN_UNLOCK(POOL_LOCK, &pool_head_buffer->lock); |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 730 | #endif |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 731 | b->size = pool_head_buffer->size - sizeof(struct buffer); |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 732 | b_reset(b); |
| 733 | *buf = b; |
| 734 | return b; |
| 735 | } |
| 736 | } |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 737 | |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 738 | /* slow path, uses malloc() */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 739 | b = __pool_refill_alloc(pool_head_buffer, margin); |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 740 | |
Willy Tarreau | f161d0f | 2018-02-22 14:05:55 +0100 | [diff] [blame] | 741 | #ifndef CONFIG_HAP_LOCKLESS_POOLS |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 742 | HA_SPIN_UNLOCK(POOL_LOCK, &pool_head_buffer->lock); |
Olivier Houchard | cf975d4 | 2018-01-24 18:38:31 +0100 | [diff] [blame] | 743 | #endif |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 744 | |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 745 | if (b) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 746 | b->size = pool_head_buffer->size - sizeof(struct buffer); |
Christopher Faulet | fa5c812 | 2017-11-10 10:39:16 +0100 | [diff] [blame] | 747 | b_reset(b); |
| 748 | *buf = b; |
| 749 | } |
| 750 | return b; |
Willy Tarreau | f4718e8 | 2014-12-02 13:54:01 +0100 | [diff] [blame] | 751 | } |
| 752 | |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 753 | |
Willy Tarreau | c41b3e8 | 2018-03-02 10:27:12 +0100 | [diff] [blame] | 754 | /* Offer a buffer currently belonging to target <from> to whoever needs one. |
| 755 | * Any pointer is valid for <from>, including NULL. Its purpose is to avoid |
| 756 | * passing a buffer to oneself in case of failed allocations (e.g. need two |
| 757 | * buffers, get one, fail, release it and wake up self again). In case of |
| 758 | * normal buffer release where it is expected that the caller is not waiting |
| 759 | * for a buffer, NULL is fine. |
| 760 | */ |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 761 | void __offer_buffer(void *from, unsigned int threshold); |
| 762 | |
| 763 | static inline void offer_buffers(void *from, unsigned int threshold) |
| 764 | { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 765 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Emeric Brun | a1dd243 | 2017-06-21 15:42:52 +0200 | [diff] [blame] | 766 | if (LIST_ISEMPTY(&buffer_wq)) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 767 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 768 | return; |
Emeric Brun | a1dd243 | 2017-06-21 15:42:52 +0200 | [diff] [blame] | 769 | } |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 770 | __offer_buffer(from, threshold); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 771 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 772 | } |
| 773 | |
Willy Tarreau | 6634b63 | 2017-09-22 15:02:54 +0200 | [diff] [blame] | 774 | /*************************************************************************/ |
| 775 | /* functions used to manipulate strings and blocks with wrapping buffers */ |
| 776 | /*************************************************************************/ |
| 777 | |
| 778 | /* returns > 0 if the first <n> characters of buffer <b> starting at |
| 779 | * offset <o> relative to b->p match <ist>. (empty strings do match). It is |
| 780 | * designed to be use with reasonably small strings (ie matches a single byte |
| 781 | * per iteration). This function is usable both with input and output data. To |
| 782 | * be used like this depending on what to match : |
| 783 | * - input contents : b_isteq(b, 0, b->i, ist); |
| 784 | * - output contents : b_isteq(b, -b->o, b->o, ist); |
| 785 | * Return value : |
| 786 | * >0 : the number of matching bytes |
| 787 | * =0 : not enough bytes (or matching of empty string) |
| 788 | * <0 : non-matching byte found |
| 789 | */ |
| 790 | static inline int b_isteq(const struct buffer *b, unsigned int o, size_t n, const struct ist ist) |
| 791 | { |
| 792 | struct ist r = ist; |
| 793 | const char *p; |
| 794 | const char *end = b->data + b->size; |
| 795 | |
| 796 | if (n < r.len) |
| 797 | return 0; |
| 798 | |
| 799 | p = b_ptr(b, o); |
| 800 | while (r.len--) { |
| 801 | if (*p++ != *r.ptr++) |
| 802 | return -1; |
| 803 | if (unlikely(p == end)) |
| 804 | p = b->data; |
| 805 | } |
| 806 | return ist.len; |
| 807 | } |
| 808 | |
| 809 | /* "eats" string <ist> from the input region of buffer <b>. Wrapping data is |
| 810 | * explicitly supported. It matches a single byte per iteration so strings |
| 811 | * should remain reasonably small. Returns : |
| 812 | * > 0 : number of bytes matched and eaten |
| 813 | * = 0 : not enough bytes (or matching an empty string) |
| 814 | * < 0 : non-matching byte found |
| 815 | */ |
| 816 | static inline int bi_eat(struct buffer *b, const struct ist ist) |
| 817 | { |
| 818 | int ret = b_isteq(b, 0, b->i, ist); |
| 819 | if (ret > 0) |
| 820 | bi_del(b, ret); |
| 821 | return ret; |
| 822 | } |
| 823 | |
Willy Tarreau | e5676e7 | 2017-09-22 15:47:51 +0200 | [diff] [blame] | 824 | /* injects string <ist> into the input region of buffer <b> provided that it |
| 825 | * fits. Wrapping is supported. It's designed for small strings as it only |
| 826 | * writes a single byte per iteration. Returns the number of characters copied |
| 827 | * (ist.len), 0 if it temporarily does not fit or -1 if it will never fit. It |
| 828 | * will only modify the buffer upon success. In all cases, the contents are |
| 829 | * copied prior to reporting an error, so that the destination at least |
| 830 | * contains a valid but truncated string. |
| 831 | */ |
| 832 | static inline int bi_istput(struct buffer *b, const struct ist ist) |
| 833 | { |
| 834 | const char *end = b->data + b->size; |
| 835 | struct ist r = ist; |
| 836 | char *p; |
| 837 | |
| 838 | if (r.len > (size_t)(b->size - b->i - b->o)) |
| 839 | return r.len < b->size ? 0 : -1; |
| 840 | |
| 841 | p = b_ptr(b, b->i); |
| 842 | b->i += r.len; |
| 843 | while (r.len--) { |
| 844 | *p++ = *r.ptr++; |
| 845 | if (unlikely(p == end)) |
| 846 | p = b->data; |
| 847 | } |
| 848 | return ist.len; |
| 849 | } |
| 850 | |
| 851 | |
| 852 | /* injects string <ist> into the output region of buffer <b> provided that it |
| 853 | * fits. Input data is assumed not to exist and will silently be overwritten. |
| 854 | * Wrapping is supported. It's designed for small strings as it only writes a |
| 855 | * single byte per iteration. Returns the number of characters copied (ist.len), |
| 856 | * 0 if it temporarily does not fit or -1 if it will never fit. It will only |
| 857 | * modify the buffer upon success. In all cases, the contents are copied prior |
| 858 | * to reporting an error, so that the destination at least contains a valid |
| 859 | * but truncated string. |
| 860 | */ |
| 861 | static inline int bo_istput(struct buffer *b, const struct ist ist) |
| 862 | { |
| 863 | const char *end = b->data + b->size; |
| 864 | struct ist r = ist; |
| 865 | char *p; |
| 866 | |
| 867 | if (r.len > (size_t)(b->size - b->o)) |
| 868 | return r.len < b->size ? 0 : -1; |
| 869 | |
| 870 | p = b->p; |
| 871 | b->o += r.len; |
| 872 | b->p = b_ptr(b, r.len); |
| 873 | while (r.len--) { |
| 874 | *p++ = *r.ptr++; |
| 875 | if (unlikely(p == end)) |
| 876 | p = b->data; |
| 877 | } |
| 878 | return ist.len; |
| 879 | } |
| 880 | |
| 881 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 882 | #endif /* _COMMON_BUFFER_H */ |
| 883 | |
| 884 | /* |
| 885 | * Local variables: |
| 886 | * c-indent-level: 8 |
| 887 | * c-basic-offset: 8 |
| 888 | * End: |
| 889 | */ |