From patchwork Thu Apr 17 11:35:37 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= X-Patchwork-Id: 23184 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 B2E6CC3213 for ; Thu, 17 Apr 2025 11:35:46 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id D071268AC0; Thu, 17 Apr 2025 13:35:45 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="b4mdfVLh"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 776DE617E8 for ; Thu, 17 Apr 2025 13:35:43 +0200 (CEST) Received: from pb-laptop.local (185.221.143.16.nat.pool.zt.hu [185.221.143.16]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id B6CD56A2 for ; Thu, 17 Apr 2025 13:33:39 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1744889619; bh=N5HOeHDcCvRz+tweAkZi65QD0yX0Kgq8ZQ3yK34x4v8=; h=From:To:Subject:Date:From; b=b4mdfVLhT6TktPwBNiCJ+8m3lGx181ackKV7XIfmX4/V5pOTP+2smk/xXUmFlEA3D p6xQnxSXtlW9LZNoUQ0vPjkQPrpuZVYUcKvy0gf+r+IE1y48uIrjZq/Q46kz9yZOHn NY+a0d7ottTrtkRjKeMsivM3Wxb/i9AK3xXH9nng= From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= To: libcamera-devel@lists.libcamera.org Subject: [PATCH v1 1/3] libcamera: controls: Give name to the union containing storage Date: Thu, 17 Apr 2025 13:35:37 +0200 Message-ID: <20250417113539.1137936-1-barnabas.pocze@ideasonboard.com> X-Mailer: git-send-email 2.49.0 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" In order to be able to copy the storage as one unit, regardless of which member is active give a name to the union member. Signed-off-by: Barnabás Pőcze Reviewed-by: Kieran Bingham Reviewed-by: Jacopo Mondi --- include/libcamera/controls.h | 6 +++--- src/libcamera/controls.cpp | 17 ++++++++--------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/include/libcamera/controls.h b/include/libcamera/controls.h index 4bfe9615c..1dc6ccffa 100644 --- a/include/libcamera/controls.h +++ b/include/libcamera/controls.h @@ -238,9 +238,9 @@ private: bool isArray_; std::size_t numElements_ : 32; union { - uint64_t value_; - void *storage_; - }; + uint64_t internal; + void *external; + } storage_; void release(); void set(ControlType type, bool isArray, const void *data, diff --git a/src/libcamera/controls.cpp b/src/libcamera/controls.cpp index 70f6f6092..d384e1ef7 100644 --- a/src/libcamera/controls.cpp +++ b/src/libcamera/controls.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -122,10 +123,8 @@ void ControlValue::release() { std::size_t size = numElements_ * ControlValueSize[type_]; - if (size > sizeof(value_)) { - delete[] reinterpret_cast(storage_); - storage_ = nullptr; - } + if (size > sizeof(storage_.internal)) + delete[] reinterpret_cast(std::exchange(storage_.external, nullptr)); } ControlValue::~ControlValue() @@ -192,9 +191,9 @@ ControlValue &ControlValue::operator=(const ControlValue &other) Span ControlValue::data() const { std::size_t size = numElements_ * ControlValueSize[type_]; - const uint8_t *data = size > sizeof(value_) - ? reinterpret_cast(storage_) - : reinterpret_cast(&value_); + const uint8_t *data = size > sizeof(storage_.internal) + ? reinterpret_cast(storage_.external) + : reinterpret_cast(&storage_.internal); return { data, size }; } @@ -391,8 +390,8 @@ void ControlValue::reserve(ControlType type, bool isArray, std::size_t numElemen if (oldSize == newSize) return; - if (newSize > sizeof(value_)) - storage_ = reinterpret_cast(new uint8_t[newSize]); + if (newSize > sizeof(storage_.internal)) + storage_.external = new uint8_t[newSize]; } /** From patchwork Thu Apr 17 11:35:38 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= X-Patchwork-Id: 23185 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 A8C62C3213 for ; Thu, 17 Apr 2025 11:35:49 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 9948A68ACA; Thu, 17 Apr 2025 13:35:46 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="WdJ04FJ5"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id A94EF68AB8 for ; Thu, 17 Apr 2025 13:35:43 +0200 (CEST) Received: from pb-laptop.local (185.221.143.16.nat.pool.zt.hu [185.221.143.16]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 071037E0 for ; Thu, 17 Apr 2025 13:33:39 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1744889620; bh=fAkcG0jCLSKsiKuClGSPd6cxE1FWezwBJPnmbcYn0To=; h=From:To:Subject:Date:In-Reply-To:References:From; b=WdJ04FJ52pzVt7nuRsc9y+i+5guLQwbfJeYh9UkTEhUIXjSJI0Dxbtx8yUEyH2vaf nZrXHiiH4HnX7V7eJBJeweJ2SeCksFfSWScFwHGZf8qP+AO67u7jvYYAi1Q1BHD9JE NmKTFhB1Ud0HXlXk5lIgQTpSPup6fGMucftotOs4= From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= To: libcamera-devel@lists.libcamera.org Subject: [PATCH v1 2/3] libcamera: controls: Implement move ctor/assignment Date: Thu, 17 Apr 2025 13:35:38 +0200 Message-ID: <20250417113539.1137936-2-barnabas.pocze@ideasonboard.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250417113539.1137936-1-barnabas.pocze@ideasonboard.com> References: <20250417113539.1137936-1-barnabas.pocze@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" Implement a move constructor and move assignment operator for `ControlValue`. The `ControlValue` type already has an "empty" state that it used when creating a default constructed `ControlValue`, so have the moved-from instance return to that state after move construction/assignment. This is useful, for example, for `std::vector` as most implementations will use the copy constructor when reallocating if no nothrow move constructor is available. Having a nothrow move constructor avoids the extra copies. It is also useful when using temporaries of `ControlValue` with other containers such as `std::optional`, and it also makes `ControlInfo` "cheaply" move constructible/assignable. Signed-off-by: Barnabás Pőcze Reviewed-by: Kieran Bingham Reviewed-by: Jacopo Mondi --- include/libcamera/controls.h | 28 ++++++++++++++++++++++++++++ src/libcamera/controls.cpp | 17 +++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/include/libcamera/controls.h b/include/libcamera/controls.h index 1dc6ccffa..86245e7a9 100644 --- a/include/libcamera/controls.h +++ b/include/libcamera/controls.h @@ -14,6 +14,7 @@ #include #include #include +#include #include #include @@ -165,6 +166,33 @@ public: ControlValue(const ControlValue &other); ControlValue &operator=(const ControlValue &other); + ControlValue(ControlValue &&other) noexcept + : type_(other.type_), + isArray_(std::exchange(other.isArray_, false)), + numElements_(other.numElements_), + storage_(std::exchange(other.storage_, {})) + { + other.type_ = ControlTypeNone; + other.numElements_ = 0; + } + + ControlValue &operator=(ControlValue &&other) noexcept + { + if (this != &other) { + release(); + + type_ = other.type_; + isArray_ = std::exchange(other.isArray_, false); + numElements_ = other.numElements_; + storage_ = std::exchange(other.storage_, {}); + + other.type_ = ControlTypeNone; + other.numElements_ = 0; + } + + return *this; + } + ControlType type() const { return type_; } bool isNone() const { return type_ == ControlTypeNone; } bool isArray() const { return isArray_; } diff --git a/src/libcamera/controls.cpp b/src/libcamera/controls.cpp index d384e1ef7..885287e71 100644 --- a/src/libcamera/controls.cpp +++ b/src/libcamera/controls.cpp @@ -155,6 +155,23 @@ ControlValue &ControlValue::operator=(const ControlValue &other) return *this; } +/** + * \fn ControlValue::ControlValue(ControlValue &&other) noexcept + * \brief Move constructor for ControlValue + * \param[in] other The ControlValue object to move from + * + * Move constructs a ControlValue instance from \a other. After this opreation + * \a other will be in the same state as a default constructed ControlValue instance. + */ + +/** + * \fn ControlValue &ControlValue::operator=(ControlValue &&other) noexcept + * \brief Move assignment operator for ControlValue + * \param[in] other The ControlValue object to move from + * + * \sa ControlValue::ControlValue(ControlValue &&other) + */ + /** * \fn ControlValue::type() * \brief Retrieve the data type of the value From patchwork Thu Apr 17 11:35:39 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= X-Patchwork-Id: 23186 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 21B2AC3284 for ; Thu, 17 Apr 2025 11:35:51 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 2DCA268AC0; Thu, 17 Apr 2025 13:35:48 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="OdA3h8Bw"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id E6E9C68AC0 for ; Thu, 17 Apr 2025 13:35:43 +0200 (CEST) Received: from pb-laptop.local (185.221.143.16.nat.pool.zt.hu [185.221.143.16]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 4AF1311E9 for ; Thu, 17 Apr 2025 13:33:40 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1744889620; bh=M06HT4DwU9iVSr+nbwhaqzltK+9vyl8VxWXUj7om2PU=; h=From:To:Subject:Date:In-Reply-To:References:From; b=OdA3h8Bw6+2yZh9it1vtPALfmdgomf3QTw0ZgC/Ay0ZLxBeTccwUt57REhxZYUtZR Iostv5Fqu49D6aTz1++Ch82fI0czBZgNpa/Kq06bazAjrqVqGHt9hOGdr1l9Uuc0PI TJQ5A5fBWrw3z7tj4GBO6JNpeEgK/oVugHhqKS40= From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= To: libcamera-devel@lists.libcamera.org Subject: [PATCH v1 3/3] libcamera: controls: Implement `swap()` Date: Thu, 17 Apr 2025 13:35:39 +0200 Message-ID: <20250417113539.1137936-3-barnabas.pocze@ideasonboard.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250417113539.1137936-1-barnabas.pocze@ideasonboard.com> References: <20250417113539.1137936-1-barnabas.pocze@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" Implement both free and member function `swap()` for `ControlValue`. The general `std::swap()` swaps two values by combining a move construction and two move assignments, but for `ControlValue` a simpler implementation can be provided by just swapping the members. Signed-off-by: Barnabás Pőcze Reviewed-by: Kieran Bingham Reviewed-by: Jacopo Mondi --- include/libcamera/controls.h | 24 ++++++++++++++++++++++++ src/libcamera/controls.cpp | 15 +++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/include/libcamera/controls.h b/include/libcamera/controls.h index 86245e7a9..27b314dbc 100644 --- a/include/libcamera/controls.h +++ b/include/libcamera/controls.h @@ -261,6 +261,30 @@ public: void reserve(ControlType type, bool isArray = false, std::size_t numElements = 1); + void swap(ControlValue &other) noexcept + { + { + auto tmp = type_; + type_ = other.type_; + other.type_ = tmp; + } + + std::swap(isArray_, other.isArray_); + + { + auto tmp = numElements_; + numElements_ = other.numElements_; + other.numElements_ = tmp; + } + + std::swap(storage_, other.storage_); + } + + friend void swap(ControlValue &a, ControlValue &b) noexcept + { + a.swap(b); + } + private: ControlType type_ : 8; bool isArray_; diff --git a/src/libcamera/controls.cpp b/src/libcamera/controls.cpp index 885287e71..c1ff60f39 100644 --- a/src/libcamera/controls.cpp +++ b/src/libcamera/controls.cpp @@ -411,6 +411,21 @@ void ControlValue::reserve(ControlType type, bool isArray, std::size_t numElemen storage_.external = new uint8_t[newSize]; } +/** + * \fn ControlValue::swap(ControlValue &other) noexcept + * \brief Swap two control values + * + * This function swaps the contained value of \a this with that of \a other. + */ + +/** + * \fn ControlValue::swap(ControlValue &a, ControlValue &b) noexcept + * \brief Swap two control values + * + * This function swaps the contained value of \a a with that of \a b. + * \sa ControlValue::swap() + */ + /** * \class ControlId * \brief Control static metadata