]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: iostreams - Use container_of()
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Fri, 12 Mar 2021 12:53:38 +0000 (14:53 +0200)
committeraki.tuomi <aki.tuomi@open-xchange.com>
Fri, 19 Mar 2021 09:22:44 +0000 (09:22 +0000)
30 files changed:
src/lib/iostream-temp.c
src/lib/istream-base64-decoder.c
src/lib/istream-base64-encoder.c
src/lib/istream-callback.c
src/lib/istream-chain.c
src/lib/istream-concat.c
src/lib/istream-crlf.c
src/lib/istream-failure-at.c
src/lib/istream-file.c
src/lib/istream-hash.c
src/lib/istream-jsonstr.c
src/lib/istream-limit.c
src/lib/istream-multiplex.c
src/lib/istream-rawlog.c
src/lib/istream-seekable.c
src/lib/istream-sized.c
src/lib/istream-tee.c
src/lib/istream-timeout.c
src/lib/istream-try.c
src/lib/istream-unix.c
src/lib/istream.c
src/lib/ostream-buffer.c
src/lib/ostream-failure-at.c
src/lib/ostream-file.c
src/lib/ostream-hash.c
src/lib/ostream-multiplex.c
src/lib/ostream-rawlog.c
src/lib/ostream-unix.c
src/lib/ostream-wrapper.c
src/lib/ostream.c

