[FFmpeg-cvslog] proto: Use .priv_data_size to allocate the private context

Martin Storsjö git at videolan.org
Fri Dec 2 01:00:53 CET 2011


ffmpeg | branch: master | Martin Storsjö <martin at martin.st> | Thu Dec  1 11:44:21 2011 +0200| [7e58050590c556643869a1cc57215026ff88b0db] | committer: Martin Storsjö

proto: Use .priv_data_size to allocate the private context

This simplifies the open functions by avoiding one function
call that needs error checking, reducing the amount of
extra bulk code.

Signed-off-by: Martin Storsjö <martin at martin.st>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=7e58050590c556643869a1cc57215026ff88b0db
---

 libavformat/applehttpproto.c |    8 ++------
 libavformat/concat.c         |   10 ++--------
 libavformat/gopher.c         |   10 ++--------
 libavformat/librtmp.c        |   24 +++++++++++-------------
 libavformat/mmsh.c           |    7 ++-----
 libavformat/mmst.c           |    7 ++-----
 libavformat/rtmpproto.c      |    8 ++------
 libavformat/rtpproto.c       |   10 ++--------
 libavformat/tcp.c            |   10 ++--------
 libavformat/udp.c            |   10 ++--------
 10 files changed, 29 insertions(+), 75 deletions(-)

diff --git a/libavformat/applehttpproto.c b/libavformat/applehttpproto.c
index 8dbcf2d..245422b 100644
--- a/libavformat/applehttpproto.c
+++ b/libavformat/applehttpproto.c
@@ -181,23 +181,18 @@ static int applehttp_close(URLContext *h)
     free_segment_list(s);
     free_variant_list(s);
     ffurl_close(s->seg_hd);
-    av_free(s);
     return 0;
 }
 
 static int applehttp_open(URLContext *h, const char *uri, int flags)
 {
-    AppleHTTPContext *s;
+    AppleHTTPContext *s = h->priv_data;
     int ret, i;
     const char *nested_url;
 
     if (flags & AVIO_FLAG_WRITE)
         return AVERROR(ENOSYS);
 
-    s = av_mallocz(sizeof(AppleHTTPContext));
-    if (!s)
-        return AVERROR(ENOMEM);
-    h->priv_data = s;
     h->is_streamed = 1;
 
     if (av_strstart(uri, "applehttp+", &nested_url)) {
@@ -304,4 +299,5 @@ URLProtocol ff_applehttp_protocol = {
     .url_read  = applehttp_read,
     .url_close = applehttp_close,
     .flags     = URL_PROTOCOL_FLAG_NESTED_SCHEME,
+    .priv_data_size = sizeof(AppleHTTPContext),
 };
diff --git a/libavformat/concat.c b/libavformat/concat.c
index 1501cbc..4bfa628 100644
--- a/libavformat/concat.c
+++ b/libavformat/concat.c
@@ -50,7 +50,6 @@ static av_cold int concat_close(URLContext *h)
         err |= ffurl_close(nodes[i].uc);
 
     av_freep(&data->nodes);
-    av_freep(&h->priv_data);
 
     return err < 0 ? -1 : 0;
 }
@@ -62,16 +61,11 @@ static av_cold int concat_open(URLContext *h, const char *uri, int flags)
     int64_t size;
     size_t  len, i;
     URLContext *uc;
-    struct concat_data  *data;
+    struct concat_data  *data = h->priv_data;
     struct concat_nodes *nodes;
 
     av_strstart(uri, "concat:", &uri);
 
-    /* creating data */
-    if (!(data = av_mallocz(sizeof(*data))))
-        return AVERROR(ENOMEM);
-    h->priv_data = data;
-
     for (i = 0, len = 1; uri[i]; i++)
         if (uri[i] == *AV_CAT_SEPARATOR)
             /* integer overflow */
@@ -81,7 +75,6 @@ static av_cold int concat_open(URLContext *h, const char *uri, int flags)
             }
 
     if (!(nodes = av_malloc(sizeof(*nodes) * len))) {
-        av_freep(&h->priv_data);
         return AVERROR(ENOMEM);
     } else
         data->nodes = nodes;
@@ -196,4 +189,5 @@ URLProtocol ff_concat_protocol = {
     .url_read  = concat_read,
     .url_seek  = concat_seek,
     .url_close = concat_close,
+    .priv_data_size = sizeof(struct concat_data),
 };
diff --git a/libavformat/gopher.c b/libavformat/gopher.c
index 5f8c320..0543878 100644
--- a/libavformat/gopher.c
+++ b/libavformat/gopher.c
@@ -72,24 +72,17 @@ static int gopher_close(URLContext *h)
         ffurl_close(s->hd);
         s->hd = NULL;
     }
