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 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 42 | extern struct pool_head *pool2_buffer; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 43 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 44 | int init_buffer(); |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 45 | int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len); |
| 46 | 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] | 47 | void buffer_dump(FILE *o, struct buffer *b, int from, int to); |
| 48 | void buffer_slow_realign(struct buffer *buf); |
| 49 | void buffer_bounce_realign(struct buffer *buf); |
| 50 | |
| 51 | /*****************************************************************/ |
| 52 | /* These functions are used to compute various buffer area sizes */ |
| 53 | /*****************************************************************/ |
| 54 | |
| 55 | /* Returns an absolute pointer for a position relative to the current buffer's |
| 56 | * pointer. It is written so that it is optimal when <ofs> is a const. It is |
| 57 | * written as a macro instead of an inline function so that the compiler knows |
| 58 | * 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] | 59 | * 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] | 60 | */ |
| 61 | #define b_ptr(b, ofs) \ |
| 62 | ({ \ |
| 63 | char *__ret = (b)->p + (ofs); \ |
| 64 | if ((ofs) > 0 && __ret >= (b)->data + (b)->size) \ |
| 65 | __ret -= (b)->size; \ |
| 66 | else if ((ofs) < 0 && __ret < (b)->data) \ |
| 67 | __ret += (b)->size; \ |
| 68 | __ret; \ |
| 69 | }) |
| 70 | |
Willy Tarreau | a75bcef | 2012-08-24 22:56:11 +0200 | [diff] [blame] | 71 | /* Advances the buffer by <adv> bytes, which means that the buffer |
| 72 | * pointer advances, and that as many bytes from in are transferred |
| 73 | * to out. The caller is responsible for ensuring that adv is always |
| 74 | * smaller than or equal to b->i. |
| 75 | */ |
| 76 | static inline void b_adv(struct buffer *b, unsigned int adv) |
| 77 | { |
| 78 | b->i -= adv; |
| 79 | b->o += adv; |
| 80 | b->p = b_ptr(b, adv); |
| 81 | } |
| 82 | |
| 83 | /* Rewinds the buffer by <adv> bytes, which means that the buffer pointer goes |
| 84 | * backwards, and that as many bytes from out are moved to in. The caller is |
| 85 | * responsible for ensuring that adv is always smaller than or equal to b->o. |
| 86 | */ |
| 87 | static inline void b_rew(struct buffer *b, unsigned int adv) |
| 88 | { |
| 89 | b->i += adv; |
| 90 | b->o -= adv; |
| 91 | b->p = b_ptr(b, (int)-adv); |
| 92 | } |
| 93 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 94 | /* Returns the start of the input data in a buffer */ |
| 95 | static inline char *bi_ptr(const struct buffer *b) |
| 96 | { |
| 97 | return b->p; |
| 98 | } |
| 99 | |
| 100 | /* Returns the end of the input data in a buffer (pointer to next |
| 101 | * insertion point). |
| 102 | */ |
| 103 | static inline char *bi_end(const struct buffer *b) |
| 104 | { |
| 105 | char *ret = b->p + b->i; |
| 106 | |
| 107 | if (ret >= b->data + b->size) |
| 108 | ret -= b->size; |
| 109 | return ret; |
| 110 | } |
| 111 | |
| 112 | /* Returns the amount of input data that can contiguously be read at once */ |
| 113 | static inline int bi_contig_data(const struct buffer *b) |
| 114 | { |
| 115 | int data = b->data + b->size - b->p; |
| 116 | |
| 117 | if (data > b->i) |
| 118 | data = b->i; |
| 119 | return data; |
| 120 | } |
| 121 | |
| 122 | /* Returns the start of the output data in a buffer */ |
| 123 | static inline char *bo_ptr(const struct buffer *b) |
| 124 | { |
| 125 | char *ret = b->p - b->o; |
| 126 | |
| 127 | if (ret < b->data) |
| 128 | ret += b->size; |
| 129 | return ret; |
| 130 | } |
| 131 | |
| 132 | /* Returns the end of the output data in a buffer */ |
| 133 | static inline char *bo_end(const struct buffer *b) |
| 134 | { |
| 135 | return b->p; |
| 136 | } |
| 137 | |
| 138 | /* Returns the amount of output data that can contiguously be read at once */ |
| 139 | static inline int bo_contig_data(const struct buffer *b) |
| 140 | { |
| 141 | char *beg = b->p - b->o; |
| 142 | |
| 143 | if (beg < b->data) |
| 144 | return b->data - beg; |
| 145 | return b->o; |
| 146 | } |
| 147 | |
| 148 | /* Return the buffer's length in bytes by summing the input and the output */ |
| 149 | static inline int buffer_len(const struct buffer *buf) |
| 150 | { |
| 151 | return buf->i + buf->o; |
| 152 | } |
| 153 | |
| 154 | /* Return non-zero only if the buffer is not empty */ |
| 155 | static inline int buffer_not_empty(const struct buffer *buf) |
| 156 | { |
| 157 | return buf->i | buf->o; |
| 158 | } |
| 159 | |
| 160 | /* Return non-zero only if the buffer is empty */ |
| 161 | static inline int buffer_empty(const struct buffer *buf) |
| 162 | { |
| 163 | return !buffer_not_empty(buf); |
| 164 | } |
| 165 | |
Willy Tarreau | 42d0666 | 2012-08-27 19:51:36 +0200 | [diff] [blame] | 166 | /* Returns non-zero if the buffer's INPUT is considered full, which means that |
| 167 | * it holds at least as much INPUT data as (size - reserve). This also means |
| 168 | * that data that are scheduled for output are considered as potential free |
| 169 | * space, and that the reserved space is always considered as not usable. This |
| 170 | * information alone cannot be used as a general purpose free space indicator. |
| 171 | * However it accurately indicates that too many data were fed in the buffer |
| 172 | * for an analyzer for instance. See the channel_full() function for a more |
| 173 | * generic function taking everything into account. |
| 174 | */ |
| 175 | static inline int buffer_full(const struct buffer *b, unsigned int reserve) |
| 176 | { |
| 177 | return (b->i + reserve >= b->size); |
| 178 | } |
| 179 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 180 | /* Normalizes a pointer after a subtract */ |
| 181 | static inline char *buffer_wrap_sub(const struct buffer *buf, char *ptr) |
| 182 | { |
| 183 | if (ptr < buf->data) |
| 184 | ptr += buf->size; |
| 185 | return ptr; |
| 186 | } |
| 187 | |
| 188 | /* Normalizes a pointer after an addition */ |
| 189 | static inline char *buffer_wrap_add(const struct buffer *buf, char *ptr) |
| 190 | { |
| 191 | if (ptr - buf->size >= buf->data) |
| 192 | ptr -= buf->size; |
| 193 | return ptr; |
| 194 | } |
| 195 | |
| 196 | /* Return the maximum amount of bytes that can be written into the buffer, |
| 197 | * including reserved space which may be overwritten. |
| 198 | */ |
| 199 | static inline int buffer_total_space(const struct buffer *buf) |
| 200 | { |
| 201 | return buf->size - buffer_len(buf); |
| 202 | } |
| 203 | |
| 204 | /* Returns the number of contiguous bytes between <start> and <start>+<count>, |
| 205 | * and enforces a limit on buf->data + buf->size. <start> must be within the |
| 206 | * buffer. |
| 207 | */ |
| 208 | static inline int buffer_contig_area(const struct buffer *buf, const char *start, int count) |
| 209 | { |
| 210 | if (count > buf->data - start + buf->size) |
| 211 | count = buf->data - start + buf->size; |
| 212 | return count; |
| 213 | } |
| 214 | |
| 215 | /* Return the amount of bytes that can be written into the buffer at once, |
| 216 | * including reserved space which may be overwritten. |
| 217 | */ |
| 218 | static inline int buffer_contig_space(const struct buffer *buf) |
| 219 | { |
| 220 | const char *left, *right; |
| 221 | |
| 222 | if (buf->data + buf->o <= buf->p) |
| 223 | right = buf->data + buf->size; |
| 224 | else |
| 225 | right = buf->p + buf->size - buf->o; |
| 226 | |
| 227 | left = buffer_wrap_add(buf, buf->p + buf->i); |
| 228 | return right - left; |
| 229 | } |
| 230 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 231 | /* Normalizes a pointer which is supposed to be relative to the beginning of a |
| 232 | * buffer, so that wrapping is correctly handled. The intent is to use this |
| 233 | * when increasing a pointer. Note that the wrapping test is only performed |
| 234 | * once, so the original pointer must be between ->data-size and ->data+2*size-1, |
| 235 | * otherwise an invalid pointer might be returned. |
| 236 | */ |
| 237 | static inline const char *buffer_pointer(const struct buffer *buf, const char *ptr) |
| 238 | { |
| 239 | if (ptr < buf->data) |
| 240 | ptr += buf->size; |
| 241 | else if (ptr - buf->size >= buf->data) |
| 242 | ptr -= buf->size; |
| 243 | return ptr; |
| 244 | } |
| 245 | |
| 246 | /* Returns the distance between two pointers, taking into account the ability |
| 247 | * to wrap around the buffer's end. |
| 248 | */ |
| 249 | static inline int buffer_count(const struct buffer *buf, const char *from, const char *to) |
| 250 | { |
| 251 | int count = to - from; |
Willy Tarreau | bf43927 | 2013-04-02 01:25:57 +0200 | [diff] [blame] | 252 | |
| 253 | count += count < 0 ? buf->size : 0; |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 254 | return count; |
| 255 | } |
| 256 | |
| 257 | /* returns the amount of pending bytes in the buffer. It is the amount of bytes |
| 258 | * that is not scheduled to be sent. |
| 259 | */ |
| 260 | static inline int buffer_pending(const struct buffer *buf) |
| 261 | { |
| 262 | return buf->i; |
| 263 | } |
| 264 | |
| 265 | /* Returns the size of the working area which the caller knows ends at <end>. |
| 266 | * If <end> equals buf->r (modulo size), then it means that the free area which |
| 267 | * follows is part of the working area. Otherwise, the working area stops at |
| 268 | * <end>. It always starts at buf->p. The work area includes the |
| 269 | * reserved area. |
| 270 | */ |
| 271 | static inline int buffer_work_area(const struct buffer *buf, const char *end) |
| 272 | { |
| 273 | end = buffer_pointer(buf, end); |
| 274 | if (end == buffer_wrap_add(buf, buf->p + buf->i)) |
| 275 | /* pointer exactly at end, lets push forwards */ |
| 276 | end = buffer_wrap_sub(buf, buf->p - buf->o); |
| 277 | return buffer_count(buf, buf->p, end); |
| 278 | } |
| 279 | |
| 280 | /* Return 1 if the buffer has less than 1/4 of its capacity free, otherwise 0 */ |
| 281 | static inline int buffer_almost_full(const struct buffer *buf) |
| 282 | { |
| 283 | if (buffer_total_space(buf) < buf->size / 4) |
| 284 | return 1; |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | /* Cut the first <n> pending bytes in a contiguous buffer. It is illegal to |
| 289 | * call this function with remaining data waiting to be sent (o > 0). The |
| 290 | * caller must ensure that <n> is smaller than the actual buffer's length. |
| 291 | * This is mainly used to remove empty lines at the beginning of a request |
| 292 | * or a response. |
| 293 | */ |
| 294 | static inline void bi_fast_delete(struct buffer *buf, int n) |
| 295 | { |
| 296 | buf->i -= n; |
| 297 | buf->p += n; |
| 298 | } |
| 299 | |
| 300 | /* |
| 301 | * Tries to realign the given buffer, and returns how many bytes can be written |
| 302 | * there at once without overwriting anything. |
| 303 | */ |
| 304 | static inline int buffer_realign(struct buffer *buf) |
| 305 | { |
| 306 | if (!(buf->i | buf->o)) { |
| 307 | /* let's realign the buffer to optimize I/O */ |
| 308 | buf->p = buf->data; |
| 309 | } |
| 310 | return buffer_contig_space(buf); |
| 311 | } |
| 312 | |
Willy Tarreau | a75bcef | 2012-08-24 22:56:11 +0200 | [diff] [blame] | 313 | /* Schedule all remaining buffer data to be sent. ->o is not touched if it |
| 314 | * already covers those data. That permits doing a flush even after a forward, |
| 315 | * although not recommended. |
| 316 | */ |
| 317 | static inline void buffer_flush(struct buffer *buf) |
| 318 | { |
| 319 | buf->p = buffer_wrap_add(buf, buf->p + buf->i); |
| 320 | buf->o += buf->i; |
| 321 | buf->i = 0; |
| 322 | } |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 323 | |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 324 | /* This function writes the string <str> at position <pos> which must be in |
| 325 | * buffer <b>, and moves <end> just after the end of <str>. <b>'s parameters |
| 326 | * (l, r, lr) are updated to be valid after the shift. the shift value |
| 327 | * (positive or negative) is returned. If there's no space left, the move is |
| 328 | * not done. The function does not adjust ->o because it does not make sense |
| 329 | * to use it on data scheduled to be sent. |
| 330 | */ |
| 331 | static inline int buffer_replace(struct buffer *b, char *pos, char *end, const char *str) |
| 332 | { |
| 333 | return buffer_replace2(b, pos, end, str, strlen(str)); |
| 334 | } |
| 335 | |
Willy Tarreau | 8c89c20 | 2012-09-28 16:02:48 +0200 | [diff] [blame] | 336 | /* Tries to write char <c> into output data at buffer <b>. Supports wrapping. |
| 337 | * Data are truncated if buffer is full. |
| 338 | */ |
| 339 | static inline void bo_putchr(struct buffer *b, char c) |
| 340 | { |
| 341 | if (buffer_len(b) == b->size) |
| 342 | return; |
| 343 | *b->p = c; |
| 344 | b->p = b_ptr(b, 1); |
| 345 | b->o++; |
| 346 | } |
| 347 | |
| 348 | /* Tries to copy block <blk> into output data at buffer <b>. Supports wrapping. |
| 349 | * Data are truncated if buffer is too short. |
| 350 | */ |
| 351 | static inline void bo_putblk(struct buffer *b, const char *blk, int len) |
| 352 | { |
| 353 | int cur_len = buffer_len(b); |
| 354 | int half; |
| 355 | |
| 356 | if (len > b->size - cur_len) |
| 357 | len = (b->size - cur_len); |
| 358 | if (!len) |
| 359 | return; |
| 360 | |
| 361 | half = buffer_contig_space(b); |
| 362 | if (half > len) |
| 363 | half = len; |
| 364 | |
| 365 | memcpy(b->p, blk, half); |
| 366 | b->p = b_ptr(b, half); |
| 367 | if (len > half) { |
| 368 | memcpy(b->p, blk, len - half); |
| 369 | b->p = b_ptr(b, half); |
| 370 | } |
| 371 | b->o += len; |
| 372 | } |
| 373 | |
| 374 | /* Tries to copy string <str> into output data at buffer <b>. Supports wrapping. |
| 375 | * Data are truncated if buffer is too short. |
| 376 | */ |
| 377 | static inline void bo_putstr(struct buffer *b, const char *str) |
| 378 | { |
| 379 | return bo_putblk(b, str, strlen(str)); |
| 380 | } |
| 381 | |
| 382 | /* Tries to copy chunk <chk> into output data at buffer <b>. Supports wrapping. |
| 383 | * Data are truncated if buffer is too short. |
| 384 | */ |
| 385 | static inline void bo_putchk(struct buffer *b, const struct chunk *chk) |
| 386 | { |
| 387 | return bo_putblk(b, chk->str, chk->len); |
| 388 | } |
| 389 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 390 | #endif /* _COMMON_BUFFER_H */ |
| 391 | |
| 392 | /* |
| 393 | * Local variables: |
| 394 | * c-indent-level: 8 |
| 395 | * c-basic-offset: 8 |
| 396 | * End: |
| 397 | */ |