From patchwork Wed Aug 11 19:00:16 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Witten X-Patchwork-Id: 29435 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:8e8b:0:0:0:0:0 with SMTP id q133csp46979iod; Wed, 11 Aug 2021 12:03:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyx9AfdS1rMcyJKciIyAEZPFQIcWstlcaVUxJvSjazGVBsLGFNRMECPuqjBWEGMuPP68fbe X-Received: by 2002:a05:6402:1d30:: with SMTP id dh16mr467114edb.224.1628708603655; Wed, 11 Aug 2021 12:03:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628708603; cv=none; d=google.com; s=arc-20160816; b=NADUnb2dNUENYt5KRFG4TlITdMZR6yQN5uUGuvr3mmD707SmqxH3bk+4d4FSshWktj 3ElLT5XkvubjJoD1uHp1lgW2Yty9fAoQIsnUwKZeHiQl8BmXFZpJbesroOsEhglcbVoO WkzVputPfhHwY54j0IU6cjuDqc65JxcU31H4CuQiMZVwKnCGYRAP5KhfM8J6+YGbCrQV hPphKDbU2QdLor8IKu29RcBrBIQFw+vnAL0rr9JxugWQA68Lt7oJTpJiPvETNVNksxNR giOWKMkisB4DFdKB3Qwpp9svezLVQb9dcfUPoTziFeOfgOA5xzv3qDx72ZUQNZLNdTcP lpdQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:in-reply-to:message-id:to :from:date:dkim-signature:delivered-to; bh=tLKh+DwJ4a6LEkuyhohj1hbjgYl5hTNFNLxC8o8OE+c=; b=LaukRPd0/RkpKBV1kY5SjKguPHdyTlpg5xA4s9qJrNPCa/XJ1w4/MECFT1xTnoLrlr Fwye5jaBLWF8iaO4RsJ1/mkmyxW1n6IEVKgqLoPHBz0mncM7XdpKY2uMJ+gl1qmBdLxR EHNvaXLn09vn/DYGPfwikiN8O4BljAkxyBZlYFduEw8yMFsOexRxTOV+VPpw4wpPspQa OCQas1J8IqdN+rtw19KpDeQHO/MdnZRncGiOs06udyloMXjtbs9T6Xr4W/w/MVjqs8RE N0erPx4UmWNODfm1BvkpaJ8Z7OGSD3ne4eGhRh99+ql51zw62rh4Mm40TR0aHkcEqocN qQcA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=u5UCMgp0; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id p26si202221edu.78.2021.08.11.12.03.23; Wed, 11 Aug 2021 12:03:23 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=u5UCMgp0; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 257D968A64A; Wed, 11 Aug 2021 22:02:03 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f43.google.com (mail-wr1-f43.google.com [209.85.221.43]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id E907068A4CB for ; Wed, 11 Aug 2021 22:02:01 +0300 (EEST) Received: by mail-wr1-f43.google.com with SMTP id c9so4480488wri.8 for ; Wed, 11 Aug 2021 12:02:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:date:from:to:message-id:in-reply-to:references; bh=bFwTym3aYvEikMEGCBzEWNsj4wpex1kAe9mfYpcn4aw=; b=u5UCMgp0s0+4GbYcPBEaxNnP1tEkjXEYUescdOEoocYyiK51lOURrWMYje5/qgSdYo VMARTm36uqu1FVSVsGEfZqItvsWwQ8bbzslbhnc2lwZOSmASeYiIAlHO7Dw4PcFWeKog szri7Jdb1b7fsytltH3UK38z6ndJ9YQQGVpPgldJWOQ9zSj6ev7wmmQBf3/D4P1D9Iu5 233OzcyZzvcxxVwQZpH+qA9bROciQycxtibCpq9MsjxiDBQSWrPvDJl04Ed7MbVpqJof 8CtkK8q7n89btD2LTwCO8FjFZea8+qDi2RziSx9r/otOVC1YkBgcMaX9r+8S6d74fIUe HTJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:date:from:to:message-id:in-reply-to :references; bh=bFwTym3aYvEikMEGCBzEWNsj4wpex1kAe9mfYpcn4aw=; b=FaQzYD+lcfQqWPBxdq1LADQI0m0Sisgnd60Xtgxv4hOtJ584jTMAHpPkIeE4on+Va4 3sLKlmlFchUR3INBovzP5BKqqfX/b8u2fcAyy4joUOCYZWJq0LYFeHs9OgRJaqPHjnqV 7GFjvlMLSyYhnOavAwU8QvduhxHmJV45F2t6EPtLPVj5VJygwL4Dz4JXJxcZpmEzrWxM 0WRJolrcFmJGjvU/WVtjKFdjdDgHeYf9ccJ8apXE8tbHYcFloTHOEGdIbOIV5nbD9yTS C/a2Hl+7LMAl9rXRqVNv+3OzqrBd7aL2eS99Kmv22RsNVqbhBlhZzxJbUbQ8NE1AbKhu sJ9Q== X-Gm-Message-State: AOAM532l1Ln342He7LLhnC+k3o0gBroKUp9h3bvsaLonnCSZvrgMKYxC KvLbSDGIjkWVm7OYbkCvBUTFHz88AOAmjTTBzYlcWQ== X-Received: by 2002:a2e:9d47:: with SMTP id y7mr119119ljj.408.1628708508453; Wed, 11 Aug 2021 12:01:48 -0700 (PDT) Received: from gmail.com (tor-exit0-readme.dfri.se. [171.25.193.20]) by smtp.gmail.com with ESMTPSA id t21sm26761ljh.5.2021.08.11.12.01.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Aug 2021 12:01:47 -0700 (PDT) Date: Wed, 11 Aug 2021 19:00:16 -0000 From: Michael Witten To: ffmpeg-devel@ffmpeg.org Message-ID: In-Reply-To: References: Subject: [FFmpeg-devel] [PATCH 2.e/3] libavformat/protocols.c: avio_enum_protocols(): Create a macro for generating different loops X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: gNiHAqglFibG The function 'avio_enum_protocols()' iterates through the list of protocols, looking for a protocol that has a certain non-zero pointer-to-function; the exact pointer-to-function to use depends on the the argument passed through the parameter 'output'. * Before this commit, the parameter 'output' was being checked on every iteration, despite the fact that its value does not change. * This commit explicitly separates out the different cases into their own loops (i.e., their own functions), so that 'output' need not be tested on each iteration. * To aid maintenance, these separate functions are the expansion of a single macro that provides a generic implementation: AVIO_ENUM_PROTOCOLS(METHOD) One of the benefits of using a macro is that it does not depend on a compiler being able to optimize the code. Before deciding to use a macro for this purpose, it was attempted to write a generic version in terms of pure C code that also would not rely on magical optimizations; this involved type conversions on pointer arithmetic around 'offsetof()', but it turns out that Standard C (not to be confused with Actual C) has no reliable way to recover the value of a pointer-to-function from an object, so that avenue was ultimately abandoned for the sake of pure pedantry. --- libavformat/protocols.c | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/libavformat/protocols.c b/libavformat/protocols.c index 9ce98968fa..4cb8ae0b63 100644 --- a/libavformat/protocols.c +++ b/libavformat/protocols.c @@ -91,18 +91,35 @@ const AVClass *ff_urlcontext_child_class_iterate(void **iter) return ret; } -const char *avio_enum_protocols(void **const opaque, const int output) +#define AVIO_ENUM_PROTOCOLS(METHOD) \ + typedef const URLProtocol *const *Iterator; \ + for(Iterator p = *opaque ? (Iterator)(*opaque) + 1 : url_protocols; *p; ++p) { \ + if ((*p)->METHOD) { \ + *opaque = (void *)p; \ + return (*p)->name; \ + } \ + } \ + *opaque = NULL; \ + return NULL; + +static inline +const char *avio_enum_protocols_for_output(void **const opaque) { - typedef const URLProtocol *const *Iterator; - for(Iterator p = *opaque ? (Iterator)(*opaque) + 1 : url_protocols; *p; ++p) { - if ((output && (*p)->url_write) || (!output && (*p)->url_read)) { - *opaque = (void *)p; - return (*p)->name; - } - } + AVIO_ENUM_PROTOCOLS(url_write); +} - *opaque = NULL; - return NULL; +static inline +const char *avio_enum_protocols_for_input(void **const opaque) +{ + AVIO_ENUM_PROTOCOLS(url_read); +} + +const char *avio_enum_protocols(void **const opaque, const int output) +{ + if (output) + return avio_enum_protocols_for_output(opaque); + else + return avio_enum_protocols_for_input(opaque); } const AVClass *avio_protocol_get_class(const char *name)