Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | include/proto/buffers.h |
| 3 | Buffer management definitions, macros and inline functions. |
| 4 | |
| 5 | Copyright (C) 2000-2006 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 _PROTO_BUFFERS_H |
| 23 | #define _PROTO_BUFFERS_H |
| 24 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 25 | #include <stdlib.h> |
| 26 | |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 27 | #include <common/config.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 28 | #include <types/buffers.h> |
| 29 | |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 30 | /* Initializes all fields in the buffer. The ->rlim field is initialized last |
| 31 | * so that the compiler can optimize it away if changed immediately after the |
| 32 | * call to this function. |
| 33 | */ |
| 34 | static inline void buffer_init(struct buffer *buf) |
| 35 | { |
| 36 | buf->l = buf->total = buf->flags = 0; |
| 37 | buf->rlim = buf->h = buf->r = buf->lr = buf->w = buf->data; |
| 38 | } |
| 39 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 40 | /* returns 1 if the buffer is empty, 0 otherwise */ |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 41 | static inline int buffer_isempty(const struct buffer *buf) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 42 | { |
| 43 | return buf->l == 0; |
| 44 | } |
| 45 | |
| 46 | /* returns 1 if the buffer is full, 0 otherwise */ |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 47 | static inline int buffer_isfull(const struct buffer *buf) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 48 | return buf->l == BUFSIZE; |
| 49 | } |
| 50 | |
| 51 | /* flushes any content from buffer <buf> */ |
| 52 | static inline void buffer_flush(struct buffer *buf) |
| 53 | { |
| 54 | buf->r = buf->h = buf->lr = buf->w = buf->data; |
| 55 | buf->l = 0; |
| 56 | } |
| 57 | |
| 58 | |
| 59 | /* returns the maximum number of bytes writable at once in this buffer */ |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 60 | static inline int buffer_max(const struct buffer *buf) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 61 | { |
| 62 | if (buf->l == BUFSIZE) |
| 63 | return 0; |
| 64 | else if (buf->r >= buf->w) |
| 65 | return buf->data + BUFSIZE - buf->r; |
| 66 | else |
| 67 | return buf->w - buf->r; |
| 68 | } |
| 69 | |
| 70 | |
| 71 | /* |
| 72 | * Tries to realign the given buffer, and returns how many bytes can be written |
| 73 | * there at once without overwriting anything. |
| 74 | */ |
| 75 | static inline int buffer_realign(struct buffer *buf) |
| 76 | { |
| 77 | if (buf->l == 0) { |
| 78 | /* let's realign the buffer to optimize I/O */ |
| 79 | buf->r = buf->w = buf->h = buf->lr = buf->data; |
| 80 | } |
| 81 | return buffer_max(buf); |
| 82 | } |
| 83 | |
| 84 | |
| 85 | int buffer_write(struct buffer *buf, const char *msg, int len); |
| 86 | int buffer_replace(struct buffer *b, char *pos, char *end, char *str); |
| 87 | int buffer_replace2(struct buffer *b, char *pos, char *end, char *str, int len); |
| 88 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 89 | /* |
| 90 | * frees the destination chunk if already allocated, allocates a new string, |
| 91 | * and copies the source into it. The pointer to the destination string is |
| 92 | * returned, or NULL if the allocation fails or if any pointer is NULL.. |
| 93 | */ |
| 94 | static inline char *chunk_dup(struct chunk *dst, const struct chunk *src) { |
| 95 | if (!dst || !src || !src->str) |
| 96 | return NULL; |
| 97 | if (dst->str) |
| 98 | free(dst->str); |
| 99 | dst->len = src->len; |
| 100 | dst->str = (char *)malloc(dst->len); |
| 101 | memcpy(dst->str, src->str, dst->len); |
| 102 | return dst->str; |
| 103 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 104 | |
| 105 | #endif /* _PROTO_BUFFERS_H */ |
| 106 | |
| 107 | /* |
| 108 | * Local variables: |
| 109 | * c-indent-level: 8 |
| 110 | * c-basic-offset: 8 |
| 111 | * End: |
| 112 | */ |