chiark / gitweb /
remove todo
[inn-innduct.git] / include / inn / buffer.h
1 /*  $Id: buffer.h 6295 2003-04-16 05:46:38Z rra $
2 **
3 **  Counted, reusable memory buffer.
4 **
5 **  A buffer is an allocated bit of memory with a known size and a separate
6 **  data length.  It's intended to store strings and can be reused repeatedly
7 **  to minimize the number of memory allocations.  Buffers increase in
8 **  increments of 1K.
9 **
10 **  A buffer contains a notion of the data that's been used and the data
11 **  that's been left, used when the buffer is an I/O buffer where lots of data
12 **  is buffered and then slowly processed out of the buffer.  The total length
13 **  of the data is used + left.  If a buffer is just used to store some data,
14 **  used can be set to 0 and left stores the length of the data.
15 */
16
17 #ifndef INN_BUFFER_H
18 #define INN_BUFFER_H 1
19
20 #include <inn/defines.h>
21
22 struct buffer {
23     size_t size;                /* Total allocated length. */
24     size_t used;                /* Data already used. */
25     size_t left;                /* Remaining unused data. */
26     char *data;                 /* Pointer to allocated memory. */
27 };
28
29 BEGIN_DECLS
30
31 /* Allocate a new buffer and initialize its contents. */
32 struct buffer *buffer_new(void);
33
34 /* Resize a buffer to be at least as large as the provided size. */
35 void buffer_resize(struct buffer *, size_t);
36
37 /* Set the buffer contents, ignoring anything currently there. */
38 void buffer_set(struct buffer *, const char *data, size_t length);
39
40 /* Append data to the buffer. */
41 void buffer_append(struct buffer *, const char *data, size_t length);
42
43 /* Swap the contents of two buffers. */
44 void buffer_swap(struct buffer *, struct buffer *);
45
46 END_DECLS
47
48 #endif /* INN_BUFFER_H */