From 463f6ea04af934a68facaca0ff089bc306de3f98 Mon Sep 17 00:00:00 2001 From: Timo Sirainen Date: Wed, 10 Feb 2016 22:02:15 +0200 Subject: [PATCH] lib: Added i_stream_set_persistent_buffers() --- src/lib/istream-private.h | 1 + src/lib/istream.c | 14 ++++++++++++++ src/lib/istream.h | 4 ++++ 3 files changed, 19 insertions(+) diff --git a/src/lib/istream-private.h b/src/lib/istream-private.h index 54a91d83af..c6b69d5754 100644 --- a/src/lib/istream-private.h +++ b/src/lib/istream-private.h @@ -52,6 +52,7 @@ struct istream_private { 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 diff --git a/src/lib/istream.c b/src/lib/istream.c index 4f2988936b..2d70a230db 100644 --- a/src/lib/istream.c +++ b/src/lib/istream.c @@ -122,6 +122,14 @@ void i_stream_set_return_partial_line(struct istream *stream, bool set) 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 @@ void i_stream_skip(struct istream *stream, uoff_t count) /* 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; } diff --git a/src/lib/istream.h b/src/lib/istream.h index d47d36bdb9..0c96e98967 100644 --- a/src/lib/istream.h +++ b/src/lib/istream.h @@ -97,6 +97,10 @@ size_t i_stream_get_max_buffer_size(struct istream *stream); /* 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. */ -- 2.47.3