[vlc-commits] http: use container_of()

Rémi Denis-Courmont git at videolan.org
Wed Jun 7 22:21:19 CEST 2017


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Wed Jun  7 23:03:48 2017 +0300| [368e104a9f62e138cfa004ddc9b676b554605e51] | committer: Rémi Denis-Courmont

http: use container_of()

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=368e104a9f62e138cfa004ddc9b676b554605e51
---

 modules/access/http/chunked.c |  8 ++++----
 modules/access/http/h1conn.c  |  8 +++-----
 modules/access/http/h2conn.c  | 17 ++++++++---------
 3 files changed, 15 insertions(+), 18 deletions(-)

diff --git a/modules/access/http/chunked.c b/modules/access/http/chunked.c
index ca20e632cf..54652f3f89 100644
--- a/modules/access/http/chunked.c
+++ b/modules/access/http/chunked.c
@@ -46,8 +46,6 @@ struct vlc_chunked_stream
     bool error;
 };
 
-static_assert(offsetof(struct vlc_chunked_stream, stream) == 0, "Cast error");
-
 static void *vlc_chunked_fatal(struct vlc_chunked_stream *s)
 {
     s->error = true;
@@ -64,7 +62,8 @@ static struct vlc_http_msg *vlc_chunked_wait(struct vlc_http_stream *stream)
 
 static block_t *vlc_chunked_read(struct vlc_http_stream *stream)
 {
-    struct vlc_chunked_stream *s = (struct vlc_chunked_stream *)stream;
+    struct vlc_chunked_stream *s =
+        container_of(stream, struct vlc_chunked_stream, stream);
     block_t *block = NULL;
 
     if (s->eof)
@@ -134,7 +133,8 @@ static block_t *vlc_chunked_read(struct vlc_http_stream *stream)
 
 static void vlc_chunked_close(struct vlc_http_stream *stream, bool abort)
 {
-    struct vlc_chunked_stream *s = (struct vlc_chunked_stream *)stream;
+    struct vlc_chunked_stream *s =
+        container_of(stream, struct vlc_chunked_stream, stream);
 
     if (!s->eof) /* Abort connection if stream is closed before end */
         vlc_chunked_fatal(s);
diff --git a/modules/access/http/h1conn.c b/modules/access/http/h1conn.c
index 963715f358..2fdd26614e 100644
--- a/modules/access/http/h1conn.c
+++ b/modules/access/http/h1conn.c
@@ -135,17 +135,15 @@ static void *vlc_h1_stream_fatal(struct vlc_h1_conn *conn)
     return NULL;
 }
 
-static_assert(offsetof(struct vlc_h1_conn, conn) == 0, "Cast error");
-
 static struct vlc_h1_conn *vlc_h1_stream_conn(struct vlc_http_stream *stream)
 {
-    return (void *)(((char *)stream) - offsetof(struct vlc_h1_conn, stream));
+    return container_of(stream, struct vlc_h1_conn, stream);
 }
 
 static struct vlc_http_stream *vlc_h1_stream_open(struct vlc_http_conn *c,
                                                 const struct vlc_http_msg *req)
 {
-    struct vlc_h1_conn *conn = (struct vlc_h1_conn *)c;
+    struct vlc_h1_conn *conn = container_of(c, struct vlc_h1_conn, conn);
     size_t len;
     ssize_t val;
 
@@ -307,7 +305,7 @@ static void vlc_h1_conn_destroy(struct vlc_h1_conn *conn)
 
 static void vlc_h1_conn_release(struct vlc_http_conn *c)
 {
-    struct vlc_h1_conn *conn = (struct vlc_h1_conn *)c;
+    struct vlc_h1_conn *conn = container_of(c, struct vlc_h1_conn, conn);
 
     assert(!conn->released);
     conn->released = true;
diff --git a/modules/access/http/h2conn.c b/modules/access/http/h2conn.c
index f7e7b88e80..fb66b8ab24 100644
--- a/modules/access/http/h2conn.c
+++ b/modules/access/http/h2conn.c
@@ -60,8 +60,6 @@ struct vlc_h2_conn
     vlc_thread_t thread; /**< Receive thread */
 };
 
-static_assert(offsetof(struct vlc_h2_conn, conn) == 0, "Cast error");
-
 static void vlc_h2_conn_destroy(struct vlc_h2_conn *conn);
 
 /** HTTP/2 stream */
@@ -237,11 +235,10 @@ static int vlc_h2_stream_unlock(struct vlc_h2_stream *s)
     return vlc_interrupt_unregister();
 }
 
-static_assert(offsetof(struct vlc_h2_stream, stream) == 0, "Cast error");
-
 static struct vlc_http_msg *vlc_h2_stream_wait(struct vlc_http_stream *stream)
 {
-    struct vlc_h2_stream *s = (struct vlc_h2_stream *)stream;
+    struct vlc_h2_stream *s =
+        container_of(stream, struct vlc_h2_stream, stream);
     struct vlc_h2_conn *conn = s->conn;
     struct vlc_http_msg *m;
 
@@ -272,7 +269,8 @@ static struct vlc_http_msg *vlc_h2_stream_wait(struct vlc_http_stream *stream)
  */
 static block_t *vlc_h2_stream_read(struct vlc_http_stream *stream)
 {
-    struct vlc_h2_stream *s = (struct vlc_h2_stream *)stream;
+    struct vlc_h2_stream *s =
+        container_of(stream, struct vlc_h2_stream, stream);
     struct vlc_h2_conn *conn = s->conn;
     struct vlc_h2_frame *f;
 
@@ -339,7 +337,8 @@ static block_t *vlc_h2_stream_read(struct vlc_http_stream *stream)
  */
 static void vlc_h2_stream_close(struct vlc_http_stream *stream, bool aborted)
 {
-    struct vlc_h2_stream *s = (struct vlc_h2_stream *)stream;
+    struct vlc_h2_stream *s =
+        container_of(stream, struct vlc_h2_stream, stream);
     struct vlc_h2_conn *conn = s->conn;
     bool destroy = false;
     uint_fast32_t code = VLC_H2_NO_ERROR;
@@ -401,7 +400,7 @@ static const struct vlc_http_stream_cbs vlc_h2_stream_callbacks =
 static struct vlc_http_stream *vlc_h2_stream_open(struct vlc_http_conn *c,
                                                 const struct vlc_http_msg *msg)
 {
-    struct vlc_h2_conn *conn = (struct vlc_h2_conn *)c;
+    struct vlc_h2_conn *conn = container_of(c, struct vlc_h2_conn, conn);
     struct vlc_h2_stream *s = malloc(sizeof (*s));
     if (unlikely(s == NULL))
         return NULL;
@@ -697,7 +696,7 @@ static void vlc_h2_conn_destroy(struct vlc_h2_conn *conn)
 
 static void vlc_h2_conn_release(struct vlc_http_conn *c)
 {
-    struct vlc_h2_conn *conn = (struct vlc_h2_conn *)c;
+    struct vlc_h2_conn *conn = container_of(c, struct vlc_h2_conn, conn);
     bool destroy;
 
     vlc_mutex_lock(&conn->lock);



More information about the vlc-commits mailing list