2006-06-26 00:48:02 +00:00
|
|
|
/*
|
|
|
|
include/proto/buffers.h
|
|
|
|
Buffer management definitions, macros and inline functions.
|
|
|
|
|
2007-03-18 15:31:29 +00:00
|
|
|
Copyright (C) 2000-2007 Willy Tarreau - w@1wt.eu
|
2006-06-26 00:48:02 +00:00
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation, version 2.1
|
|
|
|
exclusively.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _PROTO_BUFFERS_H
|
|
|
|
#define _PROTO_BUFFERS_H
|
|
|
|
|
2007-05-13 17:56:02 +00:00
|
|
|
#include <stdio.h>
|
2006-12-23 19:51:41 +00:00
|
|
|
#include <stdlib.h>
|
2007-05-13 17:56:02 +00:00
|
|
|
#include <string.h>
|
2006-12-23 19:51:41 +00:00
|
|
|
|
2006-06-29 16:54:54 +00:00
|
|
|
#include <common/config.h>
|
2007-05-13 17:56:02 +00:00
|
|
|
#include <common/memory.h>
|
2008-07-06 22:09:58 +00:00
|
|
|
#include <common/ticks.h>
|
2007-06-03 13:59:52 +00:00
|
|
|
#include <common/time.h>
|
|
|
|
|
2006-06-26 00:48:02 +00:00
|
|
|
#include <types/buffers.h>
|
|
|
|
|
2007-05-13 17:56:02 +00:00
|
|
|
extern struct pool_head *pool2_buffer;
|
|
|
|
|
|
|
|
/* perform minimal intializations, report 0 in case of error, 1 if OK. */
|
|
|
|
int init_buffer();
|
|
|
|
|
2006-07-29 14:59:06 +00:00
|
|
|
/* Initializes all fields in the buffer. The ->rlim field is initialized last
|
|
|
|
* so that the compiler can optimize it away if changed immediately after the
|
|
|
|
* call to this function.
|
|
|
|
*/
|
|
|
|
static inline void buffer_init(struct buffer *buf)
|
|
|
|
{
|
|
|
|
buf->l = buf->total = buf->flags = 0;
|
2007-03-18 15:31:29 +00:00
|
|
|
buf->rlim = buf->r = buf->lr = buf->w = buf->data;
|
2006-07-29 14:59:06 +00:00
|
|
|
}
|
|
|
|
|
2006-06-26 00:48:02 +00:00
|
|
|
/* returns 1 if the buffer is empty, 0 otherwise */
|
2006-10-15 13:17:57 +00:00
|
|
|
static inline int buffer_isempty(const struct buffer *buf)
|
2006-06-26 00:48:02 +00:00
|
|
|
{
|
|
|
|
return buf->l == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* returns 1 if the buffer is full, 0 otherwise */
|
2006-10-15 13:17:57 +00:00
|
|
|
static inline int buffer_isfull(const struct buffer *buf) {
|
2006-06-26 00:48:02 +00:00
|
|
|
return buf->l == BUFSIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* flushes any content from buffer <buf> */
|
|
|
|
static inline void buffer_flush(struct buffer *buf)
|
|
|
|
{
|
2007-03-18 15:31:29 +00:00
|
|
|
buf->r = buf->lr = buf->w = buf->data;
|
2006-06-26 00:48:02 +00:00
|
|
|
buf->l = 0;
|
|
|
|
}
|
|
|
|
|
2007-06-03 13:59:52 +00:00
|
|
|
/* marks the buffer as "shutdown pending" for reads and cancels the timeout */
|
|
|
|
static inline void buffer_shutr(struct buffer *buf)
|
|
|
|
{
|
2008-07-06 22:09:58 +00:00
|
|
|
buf->rex = TICK_ETERNITY;
|
2007-06-03 13:59:52 +00:00
|
|
|
buf->flags |= BF_SHUTR_PENDING;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* marks the buffer as "shutdown pending" for writes and cancels the timeout */
|
|
|
|
static inline void buffer_shutw(struct buffer *buf)
|
|
|
|
{
|
2008-07-06 22:09:58 +00:00
|
|
|
buf->wex = TICK_ETERNITY;
|
2007-06-03 13:59:52 +00:00
|
|
|
buf->flags |= BF_SHUTW_PENDING;
|
|
|
|
}
|
|
|
|
|
2006-06-26 00:48:02 +00:00
|
|
|
|
|
|
|
/* returns the maximum number of bytes writable at once in this buffer */
|
2006-10-15 13:17:57 +00:00
|
|
|
static inline int buffer_max(const struct buffer *buf)
|
2006-06-26 00:48:02 +00:00
|
|
|
{
|
|
|
|
if (buf->l == BUFSIZE)
|
|
|
|
return 0;
|
|
|
|
else if (buf->r >= buf->w)
|
|
|
|
return buf->data + BUFSIZE - buf->r;
|
|
|
|
else
|
|
|
|
return buf->w - buf->r;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tries to realign the given buffer, and returns how many bytes can be written
|
|
|
|
* there at once without overwriting anything.
|
|
|
|
*/
|
|
|
|
static inline int buffer_realign(struct buffer *buf)
|
|
|
|
{
|
|
|
|
if (buf->l == 0) {
|
|
|
|
/* let's realign the buffer to optimize I/O */
|
2007-03-18 15:31:29 +00:00
|
|
|
buf->r = buf->w = buf->lr = buf->data;
|
2006-06-26 00:48:02 +00:00
|
|
|
}
|
|
|
|
return buffer_max(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int buffer_write(struct buffer *buf, const char *msg, int len);
|
2007-01-01 20:38:07 +00:00
|
|
|
int buffer_write_chunk(struct buffer *buf, struct chunk *chunk);
|
2007-03-18 21:36:26 +00:00
|
|
|
int buffer_replace(struct buffer *b, char *pos, char *end, const char *str);
|
|
|
|
int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len);
|
|
|
|
int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len);
|
2007-01-01 20:38:07 +00:00
|
|
|
int chunk_printf(struct chunk *chk, int size, const char *fmt, ...);
|
2007-01-21 18:16:41 +00:00
|
|
|
void buffer_dump(FILE *o, struct buffer *b, int from, int to);
|
2006-06-26 00:48:02 +00:00
|
|
|
|
2006-12-23 19:51:41 +00:00
|
|
|
/*
|
|
|
|
* frees the destination chunk if already allocated, allocates a new string,
|
|
|
|
* and copies the source into it. The pointer to the destination string is
|
|
|
|
* returned, or NULL if the allocation fails or if any pointer is NULL..
|
|
|
|
*/
|
|
|
|
static inline char *chunk_dup(struct chunk *dst, const struct chunk *src) {
|
|
|
|
if (!dst || !src || !src->str)
|
|
|
|
return NULL;
|
|
|
|
if (dst->str)
|
|
|
|
free(dst->str);
|
|
|
|
dst->len = src->len;
|
|
|
|
dst->str = (char *)malloc(dst->len);
|
|
|
|
memcpy(dst->str, src->str, dst->len);
|
|
|
|
return dst->str;
|
|
|
|
}
|
2006-06-26 00:48:02 +00:00
|
|
|
|
|
|
|
#endif /* _PROTO_BUFFERS_H */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* c-indent-level: 8
|
|
|
|
* c-basic-offset: 8
|
|
|
|
* End:
|
|
|
|
*/
|