[{"id":16373,"web_url":"https://patchwork.libcamera.org/comment/16373/","msgid":"<YH4rPezjt7GmdDUI@pendragon.ideasonboard.com>","date":"2021-04-20T01:15:41","subject":"Re: [libcamera-devel] [PATCH v2 2/3] libcamera: V4L2Device: Use\n\tstd::vector for v4l2_ext_control in setControls()","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Hiro,\n\nThank you for the patch.\n\nOn Thu, Apr 15, 2021 at 01:48:53PM +0900, Hirokazu Honda wrote:\n> The original code uses Variable-Length-Array, which is not\n> officially supported in C++. This replaces the array with\n> std::vector.\n> \n> Signed-off-by: Hirokazu Honda <hiroh@chromium.org>\n> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n> ---\n>  src/libcamera/v4l2_device.cpp | 32 +++++++++++++++-----------------\n>  1 file changed, 15 insertions(+), 17 deletions(-)\n> \n> diff --git a/src/libcamera/v4l2_device.cpp b/src/libcamera/v4l2_device.cpp\n> index ee8c3fed..5ec1b10e 100644\n> --- a/src/libcamera/v4l2_device.cpp\n> +++ b/src/libcamera/v4l2_device.cpp\n> @@ -279,12 +279,11 @@ ControlList V4L2Device::getControls(const std::vector<uint32_t> &ids)\n>   */\n>  int V4L2Device::setControls(ControlList *ctrls)\n>  {\n> -\tunsigned int count = ctrls->size();\n> -\tif (count == 0)\n> +\tif (ctrls->empty())\n>  \t\treturn 0;\n>  \n> -\tstruct v4l2_ext_control v4l2Ctrls[count];\n> -\tmemset(v4l2Ctrls, 0, sizeof(v4l2Ctrls));\n> +\tstd::vector<v4l2_ext_control> v4l2Ctrls(ctrls->size());\n> +\tmemset(v4l2Ctrls.data(), 0, v4l2Ctrls.size());\n\nIs the memset() needed ? The std::vector constructor will default-insert\nthe elements, which value-initializes them. If my interpretation of\nhttps://en.cppreference.com/w/cpp/language/value_initialization is\ncorrect, the elements will be zero-initialized.\n\n>  \n>  \tunsigned int i = 0;\n\nShould we replace i with\n\n\tv4l2_ext_control *v4l2Ctrl = v4l2Ctrls.data();\n\nand do a\n\n\tv4l2Ctrl++;\n\nat the end of the loop ? That way we would ensure that i won't be used\nincorrectly (after being incremented) inside the loop.\n\nApart from that,\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\n>  \tfor (auto &ctrl : *ctrls) {\n> @@ -295,14 +294,14 @@ int V4L2Device::setControls(ControlList *ctrls)\n>  \t\t\t\t<< \"Control \" << utils::hex(id) << \" not found\";\n>  \t\t\treturn -EINVAL;\n>  \t\t}\n> -\n> -\t\tv4l2Ctrls[i].id = id;\n> +\t\tv4l2_ext_control &v4l2Ctrl = v4l2Ctrls[i++];\n> +\t\tv4l2Ctrl.id = id;\n>  \n>  \t\t/* Set the v4l2_ext_control value for the write operation. */\n>  \t\tControlValue &value = ctrl.second;\n>  \t\tswitch (iter->first->type()) {\n>  \t\tcase ControlTypeInteger64:\n> -\t\t\tv4l2Ctrls[i].value64 = value.get<int64_t>();\n> +\t\t\tv4l2Ctrl.value64 = value.get<int64_t>();\n>  \t\t\tbreak;\n>  \n>  \t\tcase ControlTypeByte: {\n> @@ -314,32 +313,30 @@ int V4L2Device::setControls(ControlList *ctrls)\n>  \t\t\t}\n>  \n>  \t\t\tSpan<uint8_t> data = value.data();\n> -\t\t\tv4l2Ctrls[i].p_u8 = data.data();\n> -\t\t\tv4l2Ctrls[i].size = data.size();\n> +\t\t\tv4l2Ctrl.p_u8 = data.data();\n> +\t\t\tv4l2Ctrl.size = data.size();\n>  \n>  \t\t\tbreak;\n>  \t\t}\n>  \n>  \t\tdefault:\n>  \t\t\t/* \\todo To be changed to support strings. */\n> -\t\t\tv4l2Ctrls[i].value = value.get<int32_t>();\n> +\t\t\tv4l2Ctrl.value = value.get<int32_t>();\n>  \t\t\tbreak;\n>  \t\t}\n> -\n> -\t\ti++;\n>  \t}\n>  \n>  \tstruct v4l2_ext_controls v4l2ExtCtrls = {};\n>  \tv4l2ExtCtrls.which = V4L2_CTRL_WHICH_CUR_VAL;\n> -\tv4l2ExtCtrls.controls = v4l2Ctrls;\n> -\tv4l2ExtCtrls.count = count;\n> +\tv4l2ExtCtrls.controls = v4l2Ctrls.data();\n> +\tv4l2ExtCtrls.count = v4l2Ctrls.size();\n>  \n>  \tint ret = ioctl(VIDIOC_S_EXT_CTRLS, &v4l2ExtCtrls);\n>  \tif (ret) {\n>  \t\tunsigned int errorIdx = v4l2ExtCtrls.error_idx;\n>  \n>  \t\t/* Generic validation error. */\n> -\t\tif (errorIdx == 0 || errorIdx >= count) {\n> +\t\tif (errorIdx == 0 || errorIdx >= v4l2Ctrls.size()) {\n>  \t\t\tLOG(V4L2, Error) << \"Unable to set controls: \"\n>  \t\t\t\t\t << strerror(-ret);\n>  \t\t\treturn -EINVAL;\n> @@ -348,11 +345,12 @@ int V4L2Device::setControls(ControlList *ctrls)\n>  \t\t/* A specific control failed. */\n>  \t\tLOG(V4L2, Error) << \"Unable to set control \" << errorIdx\n>  \t\t\t\t << \": \" << strerror(-ret);\n> -\t\tcount = errorIdx - 1;\n> +\n> +\t\tv4l2Ctrls.resize(errorIdx);\n>  \t\tret = errorIdx;\n>  \t}\n>  \n> -\tupdateControls(ctrls, v4l2Ctrls, count);\n> +\tupdateControls(ctrls, v4l2Ctrls.data(), v4l2Ctrls.size());\n>  \n>  \treturn ret;\n>  }","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 53181BD816\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 20 Apr 2021 01:15:47 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id BE96368824;\n\tTue, 20 Apr 2021 03:15:46 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 4D23F602CA\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 20 Apr 2021 03:15:45 +0200 (CEST)","from pendragon.ideasonboard.com (62-78-145-57.bb.dnainternet.fi\n\t[62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id C4A8A470;\n\tTue, 20 Apr 2021 03:15:44 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"PskQlV0j\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1618881345;\n\tbh=mzmwRUpSMpVSB5/3XtPK7lzREVhMpO7/eX97W2IE3c8=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=PskQlV0jSTbZ1PAn2/T4COAuEO2YsqiZd8eA+alGGKoRrVqhIDTPz8p8yzC6fxsg2\n\t/rTjxVV59KHpWl7ynjq8WpGx10jD3sXkfn9tBTk6RNtqsTqo0mUq1v2L78OSvPGBOu\n\toAjTlbfyvCiSN4K2vFUpqydTL1oATpZad2HA+f2o=","Date":"Tue, 20 Apr 2021 04:15:41 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Hirokazu Honda <hiroh@chromium.org>","Message-ID":"<YH4rPezjt7GmdDUI@pendragon.ideasonboard.com>","References":"<20210415044854.3348529-1-hiroh@chromium.org>\n\t<20210415044854.3348529-2-hiroh@chromium.org>","MIME-Version":"1.0","Content-Disposition":"inline","In-Reply-To":"<20210415044854.3348529-2-hiroh@chromium.org>","Subject":"Re: [libcamera-devel] [PATCH v2 2/3] libcamera: V4L2Device: Use\n\tstd::vector for v4l2_ext_control in setControls()","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Cc":"libcamera-devel@lists.libcamera.org","Content-Type":"text/plain; charset=\"us-ascii\"","Content-Transfer-Encoding":"7bit","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}},{"id":16442,"web_url":"https://patchwork.libcamera.org/comment/16442/","msgid":"<CAO5uPHOKd50seqrqh=qJR=M0PQu2-NWs+it3_sx=jrfaYKUWJA@mail.gmail.com>","date":"2021-04-21T09:30:50","subject":"Re: [libcamera-devel] [PATCH v2 2/3] libcamera: V4L2Device: Use\n\tstd::vector for v4l2_ext_control in setControls()","submitter":{"id":63,"url":"https://patchwork.libcamera.org/api/people/63/","name":"Hirokazu Honda","email":"hiroh@chromium.org"},"content":"Hi Laurent, thanks for reviewing.\n\nOn Tue, Apr 20, 2021 at 10:15 AM Laurent Pinchart\n<laurent.pinchart@ideasonboard.com> wrote:\n>\n> Hi Hiro,\n>\n> Thank you for the patch.\n>\n> On Thu, Apr 15, 2021 at 01:48:53PM +0900, Hirokazu Honda wrote:\n> > The original code uses Variable-Length-Array, which is not\n> > officially supported in C++. This replaces the array with\n> > std::vector.\n> >\n> > Signed-off-by: Hirokazu Honda <hiroh@chromium.org>\n> > Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n> > ---\n> >  src/libcamera/v4l2_device.cpp | 32 +++++++++++++++-----------------\n> >  1 file changed, 15 insertions(+), 17 deletions(-)\n> >\n> > diff --git a/src/libcamera/v4l2_device.cpp b/src/libcamera/v4l2_device.cpp\n> > index ee8c3fed..5ec1b10e 100644\n> > --- a/src/libcamera/v4l2_device.cpp\n> > +++ b/src/libcamera/v4l2_device.cpp\n> > @@ -279,12 +279,11 @@ ControlList V4L2Device::getControls(const std::vector<uint32_t> &ids)\n> >   */\n> >  int V4L2Device::setControls(ControlList *ctrls)\n> >  {\n> > -     unsigned int count = ctrls->size();\n> > -     if (count == 0)\n> > +     if (ctrls->empty())\n> >               return 0;\n> >\n> > -     struct v4l2_ext_control v4l2Ctrls[count];\n> > -     memset(v4l2Ctrls, 0, sizeof(v4l2Ctrls));\n> > +     std::vector<v4l2_ext_control> v4l2Ctrls(ctrls->size());\n> > +     memset(v4l2Ctrls.data(), 0, v4l2Ctrls.size());\n>\n> Is the memset() needed ? The std::vector constructor will default-insert\n> the elements, which value-initializes them. If my interpretation of\n> https://en.cppreference.com/w/cpp/language/value_initialization is\n> correct, the elements will be zero-initialized.\n>\n\nvalue initialization in c++ fills zero to the first value in union.\nIf the second or later values in the union is large than the first\nvalue, they are not zero-filled.\nSo I would use memset for a structure with a union.\n\n> >\n> >       unsigned int i = 0;\n>\n> Should we replace i with\n>\n>         v4l2_ext_control *v4l2Ctrl = v4l2Ctrls.data();\n>\n> and do a\n>\n>         v4l2Ctrl++;\n>\n> at the end of the loop ? That way we would ensure that i won't be used\n> incorrectly (after being incremented) inside the loop.\n>\n> Apart from that,\n>\n> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n>\n> >       for (auto &ctrl : *ctrls) {\n> > @@ -295,14 +294,14 @@ int V4L2Device::setControls(ControlList *ctrls)\n> >                               << \"Control \" << utils::hex(id) << \" not found\";\n> >                       return -EINVAL;\n> >               }\n> > -\n> > -             v4l2Ctrls[i].id = id;\n> > +             v4l2_ext_control &v4l2Ctrl = v4l2Ctrls[i++];\n> > +             v4l2Ctrl.id = id;\n> >\n> >               /* Set the v4l2_ext_control value for the write operation. */\n> >               ControlValue &value = ctrl.second;\n> >               switch (iter->first->type()) {\n> >               case ControlTypeInteger64:\n> > -                     v4l2Ctrls[i].value64 = value.get<int64_t>();\n> > +                     v4l2Ctrl.value64 = value.get<int64_t>();\n> >                       break;\n> >\n> >               case ControlTypeByte: {\n> > @@ -314,32 +313,30 @@ int V4L2Device::setControls(ControlList *ctrls)\n> >                       }\n> >\n> >                       Span<uint8_t> data = value.data();\n> > -                     v4l2Ctrls[i].p_u8 = data.data();\n> > -                     v4l2Ctrls[i].size = data.size();\n> > +                     v4l2Ctrl.p_u8 = data.data();\n> > +                     v4l2Ctrl.size = data.size();\n> >\n> >                       break;\n> >               }\n> >\n> >               default:\n> >                       /* \\todo To be changed to support strings. */\n> > -                     v4l2Ctrls[i].value = value.get<int32_t>();\n> > +                     v4l2Ctrl.value = value.get<int32_t>();\n> >                       break;\n> >               }\n> > -\n> > -             i++;\n> >       }\n> >\n> >       struct v4l2_ext_controls v4l2ExtCtrls = {};\n> >       v4l2ExtCtrls.which = V4L2_CTRL_WHICH_CUR_VAL;\n> > -     v4l2ExtCtrls.controls = v4l2Ctrls;\n> > -     v4l2ExtCtrls.count = count;\n> > +     v4l2ExtCtrls.controls = v4l2Ctrls.data();\n> > +     v4l2ExtCtrls.count = v4l2Ctrls.size();\n> >\n> >       int ret = ioctl(VIDIOC_S_EXT_CTRLS, &v4l2ExtCtrls);\n> >       if (ret) {\n> >               unsigned int errorIdx = v4l2ExtCtrls.error_idx;\n> >\n> >               /* Generic validation error. */\n> > -             if (errorIdx == 0 || errorIdx >= count) {\n> > +             if (errorIdx == 0 || errorIdx >= v4l2Ctrls.size()) {\n> >                       LOG(V4L2, Error) << \"Unable to set controls: \"\n> >                                        << strerror(-ret);\n> >                       return -EINVAL;\n> > @@ -348,11 +345,12 @@ int V4L2Device::setControls(ControlList *ctrls)\n> >               /* A specific control failed. */\n> >               LOG(V4L2, Error) << \"Unable to set control \" << errorIdx\n> >                                << \": \" << strerror(-ret);\n> > -             count = errorIdx - 1;\n> > +\n> > +             v4l2Ctrls.resize(errorIdx);\n> >               ret = errorIdx;\n> >       }\n> >\n> > -     updateControls(ctrls, v4l2Ctrls, count);\n> > +     updateControls(ctrls, v4l2Ctrls.data(), v4l2Ctrls.size());\n> >\n> >       return ret;\n> >  }\n>\n> --\n> Regards,\n>\n> Laurent Pinchart","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 8BA0EBDB16\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 21 Apr 2021 09:31:02 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 50EE368849;\n\tWed, 21 Apr 2021 11:31:02 +0200 (CEST)","from mail-ed1-x52a.google.com (mail-ed1-x52a.google.com\n\t[IPv6:2a00:1450:4864:20::52a])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id E7B6168843\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 21 Apr 2021 11:31:00 +0200 (CEST)","by mail-ed1-x52a.google.com with SMTP id bx20so47398677edb.12\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 21 Apr 2021 02:31:00 -0700 (PDT)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=chromium.org header.i=@chromium.org\n\theader.b=\"DOJJ6OSV\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org;\n\ts=google; \n\th=mime-version:references:in-reply-to:from:date:message-id:subject:to\n\t:cc; bh=p9+E7mH1FQrmqXr8AoinPYsLpjVLU7f98TOw5j1matU=;\n\tb=DOJJ6OSVxQX9wXw5rMxOWgg0YoNMNIRGBqwzbIboqe6JpTjKUoMp9qIFBWIZu1I17Q\n\tHhc9naLid3uZXX9e7uE4fZ80rabSrJ8rKxiMe6mfH2xBjgAaeiLBL/AVqzuz9m8VpnWG\n\tvIJ6s2k+hx9UrljsQ7u3CPgfW//FjxufkpXB8=","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20161025;\n\th=x-gm-message-state:mime-version:references:in-reply-to:from:date\n\t:message-id:subject:to:cc;\n\tbh=p9+E7mH1FQrmqXr8AoinPYsLpjVLU7f98TOw5j1matU=;\n\tb=W0HN2LzgV7edzZQxNOt4QluwYUoTgbhuqAXm3xQI4Cfdk10QgRq6SnIwuHhs0ZbWDi\n\tTZu0pZkoXZPNPmW0wkG3Y1kNBVnqYPTGqzfCSrXsPRol6lzuB4wMW4b5MsfxYdZ65zKY\n\tfLwUU3nzbllxbBdCSc6INbeqMx5zaMo4lI3QVMkyHhjdksKRYNAJ70M/pWEdTy2iNvXi\n\tXkepuLUGrDqUyqRu1GuooPK1ikDNR2dKnKs9Hle/q/FKRIuCkd1+52IGH7LTmVquP9te\n\t51XI7XkQ3iFxFaCAyCAW6xWG5zZIAEITu9QUBfWwfRUxV6Mp5bBKkLGNEPZA2U66m3Cv\n\tIfrg==","X-Gm-Message-State":"AOAM530Q0BcSnv51MtrCcXLprf+uRWyt4MbiSfAKJPNSDzMystSBMQtm\n\tJaGTmAyWWsxl8yn7GDngVZOAmWuybcPAeDI0A3uWGUOjf4E=","X-Google-Smtp-Source":"ABdhPJyOWha5eoESwFYh3M7B6IL+r9iCM8YxT2X3glN+99Z4vUxjcWNgd0WSYk/SGlwCnmA4MOxEFrEwhMEvY5p+l84=","X-Received":"by 2002:a05:6402:34cd:: with SMTP id\n\tw13mr37770584edc.73.1618997460616; \n\tWed, 21 Apr 2021 02:31:00 -0700 (PDT)","MIME-Version":"1.0","References":"<20210415044854.3348529-1-hiroh@chromium.org>\n\t<20210415044854.3348529-2-hiroh@chromium.org>\n\t<YH4rPezjt7GmdDUI@pendragon.ideasonboard.com>","In-Reply-To":"<YH4rPezjt7GmdDUI@pendragon.ideasonboard.com>","From":"Hirokazu Honda <hiroh@chromium.org>","Date":"Wed, 21 Apr 2021 18:30:50 +0900","Message-ID":"<CAO5uPHOKd50seqrqh=qJR=M0PQu2-NWs+it3_sx=jrfaYKUWJA@mail.gmail.com>","To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Subject":"Re: [libcamera-devel] [PATCH v2 2/3] libcamera: V4L2Device: Use\n\tstd::vector for v4l2_ext_control in setControls()","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Cc":"libcamera devel <libcamera-devel@lists.libcamera.org>","Content-Type":"text/plain; charset=\"us-ascii\"","Content-Transfer-Encoding":"7bit","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}},{"id":16444,"web_url":"https://patchwork.libcamera.org/comment/16444/","msgid":"<YH/xw/aEMEF/1qqy@pendragon.ideasonboard.com>","date":"2021-04-21T09:34:59","subject":"Re: [libcamera-devel] [PATCH v2 2/3] libcamera: V4L2Device: Use\n\tstd::vector for v4l2_ext_control in setControls()","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Hiro,\n\nOn Wed, Apr 21, 2021 at 06:30:50PM +0900, Hirokazu Honda wrote:\n> On Tue, Apr 20, 2021 at 10:15 AM Laurent Pinchart wrote:\n> > On Thu, Apr 15, 2021 at 01:48:53PM +0900, Hirokazu Honda wrote:\n> > > The original code uses Variable-Length-Array, which is not\n> > > officially supported in C++. This replaces the array with\n> > > std::vector.\n> > >\n> > > Signed-off-by: Hirokazu Honda <hiroh@chromium.org>\n> > > Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n> > > ---\n> > >  src/libcamera/v4l2_device.cpp | 32 +++++++++++++++-----------------\n> > >  1 file changed, 15 insertions(+), 17 deletions(-)\n> > >\n> > > diff --git a/src/libcamera/v4l2_device.cpp b/src/libcamera/v4l2_device.cpp\n> > > index ee8c3fed..5ec1b10e 100644\n> > > --- a/src/libcamera/v4l2_device.cpp\n> > > +++ b/src/libcamera/v4l2_device.cpp\n> > > @@ -279,12 +279,11 @@ ControlList V4L2Device::getControls(const std::vector<uint32_t> &ids)\n> > >   */\n> > >  int V4L2Device::setControls(ControlList *ctrls)\n> > >  {\n> > > -     unsigned int count = ctrls->size();\n> > > -     if (count == 0)\n> > > +     if (ctrls->empty())\n> > >               return 0;\n> > >\n> > > -     struct v4l2_ext_control v4l2Ctrls[count];\n> > > -     memset(v4l2Ctrls, 0, sizeof(v4l2Ctrls));\n> > > +     std::vector<v4l2_ext_control> v4l2Ctrls(ctrls->size());\n> > > +     memset(v4l2Ctrls.data(), 0, v4l2Ctrls.size());\n> >\n> > Is the memset() needed ? The std::vector constructor will default-insert\n> > the elements, which value-initializes them. If my interpretation of\n> > https://en.cppreference.com/w/cpp/language/value_initialization is\n> > correct, the elements will be zero-initialized.\n> \n> value initialization in c++ fills zero to the first value in union.\n> If the second or later values in the union is large than the first\n> value, they are not zero-filled.\n> So I would use memset for a structure with a union.\n\nGood point, let's keep the memset() then.\n\n> > >\n> > >       unsigned int i = 0;\n> >\n> > Should we replace i with\n> >\n> >         v4l2_ext_control *v4l2Ctrl = v4l2Ctrls.data();\n> >\n> > and do a\n> >\n> >         v4l2Ctrl++;\n> >\n> > at the end of the loop ? That way we would ensure that i won't be used\n> > incorrectly (after being incremented) inside the loop.\n> >\n> > Apart from that,\n> >\n> > Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n> >\n> > >       for (auto &ctrl : *ctrls) {\n> > > @@ -295,14 +294,14 @@ int V4L2Device::setControls(ControlList *ctrls)\n> > >                               << \"Control \" << utils::hex(id) << \" not found\";\n> > >                       return -EINVAL;\n> > >               }\n> > > -\n> > > -             v4l2Ctrls[i].id = id;\n> > > +             v4l2_ext_control &v4l2Ctrl = v4l2Ctrls[i++];\n> > > +             v4l2Ctrl.id = id;\n> > >\n> > >               /* Set the v4l2_ext_control value for the write operation. */\n> > >               ControlValue &value = ctrl.second;\n> > >               switch (iter->first->type()) {\n> > >               case ControlTypeInteger64:\n> > > -                     v4l2Ctrls[i].value64 = value.get<int64_t>();\n> > > +                     v4l2Ctrl.value64 = value.get<int64_t>();\n> > >                       break;\n> > >\n> > >               case ControlTypeByte: {\n> > > @@ -314,32 +313,30 @@ int V4L2Device::setControls(ControlList *ctrls)\n> > >                       }\n> > >\n> > >                       Span<uint8_t> data = value.data();\n> > > -                     v4l2Ctrls[i].p_u8 = data.data();\n> > > -                     v4l2Ctrls[i].size = data.size();\n> > > +                     v4l2Ctrl.p_u8 = data.data();\n> > > +                     v4l2Ctrl.size = data.size();\n> > >\n> > >                       break;\n> > >               }\n> > >\n> > >               default:\n> > >                       /* \\todo To be changed to support strings. */\n> > > -                     v4l2Ctrls[i].value = value.get<int32_t>();\n> > > +                     v4l2Ctrl.value = value.get<int32_t>();\n> > >                       break;\n> > >               }\n> > > -\n> > > -             i++;\n> > >       }\n> > >\n> > >       struct v4l2_ext_controls v4l2ExtCtrls = {};\n> > >       v4l2ExtCtrls.which = V4L2_CTRL_WHICH_CUR_VAL;\n> > > -     v4l2ExtCtrls.controls = v4l2Ctrls;\n> > > -     v4l2ExtCtrls.count = count;\n> > > +     v4l2ExtCtrls.controls = v4l2Ctrls.data();\n> > > +     v4l2ExtCtrls.count = v4l2Ctrls.size();\n> > >\n> > >       int ret = ioctl(VIDIOC_S_EXT_CTRLS, &v4l2ExtCtrls);\n> > >       if (ret) {\n> > >               unsigned int errorIdx = v4l2ExtCtrls.error_idx;\n> > >\n> > >               /* Generic validation error. */\n> > > -             if (errorIdx == 0 || errorIdx >= count) {\n> > > +             if (errorIdx == 0 || errorIdx >= v4l2Ctrls.size()) {\n> > >                       LOG(V4L2, Error) << \"Unable to set controls: \"\n> > >                                        << strerror(-ret);\n> > >                       return -EINVAL;\n> > > @@ -348,11 +345,12 @@ int V4L2Device::setControls(ControlList *ctrls)\n> > >               /* A specific control failed. */\n> > >               LOG(V4L2, Error) << \"Unable to set control \" << errorIdx\n> > >                                << \": \" << strerror(-ret);\n> > > -             count = errorIdx - 1;\n> > > +\n> > > +             v4l2Ctrls.resize(errorIdx);\n> > >               ret = errorIdx;\n> > >       }\n> > >\n> > > -     updateControls(ctrls, v4l2Ctrls, count);\n> > > +     updateControls(ctrls, v4l2Ctrls.data(), v4l2Ctrls.size());\n> > >\n> > >       return ret;\n> > >  }","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 277CFBDB15\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 21 Apr 2021 09:35:06 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id D749F68843;\n\tWed, 21 Apr 2021 11:35:05 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 49B70602D1\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 21 Apr 2021 11:35:04 +0200 (CEST)","from pendragon.ideasonboard.com (62-78-145-57.bb.dnainternet.fi\n\t[62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id CDD463EE;\n\tWed, 21 Apr 2021 11:35:03 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"l+vizkiU\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1618997704;\n\tbh=aCWHU48sx9El4kWvWXPTmgbp9c7vhtjiG0B/MetXChw=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=l+vizkiUeAi27oFgH1Z275fmRMHqRx0at+RdpiTshP8eTl0DEin6+2+P/ob+h0Q0k\n\tCUwvGVtmdjlIiRAQdE4Gq8fXZG2EFOJROW9Vm/m5Qj5JeHckKoHCh8Vk0zelhWbIdW\n\tdKKOB/uPe7uwR1udiSmviIO7+3NwizrFTFKUI1ik=","Date":"Wed, 21 Apr 2021 12:34:59 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Hirokazu Honda <hiroh@chromium.org>","Message-ID":"<YH/xw/aEMEF/1qqy@pendragon.ideasonboard.com>","References":"<20210415044854.3348529-1-hiroh@chromium.org>\n\t<20210415044854.3348529-2-hiroh@chromium.org>\n\t<YH4rPezjt7GmdDUI@pendragon.ideasonboard.com>\n\t<CAO5uPHOKd50seqrqh=qJR=M0PQu2-NWs+it3_sx=jrfaYKUWJA@mail.gmail.com>","MIME-Version":"1.0","Content-Disposition":"inline","In-Reply-To":"<CAO5uPHOKd50seqrqh=qJR=M0PQu2-NWs+it3_sx=jrfaYKUWJA@mail.gmail.com>","Subject":"Re: [libcamera-devel] [PATCH v2 2/3] libcamera: V4L2Device: Use\n\tstd::vector for v4l2_ext_control in setControls()","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Cc":"libcamera devel <libcamera-devel@lists.libcamera.org>","Content-Type":"text/plain; charset=\"us-ascii\"","Content-Transfer-Encoding":"7bit","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]