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 | |
| 41 | void buffer_dump(FILE *o, struct buffer *b, int from, int to); |
| 42 | void buffer_slow_realign(struct buffer *buf); |
| 43 | void buffer_bounce_realign(struct buffer *buf); |
| 44 | |
| 45 | /*****************************************************************/ |
| 46 | /* These functions are used to compute various buffer area sizes */ |
| 47 | /*****************************************************************/ |
| 48 | |
| 49 | /* Returns an absolute pointer for a position relative to the current buffer's |
| 50 | * pointer. It is written so that it is optimal when <ofs> is a const. It is |
| 51 | * written as a macro instead of an inline function so that the compiler knows |
| 52 | * when it can optimize out the sign test on <ofs> when passed an unsigned int. |
| 53 | */ |
| 54 | #define b_ptr(b, ofs) \ |
| 55 | ({ \ |
| 56 | char *__ret = (b)->p + (ofs); \ |
| 57 | if ((ofs) > 0 && __ret >= (b)->data + (b)->size) \ |
| 58 | __ret -= (b)->size; \ |
| 59 | else if ((ofs) < 0 && __ret < (b)->data) \ |
| 60 | __ret += (b)->size; \ |
| 61 | __ret; \ |
| 62 | }) |
| 63 | |
| 64 | /* Returns the start of the input data in a buffer */ |
| 65 | static inline char *bi_ptr(const struct buffer *b) |
| 66 | { |
| 67 | return b->p; |
| 68 | } |
| 69 | |
| 70 | /* Returns the end of the input data in a buffer (pointer to next |
| 71 | * insertion point). |
| 72 | */ |
| 73 | static inline char *bi_end(const struct buffer *b) |
| 74 | { |
| 75 | char *ret = b->p + b->i; |
| 76 | |
| 77 | if (ret >= b->data + b->size) |
| 78 | ret -= b->size; |
| 79 | return ret; |
| 80 | } |
| 81 | |
| 82 | /* Returns the amount of input data that can contiguously be read at once */ |
| 83 | static inline int bi_contig_data(const struct buffer *b) |
| 84 | { |
| 85 | int data = b->data + b->size - b->p; |
| 86 | |
| 87 | if (data > b->i) |
| 88 | data = b->i; |
| 89 | return data; |
| 90 | } |
| 91 | |
| 92 | /* Returns the start of the output data in a buffer */ |
| 93 | static inline char *bo_ptr(const struct buffer *b) |
| 94 | { |
| 95 | char *ret = b->p - b->o; |
| 96 | |
| 97 | if (ret < b->data) |
| 98 | ret += b->size; |
| 99 | return ret; |
| 100 | } |
| 101 | |
| 102 | /* Returns the end of the output data in a buffer */ |
| 103 | static inline char *bo_end(const struct buffer *b) |
| 104 | { |
| 105 | return b->p; |
| 106 | } |
| 107 | |
| 108 | /* Returns the amount of output data that can contiguously be read at once */ |
| 109 | static inline int bo_contig_data(const struct buffer *b) |
| 110 | { |
| 111 | char *beg = b->p - b->o; |
| 112 | |
| 113 | if (beg < b->data) |
| 114 | return b->data - beg; |
| 115 | return b->o; |
| 116 | } |
| 117 | |
| 118 | /* Return the buffer's length in bytes by summing the input and the output */ |
| 119 | static inline int buffer_len(const struct buffer *buf) |
| 120 | { |
| 121 | return buf->i + buf->o; |
| 122 | } |
| 123 | |
| 124 | /* Return non-zero only if the buffer is not empty */ |
| 125 | static inline int buffer_not_empty(const struct buffer *buf) |
| 126 | { |
| 127 | return buf->i | buf->o; |
| 128 | } |
| 129 | |
| 130 | /* Return non-zero only if the buffer is empty */ |
| 131 | static inline int buffer_empty(const struct buffer *buf) |
| 132 | { |
| 133 | return !buffer_not_empty(buf); |
| 134 | } |
| 135 | |
| 136 | /* Normalizes a pointer after a subtract */ |
| 137 | static inline char *buffer_wrap_sub(const struct buffer *buf, char *ptr) |
| 138 | { |
| 139 | if (ptr < buf->data) |
| 140 | ptr += buf->size; |
| 141 | return ptr; |
| 142 | } |
| 143 | |
| 144 | /* Normalizes a pointer after an addition */ |
| 145 | static inline char *buffer_wrap_add(const struct buffer *buf, char *ptr) |
| 146 | { |
| 147 | if (ptr - buf->size >= buf->data) |
| 148 | ptr -= buf->size; |
| 149 | return ptr; |
| 150 | } |
| 151 | |
| 152 | /* Return the maximum amount of bytes that can be written into the buffer, |
| 153 | * including reserved space which may be overwritten. |
| 154 | */ |
| 155 | static inline int buffer_total_space(const struct buffer *buf) |
| 156 | { |
| 157 | return buf->size - buffer_len(buf); |
| 158 | } |
| 159 | |
| 160 | /* Returns the number of contiguous bytes between <start> and <start>+<count>, |
| 161 | * and enforces a limit on buf->data + buf->size. <start> must be within the |
| 162 | * buffer. |
| 163 | */ |
| 164 | static inline int buffer_contig_area(const struct buffer *buf, const char *start, int count) |
| 165 | { |
| 166 | if (count > buf->data - start + buf->size) |
| 167 | count = buf->data - start + buf->size; |
| 168 | return count; |
| 169 | } |
| 170 | |
| 171 | /* Return the amount of bytes that can be written into the buffer at once, |
| 172 | * including reserved space which may be overwritten. |
| 173 | */ |
| 174 | static inline int buffer_contig_space(const struct buffer *buf) |
| 175 | { |
| 176 | const char *left, *right; |
| 177 | |
| 178 | if (buf->data + buf->o <= buf->p) |
| 179 | right = buf->data + buf->size; |
| 180 | else |
| 181 | right = buf->p + buf->size - buf->o; |
| 182 | |
| 183 | left = buffer_wrap_add(buf, buf->p + buf->i); |
| 184 | return right - left; |
| 185 | } |
| 186 | |
| 187 | /* Return the amount of bytes that can be written into the buffer at once, |
| 188 | * excluding the amount of reserved space passed in <res>, which is |
| 189 | * preserved. |
| 190 | */ |
| 191 | static inline int buffer_contig_space_with_res(const struct buffer *buf, int res) |
| 192 | { |
| 193 | /* Proceed differently if the buffer is full, partially used or empty. |
| 194 | * The hard situation is when it's partially used and either data or |
| 195 | * reserved space wraps at the end. |
| 196 | */ |
| 197 | int spare = buf->size - res; |
| 198 | |
| 199 | if (buffer_len(buf) >= spare) |
| 200 | spare = 0; |
| 201 | else if (buffer_len(buf)) { |
| 202 | spare = buffer_contig_space(buf) - res; |
| 203 | if (spare < 0) |
| 204 | spare = 0; |
| 205 | } |
| 206 | return spare; |
| 207 | } |
| 208 | |
| 209 | |
| 210 | /* Normalizes a pointer which is supposed to be relative to the beginning of a |
| 211 | * buffer, so that wrapping is correctly handled. The intent is to use this |
| 212 | * when increasing a pointer. Note that the wrapping test is only performed |
| 213 | * once, so the original pointer must be between ->data-size and ->data+2*size-1, |
| 214 | * otherwise an invalid pointer might be returned. |
| 215 | */ |
| 216 | static inline const char *buffer_pointer(const struct buffer *buf, const char *ptr) |
| 217 | { |
| 218 | if (ptr < buf->data) |
| 219 | ptr += buf->size; |
| 220 | else if (ptr - buf->size >= buf->data) |
| 221 | ptr -= buf->size; |
| 222 | return ptr; |
| 223 | } |
| 224 | |
| 225 | /* Returns the distance between two pointers, taking into account the ability |
| 226 | * to wrap around the buffer's end. |
| 227 | */ |
| 228 | static inline int buffer_count(const struct buffer *buf, const char *from, const char *to) |
| 229 | { |
| 230 | int count = to - from; |
| 231 | if (count < 0) |
| 232 | count += buf->size; |
| 233 | return count; |
| 234 | } |
| 235 | |
| 236 | /* returns the amount of pending bytes in the buffer. It is the amount of bytes |
| 237 | * that is not scheduled to be sent. |
| 238 | */ |
| 239 | static inline int buffer_pending(const struct buffer *buf) |
| 240 | { |
| 241 | return buf->i; |
| 242 | } |
| 243 | |
| 244 | /* Returns the size of the working area which the caller knows ends at <end>. |
| 245 | * If <end> equals buf->r (modulo size), then it means that the free area which |
| 246 | * follows is part of the working area. Otherwise, the working area stops at |
| 247 | * <end>. It always starts at buf->p. The work area includes the |
| 248 | * reserved area. |
| 249 | */ |
| 250 | static inline int buffer_work_area(const struct buffer *buf, const char *end) |
| 251 | { |
| 252 | end = buffer_pointer(buf, end); |
| 253 | if (end == buffer_wrap_add(buf, buf->p + buf->i)) |
| 254 | /* pointer exactly at end, lets push forwards */ |
| 255 | end = buffer_wrap_sub(buf, buf->p - buf->o); |
| 256 | return buffer_count(buf, buf->p, end); |
| 257 | } |
| 258 | |
| 259 | /* Return 1 if the buffer has less than 1/4 of its capacity free, otherwise 0 */ |
| 260 | static inline int buffer_almost_full(const struct buffer *buf) |
| 261 | { |
| 262 | if (buffer_total_space(buf) < buf->size / 4) |
| 263 | return 1; |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | /* Cut the first <n> pending bytes in a contiguous buffer. It is illegal to |
| 268 | * call this function with remaining data waiting to be sent (o > 0). The |
| 269 | * caller must ensure that <n> is smaller than the actual buffer's length. |
| 270 | * This is mainly used to remove empty lines at the beginning of a request |
| 271 | * or a response. |
| 272 | */ |
| 273 | static inline void bi_fast_delete(struct buffer *buf, int n) |
| 274 | { |
| 275 | buf->i -= n; |
| 276 | buf->p += n; |
| 277 | } |
| 278 | |
| 279 | /* |
| 280 | * Tries to realign the given buffer, and returns how many bytes can be written |
| 281 | * there at once without overwriting anything. |
| 282 | */ |
| 283 | static inline int buffer_realign(struct buffer *buf) |
| 284 | { |
| 285 | if (!(buf->i | buf->o)) { |
| 286 | /* let's realign the buffer to optimize I/O */ |
| 287 | buf->p = buf->data; |
| 288 | } |
| 289 | return buffer_contig_space(buf); |
| 290 | } |
| 291 | |
| 292 | |
| 293 | #endif /* _COMMON_BUFFER_H */ |
| 294 | |
| 295 | /* |
| 296 | * Local variables: |
| 297 | * c-indent-level: 8 |
| 298 | * c-basic-offset: 8 |
| 299 | * End: |
| 300 | */ |