From: Ben Pfaff Date: Mon, 14 Jul 2008 20:51:46 +0000 (-0700) Subject: buffer: New functions buffer_reserve(), buffer_push(), X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0ad4720dc65bb5080881b0b8fd483c5688943b14;p=openvswitch buffer: New functions buffer_reserve(), buffer_push(), buffer_try_pull(). --- diff --git a/include/buffer.h b/include/buffer.h index 16183f6e..2fe05c33 100644 --- a/include/buffer.h +++ b/include/buffer.h @@ -69,7 +69,9 @@ void *buffer_end(const struct buffer *); void *buffer_put_uninit(struct buffer *, size_t); void *buffer_put(struct buffer *, const void *, size_t); +void buffer_reserve(struct buffer *, size_t); void *buffer_push_uninit(struct buffer *b, size_t); +void *buffer_push(struct buffer *b, const void *, size_t); size_t buffer_headroom(struct buffer *); size_t buffer_tailroom(struct buffer *); @@ -78,5 +80,6 @@ void buffer_prealloc_tailroom(struct buffer *, size_t); void buffer_clear(struct buffer *); void *buffer_pull(struct buffer *, size_t); +void *buffer_try_pull(struct buffer *, size_t); #endif /* buffer.h */ diff --git a/lib/buffer.c b/lib/buffer.c index 8604913f..637828d4 100644 --- a/lib/buffer.c +++ b/lib/buffer.c @@ -181,6 +181,16 @@ buffer_put(struct buffer *b, const void *p, size_t size) return dst; } +/* Reserves 'size' bytes of headroom so that they can be later allocated with + * buffer_push_uninit() without reallocating the buffer. */ +void +buffer_reserve(struct buffer *b, size_t size) +{ + assert(!b->size); + buffer_prealloc_tailroom(b, size); + b->data += size; +} + void * buffer_push_uninit(struct buffer *b, size_t size) { @@ -190,6 +200,14 @@ buffer_push_uninit(struct buffer *b, size_t size) return b->data; } +void * +buffer_push(struct buffer *b, const void *p, size_t size) +{ + void *dst = buffer_push_uninit(b, size); + memcpy(dst, p, size); + return dst; +} + /* If 'b' contains at least 'offset + size' bytes of data, returns a pointer to * byte 'offset'. Otherwise, returns a null pointers. */ void * @@ -242,3 +260,11 @@ buffer_pull(struct buffer *b, size_t size) return data; } +/* If 'b' has at least 'size' bytes of data, removes that many bytes from the + * head end of 'b' and returns the first byte removed. Otherwise, returns a + * null pointer without modifying 'b'. */ +void * +buffer_try_pull(struct buffer *b, size_t size) +{ + return b->size >= size ? buffer_pull(b, size) : NULL; +}