1 /* $Id: buffer.c 5463 2002-05-06 05:40:46Z rra $
3 ** Counted, reusable memory buffer.
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
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.
20 #include "inn/buffer.h"
24 ** Allocate a new struct buffer and initialize it.
29 struct buffer *buffer;
31 buffer = xmalloc(sizeof(struct buffer));
41 ** Resize a buffer to be at least as large as the provided second argument.
42 ** Resize buffers to multiples of 1KB to keep the number of reallocations to
43 ** a minimum. Refuse to resize a buffer to make it smaller.
46 buffer_resize(struct buffer *buffer, size_t size)
48 if (size < buffer->size)
50 buffer->size = (size + 1023) & ~1023UL;
51 buffer->data = xrealloc(buffer->data, buffer->size);
56 ** Replace whatever data is currently in the buffer with the provided data.
59 buffer_set(struct buffer *buffer, const char *data, size_t length)
62 buffer_resize(buffer, length);
63 memmove(buffer->data, data, length);
65 buffer->left = length;
71 ** Append data to a buffer. The new data shows up as additional unused data
72 ** at the end of the buffer. Resize the buffer to multiples of 1KB.
75 buffer_append(struct buffer *buffer, const char *data, size_t length)
81 total = buffer->used + buffer->left;
82 buffer_resize(buffer, total + length);
83 buffer->left += length;
84 memcpy(buffer->data + total, data, length);
89 ** Swap the contents of two buffers.
92 buffer_swap(struct buffer *one, struct buffer *two)