blob: a5ad2081e45ed9d58db1eb1b1e78930f11894453 [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
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 Tarreaue3ba5f02006-06-29 18:54:54 +020025#include <common/config.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020026#include <types/buffers.h>
27
Willy Tarreau54469402006-07-29 16:59:06 +020028/* Initializes all fields in the buffer. The ->rlim field is initialized last
29 * so that the compiler can optimize it away if changed immediately after the
30 * call to this function.
31 */
32static inline void buffer_init(struct buffer *buf)
33{
34 buf->l = buf->total = buf->flags = 0;
35 buf->rlim = buf->h = buf->r = buf->lr = buf->w = buf->data;
36}
37
Willy Tarreaubaaee002006-06-26 02:48:02 +020038/* returns 1 if the buffer is empty, 0 otherwise */
Willy Tarreaub17916e2006-10-15 15:17:57 +020039static inline int buffer_isempty(const struct buffer *buf)
Willy Tarreaubaaee002006-06-26 02:48:02 +020040{
41 return buf->l == 0;
42}
43
44/* returns 1 if the buffer is full, 0 otherwise */
Willy Tarreaub17916e2006-10-15 15:17:57 +020045static inline int buffer_isfull(const struct buffer *buf) {
Willy Tarreaubaaee002006-06-26 02:48:02 +020046 return buf->l == BUFSIZE;
47}
48
49/* flushes any content from buffer <buf> */
50static inline void buffer_flush(struct buffer *buf)
51{
52 buf->r = buf->h = buf->lr = buf->w = buf->data;
53 buf->l = 0;
54}
55
56
57/* returns the maximum number of bytes writable at once in this buffer */
Willy Tarreaub17916e2006-10-15 15:17:57 +020058static inline int buffer_max(const struct buffer *buf)
Willy Tarreaubaaee002006-06-26 02:48:02 +020059{
60 if (buf->l == BUFSIZE)
61 return 0;
62 else if (buf->r >= buf->w)
63 return buf->data + BUFSIZE - buf->r;
64 else
65 return buf->w - buf->r;
66}
67
68
69/*
70 * Tries to realign the given buffer, and returns how many bytes can be written
71 * there at once without overwriting anything.
72 */
73static inline int buffer_realign(struct buffer *buf)
74{
75 if (buf->l == 0) {
76 /* let's realign the buffer to optimize I/O */
77 buf->r = buf->w = buf->h = buf->lr = buf->data;
78 }
79 return buffer_max(buf);
80}
81
82
83int buffer_write(struct buffer *buf, const char *msg, int len);
84int buffer_replace(struct buffer *b, char *pos, char *end, char *str);
85int buffer_replace2(struct buffer *b, char *pos, char *end, char *str, int len);
86
87
88#endif /* _PROTO_BUFFERS_H */
89
90/*
91 * Local variables:
92 * c-indent-level: 8
93 * c-basic-offset: 8
94 * End:
95 */