{"id":2646,"url":"https://patchwork.libcamera.org/api/patches/2646/?format=json","web_url":"https://patchwork.libcamera.org/patch/2646/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20200114163256.39236-1-jacopo@jmondi.org>","date":"2020-01-14T16:32:56","name":"[libcamera-devel,v1.1,17/23] libcamera: control: Deep-copy control values","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"23f8c7022c0336526316afd7b2e0534943fac4bf","submitter":{"id":3,"url":"https://patchwork.libcamera.org/api/people/3/?format=json","name":"Jacopo Mondi","email":"jacopo@jmondi.org"},"delegate":{"id":15,"url":"https://patchwork.libcamera.org/api/users/15/?format=json","username":"jmondi","first_name":"Jacopo","last_name":"Mondi","email":"jacopo@jmondi.org"},"mbox":"https://patchwork.libcamera.org/patch/2646/mbox/","series":[{"id":624,"url":"https://patchwork.libcamera.org/api/series/624/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=624","date":"2020-01-14T16:31:53","name":null,"version":1,"mbox":"https://patchwork.libcamera.org/series/624/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/2646/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/2646/checks/","tags":{},"headers":{"Return-Path":"<jacopo@jmondi.org>","Received":["from relay7-d.mail.gandi.net (relay7-d.mail.gandi.net\n\t[217.70.183.200])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 02A866074F\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 14 Jan 2020 17:30:35 +0100 (CET)","from uno.lan (2-224-242-101.ip172.fastwebnet.it [2.224.242.101])\n\t(Authenticated sender: jacopo@jmondi.org)\n\tby relay7-d.mail.gandi.net (Postfix) with ESMTPSA id 984CE2001B;\n\tTue, 14 Jan 2020 16:30:34 +0000 (UTC)"],"X-Originating-IP":"2.224.242.101","From":"Jacopo Mondi <jacopo@jmondi.org>","To":"libcamera-devel@lists.libcamera.org","Date":"Tue, 14 Jan 2020 17:32:56 +0100","Message-Id":"<20200114163256.39236-1-jacopo@jmondi.org>","X-Mailer":"git-send-email 2.24.1","In-Reply-To":"<20200113164245.52535-18-jacopo@jmondi.org>","References":"<20200113164245.52535-18-jacopo@jmondi.org>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v1.1 17/23] libcamera: control: Deep-copy\n\tcontrol values","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>","X-List-Received-Date":"Tue, 14 Jan 2020 16:30:35 -0000"},"content":"Implement operator=() and copy constructor for the ControlValue class,\ndeep-copying the content of the 'other' ControlValue by using\nControlValue::set() operation which, in case of compound ControlValue,\nguarantees proper memory allocation and data copy.\n\nSigned-off-by: Jacopo Mondi <jacopo@jmondi.org>\n---\nValgrind reports the usage of unitialized value in ControlValue::relase()\n\nThis was caused by the copy constructor calling set<>() which calls release()\nbefore the type of the control values was initialized (we're at construction\ntime)\n\nFix this by initializing type before calling operator=()\n\n--\n2.24.1","diff":"--- a/src/libcamera/controls.cpp\n+++ b/src/libcamera/controls.cpp\n@@ -201,6 +201,11 @@ void ControlValue::copyValue(const ControlValue &other)\n  */\n ControlValue::ControlValue(const ControlValue &other)\n {\n+       /*\n+        * Initialze the type: operator=() calls set() which calls release()\n+        * which inspects the control value type. Be safe and set it to none.\n+        */\n+       type_ = ControlTypeNone;\n        *this = other;\n }\n\n---\n include/libcamera/controls.h |  5 +++\n src/libcamera/controls.cpp   | 67 ++++++++++++++++++++++++++++++++++++\n 2 files changed, 72 insertions(+)\n\ndiff --git a/include/libcamera/controls.h b/include/libcamera/controls.h\nindex bdbdb213528d..d13144b69198 100644\n--- a/include/libcamera/controls.h\n+++ b/include/libcamera/controls.h\n@@ -43,6 +43,9 @@ public:\n \tControlValue(Span<float> &values);\n \t~ControlValue();\n\n+\tControlValue(const ControlValue &other);\n+\tControlValue &operator=(const ControlValue &other);\n+\n \tControlType type() const { return type_; }\n \tbool isNone() const { return type_ == ControlTypeNone; }\n \tstd::size_t numElements() const { return numElements_; }\n@@ -81,6 +84,8 @@ private:\n \tstd::size_t numElements_;\n\n \tvoid release();\n+\ttemplate<typename T>\n+\tvoid copyValue(const ControlValue &source);\n \tbool compareElement(const ControlValue &other) const;\n \tbool compareElement(const ControlValue &other, unsigned int i) const;\n \tstd::string elemToString(unsigned int i) const;\ndiff --git a/src/libcamera/controls.cpp b/src/libcamera/controls.cpp\nindex c311ab1d6af1..ddf666040ae7 100644\n--- a/src/libcamera/controls.cpp\n+++ b/src/libcamera/controls.cpp\n@@ -189,6 +189,73 @@ ControlValue::~ControlValue()\n \trelease();\n }\n\n+template<typename T>\n+void ControlValue::copyValue(const ControlValue &other)\n+{\n+\tset<T>(other.get<T>());\n+}\n+\n+/**\n+ * \\brief Contruct a ControlValue with the content of \\a other\n+ * \\param[in] other The ControlValue to copy content from\n+ */\n+ControlValue::ControlValue(const ControlValue &other)\n+{\n+\t/*\n+\t * Initialze the type: operator=() calls set() which calls release()\n+\t * which inspects the control value type. Be safe and set it to none.\n+\t */\n+\ttype_ = ControlTypeNone;\n+\t*this = other;\n+}\n+\n+/**\n+ * \\brief Replace the content of the ControlValue with the one of \\a other\n+ * \\param[in] other The ControlValue to copy content from\n+ *\n+ * Deep-copy the content of \\a other into the ControlValue by reserving memory\n+ * and copy data there in case \\a other transports arrays of values in one of\n+ * its pointer data members.\n+ *\n+ * \\return The ControlValue with its content replaced with the one of \\a other\n+ */\n+ControlValue &ControlValue::operator=(const ControlValue &other)\n+{\n+\tswitch (other.type_) {\n+\tcase ControlTypeBool:\n+\t\tcopyValue<bool>(other);\n+\t\tbreak;\n+\tcase ControlTypeInteger32:\n+\t\tcopyValue<int32_t>(other);\n+\t\tbreak;\n+\tcase ControlTypeInteger64:\n+\t\tcopyValue<int64_t>(other);\n+\t\tbreak;\n+\tcase ControlTypeFloat:\n+\t\tcopyValue<float>(other);\n+\t\tbreak;\n+\tcase ControlTypeCompoundBool:\n+\t\tcopyValue<Span<bool>>(other);\n+\t\tbreak;\n+\tcase ControlTypeCompoundInt32:\n+\t\tcopyValue<Span<int32_t>>(other);\n+\t\tbreak;\n+\tcase ControlTypeCompoundInt64:\n+\t\tcopyValue<Span<int64_t>>(other);\n+\t\tbreak;\n+\tcase ControlTypeCompoundFloat:\n+\t\tcopyValue<Span<float>>(other);\n+\t\tbreak;\n+\tdefault:\n+\t\tbreak;\n+\t}\n+\n+\ttype_ = other.type_;\n+\tnumElements_ = other.numElements_;\n+\n+\treturn *this;\n+}\n+\n /**\n  * \\fn ControlValue::type()\n  * \\brief Retrieve the data type of the value\n","prefixes":["libcamera-devel","v1.1","17/23"]}