index 8883f65ec0716d28ed693a77e0da1de009127ade..6e14ff5f76ebe51005082f0faefd3503989a2a74 100644 (file)
@@ -37,7 +37,8 @@ static void
 o_stream_temp_close(struct iostream_private *stream,
                    bool close_parent ATTR_UNUSED)
 {
-       struct temp_ostream *tstream = (struct temp_ostream *)stream;
+       struct temp_ostream *tstream =
+               container_of(stream, struct temp_ostream, ostream.iostream);
 
        i_close_fd(&tstream->fd);
        buffer_free(&tstream->buf);
@@ -80,7 +81,7 @@ static int o_stream_temp_move_to_fd(struct temp_ostream *tstream)
 int o_stream_temp_move_to_memory(struct ostream *output)
 {
        struct temp_ostream *tstream =
-               (struct temp_ostream *)output->real_stream;
+               container_of(output->real_stream, struct temp_ostream, ostream);
        unsigned char buf[IO_BLOCK_SIZE];
        uoff_t offset = 0;
        ssize_t ret = 0;
@@ -140,7 +141,8 @@ static ssize_t
 o_stream_temp_sendv(struct ostream_private *stream,
                    const struct const_iovec *iov, unsigned int iov_count)
 {
-       struct temp_ostream *tstream = (struct temp_ostream *)stream;
+       struct temp_ostream *tstream =
+               container_of(stream, struct temp_ostream, ostream);
        ssize_t ret = 0;
        unsigned int i;
        enum ostream_send_istream_result res;
@@ -250,7 +252,8 @@ static enum ostream_send_istream_result
 o_stream_temp_send_istream(struct ostream_private *_outstream,
                           struct istream *instream)
 {
-       struct temp_ostream *outstream = (struct temp_ostream *)_outstream;
+       struct temp_ostream *outstream =
+               container_of(_outstream, struct temp_ostream, ostream);
        enum ostream_send_istream_result res;
 
        if ((outstream->flags & IOSTREAM_TEMP_FLAG_TRY_FD_DUP) != 0) {
@@ -265,7 +268,8 @@ static int
 o_stream_temp_write_at(struct ostream_private *stream,
                       const void *data, size_t size, uoff_t offset)
 {
-       struct temp_ostream *tstream = (struct temp_ostream *)stream;
+       struct temp_ostream *tstream =
+               container_of(stream, struct temp_ostream, ostream);
 
        if (tstream->fd == -1) {
                i_assert(stream->ostream.offset == tstream->buf->used);
@@ -345,7 +349,8 @@ struct istream *iostream_temp_finish(struct ostream **output,
                                     size_t max_buffer_size)
 {
        struct temp_ostream *tstream =
-               (struct temp_ostream *)(*output)->real_stream;
+               container_of((*output)->real_stream, struct temp_ostream,
+                            ostream);
        struct istream *input, *input2;
        uoff_t abs_offset, size;
        const char *for_path;
index 0229ac07f9f148defe2f90e9a289f15a9890e468..6eaa84280c752fc2b1a037e95b863b2706686863 100644 (file)
@@ -80,7 +80,7 @@ i_stream_base64_finish_decode(struct base64_decoder_istream *bstream)
 static ssize_t i_stream_base64_decoder_read(struct istream_private *stream)
 {
        struct base64_decoder_istream *bstream =
-               (struct base64_decoder_istream *)stream;
+               container_of(stream, struct base64_decoder_istream, istream);
        size_t pre_count, post_count;
        int ret;
 
@@ -121,7 +121,7 @@ i_stream_base64_decoder_seek(struct istream_private *stream,
                             uoff_t v_offset, bool mark)
 {
        struct base64_decoder_istream *bstream =
-               (struct base64_decoder_istream *)stream;
+               container_of(stream, struct base64_decoder_istream, istream);
 
        if (v_offset < stream->istream.v_offset) {
                /* seeking backwards - go back to beginning and seek
index d285881e5666fa4e96b8566d1a3f2b47f4de9b37..22d2786f467ab1b92f0ab327d2e96dc0112fe9cd 100644 (file)
@@ -88,7 +88,7 @@ i_stream_base64_finish_encode(struct base64_encoder_istream *bstream)
 static ssize_t i_stream_base64_encoder_read(struct istream_private *stream)
 {
        struct base64_encoder_istream *bstream =
-               (struct base64_encoder_istream *)stream;
+               container_of(stream, struct base64_encoder_istream, istream);
        size_t pre_count, post_count;
        int ret;
 
@@ -143,7 +143,7 @@ i_stream_base64_encoder_seek(struct istream_private *stream,
                             uoff_t v_offset, bool mark)
 {
        struct base64_encoder_istream *bstream =
-               (struct base64_encoder_istream *)stream;
+               container_of(stream, struct base64_encoder_istream, istream);
 
        if (v_offset < stream->istream.v_offset) {
                /* seeking backwards - go back to beginning and seek
@@ -163,7 +163,7 @@ i_stream_base64_encoder_stat(struct istream_private *stream,
        bool exact ATTR_UNUSED)
 {
        struct base64_encoder_istream *bstream =
-               (struct base64_encoder_istream *)stream;
+               container_of(stream, struct base64_encoder_istream, istream);
        const struct stat *st;
 
        if (i_stream_stat(stream->parent, exact, &st) < 0) {
index 899643aac9a72c0b18eafe0669828aa2feadf9ca..6f07d5059517b6dd720733afd773189825cf12fc 100644 (file)
@@ -16,14 +16,16 @@ struct callback_istream {
 
 static void i_stream_callback_destroy(struct iostream_private *stream)
 {
-       struct callback_istream *cstream = (struct callback_istream *)stream;
+       struct callback_istream *cstream =
+               container_of(stream, struct callback_istream, istream.iostream);
 
        buffer_free(&cstream->buf);
 }
 
 static ssize_t i_stream_callback_read(struct istream_private *stream)
 {
-       struct callback_istream *cstream = (struct callback_istream *)stream;
+       struct callback_istream *cstream =
+               container_of(stream, struct callback_istream, istream);
        size_t pos;
 
        if (cstream->callback == NULL) {
@@ -88,7 +90,8 @@ void i_stream_callback_append(struct istream *input,
                              const void *data, size_t size)
 {
        struct callback_istream *cstream =
-               (struct callback_istream *)input->real_stream;
+               container_of(input->real_stream,
+                            struct callback_istream, istream);
 
        buffer_append(cstream->buf, data, size);
 }
@@ -101,7 +104,8 @@ void i_stream_callback_append_str(struct istream *input, const char *str)
 buffer_t *i_stream_callback_get_buffer(struct istream *input)
 {
        struct callback_istream *cstream =
-               (struct callback_istream *)input->real_stream;
+               container_of(input->real_stream,
+                            struct callback_istream, istream);
 
        return cstream->buf;
 }
index e931487836ac63af0b52d14dab21719ee5d366a5..543a99a6e7399aaa4f2edb8777517d93462e75d1 100644 (file)
@@ -50,7 +50,7 @@ i_stream_chain_append_internal(struct istream_chain *chain,
                i_stream_ref(stream);   
 
        if (chain->head == NULL && stream != NULL) {
-               struct chain_istream *cstream = (struct chain_istream *)chain->stream;
+               struct chain_istream *cstream = chain->stream;
 
                if (cstream->have_explicit_max_buffer_size) {
                        i_stream_set_max_buffer_size(stream,
@@ -83,7 +83,8 @@ static void
 i_stream_chain_set_max_buffer_size(struct iostream_private *stream,
                                    size_t max_size)
 {
-       struct chain_istream *cstream = (struct chain_istream *)stream;
+       struct chain_istream *cstream =
+               container_of(stream, struct chain_istream, istream.iostream);
        struct istream_chain_link *link = cstream->chain.head;
 
        cstream->have_explicit_max_buffer_size = TRUE;
@@ -97,7 +98,8 @@ i_stream_chain_set_max_buffer_size(struct iostream_private *stream,
 
 static void i_stream_chain_destroy(struct iostream_private *stream)
 {
-       struct chain_istream *cstream = (struct chain_istream *)stream;
+       struct chain_istream *cstream =
+               container_of(stream, struct chain_istream, istream.iostream);
        struct istream_chain_link *link = cstream->chain.head;
 
        while (link != NULL) {
@@ -198,7 +200,8 @@ static bool i_stream_chain_skip(struct chain_istream *cstream)
 
 static ssize_t i_stream_chain_read(struct istream_private *stream)
 {
-       struct chain_istream *cstream = (struct chain_istream *)stream;
+       struct chain_istream *cstream =
+               container_of(stream, struct chain_istream, istream);
        struct istream_chain_link *link = cstream->chain.head;
        const unsigned char *data;
        size_t data_size, cur_data_pos, new_pos;
@@ -281,7 +284,8 @@ static ssize_t i_stream_chain_read(struct istream_private *stream)
 static void i_stream_chain_close(struct iostream_private *stream,
                                 bool close_parent)
 {
-       struct chain_istream *cstream = (struct chain_istream *)stream;
+       struct chain_istream *cstream =
+               container_of(stream, struct chain_istream, istream.iostream);
 
        /* seek to the correct position in parent stream in case it didn't
           end with EOF */
index e04e77dc40679359817b83dfba94637672ad67c8..8774d3fc5bc330982a348d1d5155c15b502ad58a 100644 (file)
@@ -21,7 +21,8 @@ static void i_stream_concat_skip(struct concat_istream *cstream);
 static void i_stream_concat_close(struct iostream_private *stream,
                                  bool close_parent)
 {
-       struct concat_istream *cstream = (struct concat_istream *)stream;
+       struct concat_istream *cstream =
+               container_of(stream, struct concat_istream, istream.iostream);
        i_assert(cstream->cur_input == cstream->input[cstream->cur_idx]);
        unsigned int i;
 
@@ -38,7 +39,8 @@ static void i_stream_concat_close(struct iostream_private *stream,
 
 static void i_stream_concat_destroy(struct iostream_private *stream)
 {
-       struct concat_istream *cstream = (struct concat_istream *)stream;
+       struct concat_istream *cstream =
+               container_of(stream, struct concat_istream, istream.iostream);
        i_assert(cstream->cur_input == cstream->input[cstream->cur_idx]);
        unsigned int i;
 
@@ -53,7 +55,8 @@ static void
 i_stream_concat_set_max_buffer_size(struct iostream_private *stream,
                                    size_t max_size)
 {
-       struct concat_istream *cstream = (struct concat_istream *)stream;
+       struct concat_istream *cstream =
+               container_of(stream, struct concat_istream, istream.iostream);
        i_assert(cstream->cur_input == cstream->input[cstream->cur_idx]);
        unsigned int i;
 
@@ -140,7 +143,8 @@ static void i_stream_concat_skip(struct concat_istream *cstream)
 
 static ssize_t i_stream_concat_read(struct istream_private *stream)
 {
-       struct concat_istream *cstream = (struct concat_istream *)stream;
+       struct concat_istream *cstream =
+               container_of(stream, struct concat_istream, istream);
        i_assert(cstream->cur_input == cstream->input[cstream->cur_idx]);
        const unsigned char *data;
        size_t size, data_size, cur_data_pos, new_pos;
@@ -274,7 +278,8 @@ find_v_offset(struct concat_istream *cstream, uoff_t *v_offset,
 static void i_stream_concat_seek(struct istream_private *stream,
                                 uoff_t v_offset, bool mark ATTR_UNUSED)
 {
-       struct concat_istream *cstream = (struct concat_istream *)stream;
+       struct concat_istream *cstream =
+               container_of(stream, struct concat_istream, istream);
        i_assert(cstream->cur_input == cstream->input[cstream->cur_idx]);
 
        stream->istream.v_offset = v_offset;
@@ -310,7 +315,8 @@ static void i_stream_concat_seek(struct istream_private *stream,
 static int
 i_stream_concat_stat(struct istream_private *stream, bool exact ATTR_UNUSED)
 {
-       struct concat_istream *cstream = (struct concat_istream *)stream;
+       struct concat_istream *cstream =
+               container_of(stream, struct concat_istream, istream);
        i_assert(cstream->cur_input == cstream->input[cstream->cur_idx]);
        uoff_t v_offset = UOFF_T_MAX;
        unsigned int i, cur_idx;
index 6e07bb31aa7e1636d984adfcc502d332ea3a5f73..2d111b9ae2f937e7b3b18cd502e6828147625306 100644 (file)
@@ -38,7 +38,8 @@ static int i_stream_crlf_read_common(struct crlf_istream *cstream)
 
 static ssize_t i_stream_crlf_read_crlf(struct istream_private *stream)
 {
-       struct crlf_istream *cstream = (struct crlf_istream *)stream;
+       struct crlf_istream *cstream =
+               container_of(stream, struct crlf_istream, istream);
        const unsigned char *data, *ptr, *src, *src_end;
        unsigned char *dest, *dest_end;
        size_t size, copy_len;
@@ -115,7 +116,8 @@ static ssize_t i_stream_crlf_read_crlf(struct istream_private *stream)
 
 static ssize_t i_stream_crlf_read_lf(struct istream_private *stream)
 {
-       struct crlf_istream *cstream = (struct crlf_istream *)stream;
+       struct crlf_istream *cstream =
+               container_of(stream, struct crlf_istream, istream);
        const unsigned char *data, *p;
        size_t i, dest, size, max;
        ssize_t ret;
index 222d46f7fb2fe180b03cfa55df9fd696823c4316..7dccdd3b8955955f093360e9ca7aad63b85a01bc 100644 (file)
@@ -14,7 +14,8 @@ struct failure_at_istream {
 static void i_stream_failure_at_destroy(struct iostream_private *stream)
 {
        struct failure_at_istream *fstream =
-               (struct failure_at_istream *)stream;
+               container_of(stream, struct failure_at_istream,
+                            istream.iostream);
 
        i_free(fstream->error_string);
 }
@@ -22,7 +23,8 @@ static void i_stream_failure_at_destroy(struct iostream_private *stream)
 static ssize_t
 i_stream_failure_at_read(struct istream_private *stream)
 {
-       struct failure_at_istream *fstream = (struct failure_at_istream *)stream;
+       struct failure_at_istream *fstream =
+               container_of(stream, struct failure_at_istream, istream);
        uoff_t new_offset;
        ssize_t ret;
 
index 464ca967599036b4c9511cef25bebb8cf2f5ea63..8c945bd9109f5ce7f01f33aa2b3896ef3cd95595 100644 (file)
 void i_stream_file_close(struct iostream_private *stream,
                         bool close_parent ATTR_UNUSED)
 {
-       struct file_istream *fstream = (struct file_istream *)stream;
-       struct istream_private *_stream = (struct istream_private *)stream;
+       struct istream_private *_stream =
+               container_of(stream, struct istream_private, iostream);
+       struct file_istream *fstream =
+               container_of(_stream, struct file_istream, istream);
 
        if (fstream->autoclose_fd && _stream->fd != -1) {
                /* Ignore ECONNRESET because we don't really care about it here,
@@ -46,7 +48,8 @@ static int i_stream_file_open(struct istream_private *stream)
 
 ssize_t i_stream_file_read(struct istream_private *stream)
 {
-       struct file_istream *fstream = (struct file_istream *) stream;
+       struct file_istream *fstream =
+               container_of(stream, struct file_istream, istream);
        uoff_t offset;
        size_t size;
        ssize_t ret;
@@ -128,7 +131,8 @@ ssize_t i_stream_file_read(struct istream_private *stream)
 static void i_stream_file_seek(struct istream_private *stream, uoff_t v_offset,
                               bool mark ATTR_UNUSED)
 {
-       struct file_istream *fstream = (struct file_istream *) stream;
+       struct file_istream *fstream =
+               container_of(stream, struct file_istream, istream);
 
        if (!stream->istream.seekable) {
                if (v_offset < stream->istream.v_offset)
@@ -155,7 +159,8 @@ static void i_stream_file_sync(struct istream_private *stream)
 static int
 i_stream_file_stat(struct istream_private *stream, bool exact ATTR_UNUSED)
 {
-       struct file_istream *fstream = (struct file_istream *) stream;
+       struct file_istream *fstream =
+               container_of(stream, struct file_istream, istream);
        const char *name = i_stream_get_name(&stream->istream);
 
        if (!fstream->file) {
index 8d5a5ddbfc7fdec43f2e6ff913bf1f5927f147fe..09d2ac29144acf4a9493a2b0ebcc2ced184de3e8 100644 (file)
@@ -16,7 +16,8 @@ struct hash_istream {
 static ssize_t
 i_stream_hash_read(struct istream_private *stream)
 {
-       struct hash_istream *hstream = (struct hash_istream *)stream;
+       struct hash_istream *hstream =
+               container_of(stream, struct hash_istream, istream);
        const unsigned char *data;
        size_t size;
        uoff_t skip;
@@ -50,7 +51,8 @@ static void
 i_stream_hash_seek(struct istream_private *stream,
                   uoff_t v_offset, bool mark ATTR_UNUSED)
 {
-       struct hash_istream *hstream = (struct hash_istream *)stream;
+       struct hash_istream *hstream =
+               container_of(stream, struct hash_istream, istream);
 
        if (hstream->hash_context != NULL) {
                io_stream_set_error(&stream->iostream,
index f1cf038fcf04ce7a5396c0ad1474117d2fa8b8d7..727f21c3bfd71018208f5b705fdc2f0c15827fdf 100644 (file)
@@ -118,7 +118,8 @@ i_stream_json_unescape(const unsigned char *src, size_t len,
 
 static ssize_t i_stream_jsonstr_read(struct istream_private *stream)
 {
-       struct jsonstr_istream *jstream = (struct jsonstr_istream *)stream;
+       struct jsonstr_istream *jstream =
+               container_of(stream, struct jsonstr_istream, istream);
        const unsigned char *data;
        unsigned int srcskip, destskip, extra;
        size_t i, dest, size;
index 7379b23815ffb34e7727e72752ca32654b4fbfac..f66cef3c30d8b9a841d8df7ae0bc6d0bd945d676 100644 (file)
@@ -11,7 +11,8 @@ struct limit_istream {
 
 static void i_stream_limit_destroy(struct iostream_private *stream)
 {
-       struct limit_istream *lstream = (struct limit_istream *) stream;
+       struct limit_istream *lstream =
+               container_of(stream, struct limit_istream, istream.iostream);
        uoff_t v_offset;
 
        v_offset = lstream->istream.parent_start_offset +
@@ -25,7 +26,8 @@ static void i_stream_limit_destroy(struct iostream_private *stream)
 
 static ssize_t i_stream_limit_read(struct istream_private *stream)
 {
-       struct limit_istream *lstream = (struct limit_istream *) stream;
+       struct limit_istream *lstream =
+               container_of(stream, struct limit_istream, istream);
        uoff_t left;
        ssize_t ret;
        size_t pos;
@@ -73,7 +75,8 @@ static ssize_t i_stream_limit_read(struct istream_private *stream)
 static int
 i_stream_limit_stat(struct istream_private *stream, bool exact)
 {
-       struct limit_istream *lstream = (struct limit_istream *) stream;
+       struct limit_istream *lstream =
+               container_of(stream, struct limit_istream, istream);
        const struct stat *st;
 
        if (i_stream_stat(stream->parent, exact, &st) < 0) {
@@ -90,7 +93,8 @@ i_stream_limit_stat(struct istream_private *stream, bool exact)
 static int i_stream_limit_get_size(struct istream_private *stream,
                                   bool exact, uoff_t *size_r)
 {
-       struct limit_istream *lstream = (struct limit_istream *) stream;
+       struct limit_istream *lstream =
+               container_of(stream, struct limit_istream, istream);
        const struct stat *st;
 
        if (lstream->v_size != UOFF_T_MAX) {
index 65602d4c05b7546926c29292e1fd409c991d4571..4cacbbebb23b01143d48a06f3a89b0afa9ed44ad 100644 (file)
@@ -176,7 +176,8 @@ i_stream_multiplex_read(struct multiplex_istream *mstream,
 
 static ssize_t i_stream_multiplex_ichannel_read(struct istream_private *stream)
 {
-       struct multiplex_ichannel *channel = (struct multiplex_ichannel*)stream;
+       struct multiplex_ichannel *channel =
+               container_of(stream, struct multiplex_ichannel, istream);
        /* if previous multiplex read dumped data for us
           actually serve it here. */
        if (channel->pending_pos > 0) {
@@ -192,7 +193,8 @@ static void
 i_stream_multiplex_ichannel_switch_ioloop_to(struct istream_private *stream,
                                             struct ioloop *ioloop)
 {
-       struct multiplex_ichannel *channel = (struct multiplex_ichannel*)stream;
+       struct multiplex_ichannel *channel =
+               container_of(stream, struct multiplex_ichannel, istream);
 
        i_stream_switch_ioloop_to(channel->mstream->parent, ioloop);
 }
@@ -201,7 +203,9 @@ static void
 i_stream_multiplex_ichannel_close(struct iostream_private *stream, bool close_parent)
 {
        struct multiplex_ichannel *const *channelp;
-       struct multiplex_ichannel *channel = (struct multiplex_ichannel*)stream;
+       struct multiplex_ichannel *channel =
+               container_of(stream, struct multiplex_ichannel,
+                            istream.iostream);
        channel->closed = TRUE;
        if (close_parent) {
                array_foreach(&channel->mstream->channels, channelp)
@@ -226,7 +230,9 @@ static void i_stream_multiplex_try_destroy(struct multiplex_istream *mstream)
 static void i_stream_multiplex_ichannel_destroy(struct iostream_private *stream)
 {
        struct multiplex_ichannel **channelp;
-       struct multiplex_ichannel *channel = (struct multiplex_ichannel*)stream;
+       struct multiplex_ichannel *channel =
+               container_of(stream, struct multiplex_ichannel,
+                            istream.iostream);
        i_stream_multiplex_ichannel_close(stream, TRUE);
        i_stream_free_buffer(&channel->istream);
        array_foreach_modifiable(&channel->mstream->channels, channelp) {
@@ -262,7 +268,8 @@ i_stream_add_channel_real(struct multiplex_istream *mstream, uint8_t cid)
 struct istream *i_stream_multiplex_add_channel(struct istream *stream, uint8_t cid)
 {
        struct multiplex_ichannel *chan =
-               (struct multiplex_ichannel *)stream->real_stream;
+               container_of(stream->real_stream,
+                            struct multiplex_ichannel, istream);
        i_assert(get_channel(chan->mstream, cid) == NULL);
 
        return i_stream_add_channel_real(chan->mstream, cid);
@@ -285,6 +292,7 @@ struct istream *i_stream_create_multiplex(struct istream *parent, size_t bufsize
 uint8_t i_stream_multiplex_get_channel_id(struct istream *stream)
 {
        struct multiplex_ichannel *channel =
-               (struct multiplex_ichannel *)stream->real_stream;
+               container_of(stream->real_stream,
+                            struct multiplex_ichannel, istream);
        return channel->cid;
 }
index aa0a75f0f899e71bd45e7017d7798125878b8405..2a4442948489d25238edff60935547d7971f796e 100644 (file)
@@ -14,7 +14,8 @@ struct rawlog_istream {
 static void i_stream_rawlog_close(struct iostream_private *stream,
                                  bool close_parent)
 {
-       struct rawlog_istream *rstream = (struct rawlog_istream *)stream;
+       struct rawlog_istream *rstream =
+               container_of(stream, struct rawlog_istream, istream.iostream);
 
        iostream_rawlog_close(&rstream->riostream);
        if (close_parent)
@@ -23,7 +24,8 @@ static void i_stream_rawlog_close(struct iostream_private *stream,
 
 static void i_stream_rawlog_destroy(struct iostream_private *stream)
 {
-       struct rawlog_istream *rstream = (struct rawlog_istream *)stream;
+       struct rawlog_istream *rstream =
+               container_of(stream, struct rawlog_istream, istream.iostream);
        uoff_t v_offset;
 
        v_offset = rstream->istream.parent_start_offset +
@@ -37,7 +39,8 @@ static void i_stream_rawlog_destroy(struct iostream_private *stream)
 
 static ssize_t i_stream_rawlog_read(struct istream_private *stream)
 {
-       struct rawlog_istream *rstream = (struct rawlog_istream *)stream;
+       struct rawlog_istream *rstream =
+               container_of(stream, struct rawlog_istream, istream);
        ssize_t ret;
        size_t pos;
 
index fdaaaaee6a259909cb9240630d139f82edf6362d..00648f164a7b3d1a81e44062949790af8a4e47b8 100644 (file)
@@ -36,7 +36,8 @@ struct seekable_istream {
 static void i_stream_seekable_close(struct iostream_private *stream,
                                    bool close_parent ATTR_UNUSED)
 {
-       struct seekable_istream *sstream = (struct seekable_istream *)stream;
+       struct seekable_istream *sstream =
+               container_of(stream, struct seekable_istream, istream.iostream);
 
        sstream->fd = -1;
        i_stream_close(sstream->fd_input);
@@ -52,7 +53,8 @@ static void unref_streams(struct seekable_istream *sstream)
 
 static void i_stream_seekable_destroy(struct iostream_private *stream)
 {
-       struct seekable_istream *sstream = (struct seekable_istream *)stream;
+       struct seekable_istream *sstream =
+               container_of(stream, struct seekable_istream, istream.iostream);
 
        i_stream_free_buffer(&sstream->istream);
        i_stream_unref(&sstream->fd_input);
@@ -68,7 +70,8 @@ static void
 i_stream_seekable_set_max_buffer_size(struct iostream_private *stream,
                                      size_t max_size)
 {
-       struct seekable_istream *sstream = (struct seekable_istream *)stream;
+       struct seekable_istream *sstream =
+               container_of(stream, struct seekable_istream, istream.iostream);
        unsigned int i;
 
        sstream->istream.max_buffer_size = max_size;
@@ -256,7 +259,8 @@ static int i_stream_seekable_write_failed(struct seekable_istream *sstream)
 
 static ssize_t i_stream_seekable_read(struct istream_private *stream)
 {
-       struct seekable_istream *sstream = (struct seekable_istream *)stream;
+       struct seekable_istream *sstream =
+               container_of(stream, struct seekable_istream, istream);
        const unsigned char *data;
        size_t size, pos;
        ssize_t ret;
@@ -330,7 +334,8 @@ static ssize_t i_stream_seekable_read(struct istream_private *stream)
 static int
 i_stream_seekable_stat(struct istream_private *stream, bool exact)
 {
-       struct seekable_istream *sstream = (struct seekable_istream *)stream;
+       struct seekable_istream *sstream =
+               container_of(stream, struct seekable_istream, istream);
        const struct stat *st;
        uoff_t old_offset, len;
        ssize_t ret;
@@ -395,7 +400,8 @@ static struct istream_snapshot *
 i_stream_seekable_snapshot(struct istream_private *stream,
                           struct istream_snapshot *prev_snapshot)
 {
-       struct seekable_istream *sstream = (struct seekable_istream *)stream;
+       struct seekable_istream *sstream =
+               container_of(stream, struct seekable_istream, istream);
 
        if (sstream->fd == -1) {
                /* still in memory */
@@ -534,7 +540,8 @@ i_stream_create_seekable_path(struct istream *input[],
        stream = i_stream_create_seekable(input, max_buffer_size,
                                          seekable_fd_callback,
                                          i_strdup(temp_path_prefix));
-       sstream = (struct seekable_istream *)stream->real_stream;
+       sstream = container_of(stream->real_stream,
+                              struct seekable_istream, istream);
        sstream->free_context = TRUE;
        return stream;
 }
index d5c1e863bdd16a9224ca6a325628aeb6042b3b7e..afe95b79a25680755761a66e4142417de44d393c 100644 (file)
@@ -16,7 +16,8 @@ struct sized_istream {
 
 static void i_stream_sized_destroy(struct iostream_private *stream)
 {
-       struct sized_istream *sstream = (struct sized_istream *)stream;
+       struct sized_istream *sstream =
+               container_of(stream, struct sized_istream, istream.iostream);
        uoff_t v_offset;
 
        v_offset = sstream->istream.parent_start_offset +
@@ -61,7 +62,7 @@ i_stream_sized_parent_read(struct istream_private *stream, size_t *pos_r)
 static ssize_t i_stream_sized_read(struct istream_private *stream)
 {
        struct sized_istream *sstream =
-               (struct sized_istream *)stream;
+               container_of(stream, struct sized_istream, istream);
        struct istream_sized_error_data data;
        const char *error;
        uoff_t left;
@@ -133,7 +134,8 @@ static ssize_t i_stream_sized_read(struct istream_private *stream)
 static int
 i_stream_sized_stat(struct istream_private *stream, bool exact ATTR_UNUSED)
 {
-       struct sized_istream *sstream = (struct sized_istream *)stream;
+       struct sized_istream *sstream =
+               container_of(stream, struct sized_istream, istream);
        const struct stat *st;
 
        /* parent stream may be base64-decoder. don't waste time decoding the
@@ -197,7 +199,9 @@ struct istream *i_stream_create_min_sized(struct istream *input, uoff_t min_size
        struct istream *ret;
 
        ret= i_stream_create_sized(input, min_size);
-       ((struct sized_istream *)ret->real_stream)->min_size_only = TRUE;
+       struct sized_istream *ret_sstream =
+               container_of(ret->real_stream, struct sized_istream, istream);
+       ret_sstream->min_size_only = TRUE;
        return ret;
 }
 
@@ -207,7 +211,9 @@ struct istream *i_stream_create_min_sized_range(struct istream *input,
        struct istream *ret;
 
        ret = i_stream_create_sized_range(input, offset, min_size);
-       ((struct sized_istream *)ret->real_stream)->min_size_only = TRUE;
+       struct sized_istream *ret_sstream =
+               container_of(ret->real_stream, struct sized_istream, istream);
+       ret_sstream->min_size_only = TRUE;
        return ret;
 }
 
index 268b720be47d240df197229880144d9a9d7440ae..858afd74f81621d6e917c02866ee09fbc18479aa 100644 (file)
@@ -69,14 +69,18 @@ static void tee_streams_skip(struct tee_istream *tee)
 static void i_stream_tee_close(struct iostream_private *stream,
                               bool close_parent ATTR_UNUSED)
 {
-       struct tee_child_istream *tstream = (struct tee_child_istream *)stream;
+       struct tee_child_istream *tstream =
+               container_of(stream, struct tee_child_istream,
+                            istream.iostream);
 
        tee_streams_skip(tstream->tee);
 }
 
 static void i_stream_tee_destroy(struct iostream_private *stream)
 {
-       struct tee_child_istream *tstream = (struct tee_child_istream *)stream;
+       struct tee_child_istream *tstream =
+               container_of(stream, struct tee_child_istream,
+                            istream.iostream);
        struct tee_istream *tee = tstream->tee;
        struct tee_child_istream **p;
 
@@ -109,7 +113,9 @@ static void
 i_stream_tee_set_max_buffer_size(struct iostream_private *stream,
                                 size_t max_size)
 {
-       struct tee_child_istream *tstream = (struct tee_child_istream *)stream;
+       struct tee_child_istream *tstream =
+               container_of(stream, struct tee_child_istream,
+                            istream.iostream);
 
        tstream->istream.max_buffer_size = max_size;
        i_stream_set_max_buffer_size(tstream->tee->input, max_size);
@@ -117,7 +123,8 @@ i_stream_tee_set_max_buffer_size(struct iostream_private *stream,
 
 static ssize_t i_stream_tee_read(struct istream_private *stream)
 {
-       struct tee_child_istream *tstream = (struct tee_child_istream *)stream;
+       struct tee_child_istream *tstream =
+               container_of(stream, struct tee_child_istream, istream);
        struct istream *input = tstream->tee->input;
        const unsigned char *data;
        size_t size;
@@ -175,7 +182,8 @@ static ssize_t i_stream_tee_read(struct istream_private *stream)
 static int
 i_stream_tee_stat(struct istream_private *stream, bool exact)
 {
-       struct tee_child_istream *tstream = (struct tee_child_istream *)stream;
+       struct tee_child_istream *tstream =
+               container_of(stream, struct tee_child_istream, istream);
        const struct stat *st;
 
        if (i_stream_stat(tstream->tee->input, exact, &st) < 0)
@@ -186,7 +194,8 @@ i_stream_tee_stat(struct istream_private *stream, bool exact)
 
 static void i_stream_tee_sync(struct istream_private *stream)
 {
-       struct tee_child_istream *tstream = (struct tee_child_istream *)stream;
+       struct tee_child_istream *tstream =
+               container_of(stream, struct tee_child_istream, istream);
 
        tee_streams_skip(tstream->tee);
        if (i_stream_get_data_size(tstream->tee->input) != 0) {
@@ -242,7 +251,8 @@ struct istream *tee_i_stream_create_child(struct tee_istream *tee)
 bool tee_i_stream_child_is_waiting(struct istream *input)
 {
        struct tee_child_istream *tstream =
-               (struct tee_child_istream *)input->real_stream;
+               container_of(input->real_stream,
+                            struct tee_child_istream, istream);
 
        return tstream->last_read_waiting;
 }
index d7d8f98bb4202a6ef197e885f9a7284f16a7dbdd..4fcced1758128207a490cdf3d6294f8ad4b16a77 100644 (file)
@@ -20,7 +20,8 @@ struct timeout_istream {
 static void i_stream_timeout_close(struct iostream_private *stream,
                                   bool close_parent)
 {
-       struct timeout_istream *tstream = (struct timeout_istream *)stream;
+       struct timeout_istream *tstream =
+               container_of(stream, struct timeout_istream, istream.iostream);
 
        timeout_remove(&tstream->to);
        if (close_parent)
@@ -30,7 +31,8 @@ static void i_stream_timeout_close(struct iostream_private *stream,
 static void i_stream_timeout_switch_ioloop_to(struct istream_private *stream,
                                              struct ioloop *ioloop)
 {
-       struct timeout_istream *tstream = (struct timeout_istream *)stream;
+       struct timeout_istream *tstream =
+               container_of(stream, struct timeout_istream, istream);
 
        if (tstream->to != NULL)
                tstream->to = io_loop_move_timeout_to(ioloop, &tstream->to);
@@ -88,7 +90,8 @@ static void i_stream_timeout_set_pending(struct timeout_istream *tstream)
 static ssize_t
 i_stream_timeout_read(struct istream_private *stream)
 {
-       struct timeout_istream *tstream = (struct timeout_istream *)stream;
+       struct timeout_istream *tstream =
+               container_of(stream, struct timeout_istream, istream);
        struct iostream_private *iostream = &tstream->istream.iostream;
        ssize_t ret;
 
index c22ee4dbe721dc16434107e1f165986b84442ffa..3b83a7f681d9add783911c967510ef644ac58c64 100644 (file)
@@ -28,7 +28,8 @@ static void i_stream_unref_try_inputs(struct try_istream *tstream)
 static void i_stream_try_close(struct iostream_private *stream,
                               bool close_parent)
 {
-       struct try_istream *tstream = (struct try_istream *)stream;
+       struct try_istream *tstream =
+               container_of(stream, struct try_istream, istream.iostream);
 
        if (close_parent) {
                if (tstream->istream.parent != NULL)
@@ -113,7 +114,8 @@ static int i_stream_try_detect(struct try_istream *tstream)
 static ssize_t
 i_stream_try_read(struct istream_private *stream)
 {
-       struct try_istream *tstream = (struct try_istream *)stream;
+       struct try_istream *tstream =
+               container_of(stream, struct try_istream, istream);
        int ret;
 
        if (stream->parent == NULL) {
index d88e5ac399914620e25ffcb3979576e9c0d62314..3a8fabd039601be6dd4be5f6084d697c3e5c97ac 100644 (file)
@@ -14,7 +14,9 @@ struct unix_istream {
 static void
 i_stream_unix_close(struct iostream_private *stream, bool close_parent)
 {
-       struct unix_istream *ustream = (struct unix_istream *)stream;
+       struct unix_istream *ustream =
+               container_of(stream, struct unix_istream,
+                            fstream.istream.iostream);
 
        i_close_fd(&ustream->read_fd);
        i_stream_file_close(stream, close_parent);
@@ -22,7 +24,8 @@ i_stream_unix_close(struct iostream_private *stream, bool close_parent)
 
 static ssize_t i_stream_unix_read(struct istream_private *stream)
 {
-       struct unix_istream *ustream = (struct unix_istream *)stream;
+       struct unix_istream *ustream =
+               container_of(stream, struct unix_istream, fstream.istream);
        size_t size;
        ssize_t ret;
 
@@ -82,7 +85,8 @@ struct istream *i_stream_create_unix(int fd, size_t max_buffer_size)
 void i_stream_unix_set_read_fd(struct istream *input)
 {
        struct unix_istream *ustream =
-               (struct unix_istream *)input->real_stream;
+               container_of(input->real_stream, struct unix_istream,
+                            fstream.istream);
 
        ustream->next_read_fd = TRUE;
 }
@@ -90,7 +94,8 @@ void i_stream_unix_set_read_fd(struct istream *input)
 void i_stream_unix_unset_read_fd(struct istream *input)
 {
        struct unix_istream *ustream =
-               (struct unix_istream *)input->real_stream;
+               container_of(input->real_stream, struct unix_istream,
+                            fstream.istream);
 
        ustream->next_read_fd = FALSE;
 }
@@ -98,7 +103,8 @@ void i_stream_unix_unset_read_fd(struct istream *input)
 int i_stream_unix_get_read_fd(struct istream *input)
 {
        struct unix_istream *ustream =
-               (struct unix_istream *)input->real_stream;
+               container_of(input->real_stream, struct unix_istream,
+                            fstream.istream);
        int fd;
 
        fd = ustream->read_fd;
index 09e15bf4ba5ae1cb9cf6eb4703b0460601f3d33b..55b9a9eef00908ba339707ae5f1e59bb00564210 100644 (file)
@@ -983,7 +983,8 @@ static void
 i_stream_default_set_max_buffer_size(struct iostream_private *stream,
                                     size_t max_size)
 {
-       struct istream_private *_stream = (struct istream_private *)stream;
+       struct istream_private *_stream =
+               container_of(stream, struct istream_private, iostream);
 
        _stream->max_buffer_size = max_size;
        if (_stream->parent != NULL)
@@ -993,7 +994,8 @@ i_stream_default_set_max_buffer_size(struct iostream_private *stream,
 static void i_stream_default_close(struct iostream_private *stream,
                                   bool close_parent)
 {
-       struct istream_private *_stream = (struct istream_private *)stream;
+       struct istream_private *_stream =
+               container_of(stream, struct istream_private, iostream);
 
        if (close_parent)
                i_stream_close(_stream->parent);
@@ -1001,7 +1003,8 @@ static void i_stream_default_close(struct iostream_private *stream,
 
 static void i_stream_default_destroy(struct iostream_private *stream)
 {
-       struct istream_private *_stream = (struct istream_private *)stream;
+       struct istream_private *_stream =
+               container_of(stream, struct istream_private, iostream);
 
        i_stream_free_buffer(_stream);
        i_stream_unref(&_stream->parent);
index 76e192db796d38f4631a7c76508c1dabc9882952..af3dbef84f4a886122f4c5ccec1eb6591dcd65d6 100644 (file)
@@ -12,7 +12,8 @@ struct buffer_ostream {
 
 static int o_stream_buffer_seek(struct ostream_private *stream, uoff_t offset)
 {
-       struct buffer_ostream *bstream = (struct buffer_ostream *)stream;
+       struct buffer_ostream *bstream =
+               container_of(stream, struct buffer_ostream, ostream);
 
        bstream->seeked = TRUE;
        stream->ostream.offset = offset;
@@ -23,7 +24,8 @@ static int
 o_stream_buffer_write_at(struct ostream_private *stream,
                         const void *data, size_t size, uoff_t offset)
 {
-       struct buffer_ostream *bstream = (struct buffer_ostream *)stream;
+       struct buffer_ostream *bstream =
+               container_of(stream, struct buffer_ostream, ostream);
 
        buffer_write(bstream->buf, offset, data, size);
        return 0;
@@ -33,7 +35,8 @@ static ssize_t
 o_stream_buffer_sendv(struct ostream_private *stream,
                      const struct const_iovec *iov, unsigned int iov_count)
 {
-       struct buffer_ostream *bstream = (struct buffer_ostream *)stream;
+       struct buffer_ostream *bstream =
+               container_of(stream, struct buffer_ostream, ostream);
        size_t left, n, offset;
        ssize_t ret = 0;
        unsigned int i;
@@ -56,7 +59,8 @@ o_stream_buffer_sendv(struct ostream_private *stream,
 static size_t
 o_stream_buffer_get_buffer_used_size(const struct ostream_private *stream)
 {
-       struct buffer_ostream *bstream = (struct buffer_ostream *)stream;
+       const struct buffer_ostream *bstream =
+               container_of(stream, const struct buffer_ostream, ostream);
 
        return bstream->buf->used;
 }
index 32aa1317bc78ebf7e2292ed9117605701469ffe2..87bd7b88cecc5d467ce161985d4489da71ab302f 100644 (file)
@@ -15,7 +15,8 @@ struct failure_at_ostream {
 static void o_stream_failure_at_destroy(struct iostream_private *stream)
 {
        struct failure_at_ostream *fstream =
-               (struct failure_at_ostream *)stream;
+               container_of(stream, struct failure_at_ostream,
+                            ostream.iostream);
 
        i_free(fstream->error_string);
        o_stream_unref(&fstream->ostream.parent);
@@ -26,7 +27,7 @@ o_stream_failure_at_sendv(struct ostream_private *stream,
                          const struct const_iovec *iov, unsigned int iov_count)
 {
        struct failure_at_ostream *fstream =
-               (struct failure_at_ostream *)stream;
+               container_of(stream, struct failure_at_ostream, ostream);
        unsigned int i;
        struct const_iovec *iov_dup;
        unsigned int iov_dup_count;
@@ -80,7 +81,7 @@ static int
 o_stream_failure_at_flush(struct ostream_private *stream)
 {
        struct failure_at_ostream *fstream =
-               (struct failure_at_ostream *)stream;
+               container_of(stream, struct failure_at_ostream, ostream);
 
        if (fstream->failed) {
                io_stream_set_error(&stream->iostream, "%s",
index f2abefd873ae4f546bb94ef4de1703f99255d1d0..2be00d20a7720959ec3541d632b6d9dca7ca49e7 100644 (file)
@@ -54,14 +54,16 @@ static void stream_closed(struct file_ostream *fstream)
 void o_stream_file_close(struct iostream_private *stream,
                                bool close_parent ATTR_UNUSED)
 {
-       struct file_ostream *fstream = (struct file_ostream *)stream;
+       struct file_ostream *fstream =
+               container_of(stream, struct file_ostream, ostream.iostream);
 
        stream_closed(fstream);
 }
 
 static void o_stream_file_destroy(struct iostream_private *stream)
 {
-       struct file_ostream *fstream = (struct file_ostream *)stream;
+       struct file_ostream *fstream =
+               container_of(stream, struct file_ostream, ostream.iostream);
 
        i_free(fstream->buffer);
 }
@@ -354,7 +356,8 @@ static void o_stream_tcp_flush_via_nodelay(struct file_ostream *fstream)
 
 static void o_stream_file_cork(struct ostream_private *stream, bool set)
 {
-       struct file_ostream *fstream = (struct file_ostream *)stream;
+       struct file_ostream *fstream =
+               container_of(stream, struct file_ostream, ostream);
        struct iostream_private *iostream = &fstream->ostream.iostream;
        int ret;
 
@@ -403,7 +406,8 @@ static void o_stream_file_cork(struct ostream_private *stream, bool set)
 
 static int o_stream_file_flush(struct ostream_private *stream)
 {
-       struct file_ostream *fstream = (struct file_ostream *) stream;
+       struct file_ostream *fstream =
+               container_of(stream, struct file_ostream, ostream);
 
        return buffer_flush(fstream);
 }
@@ -411,7 +415,8 @@ static int o_stream_file_flush(struct ostream_private *stream)
 static void
 o_stream_file_flush_pending(struct ostream_private *stream, bool set)
 {
-       struct file_ostream *fstream = (struct file_ostream *) stream;
+       struct file_ostream *fstream =
+               container_of(stream, struct file_ostream, ostream);
        struct iostream_private *iostream = &fstream->ostream.iostream;
 
        fstream->flush_pending = set;
@@ -440,14 +445,15 @@ static size_t
 o_stream_file_get_buffer_used_size(const struct ostream_private *stream)
 {
        const struct file_ostream *fstream =
-               (const struct file_ostream *)stream;
+               container_of(stream, const struct file_ostream, ostream);
 
        return fstream->buffer_size - get_unused_space(fstream);
 }
 
 static int o_stream_file_seek(struct ostream_private *stream, uoff_t offset)
 {
-       struct file_ostream *fstream = (struct file_ostream *)stream;
+       struct file_ostream *fstream =
+               container_of(stream, struct file_ostream, ostream);
 
        if (offset > OFF_T_MAX) {
                stream->ostream.stream_errno = EINVAL;
@@ -588,7 +594,8 @@ ssize_t o_stream_file_sendv(struct ostream_private *stream,
                                   const struct const_iovec *iov,
                                   unsigned int iov_count)
 {
-       struct file_ostream *fstream = (struct file_ostream *)stream;
+       struct file_ostream *fstream =
+               container_of(stream, struct file_ostream, ostream);
        size_t size, total_size, added, optimal_size;
        unsigned int i;
        ssize_t ret = 0;
@@ -682,7 +689,8 @@ static int
 o_stream_file_write_at(struct ostream_private *stream,
                       const void *data, size_t size, uoff_t offset)
 {
-       struct file_ostream *fstream = (struct file_ostream *)stream;
+       struct file_ostream *fstream =
+               container_of(stream, struct file_ostream, ostream);
        size_t used, pos, skip, left;
 
        /* update buffer if the write overlaps it */
@@ -741,7 +749,8 @@ io_stream_sendfile(struct ostream_private *outstream,
                   struct istream *instream, int in_fd,
                   enum ostream_send_istream_result *res_r)
 {
-       struct file_ostream *foutstream = (struct file_ostream *)outstream;
+       struct file_ostream *foutstream =
+               container_of(outstream, struct file_ostream, ostream);
        uoff_t in_size, offset, send_size, v_offset, abs_start_offset;
        ssize_t ret;
        bool sendfile_not_supported = FALSE;
@@ -837,7 +846,8 @@ static enum ostream_send_istream_result
 io_stream_copy_backwards(struct ostream_private *outstream,
                         struct istream *instream, uoff_t in_size)
 {
-       struct file_ostream *foutstream = (struct file_ostream *)outstream;
+       struct file_ostream *foutstream =
+               container_of(outstream, struct file_ostream, ostream);
        uoff_t in_start_offset, in_offset, in_limit, out_offset;
        const unsigned char *data;
        size_t buffer_size, size, read_size;
@@ -957,7 +967,8 @@ static enum ostream_send_istream_result
 o_stream_file_send_istream(struct ostream_private *outstream,
                           struct istream *instream)
 {
-       struct file_ostream *foutstream = (struct file_ostream *)outstream;
+       struct file_ostream *foutstream =
+               container_of(outstream, struct file_ostream, ostream);
        bool same_stream;
        int in_fd;
        enum ostream_send_istream_result res;
@@ -983,7 +994,8 @@ o_stream_file_send_istream(struct ostream_private *outstream,
 static void o_stream_file_switch_ioloop_to(struct ostream_private *stream,
                                           struct ioloop *ioloop)
 {
-       struct file_ostream *fstream = (struct file_ostream *)stream;
+       struct file_ostream *fstream =
+               container_of(stream, struct file_ostream, ostream);
 
        if (fstream->io != NULL)
                fstream->io = io_loop_move_io_to(ioloop, &fstream->io);
index adee491dbcaefd9c1c27565ed07b4135669c207d..c83b43e06c8a408f179dd02a723902416d8a29b8 100644 (file)
@@ -15,7 +15,8 @@ static ssize_t
 o_stream_hash_sendv(struct ostream_private *stream,
                    const struct const_iovec *iov, unsigned int iov_count)
 {
-       struct hash_ostream *hstream = (struct hash_ostream *)stream;
+       struct hash_ostream *hstream =
+               container_of(stream, struct hash_ostream, ostream);
        unsigned int i;
        size_t bytes_left, block_len;
        ssize_t ret;
index 323a0d32c9915bc22cd4480068c052435350a900..822d6150e47853312ead6be23693604897cd1408 100644 (file)
@@ -145,7 +145,8 @@ static int o_stream_multiplex_flush(struct multiplex_ostream *mstream)
 static int o_stream_multiplex_ochannel_flush(struct ostream_private *stream)
 {
        ssize_t ret;
-       struct multiplex_ochannel *channel = (struct multiplex_ochannel *)stream;
+       struct multiplex_ochannel *channel =
+               container_of(stream, struct multiplex_ochannel, ostream);
        struct multiplex_ostream *mstream = channel->mstream;
 
        /* flush parent stream always, so there is room for more. */
@@ -165,7 +166,8 @@ static int o_stream_multiplex_ochannel_flush(struct ostream_private *stream)
 
 static void o_stream_multiplex_ochannel_cork(struct ostream_private *stream, bool set)
 {
-       struct multiplex_ochannel *channel = (struct multiplex_ochannel*)stream;
+       struct multiplex_ochannel *channel =
+               container_of(stream, struct multiplex_ochannel, ostream);
        if (channel->corked != set && !set) {
                /* flush */
                (void)o_stream_multiplex_ochannel_flush(stream);
@@ -177,7 +179,8 @@ static ssize_t
 o_stream_multiplex_ochannel_sendv(struct ostream_private *stream,
                                 const struct const_iovec *iov, unsigned int iov_count)
 {
-       struct multiplex_ochannel *channel = (struct multiplex_ochannel*)stream;
+       struct multiplex_ochannel *channel =
+               container_of(stream, struct multiplex_ochannel, ostream);
        size_t total = 0, avail = o_stream_get_buffer_avail_size(&stream->ostream);
        size_t optimal_size = I_MIN(IO_BLOCK_SIZE, avail);
 
@@ -227,7 +230,7 @@ static size_t
 o_stream_multiplex_ochannel_get_buffer_used_size(const struct ostream_private *stream)
 {
        const struct multiplex_ochannel *channel =
-               (const struct multiplex_ochannel*)stream;
+               container_of(stream, const struct multiplex_ochannel, ostream);
 
        return channel->buf->used +
                o_stream_get_buffer_used_size(channel->mstream->parent);
@@ -237,7 +240,7 @@ static size_t
 o_stream_multiplex_ochannel_get_buffer_avail_size(const struct ostream_private *stream)
 {
        const struct multiplex_ochannel *channel =
-               (const struct multiplex_ochannel*)stream;
+               container_of(stream, const struct multiplex_ochannel, ostream);
        size_t max_avail = I_MIN(channel->mstream->bufsize,
                                 o_stream_get_buffer_avail_size(stream->parent));
 
@@ -250,7 +253,8 @@ static void
 o_stream_multiplex_ochannel_close(struct iostream_private *stream, bool close_parent)
 {
        struct multiplex_ochannel *const *channelp;
-       struct multiplex_ochannel *channel = (struct multiplex_ochannel*)stream;
+       struct multiplex_ochannel *channel =
+               container_of(stream, struct multiplex_ochannel, ostream.iostream);
 
        channel->closed = TRUE;
        if (close_parent) {
@@ -282,7 +286,8 @@ static void o_stream_multiplex_try_destroy(struct multiplex_ostream *mstream)
 static void o_stream_multiplex_ochannel_destroy(struct iostream_private *stream)
 {
        struct multiplex_ochannel **channelp;
-       struct multiplex_ochannel *channel = (struct multiplex_ochannel*)stream;
+       struct multiplex_ochannel *channel =
+               container_of(stream, struct multiplex_ochannel, ostream.iostream);
        o_stream_unref(&channel->ostream.parent);
        if (channel->buf != NULL)
                buffer_free(&channel->buf);
@@ -329,7 +334,8 @@ o_stream_add_channel_real(struct multiplex_ostream *mstream, uint8_t cid)
 struct ostream *o_stream_multiplex_add_channel(struct ostream *stream, uint8_t cid)
 {
        struct multiplex_ochannel *chan =
-               (struct multiplex_ochannel *)stream->real_stream;
+               container_of(stream->real_stream, struct multiplex_ochannel,
+                            ostream);
        i_assert(get_channel(chan->mstream, cid) == NULL);
 
        return o_stream_add_channel_real(chan->mstream, cid);
@@ -354,6 +360,7 @@ struct ostream *o_stream_create_multiplex(struct ostream *parent, size_t bufsize
 uint8_t o_stream_multiplex_get_channel_id(struct ostream *stream)
 {
        struct multiplex_ochannel *channel =
-               (struct multiplex_ochannel *)stream->real_stream;
+               container_of(stream->real_stream, struct multiplex_ochannel,
+                            ostream);
        return channel->cid;
 }
index 081ca661e6f020367386f26b8e009a1906d02726..8066392e3329579d2a37c4021072e6ddbb414a61 100644 (file)
@@ -13,7 +13,8 @@ struct rawlog_ostream {
 static void o_stream_rawlog_close(struct iostream_private *stream,
                                  bool close_parent)
 {
-       struct rawlog_ostream *rstream = (struct rawlog_ostream *)stream;
+       struct rawlog_ostream *rstream =
+               container_of(stream, struct rawlog_ostream, ostream.iostream);
 
        iostream_rawlog_close(&rstream->riostream);
        if (close_parent)
@@ -24,7 +25,8 @@ static ssize_t
 o_stream_rawlog_sendv(struct ostream_private *stream,
                      const struct const_iovec *iov, unsigned int iov_count)
 {
-       struct rawlog_ostream *rstream = (struct rawlog_ostream *)stream;
+       struct rawlog_ostream *rstream =
+               container_of(stream, struct rawlog_ostream, ostream);
        unsigned int i;
        ssize_t ret, bytes;
 
index 66192873bfaf62b79da898e03e47fa239061f1e0..06e918f3b28bff5f2602f17bda051844761e9be6 100644 (file)
@@ -13,7 +13,9 @@ struct unix_ostream {
 static void
 o_stream_unix_close(struct iostream_private *stream, bool close_parent)
 {
-       struct unix_ostream *ustream = (struct unix_ostream *)stream;
+       struct unix_ostream *ustream =
+               container_of(stream, struct unix_ostream,
+                            fstream.ostream.iostream);
 
        i_close_fd(&ustream->write_fd);
        o_stream_file_close(stream, close_parent);
@@ -23,7 +25,8 @@ static ssize_t o_stream_unix_writev(struct file_ostream *fstream,
                                   const struct const_iovec *iov,
                                   unsigned int iov_count)
 {
-       struct unix_ostream *ustream = (struct unix_ostream *)fstream;
+       struct unix_ostream *ustream =
+               container_of(fstream, struct unix_ostream, fstream);
        size_t sent;
        ssize_t ret;
 
@@ -80,7 +83,8 @@ struct ostream *o_stream_create_unix(int fd, size_t max_buffer_size)
 bool o_stream_unix_write_fd(struct ostream *output, int fd)
 {
        struct unix_ostream *ustream =
-               (struct unix_ostream *)output->real_stream;
+               container_of(output->real_stream, struct unix_ostream,
+                            fstream.ostream);
 
        i_assert(fd >= 0);
 
index b5d155a6f274abe60879478445c90c19dfed8f4d..566a977cbe79bc3589a4255a84209421d5a816da 100644 (file)
@@ -735,7 +735,8 @@ static void
 wrapper_ostream_close(struct iostream_private *stream,
                      bool close_parent ATTR_UNUSED)
 {
-       struct wrapper_ostream *wostream = (struct wrapper_ostream *)stream;
+       struct wrapper_ostream *wostream =
+               container_of(stream, struct wrapper_ostream, ostream.iostream);
 
        timeout_remove(&wostream->to_event);
        wrapper_ostream_output_close(wostream);
@@ -745,7 +746,8 @@ wrapper_ostream_close(struct iostream_private *stream,
 
 static void wrapper_ostream_destroy(struct iostream_private *stream)
 {
-       struct wrapper_ostream *wostream = (struct wrapper_ostream *)stream;
+       struct wrapper_ostream *wostream =
+               container_of(stream, struct wrapper_ostream, ostream.iostream);
 
        timeout_remove(&wostream->to_event);
        i_free(wostream->pending_error);
@@ -763,7 +765,8 @@ static void wrapper_ostream_destroy(struct iostream_private *stream)
 
 static void wrapper_ostream_cork(struct ostream_private *stream, bool set)
 {
-       struct wrapper_ostream *wostream = (struct wrapper_ostream *)stream;
+       struct wrapper_ostream *wostream =
+               container_of(stream, struct wrapper_ostream, ostream);
        int ret;
 
        if (stream->ostream.closed || wostream->pending_errno != 0)
@@ -801,7 +804,8 @@ static ssize_t
 wrapper_ostream_sendv(struct ostream_private *stream,
                      const struct const_iovec *iov, unsigned int iov_count)
 {
-       struct wrapper_ostream *wostream = (struct wrapper_ostream *)stream;
+       struct wrapper_ostream *wostream =
+               container_of(stream, struct wrapper_ostream, ostream);
        bool must_uncork = FALSE;
        ssize_t sret;
 
@@ -842,7 +846,8 @@ wrapper_ostream_sendv(struct ostream_private *stream,
 
 static int wrapper_ostream_flush(struct ostream_private *stream)
 {
-       struct wrapper_ostream *wostream = (struct wrapper_ostream *)stream;
+       struct wrapper_ostream *wostream =
+               container_of(stream, struct wrapper_ostream, ostream);
        struct ostream *ostream = &stream->ostream;
        bool must_uncork = FALSE;
        int ret;
@@ -953,7 +958,8 @@ wrapper_ostream_set_flush_callback(struct ostream_private *stream,
                                   stream_flush_callback_t *callback,
                                   void *context)
 {
-       struct wrapper_ostream *wostream = (struct wrapper_ostream *)stream;
+       struct wrapper_ostream *wostream =
+               container_of(stream, struct wrapper_ostream, ostream);
 
        stream->callback = callback;
        stream->context = context;
@@ -973,7 +979,8 @@ wrapper_ostream_set_flush_callback(struct ostream_private *stream,
 static void
 wrapper_ostream_flush_pending(struct ostream_private *stream, bool set)
 {
-       struct wrapper_ostream *wostream = (struct wrapper_ostream *)stream;
+       struct wrapper_ostream *wostream =
+               container_of(stream, struct wrapper_ostream, ostream);
 
        wostream->flush_pending = set;
        if (!set)
@@ -991,7 +998,8 @@ wrapper_ostream_flush_pending(struct ostream_private *stream, bool set)
 static size_t
 wrapper_ostream_get_buffer_used_size(const struct ostream_private *stream)
 {
-       struct wrapper_ostream *wostream = (struct wrapper_ostream *)stream;
+       const struct wrapper_ostream *wostream =
+               container_of(stream, const struct wrapper_ostream, ostream);
        size_t size = 0;
 
        if (wostream->buffer != NULL)
@@ -1004,7 +1012,8 @@ wrapper_ostream_get_buffer_used_size(const struct ostream_private *stream)
 static size_t
 wrapper_ostream_get_buffer_avail_size(const struct ostream_private *stream)
 {
-       struct wrapper_ostream *wostream = (struct wrapper_ostream *)stream;
+       const struct wrapper_ostream *wostream =
+               container_of(stream, const struct wrapper_ostream, ostream);
        size_t size = 0;
 
        if (wostream->ostream.max_buffer_size == SIZE_MAX)
@@ -1027,7 +1036,8 @@ static void
 wrapper_ostream_switch_ioloop_to(struct ostream_private *stream,
                                 struct ioloop *ioloop)
 {
-       struct wrapper_ostream *wostream = (struct wrapper_ostream *)stream;
+       struct wrapper_ostream *wostream =
+               container_of(stream, struct wrapper_ostream, ostream);
 
        if (wostream->flush_ioloop != ioloop &&
            wostream->switch_ioloop_to != NULL)
index acfa527d1f29ded633bf16f772d2cc70e86db5c7..eab68937a4995a19a3339bdd2ea82f478c14b2b9 100644 (file)
@@ -523,7 +523,8 @@ void o_stream_switch_ioloop(struct ostream *stream)
 static void o_stream_default_close(struct iostream_private *stream,
                                   bool close_parent)
 {
-       struct ostream_private *_stream = (struct ostream_private *)stream;
+       struct ostream_private *_stream =
+               container_of(stream, struct ostream_private, iostream);
 
        (void)o_stream_flush(&_stream->ostream);
        if (close_parent)
@@ -532,7 +533,8 @@ static void o_stream_default_close(struct iostream_private *stream,
 
 static void o_stream_default_destroy(struct iostream_private *stream)
 {
-       struct ostream_private *_stream = (struct ostream_private *)stream;
+       struct ostream_private *_stream =
+               container_of(stream, struct ostream_private, iostream);
 
        o_stream_unref(&_stream->parent);
 }
@@ -541,7 +543,8 @@ static void
 o_stream_default_set_max_buffer_size(struct iostream_private *stream,
                                     size_t max_size)
 {
-       struct ostream_private *_stream = (struct ostream_private *)stream;
+       struct ostream_private *_stream =
+               container_of(stream, struct ostream_private, iostream);
 
        if (_stream->parent != NULL)
                o_stream_set_max_buffer_size(_stream->parent, max_size);