[v4,2/2] libcamera: v4l2_videodevice: Use scope_exit
diff mbox series

Message ID 20250804233152.11538-3-laurent.pinchart@ideasonboard.com
State Accepted
Commit 4e567d1981aa177f2db9f442c573b881c25eb963
Headers show
Series
  • libcamera: Introduce and use scope_exit
Related show

Commit Message

Laurent Pinchart Aug. 4, 2025, 11:31 p.m. UTC
The V4L2VideoDevice::queueBuffer() function performs the same cleanup
action in many error paths. Use scope_exit to simplify it.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 src/libcamera/v4l2_videodevice.cpp | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)

Comments

Stefan Klug Aug. 6, 2025, 6:36 a.m. UTC | #1
Hi Laurent,

Thank you for the patch. 

Quoting Laurent Pinchart (2025-08-05 01:31:51)
> The V4L2VideoDevice::queueBuffer() function performs the same cleanup
> action in many error paths. Use scope_exit to simplify it.
> 
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

Reviewed-by: Stefan Klug <stefan.klug@ideasonboard.com> 

Cheers,
Stefan

> ---
>  src/libcamera/v4l2_videodevice.cpp | 11 +++--------
>  1 file changed, 3 insertions(+), 8 deletions(-)
> 
> diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp
> index 71cc7e895d8c..d206244fa9a6 100644
> --- a/src/libcamera/v4l2_videodevice.cpp
> +++ b/src/libcamera/v4l2_videodevice.cpp
> @@ -1668,6 +1668,8 @@ int V4L2VideoDevice::queueBuffer(FrameBuffer *buffer)
>         if (ret < 0)
>                 return ret;
>  
> +       auto guard = utils::scope_exit{ [&]() { cache_->put(buf.index); } };
> +
>         buf.index = ret;
>         buf.type = bufferType_;
>         buf.memory = memoryType_;
> @@ -1683,15 +1685,11 @@ int V4L2VideoDevice::queueBuffer(FrameBuffer *buffer)
>          */
>         if (planes.size() < numV4l2Planes) {
>                 LOG(V4L2, Error) << "Frame buffer has too few planes";
> -               cache_->put(buf.index);
> -
>                 return -EINVAL;
>         }
>  
>         if (planes.size() != numV4l2Planes && !buffer->_d()->isContiguous()) {
>                 LOG(V4L2, Error) << "Device format requires contiguous buffer";
> -               cache_->put(buf.index);
> -
>                 return -EINVAL;
>         }
>  
> @@ -1734,8 +1732,6 @@ int V4L2VideoDevice::queueBuffer(FrameBuffer *buffer)
>                                 if (i != planes.size() - 1 && bytesused != length) {
>                                         LOG(V4L2, Error)
>                                                 << "Holes in multi-planar buffer not supported";
> -                                       cache_->put(buf.index);
> -
>                                         return -EINVAL;
>                                 }
>                         }
> @@ -1785,8 +1781,6 @@ int V4L2VideoDevice::queueBuffer(FrameBuffer *buffer)
>                 LOG(V4L2, Error)
>                         << "Failed to queue buffer " << buf.index << ": "
>                         << strerror(-ret);
> -               cache_->put(buf.index);
> -
>                 return ret;
>         }
>  
> @@ -1798,6 +1792,7 @@ int V4L2VideoDevice::queueBuffer(FrameBuffer *buffer)
>  
>         queuedBuffers_[buf.index] = buffer;
>  
> +       guard.release();
>         return 0;
>  }
>  
> -- 
> Regards,
> 
> Laurent Pinchart
>
Kieran Bingham Aug. 6, 2025, 9:21 a.m. UTC | #2
Quoting Laurent Pinchart (2025-08-05 00:31:51)
> The V4L2VideoDevice::queueBuffer() function performs the same cleanup
> action in many error paths. Use scope_exit to simplify it.
> 
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

Indeed, this looks cleaner to me.

Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>

