~ubuntu-branches/ubuntu/quantal/haproxy/quantal

« back to all changes in this revision

Viewing changes to include/proto/buffers.h

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Cornet
  • Date: 2007-08-17 09:33:41 UTC
  • Revision ID: james.westby@ubuntu.com-20070817093341-h0t6aeeoyzo25z3r
Tags: upstream-1.3.12.dfsg
ImportĀ upstreamĀ versionĀ 1.3.12.dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  include/proto/buffers.h
 
3
  Buffer management definitions, macros and inline functions.
 
4
 
 
5
  Copyright (C) 2000-2007 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
 
 
25
#include <stdio.h>
 
26
#include <stdlib.h>
 
27
#include <string.h>
 
28
 
 
29
#include <common/config.h>
 
30
#include <common/memory.h>
 
31
#include <common/time.h>
 
32
 
 
33
#include <types/buffers.h>
 
34
 
 
35
extern struct pool_head *pool2_buffer;
 
36
 
 
37
/* perform minimal intializations, report 0 in case of error, 1 if OK. */
 
38
int init_buffer();
 
39
 
 
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.
 
43
 */
 
44
static inline void buffer_init(struct buffer *buf)
 
45
{
 
46
        buf->l = buf->total = buf->flags = 0;
 
47
        buf->rlim = buf->r = buf->lr = buf->w = buf->data;
 
48
}
 
49
 
 
50
/* returns 1 if the buffer is empty, 0 otherwise */
 
51
static inline int buffer_isempty(const struct buffer *buf)
 
52
{
 
53
        return buf->l == 0;
 
54
}
 
55
 
 
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;
 
59
}
 
60
 
 
61
/* flushes any content from buffer <buf> */
 
62
static inline void buffer_flush(struct buffer *buf)
 
63
{
 
64
        buf->r = buf->lr = buf->w = buf->data;
 
65
        buf->l = 0;
 
66
}
 
67
 
 
68
/* marks the buffer as "shutdown pending" for reads and cancels the timeout */
 
69
static inline void buffer_shutr(struct buffer *buf)
 
70
{
 
71
        tv_eternity(&buf->rex);
 
72
        buf->flags |= BF_SHUTR_PENDING;
 
73
}
 
74
 
 
75
/* marks the buffer as "shutdown pending" for writes and cancels the timeout */
 
76
static inline void buffer_shutw(struct buffer *buf)
 
77
{
 
78
        tv_eternity(&buf->wex);
 
79
        buf->flags |= BF_SHUTW_PENDING;
 
80
}
 
81
 
 
82
 
 
83
/* returns the maximum number of bytes writable at once in this buffer */
 
84
static inline int buffer_max(const struct buffer *buf)
 
85
{
 
86
        if (buf->l == BUFSIZE)
 
87
                return 0;
 
88
        else if (buf->r >= buf->w)
 
89
                return buf->data + BUFSIZE - buf->r;
 
90
        else
 
91
                return buf->w - buf->r;
 
92
}
 
93
 
 
94
 
 
95
/*
 
96
 * Tries to realign the given buffer, and returns how many bytes can be written
 
97
 * there at once without overwriting anything.
 
98
 */
 
99
static inline int buffer_realign(struct buffer *buf)
 
100
{
 
101
        if (buf->l == 0) {
 
102
                /* let's realign the buffer to optimize I/O */
 
103
                buf->r = buf->w = buf->lr = buf->data;
 
104
        }
 
105
        return buffer_max(buf);
 
106
}
 
107
 
 
108
 
 
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);
 
116
 
 
117
/*
 
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..
 
121
 */
 
122
static inline char *chunk_dup(struct chunk *dst, const struct chunk *src) {
 
123
        if (!dst || !src || !src->str)
 
124
                return NULL;
 
125
        if (dst->str)
 
126
                free(dst->str);
 
127
        dst->len = src->len;
 
128
        dst->str = (char *)malloc(dst->len);
 
129
        memcpy(dst->str, src->str, dst->len);
 
130
        return dst->str;
 
131
}
 
132
 
 
133
#endif /* _PROTO_BUFFERS_H */
 
134
 
 
135
/*
 
136
 * Local variables:
 
137
 *  c-indent-level: 8
 
138
 *  c-basic-offset: 8
 
139
 * End:
 
140
 */