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 authored and villesavolainen committed Feb 14, 2018
1 parent 8bcd5e5 commit abf71b6
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 @@ -105,6 +105,7 @@ i_stream_default_snapshot(struct istream_private *stream,
struct istream_snapshot *prev_snapshot);
void i_stream_snapshot_free(struct istream_snapshot **snapshot);

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 @@ -907,15 +907,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 @@ -931,21 +937,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 abf71b6

Please sign in to comment.