-    av_freep(&h->priv_data);
     return 0;
 }
 
 static int gopher_open(URLContext *h, const char *uri, int flags)
 {
-    GopherContext *s;
+    GopherContext *s = h->priv_data;
     char hostname[1024], auth[1024], path[1024], buf[1024];
     int port, err;
 
     h->is_streamed = 1;
 
-    s = av_malloc(sizeof(GopherContext));
-    if (!s) {
-        return AVERROR(ENOMEM);
-    }
-    h->priv_data = s;
-
     /* needed in any case to build the host string */
     av_url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
                  path, sizeof(path), uri);
@@ -127,4 +120,5 @@ URLProtocol ff_gopher_protocol = {
     .url_read  = gopher_read,
     .url_write = gopher_write,
     .url_close = gopher_close,
+    .priv_data_size = sizeof(GopherContext),
 };
diff --git a/libavformat/librtmp.c b/libavformat/librtmp.c
index 6ccb66b..c04c833 100644
--- a/libavformat/librtmp.c
+++ b/libavformat/librtmp.c
@@ -52,7 +52,6 @@ static int rtmp_close(URLContext *s)
     RTMP *r = s->priv_data;
 
     RTMP_Close(r);
-    av_free(r);
     return 0;
 }
 
@@ -70,13 +69,9 @@ static int rtmp_close(URLContext *s)
  */
 static int rtmp_open(URLContext *s, const char *uri, int flags)
 {
-    RTMP *r;
+    RTMP *r = s->priv_data;
     int rc;
 
-    r = av_mallocz(sizeof(RTMP));
-    if (!r)
-        return AVERROR(ENOMEM);
-
     switch (av_log_get_level()) {
     default:
     case AV_LOG_FATAL:   rc = RTMP_LOGCRIT;    break;
@@ -103,11 +98,9 @@ static int rtmp_open(URLContext *s, const char *uri, int flags)
         goto fail;
     }
 
-    s->priv_data   = r;
     s->is_streamed = 1;
     return 0;
 fail:
-    av_free(r);
     return rc;
 }
 
