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