/* Ensures that 'b' has room for at least 'size' bytes at its tail end,
* reallocating and copying its data if necessary. */
void
-buffer_reserve_tailroom(struct buffer *b, size_t size)
+buffer_prealloc_tailroom(struct buffer *b, size_t size)
{
if (size > buffer_tailroom(b)) {
size_t new_allocated = b->allocated + MAX(size, 64);
}
void
-buffer_reserve_headroom(struct buffer *b, size_t size)
+buffer_prealloc_headroom(struct buffer *b, size_t size)
{
assert(size <= buffer_headroom(b));
}
buffer_put_uninit(struct buffer *b, size_t size)
{
void *p;
- buffer_reserve_tailroom(b, size);
+ buffer_prealloc_tailroom(b, size);
p = buffer_tail(b);
b->size += size;
return p;
}
/* Appends the 'size' bytes of data in 'p' to the tail end of 'b'. Data in 'b'
- * is reallocated and copied if necessary. */
-void
+ * is reallocated and copied if necessary. Returns a pointer to the first
+ * byte of the data's location in the buffer. */
+void *
buffer_put(struct buffer *b, const void *p, size_t size)
{
- memcpy(buffer_put_uninit(b, size), p, size);
+ void *dst = buffer_put_uninit(b, size);
+ memcpy(dst, p, size);
+ return dst;
}
void *
buffer_push_uninit(struct buffer *b, size_t size)
{
- buffer_reserve_headroom(b, size);
+ buffer_prealloc_headroom(b, size);
b->data -= size;
b->size += size;
return b->data;
}
/* Removes 'size' bytes from the head end of 'b', which must contain at least
- * 'size' bytes of data. */
-void
+ * 'size' bytes of data. Returns the first byte of data removed. */
+void *
buffer_pull(struct buffer *b, size_t size)
{
+ void *data = b->data;
assert(b->size >= size);
b->data += size;
b->size -= size;
+ return data;
}