@@ -167,7 +160,8 @@ URLProtocol ff_rtmp_protocol = {
     .url_close           = rtmp_close,
     .url_read_pause      = rtmp_read_pause,
     .url_read_seek       = rtmp_read_seek,
-    .url_get_file_handle = rtmp_get_file_handle
+    .url_get_file_handle = rtmp_get_file_handle,
+    .priv_data_size      = sizeof(RTMP),
 };
 
 URLProtocol ff_rtmpt_protocol = {
@@ -178,7 +172,8 @@ URLProtocol ff_rtmpt_protocol = {
     .url_close           = rtmp_close,
     .url_read_pause      = rtmp_read_pause,
     .url_read_seek       = rtmp_read_seek,
-    .url_get_file_handle = rtmp_get_file_handle
+    .url_get_file_handle = rtmp_get_file_handle,
+    .priv_data_size      = sizeof(RTMP),
 };
 
 URLProtocol ff_rtmpe_protocol = {
@@ -189,7 +184,8 @@ URLProtocol ff_rtmpe_protocol = {
     .url_close           = rtmp_close,
     .url_read_pause      = rtmp_read_pause,
     .url_read_seek       = rtmp_read_seek,
-    .url_get_file_handle = rtmp_get_file_handle
+    .url_get_file_handle = rtmp_get_file_handle,
+    .priv_data_size      = sizeof(RTMP),
 };
 
 URLProtocol ff_rtmpte_protocol = {
@@ -200,7 +196,8 @@ URLProtocol ff_rtmpte_protocol = {
     .url_close           = rtmp_close,
     .url_read_pause      = rtmp_read_pause,
     .url_read_seek       = rtmp_read_seek,
-    .url_get_file_handle = rtmp_get_file_handle
+    .url_get_file_handle = rtmp_get_file_handle,
+    .priv_data_size      = sizeof(RTMP),
 };
 
 URLProtocol ff_rtmps_protocol = {
@@ -211,5 +208,6 @@ URLProtocol ff_rtmps_protocol = {
     .url_close           = rtmp_close,
     .url_read_pause      = rtmp_read_pause,
     .url_read_seek       = rtmp_read_seek,
-    .url_get_file_handle = rtmp_get_file_handle
+    .url_get_file_handle = rtmp_get_file_handle,
+    .priv_data_size      = sizeof(RTMP),
 };
diff --git a/libavformat/mmsh.c b/libavformat/mmsh.c
index 3761016..a1ae894 100644
--- a/libavformat/mmsh.c
+++ b/libavformat/mmsh.c
@@ -68,7 +68,6 @@ static int mmsh_close(URLContext *h)
         ffurl_close(mms->mms_hd);
     av_free(mms->streams);
     av_free(mms->asf_header);
-    av_freep(&h->priv_data);
     return 0;
 }
 
@@ -217,12 +216,9 @@ static int mmsh_open(URLContext *h, const char *uri, int flags)
     char httpname[256], path[256], host[128], location[1024];
     char *stream_selection = NULL;
     char headers[1024];
-    MMSHContext *mmsh;
+    MMSHContext *mmsh = h->priv_data;
     MMSContext *mms;
 
-    mmsh = h->priv_data = av_mallocz(sizeof(MMSHContext));
-    if (!h->priv_data)
-        return AVERROR(ENOMEM);
     mmsh->request_seq = h->is_streamed = 1;
     mms = &mmsh->mms;
     av_strlcpy(location, uri, sizeof(location));
@@ -367,4 +363,5 @@ URLProtocol ff_mmsh_protocol = {
     .url_open  = mmsh_open,
     .url_read  = mmsh_read,
     .url_close = mmsh_close,
+    .priv_data_size = sizeof(MMSHContext),
 };
diff --git a/libavformat/mmst.c b/libavformat/mmst.c
index 7bb4358..9d7fd74 100644
--- a/libavformat/mmst.c
+++ b/libavformat/mmst.c
@@ -470,7 +470,6 @@ static int mms_close(URLContext *h)
     /* free all separately allocated pointers in mms */
     av_free(mms->streams);
     av_free(mms->asf_header);
-    av_freep(&h->priv_data);
 
     return 0;
 }
@@ -502,15 +501,12 @@ static void clear_stream_buffers(MMSContext *mms)
 
 static int mms_open(URLContext *h, const char *uri, int flags)
 {
-    MMSTContext *mmst;
+    MMSTContext *mmst = h->priv_data;
     MMSContext *mms;
     int port, err;
     char tcpname[256];
 
     h->is_streamed = 1;
-    mmst = h->priv_data = av_mallocz(sizeof(MMSTContext));
-    if (!h->priv_data)
-        return AVERROR(ENOMEM);
     mms = &mmst->mms;
 
     // only for MMS over TCP, so set proto = NULL
@@ -628,4 +624,5 @@ URLProtocol ff_mmst_protocol = {
     .url_open  = mms_open,
     .url_read  = mms_read,
     .url_close = mms_close,
+    .priv_data_size = sizeof(MMSTContext),
 };
diff --git a/libavformat/rtmpproto.c b/libavformat/rtmpproto.c
index 979c5b6..f477338 100644
--- a/libavformat/rtmpproto.c
+++ b/libavformat/rtmpproto.c
@@ -784,7 +784,6 @@ static int rtmp_close(URLContext *h)
 
     av_freep(&rt->flv_data);
     ffurl_close(rt->stream);
-    av_free(rt);
     return 0;
 }
 
@@ -799,16 +798,12 @@ static int rtmp_close(URLContext *h)
  */
 static int rtmp_open(URLContext *s, const char *uri, int flags)
 {
-    RTMPContext *rt;
+    RTMPContext *rt = s->priv_data;
     char proto[8], hostname[256], path[1024], *fname;
     uint8_t buf[2048];
     int port;
     int ret;
 
-    rt = av_mallocz(sizeof(RTMPContext));
-    if (!rt)
-        return AVERROR(ENOMEM);
-    s->priv_data = rt;
     rt->is_input = !(flags & AVIO_FLAG_WRITE);
 
     av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname), &port,
@@ -1002,4 +997,5 @@ URLProtocol ff_rtmp_protocol = {
     .url_read  = rtmp_read,
     .url_write = rtmp_write,
     .url_close = rtmp_close,
+    .priv_data_size = sizeof(RTMPContext),
 };
diff --git a/libavformat/rtpproto.c b/libavformat/rtpproto.c
index 9707bdc..93cad3c 100644
--- a/libavformat/rtpproto.c
+++ b/libavformat/rtpproto.c
@@ -136,7 +136,7 @@ static void build_udp_url(char *buf, int buf_size,
 
 static int rtp_open(URLContext *h, const char *uri, int flags)
 {
-    RTPContext *s;
+    RTPContext *s = h->priv_data;
     int rtp_port, rtcp_port,
         ttl, connect,
         local_rtp_port, local_rtcp_port, max_packet_size;
@@ -145,11 +145,6 @@ static int rtp_open(URLContext *h, const char *uri, int flags)
     char path[1024];
     const char *p;
 
-    s = av_mallocz(sizeof(RTPContext));
-    if (!s)
-        return AVERROR(ENOMEM);
-    h->priv_data = s;
-
     av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &rtp_port,
                  path, sizeof(path), uri);
     /* extract parameters */
@@ -213,7 +208,6 @@ static int rtp_open(URLContext *h, const char *uri, int flags)
         ffurl_close(s->rtp_hd);
     if (s->rtcp_hd)
         ffurl_close(s->rtcp_hd);
-    av_free(s);
     return AVERROR(EIO);
 }
 
@@ -290,7 +284,6 @@ static int rtp_close(URLContext *h)
 
     ffurl_close(s->rtp_hd);
     ffurl_close(s->rtcp_hd);
-    av_free(s);
     return 0;
 }
 
@@ -336,4 +329,5 @@ URLProtocol ff_rtp_protocol = {
     .url_write           = rtp_write,
     .url_close           = rtp_close,
     .url_get_file_handle = rtp_get_file_handle,
+    .priv_data_size      = sizeof(RTPContext),
 };
diff --git a/libavformat/tcp.c b/libavformat/tcp.c
index fda34a3..0d3aeaf 100644
--- a/libavformat/tcp.c
+++ b/libavformat/tcp.c
@@ -39,7 +39,7 @@ static int tcp_open(URLContext *h, const char *uri, int flags)
 {
     struct addrinfo hints, *ai, *cur_ai;
     int port, fd = -1;
-    TCPContext *s = NULL;
+    TCPContext *s = h->priv_data;
     int listen_socket = 0;
     const char *p;
     char buf[256];
@@ -135,12 +135,6 @@ static int tcp_open(URLContext *h, const char *uri, int flags)
             goto fail;
         }
     }
