From patchwork Tue Jul 29 16:21:00 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 24029 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id C8718BDC71 for ; Tue, 29 Jul 2025 16:21:16 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 1BDA1691F2; Tue, 29 Jul 2025 18:21:15 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="PnLuO7Eo"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id E37D3691E6 for ; Tue, 29 Jul 2025 18:21:11 +0200 (CEST) Received: from pendragon.ideasonboard.com (81-175-209-231.bb.dnainternet.fi [81.175.209.231]) by perceval.ideasonboard.com (Postfix) with UTF8SMTPSA id 0E4226DC; Tue, 29 Jul 2025 18:20:28 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1753806029; bh=fELHw0T9n6rIt/jQQQnILmMTd+goY2x7E88yVNA6LEs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PnLuO7EosRnW/wcKys4ExaNQKE82ZH3KJ4pjpE/ZyllJKNjzWp6xfSv7T5bF+zMnk Bp7YcFhf4cp05vWgCPaT06jE2lTMTU+wSaMEvRJbB/o5x+0QBh3aARC9+94B3tsuR5 WjcOw9/wbrdh6YC0idPE4kM8I1zENz28zQXoTN/c= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Cc: Han-Lin Chen , Harvey Yang Subject: [PATCH v2 1/2] libcamera: utils: Add scope_exit class Date: Tue, 29 Jul 2025 19:21:00 +0300 Message-ID: <20250729162101.13836-2-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.49.1 In-Reply-To: <20250729162101.13836-1-laurent.pinchart@ideasonboard.com> References: <20250729162101.13836-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" The scope_exit class is an implementation of the identically named C++ library fundamentals TS v3 class, as documented in [1]. It is a simpler version of the libcamera-specific ScopeExitActions class and doesn't require dynamic heap memory allocation, making it more suitable for hot paths. The class is not documented as it implements a C++ standard (even if experimental) API. [1] https://en.cppreference.com/w/cpp/experimental/scope_exit/scope_exit.html Signed-off-by: Laurent Pinchart --- I have named the class scope_exit instead of ScopeExit to match the C++ library fundamentals TS v3 class, and have not documented it as it's meant as an implementation of the standard. It is however not clear if and when scope_exit would become part of a ratified C++ standard, so I'm open to instead create a libcamera-specific ScopeExit class and document it. --- include/libcamera/base/utils.h | 45 ++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) -- Regards, Laurent Pinchart diff --git a/include/libcamera/base/utils.h b/include/libcamera/base/utils.h index 8d5c35782ee3..1d8e5aef9b54 100644 --- a/include/libcamera/base/utils.h +++ b/include/libcamera/base/utils.h @@ -428,6 +428,51 @@ private: std::vector> actions_; }; +#ifndef __DOXYGEN__ +template +class scope_exit +{ +public: + template>, scope_exit> && + std::is_constructible_v && + !std::is_lvalue_reference_v> * = nullptr> + explicit scope_exit(Fn &&fn) + : exitFunction_(std::forward(fn)) + { + static_assert(std::is_nothrow_constructible_v); + } + + scope_exit(scope_exit &&other) + : exitFunction_(std::move(other.exitFunction_)), + active_(other.active_) + { + other.release(); + } + + scope_exit(const scope_exit &) = delete; + + ~scope_exit() + { + if (active_) + exitFunction_(); + } + + void release() + { + active_ = false; + } + +private: + EF exitFunction_; + bool active_ = true; +}; + +template +scope_exit(EF) -> scope_exit; + +#endif /* __DOXYGEN__ */ + } /* namespace utils */ #ifndef __DOXYGEN__ From patchwork Tue Jul 29 16:21:01 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 24030 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id BC024C32B5 for ; Tue, 29 Jul 2025 16:21:18 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 81E7F691F0; Tue, 29 Jul 2025 18:21:17 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="umb0M/76"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 85309691F0 for ; Tue, 29 Jul 2025 18:21:13 +0200 (CEST) Received: from pendragon.ideasonboard.com (81-175-209-231.bb.dnainternet.fi [81.175.209.231]) by perceval.ideasonboard.com (Postfix) with UTF8SMTPSA id 71EF96DC; Tue, 29 Jul 2025 18:20:30 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1753806030; bh=HQQpoa160C8j3jA6/OnpsPJXD9zOnZ0N0TshqI/S0o4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=umb0M/76YXkIK9VQ21HGtwIbFFsLBz+tx8Lb6ltro3wu4HdHKjibiii+tOmpRuy0K RyXMul33JwSXxTI1SJl12vpytSJaL3O1dutySEOBqhfyVupAqZd12Pv1ZGqFV7L/TG Ryd6YSoTIiW/Ht9LjbJhj0Jo7yEfwveRERiaoi0s= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Cc: Han-Lin Chen , Harvey Yang Subject: [PATCH v2 2/2] libcamera: v4l2_videodevice: Use scope_exit Date: Tue, 29 Jul 2025 19:21:01 +0300 Message-ID: <20250729162101.13836-3-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.49.1 In-Reply-To: <20250729162101.13836-1-laurent.pinchart@ideasonboard.com> References: <20250729162101.13836-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" The V4L2VideoDevice::queueBuffer() function performs the same cleanup action in many error paths. Use scope_exit to simplify it. Signed-off-by: Laurent Pinchart --- 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; }