> ---
>  src/libcamera/v4l2_videodevice.cpp | 11 +++--------
>  1 file changed, 3 insertions(+), 8 deletions(-)
> 
> diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp
> index 71cc7e895d8c..d206244fa9a6 100644
> --- a/src/libcamera/v4l2_videodevice.cpp
> +++ b/src/libcamera/v4l2_videodevice.cpp
> @@ -1668,6 +1668,8 @@ int V4L2VideoDevice::queueBuffer(FrameBuffer *buffer)
>         if (ret < 0)
>                 return ret;
>  
> +       auto guard = utils::scope_exit{ [&]() { cache_->put(buf.index); } };
> +
>         buf.index = ret;
>         buf.type = bufferType_;
>         buf.memory = memoryType_;
> @@ -1683,15 +1685,11 @@ int V4L2VideoDevice::queueBuffer(FrameBuffer *buffer)
>          */
>         if (planes.size() < numV4l2Planes) {
>                 LOG(V4L2, Error) << "Frame buffer has too few planes";
> -               cache_->put(buf.index);
> -
>                 return -EINVAL;
>         }
>  
>         if (planes.size() != numV4l2Planes && !buffer->_d()->isContiguous()) {
>                 LOG(V4L2, Error) << "Device format requires contiguous buffer";
> -               cache_->put(buf.index);
> -
>                 return -EINVAL;
>         }
>  
> @@ -1734,8 +1732,6 @@ int V4L2VideoDevice::queueBuffer(FrameBuffer *buffer)
>                                 if (i != planes.size() - 1 && bytesused != length) {
>                                         LOG(V4L2, Error)
>                                                 << "Holes in multi-planar buffer not supported";
> -                                       cache_->put(buf.index);
> -
>                                         return -EINVAL;
>                                 }
>                         }
> @@ -1785,8 +1781,6 @@ int V4L2VideoDevice::queueBuffer(FrameBuffer *buffer)
>                 LOG(V4L2, Error)
>                         << "Failed to queue buffer " << buf.index << ": "
>                         << strerror(-ret);
> -               cache_->put(buf.index);
> -
>                 return ret;
>         }
>  
> @@ -1798,6 +1792,7 @@ int V4L2VideoDevice::queueBuffer(FrameBuffer *buffer)
>  
>         queuedBuffers_[buf.index] = buffer;
>  
> +       guard.release();
>         return 0;
>  }
>  
> -- 
> Regards,
> 
> Laurent Pinchart
>

Patch
diff mbox series

diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp
index 71cc7e895d8c..d206244fa9a6 100644
--- a/src/libcamera/v4l2_videodevice.cpp
+++ b/src/libcamera/v4l2_videodevice.cpp
@@ -1668,6 +1668,8 @@  int V4L2VideoDevice::queueBuffer(FrameBuffer *buffer)
 	if (ret < 0)
 		return ret;
 
+	auto guard = utils::scope_exit{ [&]() { cache_->put(buf.index); } };
+
 	buf.index = ret;
 	buf.type = bufferType_;
 	buf.memory = memoryType_;
@@ -1683,15 +1685,11 @@  int V4L2VideoDevice::queueBuffer(FrameBuffer *buffer)
 	 */
 	if (planes.size() < numV4l2Planes) {
 		LOG(V4L2, Error) << "Frame buffer has too few planes";
-		cache_->put(buf.index);
-
 		return -EINVAL;
 	}
 
 	if (planes.size() != numV4l2Planes && !buffer->_d()->isContiguous()) {
 		LOG(V4L2, Error) << "Device format requires contiguous buffer";
-		cache_->put(buf.index);
-
 		return -EINVAL;
 	}
 
@@ -1734,8 +1732,6 @@  int V4L2VideoDevice::queueBuffer(FrameBuffer *buffer)
 				if (i != planes.size() - 1 && bytesused != length) {
 					LOG(V4L2, Error)
 						<< "Holes in multi-planar buffer not supported";
-					cache_->put(buf.index);
-
 					return -EINVAL;
 				}
 			}
@@ -1785,8 +1781,6 @@  int V4L2VideoDevice::queueBuffer(FrameBuffer *buffer)
 		LOG(V4L2, Error)
 			<< "Failed to queue buffer " << buf.index << ": "
 			<< strerror(-ret);
-		cache_->put(buf.index);
-
 		return ret;
 	}
 
@@ -1798,6 +1792,7 @@  int V4L2VideoDevice::queueBuffer(FrameBuffer *buffer)
 
 	queuedBuffers_[buf.index] = buffer;
 
+	guard.release();
 	return 0;
 }