#ifndef BUFFER_H
#define BUFFER_H
struct buffer {
const void *data;
};
/* WARNING: Be careful with functions that return pointers to data.
With dynamic buffers they are valid only as long as buffer is not
realloc()ed. You shouldn't rely on it being valid if you have modified
buffer in any way. */
/* Create a modifiable buffer from given data. Writes past this size will
i_panic(). */
/* Create a non-modifiable buffer from given data. */
#define buffer_create_from_data(b,d,s) ({ \
buffer_create_from_data((b), (d), (s)); })
#define buffer_create_from_const_data(b,d,s) ({ \
buffer_create_from_const_data((b), (d), (s)); })
#endif
/* Creates a dynamically growing buffer. Whenever write would exceed the
current size it's grown. */
/* Free the memory used by buffer. Not needed if the memory is free'd
directly from the memory pool. */
/* Free the memory used by buffer structure, but return the buffer data
unfree'd. */
/* Returns the pool buffer was created with. */
/* Write data to buffer at specified position. If pos is beyond the buffer's
current size, it is zero-filled up to that point (even if data_size==0). */
/* Append data to buffer. */
/* Append character to buffer. */
/* Insert data to buffer. */
/* Delete data from buffer. */
/* Fill buffer with zero bytes. */
/* Copy data from buffer to another. The buffers may be same in which case
it's internal copying, possibly with overlapping positions (ie. memmove()
like functionality). copy_size may be set to (size_t)-1 to copy the rest of
the used data in buffer. */
/* Append data to buffer from another. copy_size may be set to (size_t)-1 to
copy the rest of the used data in buffer. */
/* Returns pointer to specified position in buffer. WARNING: The returned
address may become invalid if you add more data to buffer. */
/* Increase the buffer usage by given size, and return a pointer to beginning
of it. */
/* Like buffer_get_data(), but don't return it as const. Returns NULL if the
buffer is non-modifiable. WARNING: The returned address may become invalid
if you add more data to buffer. */
ATTR_NULL(2);
/* Set the "used size" of buffer, ie. 0 would set the buffer empty.
Must not be used to grow buffer. The data after the buffer's new size will
be effectively lost, because e.g. buffer_get_space_unsafe() will zero out
the contents. */
/* Returns the current buffer size. */
/* Returns how many bytes we can write to buffer without increasing its size.
With dynamic buffers this is buffer_get_size()-1, because the extra 1 byte
is reserved for str_c()'s NUL. */
/* Returns TRUE if buffer contents are identical. */
/* Returns pointer to beginning of buffer data. Current used size of buffer is
stored in used_size if it's non-NULL. */
static inline const void * ATTR_NULL(2)
{
if (used_size_r != NULL)
}
/* Returns the current used buffer size. */
{
}
/* Crash if buffer was allocated from data stack and stack frame has changed.
This can be used as an assert-like check to verify that it's valid to
increase the buffer size here, instead of crashing only randomly when the
buffer needs to be increased. */
/* This will truncate your byte buffer to contain at most
given number of bits.
1 bits: 01 00000001
2 bits: 03 00000011
3 bits: 07 00000111
4 bits: 0f 00001111
5 bits: 1f 00011111
6 bits: 3f 00111111
7 bits: 7f 01111111
8 bits: ff 11111111
9 bits: 01ff 0000000111111111
10 bits: 03ff 0000001111111111
11 bits: 07ff 0000011111111111
12 bits: 0fff 0000111111111111
13 bits: 1fff 0001111111111111
14 bits: 3fff 0011111111111111
15 bits: 7fff 0111111111111111
16 bits: ffff 1111111111111111
and so forth
*/
#endif