blob: 4ceedc077e6ac6314ebaf7bb7fb933e496403f93 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 include/proto/buffers.h
3 Buffer management definitions, macros and inline functions.
4
Willy Tarreauba392ce2008-08-16 21:13:23 +02005 Copyright (C) 2000-2008 Willy Tarreau - w@1wt.eu
Willy Tarreaubaaee002006-06-26 02:48:02 +02006
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 Tarreau7341d942007-05-13 19:56:02 +020025#include <stdio.h>
Willy Tarreau0f772532006-12-23 20:51:41 +010026#include <stdlib.h>
Willy Tarreau7341d942007-05-13 19:56:02 +020027#include <string.h>
Willy Tarreau0f772532006-12-23 20:51:41 +010028
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020029#include <common/config.h>
Willy Tarreau7341d942007-05-13 19:56:02 +020030#include <common/memory.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020031#include <common/ticks.h>
Willy Tarreaufa645582007-06-03 15:59:52 +020032#include <common/time.h>
33
Willy Tarreaubaaee002006-06-26 02:48:02 +020034#include <types/buffers.h>
35
Willy Tarreau7341d942007-05-13 19:56:02 +020036extern struct pool_head *pool2_buffer;
37
38/* perform minimal intializations, report 0 in case of error, 1 if OK. */
39int init_buffer();
40
Willy Tarreau54469402006-07-29 16:59:06 +020041/* Initializes all fields in the buffer. The ->rlim field is initialized last
42 * so that the compiler can optimize it away if changed immediately after the
Willy Tarreaue393fe22008-08-16 22:18:07 +020043 * call to this function. By default, it is set to the full size of the buffer.
44 * The BF_EMPTY flags is set.
Willy Tarreau54469402006-07-29 16:59:06 +020045 */
46static inline void buffer_init(struct buffer *buf)
47{
Willy Tarreaue393fe22008-08-16 22:18:07 +020048 buf->l = buf->total = 0;
Willy Tarreau2df28e82008-08-17 15:20:19 +020049 buf->analysers = 0;
Willy Tarreaue393fe22008-08-16 22:18:07 +020050 buf->flags = BF_EMPTY;
Willy Tarreau2df28e82008-08-17 15:20:19 +020051 buf->r = buf->lr = buf->w = buf->data;
Willy Tarreaue393fe22008-08-16 22:18:07 +020052 buf->rlim = buf->data + BUFSIZE;
Willy Tarreau54469402006-07-29 16:59:06 +020053}
54
Willy Tarreaubaaee002006-06-26 02:48:02 +020055/* returns 1 if the buffer is empty, 0 otherwise */
Willy Tarreaub17916e2006-10-15 15:17:57 +020056static inline int buffer_isempty(const struct buffer *buf)
Willy Tarreaubaaee002006-06-26 02:48:02 +020057{
58 return buf->l == 0;
59}
60
61/* returns 1 if the buffer is full, 0 otherwise */
Willy Tarreaub17916e2006-10-15 15:17:57 +020062static inline int buffer_isfull(const struct buffer *buf) {
Willy Tarreaubaaee002006-06-26 02:48:02 +020063 return buf->l == BUFSIZE;
64}
65
Willy Tarreaue393fe22008-08-16 22:18:07 +020066/* flushes any content from buffer <buf> and adjusts flags
67 * accordingly.
68 */
Willy Tarreaubaaee002006-06-26 02:48:02 +020069static inline void buffer_flush(struct buffer *buf)
70{
Willy Tarreaue09e0ce2007-03-18 16:31:29 +010071 buf->r = buf->lr = buf->w = buf->data;
Willy Tarreaubaaee002006-06-26 02:48:02 +020072 buf->l = 0;
Willy Tarreaue393fe22008-08-16 22:18:07 +020073 buf->flags |= BF_EMPTY | BF_FULL;
74 if (buf->rlim)
75 buf->flags &= ~BF_FULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +020076}
77
Willy Tarreauba392ce2008-08-16 21:13:23 +020078/* marks the buffer as "shutdown" for reads and cancels the timeout */
Willy Tarreaufa645582007-06-03 15:59:52 +020079static inline void buffer_shutr(struct buffer *buf)
80{
Willy Tarreau0c303ee2008-07-07 00:09:58 +020081 buf->rex = TICK_ETERNITY;
Willy Tarreauba392ce2008-08-16 21:13:23 +020082 buf->flags |= BF_SHUTR;
Willy Tarreaufa645582007-06-03 15:59:52 +020083}
84
Willy Tarreauba392ce2008-08-16 21:13:23 +020085/* marks the buffer as "shutdown" for writes and cancels the timeout */
Willy Tarreaufa645582007-06-03 15:59:52 +020086static inline void buffer_shutw(struct buffer *buf)
87{
Willy Tarreau0c303ee2008-07-07 00:09:58 +020088 buf->wex = TICK_ETERNITY;
Willy Tarreauba392ce2008-08-16 21:13:23 +020089 buf->flags |= BF_SHUTW;
Willy Tarreaufa645582007-06-03 15:59:52 +020090}
91
Willy Tarreaubaaee002006-06-26 02:48:02 +020092/* returns the maximum number of bytes writable at once in this buffer */
Willy Tarreaub17916e2006-10-15 15:17:57 +020093static inline int buffer_max(const struct buffer *buf)
Willy Tarreaubaaee002006-06-26 02:48:02 +020094{
95 if (buf->l == BUFSIZE)
96 return 0;
97 else if (buf->r >= buf->w)
98 return buf->data + BUFSIZE - buf->r;
99 else
100 return buf->w - buf->r;
101}
102
Willy Tarreaue393fe22008-08-16 22:18:07 +0200103/* sets the buffer read limit to <size> bytes, and adjusts the FULL
104 * flag accordingly.
105 */
106static inline void buffer_set_rlim(struct buffer *buf, int size)
107{
108 buf->rlim = buf->data + size;
109 if (buf->l < size)
110 buf->flags &= ~BF_FULL;
111 else
112 buf->flags |= BF_FULL;
113}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200114
115/*
116 * Tries to realign the given buffer, and returns how many bytes can be written
117 * there at once without overwriting anything.
118 */
119static inline int buffer_realign(struct buffer *buf)
120{
121 if (buf->l == 0) {
122 /* let's realign the buffer to optimize I/O */
Willy Tarreaue09e0ce2007-03-18 16:31:29 +0100123 buf->r = buf->w = buf->lr = buf->data;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200124 }
125 return buffer_max(buf);
126}
127
128
129int buffer_write(struct buffer *buf, const char *msg, int len);
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100130int buffer_write_chunk(struct buffer *buf, struct chunk *chunk);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100131int buffer_replace(struct buffer *b, char *pos, char *end, const char *str);
132int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len);
133int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len);
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100134int chunk_printf(struct chunk *chk, int size, const char *fmt, ...);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100135void buffer_dump(FILE *o, struct buffer *b, int from, int to);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200136
Willy Tarreau0f772532006-12-23 20:51:41 +0100137/*
138 * frees the destination chunk if already allocated, allocates a new string,
139 * and copies the source into it. The pointer to the destination string is
140 * returned, or NULL if the allocation fails or if any pointer is NULL..
141 */
142static inline char *chunk_dup(struct chunk *dst, const struct chunk *src) {
143 if (!dst || !src || !src->str)
144 return NULL;
145 if (dst->str)
146 free(dst->str);
147 dst->len = src->len;
148 dst->str = (char *)malloc(dst->len);
149 memcpy(dst->str, src->str, dst->len);
150 return dst->str;
151}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200152
153#endif /* _PROTO_BUFFERS_H */
154
155/*
156 * Local variables:
157 * c-indent-level: 8
158 * c-basic-offset: 8
159 * End:
160 */