[libcamera-devel] [PATCH 2/3] libcamera: V4L2Device: Use std::vector for v4l2_ext_control in setControls()

Hirokazu Honda hiroh at chromium.org
Tue Apr 13 08:19:24 CEST 2021


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());
 
 	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;
 }
-- 
2.31.1.295.g9ea45b61b8-goog



More information about the libcamera-devel mailing list