-    s = av_malloc(sizeof(TCPContext));
-    if (!s) {
-        freeaddrinfo(ai);
-        return AVERROR(ENOMEM);
-    }
-    h->priv_data = s;
     h->is_streamed = 1;
     s->fd = fd;
     freeaddrinfo(ai);
@@ -193,7 +187,6 @@ static int tcp_close(URLContext *h)
 {
     TCPContext *s = h->priv_data;
     closesocket(s->fd);
-    av_free(s);
     return 0;
 }
 
@@ -210,4 +203,5 @@ URLProtocol ff_tcp_protocol = {
     .url_write           = tcp_write,
     .url_close           = tcp_close,
     .url_get_file_handle = tcp_get_file_handle,
+    .priv_data_size      = sizeof(TCPContext),
 };
diff --git a/libavformat/udp.c b/libavformat/udp.c
index 178cef4..529114a 100644
--- a/libavformat/udp.c
+++ b/libavformat/udp.c
@@ -306,7 +306,7 @@ static int udp_open(URLContext *h, const char *uri, int flags)
 {
     char hostname[1024], localaddr[1024] = "";
     int port, udp_fd = -1, tmp, bind_ret = -1;
-    UDPContext *s = NULL;
+    UDPContext *s = h->priv_data;
     int is_output;
     const char *p;
     char buf[256];
@@ -319,11 +319,6 @@ static int udp_open(URLContext *h, const char *uri, int flags)
 
     is_output = !(flags & AVIO_FLAG_READ);
 
-    s = av_mallocz(sizeof(UDPContext));
-    if (!s)
-        return AVERROR(ENOMEM);
-
-    h->priv_data = s;
     s->ttl = 16;
     s->buffer_size = is_output ? UDP_TX_BUF_SIZE : UDP_MAX_PKT_SIZE;
 
@@ -440,7 +435,6 @@ static int udp_open(URLContext *h, const char *uri, int flags)
  fail:
     if (udp_fd >= 0)
         closesocket(udp_fd);
-    av_free(s);
     return AVERROR(EIO);
 }
 
@@ -486,7 +480,6 @@ static int udp_close(URLContext *h)
     if (s->is_multicast && (h->flags & AVIO_FLAG_READ))
         udp_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr);
     closesocket(s->udp_fd);
-    av_free(s);
     return 0;
 }
 
@@ -497,4 +490,5 @@ URLProtocol ff_udp_protocol = {
     .url_write           = udp_write,
     .url_close           = udp_close,
     .url_get_file_handle = udp_get_file_handle,
+    .priv_data_size      = sizeof(UDPContext),
 };



More information about the ffmpeg-cvslog mailing list