dovecot-2.2: lib: Added buffer_get_writable_size()

dovecot at dovecot.org dovecot at dovecot.org
Sat May 16 15:49:38 UTC 2015


details:   http://hg.dovecot.org/dovecot-2.2/rev/3deb3fd654c6
changeset: 18719:3deb3fd654c6
user:      Timo Sirainen <tss at iki.fi>
date:      Sat May 16 18:41:44 2015 +0300
description:
lib: Added buffer_get_writable_size()

diffstat:

 src/lib/buffer.c |  14 ++++++++++++++
 src/lib/buffer.h |   4 ++++
 2 files changed, 18 insertions(+), 0 deletions(-)

diffs (38 lines):

diff -r a7175a018348 -r 3deb3fd654c6 src/lib/buffer.c
--- a/src/lib/buffer.c	Sat May 16 18:20:08 2015 +0300
+++ b/src/lib/buffer.c	Sat May 16 18:41:44 2015 +0300
@@ -322,6 +322,20 @@
 	return buf->alloc;
 }
 
+size_t buffer_get_writable_size(const buffer_t *_buf)
+{
+	const struct real_buffer *buf = (const struct real_buffer *)_buf;
+
+	if (!buf->dynamic || buf->alloc == 0)
+		return buf->alloc;
+
+	/* we reserve +1 for str_c() NUL in buffer_check_limits(), so don't
+	   include that in our return value. otherwise the caller might
+	   increase the buffer's alloc size unnecessarily when it just wants
+	   to access the entire buffer. */
+	return buf->alloc-1;
+}
+
 bool buffer_cmp(const buffer_t *buf1, const buffer_t *buf2)
 {
 	if (buf1->used != buf2->used)
diff -r a7175a018348 -r 3deb3fd654c6 src/lib/buffer.h
--- a/src/lib/buffer.h	Sat May 16 18:20:08 2015 +0300
+++ b/src/lib/buffer.h	Sat May 16 18:41:44 2015 +0300
@@ -91,6 +91,10 @@
 
 /* Returns the current buffer size. */
 size_t buffer_get_size(const buffer_t *buf) ATTR_PURE;
+/* 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. */
+size_t buffer_get_writable_size(const buffer_t *buf) ATTR_PURE;
 
 /* Returns TRUE if buffer contents are identical. */
 bool buffer_cmp(const buffer_t *buf1, const buffer_t *buf2);


More information about the dovecot-cvs mailing list