blob: b332a4bcf6064321f533433ade6c1f1eff03f93a [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 Tarreau2dd0d472006-06-29 17:53:05 +020025#include <common/defaults.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020026#include <types/buffers.h>
27
28/* returns 1 if the buffer is empty, 0 otherwise */
29static inline int buffer_isempty(struct buffer *buf)
30{
31 return buf->l == 0;
32}
33
34/* returns 1 if the buffer is full, 0 otherwise */
35static inline int buffer_isfull(struct buffer *buf) {
36 return buf->l == BUFSIZE;
37}
38
39/* flushes any content from buffer <buf> */
40static inline void buffer_flush(struct buffer *buf)
41{
42 buf->r = buf->h = buf->lr = buf->w = buf->data;
43 buf->l = 0;
44}
45
46
47/* returns the maximum number of bytes writable at once in this buffer */
48static inline int buffer_max(struct buffer *buf)
49{
50 if (buf->l == BUFSIZE)
51 return 0;
52 else if (buf->r >= buf->w)
53 return buf->data + BUFSIZE - buf->r;
54 else
55 return buf->w - buf->r;
56}
57
58
59/*
60 * Tries to realign the given buffer, and returns how many bytes can be written
61 * there at once without overwriting anything.
62 */
63static inline int buffer_realign(struct buffer *buf)
64{
65 if (buf->l == 0) {
66 /* let's realign the buffer to optimize I/O */
67 buf->r = buf->w = buf->h = buf->lr = buf->data;
68 }
69 return buffer_max(buf);
70}
71
72
73int buffer_write(struct buffer *buf, const char *msg, int len);
74int buffer_replace(struct buffer *b, char *pos, char *end, char *str);
75int buffer_replace2(struct buffer *b, char *pos, char *end, char *str, int len);
76
77
78#endif /* _PROTO_BUFFERS_H */
79
80/*
81 * Local variables:
82 * c-indent-level: 8
83 * c-basic-offset: 8
84 * End:
85 */