[{"id":33958,"web_url":"https://patchwork.libcamera.org/comment/33958/","msgid":"<174490454803.2882969.17536552906996132460@ping.linuxembedded.co.uk>","date":"2025-04-17T15:42:28","subject":"Re: [PATCH v1 1/3] libcamera: controls: Give name to the union\n\tcontaining storage","submitter":{"id":4,"url":"https://patchwork.libcamera.org/api/people/4/","name":"Kieran Bingham","email":"kieran.bingham@ideasonboard.com"},"content":"Quoting Barnabás Pőcze (2025-04-17 12:35:37)\n> In order to be able to copy the storage as one unit, regardless of\n> which member is active give a name to the union member.\n> \n> Signed-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>\n\nReviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n\n> ---\n>  include/libcamera/controls.h |  6 +++---\n>  src/libcamera/controls.cpp   | 17 ++++++++---------\n>  2 files changed, 11 insertions(+), 12 deletions(-)\n> \n> diff --git a/include/libcamera/controls.h b/include/libcamera/controls.h\n> index 4bfe9615c..1dc6ccffa 100644\n> --- a/include/libcamera/controls.h\n> +++ b/include/libcamera/controls.h\n> @@ -238,9 +238,9 @@ private:\n>         bool isArray_;\n>         std::size_t numElements_ : 32;\n>         union {\n> -               uint64_t value_;\n> -               void *storage_;\n> -       };\n> +               uint64_t internal;\n> +               void *external;\n> +       } storage_;\n>  \n>         void release();\n>         void set(ControlType type, bool isArray, const void *data,\n> diff --git a/src/libcamera/controls.cpp b/src/libcamera/controls.cpp\n> index 70f6f6092..d384e1ef7 100644\n> --- a/src/libcamera/controls.cpp\n> +++ b/src/libcamera/controls.cpp\n> @@ -10,6 +10,7 @@\n>  #include <sstream>\n>  #include <string.h>\n>  #include <string>\n> +#include <utility>\n>  \n>  #include <libcamera/base/log.h>\n>  #include <libcamera/base/utils.h>\n> @@ -122,10 +123,8 @@ void ControlValue::release()\n>  {\n>         std::size_t size = numElements_ * ControlValueSize[type_];\n>  \n> -       if (size > sizeof(value_)) {\n> -               delete[] reinterpret_cast<uint8_t *>(storage_);\n> -               storage_ = nullptr;\n> -       }\n> +       if (size > sizeof(storage_.internal))\n> +               delete[] reinterpret_cast<uint8_t *>(std::exchange(storage_.external, nullptr));\n>  }\n>  \n>  ControlValue::~ControlValue()\n> @@ -192,9 +191,9 @@ ControlValue &ControlValue::operator=(const ControlValue &other)\n>  Span<const uint8_t> ControlValue::data() const\n>  {\n>         std::size_t size = numElements_ * ControlValueSize[type_];\n> -       const uint8_t *data = size > sizeof(value_)\n> -                           ? reinterpret_cast<const uint8_t *>(storage_)\n> -                           : reinterpret_cast<const uint8_t *>(&value_);\n> +       const uint8_t *data = size > sizeof(storage_.internal)\n> +                           ? reinterpret_cast<const uint8_t *>(storage_.external)\n> +                           : reinterpret_cast<const uint8_t *>(&storage_.internal);\n>         return { data, size };\n>  }\n>  \n> @@ -391,8 +390,8 @@ void ControlValue::reserve(ControlType type, bool isArray, std::size_t numElemen\n>         if (oldSize == newSize)\n>                 return;\n>  \n> -       if (newSize > sizeof(value_))\n> -               storage_ = reinterpret_cast<void *>(new uint8_t[newSize]);\n> +       if (newSize > sizeof(storage_.internal))\n> +               storage_.external = new uint8_t[newSize];\n>  }\n>  \n>  /**\n> -- \n> 2.49.0\n>","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 92AC4C327D\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 17 Apr 2025 15:42:34 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id AA3D268AC2;\n\tThu, 17 Apr 2025 17:42:33 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 2B5AA68AC0\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 17 Apr 2025 17:42:31 +0200 (CEST)","from pendragon.ideasonboard.com\n\t(cpc89244-aztw30-2-0-cust6594.18-1.cable.virginm.net [86.31.185.195])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 55870415;\n\tThu, 17 Apr 2025 17:40:27 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"Rt4TfjCW\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1744904427;\n\tbh=MqSLWbttAKmx0AQK6zFkmAcx5UiB/takAhtuaEnAck8=;\n\th=In-Reply-To:References:Subject:From:To:Date:From;\n\tb=Rt4TfjCWDY0EdheR2TkSGmAESUrWG67W3pxomDP2zlF6uRh+cqh57t1VvLg04srHj\n\tikplp43BA7tUgqY1CTgUYhur0NTQqKglwk9akOJQ/QP6jQCN1o9IAyKjslSH4Eva0/\n\tB/f0o0rbtHF0A2PrXLHQNTwpd/fgUhYPz33dQG4A=","Content-Type":"text/plain; charset=\"utf-8\"","MIME-Version":"1.0","Content-Transfer-Encoding":"quoted-printable","In-Reply-To":"<20250417113539.1137936-1-barnabas.pocze@ideasonboard.com>","References":"<20250417113539.1137936-1-barnabas.pocze@ideasonboard.com>","Subject":"Re: [PATCH v1 1/3] libcamera: controls: Give name to the union\n\tcontaining storage","From":"Kieran Bingham <kieran.bingham@ideasonboard.com>","To":"=?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= <barnabas.pocze@ideasonboard.com>,\n\tlibcamera-devel@lists.libcamera.org","Date":"Thu, 17 Apr 2025 16:42:28 +0100","Message-ID":"<174490454803.2882969.17536552906996132460@ping.linuxembedded.co.uk>","User-Agent":"alot/0.10","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}},{"id":33965,"web_url":"https://patchwork.libcamera.org/comment/33965/","msgid":"<zaza3ubqdspyctvt6jb42dha5kocv7vsq45fytmw5i2rjvr72s@5nixjc2z2b7r>","date":"2025-04-18T10:15:52","subject":"Re: [PATCH v1 1/3] libcamera: controls: Give name to the union\n\tcontaining storage","submitter":{"id":143,"url":"https://patchwork.libcamera.org/api/people/143/","name":"Jacopo Mondi","email":"jacopo.mondi@ideasonboard.com"},"content":"Hi Barnabás\n\nOn Thu, Apr 17, 2025 at 01:35:37PM +0200, Barnabás Pőcze wrote:\n> In order to be able to copy the storage as one unit, regardless of\n> which member is active give a name to the union member.\n>\n> Signed-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>\n> ---\n>  include/libcamera/controls.h |  6 +++---\n>  src/libcamera/controls.cpp   | 17 ++++++++---------\n>  2 files changed, 11 insertions(+), 12 deletions(-)\n>\n> diff --git a/include/libcamera/controls.h b/include/libcamera/controls.h\n> index 4bfe9615c..1dc6ccffa 100644\n> --- a/include/libcamera/controls.h\n> +++ b/include/libcamera/controls.h\n> @@ -238,9 +238,9 @@ private:\n>  \tbool isArray_;\n>  \tstd::size_t numElements_ : 32;\n>  \tunion {\n> -\t\tuint64_t value_;\n> -\t\tvoid *storage_;\n> -\t};\n> +\t\tuint64_t internal;\n> +\t\tvoid *external;\n\nWe could nitpick on names here, but I won't :)\n\nReviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>\n\n> +\t} storage_;\n>\n>  \tvoid release();\n>  \tvoid set(ControlType type, bool isArray, const void *data,\n> diff --git a/src/libcamera/controls.cpp b/src/libcamera/controls.cpp\n> index 70f6f6092..d384e1ef7 100644\n> --- a/src/libcamera/controls.cpp\n> +++ b/src/libcamera/controls.cpp\n> @@ -10,6 +10,7 @@\n>  #include <sstream>\n>  #include <string.h>\n>  #include <string>\n> +#include <utility>\n>\n>  #include <libcamera/base/log.h>\n>  #include <libcamera/base/utils.h>\n> @@ -122,10 +123,8 @@ void ControlValue::release()\n>  {\n>  \tstd::size_t size = numElements_ * ControlValueSize[type_];\n>\n> -\tif (size > sizeof(value_)) {\n> -\t\tdelete[] reinterpret_cast<uint8_t *>(storage_);\n> -\t\tstorage_ = nullptr;\n> -\t}\n> +\tif (size > sizeof(storage_.internal))\n> +\t\tdelete[] reinterpret_cast<uint8_t *>(std::exchange(storage_.external, nullptr));\n>  }\n>\n>  ControlValue::~ControlValue()\n> @@ -192,9 +191,9 @@ ControlValue &ControlValue::operator=(const ControlValue &other)\n>  Span<const uint8_t> ControlValue::data() const\n>  {\n>  \tstd::size_t size = numElements_ * ControlValueSize[type_];\n> -\tconst uint8_t *data = size > sizeof(value_)\n> -\t\t\t    ? reinterpret_cast<const uint8_t *>(storage_)\n> -\t\t\t    : reinterpret_cast<const uint8_t *>(&value_);\n> +\tconst uint8_t *data = size > sizeof(storage_.internal)\n> +\t\t\t    ? reinterpret_cast<const uint8_t *>(storage_.external)\n> +\t\t\t    : reinterpret_cast<const uint8_t *>(&storage_.internal);\n>  \treturn { data, size };\n>  }\n>\n> @@ -391,8 +390,8 @@ void ControlValue::reserve(ControlType type, bool isArray, std::size_t numElemen\n>  \tif (oldSize == newSize)\n>  \t\treturn;\n>\n> -\tif (newSize > sizeof(value_))\n> -\t\tstorage_ = reinterpret_cast<void *>(new uint8_t[newSize]);\n> +\tif (newSize > sizeof(storage_.internal))\n> +\t\tstorage_.external = new uint8_t[newSize];\n>  }\n>\n>  /**\n> --\n> 2.49.0\n>","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 00655C327D\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 18 Apr 2025 10:15:57 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 69014617E8;\n\tFri, 18 Apr 2025 12:15:57 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 73D27617E7\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 18 Apr 2025 12:15:55 +0200 (CEST)","from ideasonboard.com (93-61-96-190.ip145.fastwebnet.it\n\t[93.61.96.190])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 179C2F6;\n\tFri, 18 Apr 2025 12:13:50 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"mAs7FZPg\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1744971231;\n\tbh=mW5thuFS9memtxme2wAQCwrrD6yDdK+aVPCC+8+/CL4=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=mAs7FZPgNDsu/6CGBSLBMY8PgrxjNCr/W+I91Vr7U1B2ZRxqLi+oPFU8onsI4jUk2\n\t+itSYPQJoqY/7G0MCWKnvnc2z9PvVSgTbsHA7rrxolQ27L8RwYsKuKfoKEYB2IVhBX\n\tgjA2mvw5O3RC1osWl1Zq47xT6Y60iikTzFXR/JtA=","Date":"Fri, 18 Apr 2025 12:15:52 +0200","From":"Jacopo Mondi <jacopo.mondi@ideasonboard.com>","To":"=?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= <barnabas.pocze@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Subject":"Re: [PATCH v1 1/3] libcamera: controls: Give name to the union\n\tcontaining storage","Message-ID":"<zaza3ubqdspyctvt6jb42dha5kocv7vsq45fytmw5i2rjvr72s@5nixjc2z2b7r>","References":"<20250417113539.1137936-1-barnabas.pocze@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","Content-Transfer-Encoding":"8bit","In-Reply-To":"<20250417113539.1137936-1-barnabas.pocze@ideasonboard.com>","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]