2
include/proto/buffers.h
3
Buffer management definitions, macros and inline functions.
5
Copyright (C) 2000-2007 Willy Tarreau - w@1wt.eu
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
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.
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
22
#ifndef _PROTO_BUFFERS_H
23
#define _PROTO_BUFFERS_H
29
#include <common/config.h>
30
#include <common/memory.h>
31
#include <common/time.h>
33
#include <types/buffers.h>
35
extern struct pool_head *pool2_buffer;
37
/* perform minimal intializations, report 0 in case of error, 1 if OK. */
40
/* Initializes all fields in the buffer. The ->rlim field is initialized last
41
* so that the compiler can optimize it away if changed immediately after the
42
* call to this function.
44
static inline void buffer_init(struct buffer *buf)
46
buf->l = buf->total = buf->flags = 0;
47
buf->rlim = buf->r = buf->lr = buf->w = buf->data;
50
/* returns 1 if the buffer is empty, 0 otherwise */
51
static inline int buffer_isempty(const struct buffer *buf)
56
/* returns 1 if the buffer is full, 0 otherwise */
57
static inline int buffer_isfull(const struct buffer *buf) {
58
return buf->l == BUFSIZE;
61
/* flushes any content from buffer <buf> */
62
static inline void buffer_flush(struct buffer *buf)
64
buf->r = buf->lr = buf->w = buf->data;
68
/* marks the buffer as "shutdown pending" for reads and cancels the timeout */
69
static inline void buffer_shutr(struct buffer *buf)
71
tv_eternity(&buf->rex);
72
buf->flags |= BF_SHUTR_PENDING;
75
/* marks the buffer as "shutdown pending" for writes and cancels the timeout */
76
static inline void buffer_shutw(struct buffer *buf)
78
tv_eternity(&buf->wex);
79
buf->flags |= BF_SHUTW_PENDING;
83
/* returns the maximum number of bytes writable at once in this buffer */
84
static inline int buffer_max(const struct buffer *buf)
86
if (buf->l == BUFSIZE)
88
else if (buf->r >= buf->w)
89
return buf->data + BUFSIZE - buf->r;
91
return buf->w - buf->r;
96
* Tries to realign the given buffer, and returns how many bytes can be written
97
* there at once without overwriting anything.
99
static inline int buffer_realign(struct buffer *buf)
102
/* let's realign the buffer to optimize I/O */
103
buf->r = buf->w = buf->lr = buf->data;
105
return buffer_max(buf);
109
int buffer_write(struct buffer *buf, const char *msg, int len);
110
int buffer_write_chunk(struct buffer *buf, struct chunk *chunk);
111
int buffer_replace(struct buffer *b, char *pos, char *end, const char *str);
112
int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len);
113
int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len);
114
int chunk_printf(struct chunk *chk, int size, const char *fmt, ...);
115
void buffer_dump(FILE *o, struct buffer *b, int from, int to);
118
* frees the destination chunk if already allocated, allocates a new string,
119
* and copies the source into it. The pointer to the destination string is
120
* returned, or NULL if the allocation fails or if any pointer is NULL..
122
static inline char *chunk_dup(struct chunk *dst, const struct chunk *src) {
123
if (!dst || !src || !src->str)
128
dst->str = (char *)malloc(dst->len);
129
memcpy(dst->str, src->str, dst->len);
133
#endif /* _PROTO_BUFFERS_H */