blob: 74efe8fdf63c5901c55525f1bd2de9782f8e4ef2 [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 Tarreaue09e0ce2007-03-18 16:31:29 +01005 Copyright (C) 2000-2007 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 Tarreaubaaee002006-06-26 02:48:02 +020031#include <types/buffers.h>
32
Willy Tarreau7341d942007-05-13 19:56:02 +020033extern struct pool_head *pool2_buffer;
34
35/* perform minimal intializations, report 0 in case of error, 1 if OK. */
36int init_buffer();
37
Willy Tarreau54469402006-07-29 16:59:06 +020038/* Initializes all fields in the buffer. The ->rlim field is initialized last
39 * so that the compiler can optimize it away if changed immediately after the
40 * call to this function.
41 */
42static inline void buffer_init(struct buffer *buf)
43{
44 buf->l = buf->total = buf->flags = 0;
Willy Tarreaue09e0ce2007-03-18 16:31:29 +010045 buf->rlim = buf->r = buf->lr = buf->w = buf->data;
Willy Tarreau54469402006-07-29 16:59:06 +020046}
47
Willy Tarreaubaaee002006-06-26 02:48:02 +020048/* returns 1 if the buffer is empty, 0 otherwise */
Willy Tarreaub17916e2006-10-15 15:17:57 +020049static inline int buffer_isempty(const struct buffer *buf)
Willy Tarreaubaaee002006-06-26 02:48:02 +020050{
51 return buf->l == 0;
52}
53
54/* returns 1 if the buffer is full, 0 otherwise */
Willy Tarreaub17916e2006-10-15 15:17:57 +020055static inline int buffer_isfull(const struct buffer *buf) {
Willy Tarreaubaaee002006-06-26 02:48:02 +020056 return buf->l == BUFSIZE;
57}
58
59/* flushes any content from buffer <buf> */
60static inline void buffer_flush(struct buffer *buf)
61{
Willy Tarreaue09e0ce2007-03-18 16:31:29 +010062 buf->r = buf->lr = buf->w = buf->data;
Willy Tarreaubaaee002006-06-26 02:48:02 +020063 buf->l = 0;
64}
65
66
67/* returns the maximum number of bytes writable at once in this buffer */
Willy Tarreaub17916e2006-10-15 15:17:57 +020068static inline int buffer_max(const struct buffer *buf)
Willy Tarreaubaaee002006-06-26 02:48:02 +020069{
70 if (buf->l == BUFSIZE)
71 return 0;
72 else if (buf->r >= buf->w)
73 return buf->data + BUFSIZE - buf->r;
74 else
75 return buf->w - buf->r;
76}
77
78
79/*
80 * Tries to realign the given buffer, and returns how many bytes can be written
81 * there at once without overwriting anything.
82 */
83static inline int buffer_realign(struct buffer *buf)
84{
85 if (buf->l == 0) {
86 /* let's realign the buffer to optimize I/O */
Willy Tarreaue09e0ce2007-03-18 16:31:29 +010087 buf->r = buf->w = buf->lr = buf->data;
Willy Tarreaubaaee002006-06-26 02:48:02 +020088 }
89 return buffer_max(buf);
90}
91
92
93int buffer_write(struct buffer *buf, const char *msg, int len);
Willy Tarreauc0dde7a2007-01-01 21:38:07 +010094int buffer_write_chunk(struct buffer *buf, struct chunk *chunk);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +010095int buffer_replace(struct buffer *b, char *pos, char *end, const char *str);
96int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len);
97int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len);
Willy Tarreauc0dde7a2007-01-01 21:38:07 +010098int chunk_printf(struct chunk *chk, int size, const char *fmt, ...);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +010099void buffer_dump(FILE *o, struct buffer *b, int from, int to);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200100
Willy Tarreau0f772532006-12-23 20:51:41 +0100101/*
102 * frees the destination chunk if already allocated, allocates a new string,
103 * and copies the source into it. The pointer to the destination string is
104 * returned, or NULL if the allocation fails or if any pointer is NULL..
105 */
106static inline char *chunk_dup(struct chunk *dst, const struct chunk *src) {
107 if (!dst || !src || !src->str)
108 return NULL;
109 if (dst->str)
110 free(dst->str);
111 dst->len = src->len;
112 dst->str = (char *)malloc(dst->len);
113 memcpy(dst->str, src->str, dst->len);
114 return dst->str;
115}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200116
117#endif /* _PROTO_BUFFERS_H */
118
119/*
120 * Local variables:
121 * c-indent-level: 8
122 * c-basic-offset: 8
123 * End:
124 */