changeset 19751:1e0cfc3dc89a

lib: Added i_stream_set_persistent_buffers()
author Timo Sirainen <timo.sirainen@dovecot.fi>
date Wed, 10 Feb 2016 22:02:15 +0200
parents ac29347cf81e
children d9a6a40ecf15
files src/lib/istream-private.h src/lib/istream.c src/lib/istream.h
diffstat 3 files changed, 19 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib/istream-private.h	Wed Feb 10 19:30:42 2016 +0200
+++ b/src/lib/istream-private.h	Wed Feb 10 22:02:15 2016 +0200
@@ -52,6 +52,7 @@
 	unsigned int line_crlf:1;
 	unsigned int return_nolf_line:1;
 	unsigned int stream_size_passthrough:1; /* stream is parent's size */
+	unsigned int nonpersistent_buffers:1;
 };
 
 struct istream * ATTR_NOWARN_UNUSED_RESULT
--- a/src/lib/istream.c	Wed Feb 10 19:30:42 2016 +0200
+++ b/src/lib/istream.c	Wed Feb 10 22:02:15 2016 +0200
@@ -122,6 +122,14 @@
 	stream->real_stream->return_nolf_line = set;
 }
 
+void i_stream_set_persistent_buffers(struct istream *stream, bool set)
+{
+	do {
+		stream->real_stream->nonpersistent_buffers = !set;
+		stream = stream->real_stream->parent;
+	} while (stream != NULL);
+}
+
 static void i_stream_update(struct istream_private *stream)
 {
 	if (stream->parent == NULL)
@@ -238,6 +246,12 @@
 		/* within buffer */
 		stream->v_offset += count;
 		_stream->skip += count;
+		if (_stream->nonpersistent_buffers &&
+		    _stream->skip == _stream->pos) {
+			_stream->skip = _stream->pos = 0;
+			_stream->buffer_size = 0;
+			i_free_and_null(_stream->w_buffer);
+		}
 		return;
 	}
 
--- a/src/lib/istream.h	Wed Feb 10 19:30:42 2016 +0200
+++ b/src/lib/istream.h	Wed Feb 10 22:02:15 2016 +0200
@@ -97,6 +97,10 @@
 /* Enable/disable i_stream[_read]_next_line() returning the last line if it
    doesn't end with LF. */
 void i_stream_set_return_partial_line(struct istream *stream, bool set);
+/* Change whether buffers are allocated persistently (default=TRUE). When not,
+   the memory usage is minimized by freeing the stream's buffers whenever they
+   become empty. */
+void i_stream_set_persistent_buffers(struct istream *stream, bool set);
 
 /* Returns number of bytes read if read was ok, -1 if EOF or error, -2 if the
    input buffer is full. */