Skip to content

Commit

Permalink
lib: Add i_stream_get_root_io() and use it to deduplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
sirainen committed Feb 15, 2018
1 parent 0a435cb commit e504fd5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/lib/istream-private.h
Expand Up @@ -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);

Expand Down
24 changes: 12 additions & 12 deletions src/lib/istream.c
Expand Up @@ -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);
}
Expand All @@ -706,21 +712,15 @@ 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;
}

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;
Expand Down

0 comments on commit e504fd5

Please sign in to comment.