[libcamera-devel] [PATCH 2/3] libcamera: V4L2Device: Use std::vector for v4l2_ext_control in setControls()
Hirokazu Honda
hiroh at chromium.org
Thu Apr 15 05:58:42 CEST 2021
On Wed, Apr 14, 2021 at 7:14 AM Kieran Bingham
<kieran.bingham at ideasonboard.com> wrote:
>
> Hi Hiro,
>
> On 13/04/2021 07:19, Hirokazu Honda wrote:
> > The original code uses Variable-Length-Array, which is not
> > officially supported in C++. This replaces the array with
> > std::vector.
> > ---
> > src/libcamera/v4l2_device.cpp | 18 +++++++++---------
> > 1 file changed, 9 insertions(+), 9 deletions(-)
> >
> > diff --git a/src/libcamera/v4l2_device.cpp b/src/libcamera/v4l2_device.cpp
> > index 78c289c2..8625dde8 100644
> > --- a/src/libcamera/v4l2_device.cpp
> > +++ b/src/libcamera/v4l2_device.cpp
> > @@ -280,12 +280,11 @@ ControlList V4L2Device::getControls(const std::vector<uint32_t> &ids)
> > */
> > int V4L2Device::setControls(ControlList *ctrls)
> > {
> > - unsigned int count = ctrls->size();
> > - if (count == 0)
> > + if (ctrls->empty())
> > return 0;
> >
> > - struct v4l2_ext_control v4l2Ctrls[count];
> > - memset(v4l2Ctrls, 0, sizeof(v4l2Ctrls));
> > + std::vector<v4l2_ext_control> v4l2Ctrls(ctrls->size());
> > + memset(v4l2Ctrls.data(), 0, v4l2Ctrls.size());
>
> Does the vector not initialise in anyway ?
>
> Looking at https://www.cplusplus.com/reference/vector/vector/vector/
> this must be using the fill(2) constructor.
>
> I suspect if nothing is provided, it won't fill, so perhaps it's not
> initialised, but if that's the case, I wonder if the right way to
> construct this is more like:
>
> std::vector<v4l2_ext_control> v4l2Ctrls(ctrls->size(), {});
As replied to 1/3, I don't trust the zero initialization by {}.
So I would rather use std::memset.
Best Regards,
-Hiro
>
> But I'm not particular certain.
>
> Otherwise, this looks Ok to me...
>
> Reviewed-by: Kieran Bingham <kieran.bingham at ideasonboard.com>
>
> >
> > unsigned int i = 0;
> > for (auto &ctrl : *ctrls) {
> > @@ -332,15 +331,15 @@ int V4L2Device::setControls(ControlList *ctrls)
> >
> > struct v4l2_ext_controls v4l2ExtCtrls = {};
> > v4l2ExtCtrls.which = V4L2_CTRL_WHICH_CUR_VAL;
> > - v4l2ExtCtrls.controls = v4l2Ctrls;
> > - v4l2ExtCtrls.count = count;
> > + v4l2ExtCtrls.controls = v4l2Ctrls.data();
> > + v4l2ExtCtrls.count = v4l2Ctrls.size();
> >
> > int ret = ioctl(VIDIOC_S_EXT_CTRLS, &v4l2ExtCtrls);
> > if (ret) {
> > unsigned int errorIdx = v4l2ExtCtrls.error_idx;
> >
> > /* Generic validation error. */
> > - if (errorIdx == 0 || errorIdx >= count) {
> > + if (errorIdx == 0 || errorIdx >= v4l2Ctrls.size()) {
> > LOG(V4L2, Error) << "Unable to set controls: "
> > << strerror(-ret);
> > return -EINVAL;
> > @@ -349,11 +348,12 @@ int V4L2Device::setControls(ControlList *ctrls)
> > /* A specific control failed. */
> > LOG(V4L2, Error) << "Unable to set control " << errorIdx
> > << ": " << strerror(-ret);
> > - count = errorIdx - 1;
> > +
> > + v4l2Ctrls.resize(errorIdx);
> > ret = errorIdx;
> > }
> >
> > - updateControls(ctrls, v4l2Ctrls, count);
> > + updateControls(ctrls, v4l2Ctrls.data(), v4l2Ctrls.size());
> >
> > return ret;
> > }
> >
>
> --
> Regards
> --
> Kieran
More information about the libcamera-devel
mailing list