From e504fd5d27eb2a8cd5de5ccd4f54173fc0b8064d Mon Sep 17 00:00:00 2001 From: Timo Sirainen Date: Thu, 8 Feb 2018 02:22:18 +0200 Subject: [PATCH] lib: Add i_stream_get_root_io() and use it to deduplicate code --- src/lib/istream-private.h | 1 + src/lib/istream.c | 24 ++++++++++++------------ 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/lib/istream-private.h b/src/lib/istream-private.h index c6b69d5754..ff6003a987 100644 --- a/src/lib/istream-private.h +++ b/src/lib/istream-private.h @@ -72,6 +72,7 @@ ssize_t i_stream_read_copy_from_parent(struct istream *istream); void i_stream_default_seek_nonseekable(struct istream_private *stream, uoff_t v_offset, bool mark); +struct istream *i_stream_get_root_io(struct istream *stream); void i_stream_set_io(struct istream *stream, struct io *io); void i_stream_unset_io(struct istream *stream, struct io *io); diff --git a/src/lib/istream.c b/src/lib/istream.c index ab957a3000..3937118e17 100644 --- a/src/lib/istream.c +++ b/src/lib/istream.c @@ -682,15 +682,21 @@ bool i_stream_add_data(struct istream *_stream, const unsigned char *data, return TRUE; } -void i_stream_set_input_pending(struct istream *stream, bool pending) +struct istream *i_stream_get_root_io(struct istream *stream) { - if (!pending) - return; - while (stream->real_stream->parent != NULL) { i_assert(stream->real_stream->io == NULL); stream = stream->real_stream->parent; } + return stream; +} + +void i_stream_set_input_pending(struct istream *stream, bool pending) +{ + if (!pending) + return; + + stream = i_stream_get_root_io(stream); if (stream->real_stream->io != NULL) io_set_pending(stream->real_stream->io); } @@ -706,10 +712,7 @@ void i_stream_switch_ioloop(struct istream *stream) void i_stream_set_io(struct istream *stream, struct io *io) { - while (stream->real_stream->parent != NULL) { - i_assert(stream->real_stream->io == NULL); - stream = stream->real_stream->parent; - } + stream = i_stream_get_root_io(stream); i_assert(stream->real_stream->io == NULL); stream->real_stream->io = io; @@ -717,10 +720,7 @@ void i_stream_set_io(struct istream *stream, struct io *io) void i_stream_unset_io(struct istream *stream, struct io *io) { - while (stream->real_stream->parent != NULL) { - i_assert(stream->real_stream->io == NULL); - stream = stream->real_stream->parent; - } + stream = i_stream_get_root_io(stream); i_assert(stream->real_stream->io == io); stream->real_stream->io = NULL;