From patchwork Wed Aug 11 19:00:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Witten X-Patchwork-Id: 29437 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:8e8b:0:0:0:0:0 with SMTP id q133csp44403iod; Wed, 11 Aug 2021 12:00:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyTI2EQMFXGD7YXqvsfAaGsVZm0PuaeTGpmGjiRf7HsZCFi6D7PaggHlL8VHiYXlWrap8xG X-Received: by 2002:a17:906:f2c9:: with SMTP id gz9mr2827306ejb.41.1628708426152; Wed, 11 Aug 2021 12:00:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628708426; cv=none; d=google.com; s=arc-20160816; b=S00bI0GVlHhmjvVP3ZFGCk0XKsd9nmz8z6U6yuFJWfcTIBwCz4HPnZl6WP4osEOggE mFiJ3v4vcy/OkOmQoClrUFfpOX4VS2XzjO3sKZUqhOuTe5MLmDpSL1eAARpv7CFlzFRS fLj0eJxaD/bikHZ4VfSC8QA75Gi+MkkWaMqojOJo5sGwj8XsLZSHHU65sly0lQqeUEYA n0qD78+YEZVAz8+koRDKdi50T8rBNAcEdKeULLMEScKZYsXXikO1CEtN6CgV1D8ZZ63m rzfhvx0ORWuJhTXD6CwAzsEMxLR7B+rk5PKAQTNXN5ty0CK33/gBwqoGlL7ADyET2/Ka cAAQ== 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:message-id:to:from:date:dkim-signature :delivered-to; bh=kNWbEvrCx6cgLQL7P0wOlFPfvEJQq9V6Y0VKwdLPusM=; b=kDM1eVL547UvJbarhqrtVOqcl/chWsnAzoUZTuB5jQJhkTqaaayjTjoJaQ5Qn/F4Dp jX2oWnERmQrnlyvbCt3OhqFW1csGLbvV3dTMlLfaTcsgeHGPnyFGJkN9rDmaNef0Berq UVpKfsSeRd53zREAfK0PiPciKM+pRDZdOT4BD15dQkbdaTayqqmz+ejfV+xH+KMAOeaR DOHMLT2ZviXwsgPeIZk//fckH4h6hg58hL0Wsk/rGhLtoOZrsvW+FqbA9cFHghi5OCd1 BzBlV4UvfKU7i1zR824q6eil279GmXTrTBoomEGdMGoMKMW8Y/H5vNNcR44M5r2XGE7h e1Jg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=pz53nKaH; 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 s18si26661080ejh.472.2021.08.11.12.00.25; Wed, 11 Aug 2021 12:00:26 -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=pz53nKaH; 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 C8F9568A305; Wed, 11 Aug 2021 22:00:20 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ed1-f46.google.com (mail-ed1-f46.google.com [209.85.208.46]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 93FB3687F0D for ; Wed, 11 Aug 2021 22:00:14 +0300 (EEST) Received: by mail-ed1-f46.google.com with SMTP id bo19so5254830edb.9 for ; Wed, 11 Aug 2021 12:00:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:date:from:to:message-id; bh=qiRdAtJzYUOm08DepzeFQHOhXKK6oMPvZuA3gFWNrk4=; b=pz53nKaHOX5DcAEux9RDicAg7wEUEaztUKDqHaxfliO+DoNATd5kVeBC4HSF7r1G+S 09vNxgjUFK2sIdN88l7UFHWOVtgkS6pkZklGlTJZY3vI/dByUYMYcgoc0FZEAUKRdZSr TASWtWHmpakCNNcKStppLPnYCNlVbuW7TvqaPoSgeBPF0EOI5HIm0zh9fhvnNNfmcFb4 VlkrYyHp4KVz5TnL1W8BFvI8TV2ad/yxxmn21PXocvhZrNshV5Bz+VCbUbnqgi7w0Zng 0oxwzXtDvpyI7PhP4qH0hhkUrpDo1DSteZ1LCJ0qzjXCbmxXG1fXp64LDswFa/XhNbaN D0GA== 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; bh=qiRdAtJzYUOm08DepzeFQHOhXKK6oMPvZuA3gFWNrk4=; b=Gb2D/VuLdbY556TH4UcqD7o3a2RE7YKVj1Pr640+JtCyBonMXgP0jPky+GVI0pQ62c zPpHIMHtc+mgJeCn4ak+PbzTzM1cJZ5jI61Wyf3w7sCRBP13qBY51qI9aPkKDexiZqEl RKleNTvIPst1lu9/UVTPibJb21O3U9f8iMUN7x41CoSaYkKN51tI1HWlQrt9PlRpBQKr uqlOMqVrWc2nCwdUBvdOt7rL414mSEtKQSnB7v1/fD7ItUCL0Iw3nL29e7ZaTguRjO28 U+Ryx0Fa9V3Twl2yPLUhHCespkGtOMa7Entao8dZWQDxWGD3zhuwMBrWwxJb4NwkySei 2ENg== X-Gm-Message-State: AOAM533fXeotPfFgm4HC2FL4bUf3BvLlAjCCAlOMpVbfOEXfCJ0YMJIz V2kcJoF8KctNRkP93KKd9eH0W1lYX7+HLzmX0zypGA== X-Received: by 2002:a05:6402:94b:: with SMTP id h11mr467845edz.76.1628708413534; Wed, 11 Aug 2021 12:00:13 -0700 (PDT) Received: from gmail.com (tor-exit0-readme.dfri.se. [171.25.193.20]) by smtp.gmail.com with ESMTPSA id z8sm84780ejd.94.2021.08.11.12.00.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Aug 2021 12:00:13 -0700 (PDT) Date: Wed, 11 Aug 2021 19:00:00 -0000 From: Michael Witten To: ffmpeg-devel@ffmpeg.org Message-ID: Subject: [FFmpeg-devel] [PATCH 0/3] libavformat/protocols.c: avio_enum_protocols(): Cleanup 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: cCCOaB/GUUFZ This series improves the following function: libavformat/protocols.c: avio_enum_protocols() There are really only 3 logical commits: [1] Add const-correctness (fixes a compile-time warning) {2} Refactoring (cleanup) [3] Add functions to the API (use them too) However, {2} is presented as a bunch of tiny little transformations that are intended to aid comprehension; they can be squashed into one commit as the maintainer sees fit (indeed, as shown below, the squashed diff is already quite comprehensible): {2} Refactoring [2.0] Add more const-correctness [2.1] Split declaration and initialization [2.2] Convert recursion to iteration [2.3] Move 'iterate' label [2.4] Consolidate initialization of 'p' [2.5] Add block curly braces to 'if' statement [2.6] Move assignment to '*opaque' [2.7] Indent code [2.8] Make the 'else' logic explicit [2.9] Reverse the conditional [2.a] Move branch to bottom of function [2.b] Convert the 'goto' loop to a 'for(;;)' block [2.c] Move the loop variables [2.d] Move loop initialization [2.e] Create a macro for generating different loops For your convenience, this email itself provides an example squashing of those commits; to apply it, do something like the following: $ git am /path/to/patch-1.eml # Patch [1] $ git am --scissors /path/to/this-email # This email's Patch {2} $ git am /path/to/patch-3.eml # Patch [3] Alternatively, you can have the best of both worlds by means of a merge: $ git am /path/to/patch-1.eml # Patch [1] $ patch_1=$(git rev-parse HEAD) $ git am /path/to/patch-2.*.eml # Patches [2.*] $ git reset --hard "$patch_1" $ git merge --no-ff -m \ 'libavformat/protocols.c: avio_enum_protocols(): Refactor' \ ORIG_HEAD $ git am /path/to/patch-3.eml # Patch [3] That will produce a very nice history: $ git log --oneline --graph Here is the overall change for the entire series: fftools/cmdutils.c | 4 ++-- libavformat/avio.h | 24 +++++++++++++++++++++++- libavformat/protocols.c | 36 +++++++++++++++++++++++++----------- 3 files changed, 50 insertions(+), 14 deletions(-) Sincerely, Michael Witten --8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<-- Subject: libavformat/protocols.c: avio_enum_protocols(): Refactor Date: Wed, 11 Aug 2021 19:00:09 -0000 This commit is the result of squashing a series of very tiny transformations; it refactors 'avio_enum_protocols()' into 2 functions: * avio_enum_protocols_for_input() * avio_enum_protocols_for_output() Those functions are in turn mostly implemented by this macro: * AVIO_ENUM_PROTOCOLS() The goal of this refactoring was the following: * To make the code more immediately understandable. * To reduce the potential for redundant computation. diff --git a/libavformat/avio.h b/libavformat/avio.h index 0b35409787..3b92cf742a 100644 --- a/libavformat/avio.h +++ b/libavformat/avio.h @@ -786,7 +786,7 @@ int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer); * * @return A static string containing the name of current protocol or NULL */ -const char *avio_enum_protocols(void **opaque, int output); +const char *avio_enum_protocols(void **const opaque, const int output); /** * Get AVClass by names of available protocols. diff --git a/libavformat/protocols.c b/libavformat/protocols.c index e0b3405ab8..4cb8ae0b63 100644 --- a/libavformat/protocols.c +++ b/libavformat/protocols.c @@ -91,19 +91,35 @@ const AVClass *ff_urlcontext_child_class_iterate(void **iter) return ret; } -const char *avio_enum_protocols(void **opaque, 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) { - const URLProtocol *const *p = *opaque; + AVIO_ENUM_PROTOCOLS(url_write); +} - p = p ? p + 1 : url_protocols; - *opaque = (void *)p; - if (!*p) { - *opaque = NULL; - return NULL; - } - if ((output && (*p)->url_write) || (!output && (*p)->url_read)) - return (*p)->name; - return avio_enum_protocols(opaque, output); +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)