[{"id":33274,"web_url":"https://patchwork.libcamera.org/comment/33274/","msgid":"<ck7wz2cpykenqof4fbvd7kncxynjkdhm7tpixkwdcytxzneybw@yr25qez6sdjo>","date":"2025-02-03T18:43:28","subject":"Re: [RFC PATCH v2 9/9] libcamera: base: log: Store categories in\n\tlist","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 Mon, Feb 03, 2025 at 05:59:39PM +0000, Barnabás Pőcze wrote:\n> Store the `LogCategory` objects in an `std::list`. This eliminates\n> the need for manually deleting them in the destructor while guaranteeing\n> address stability.\n>\n> Signed-off-by: Barnabás Pőcze <pobrn@protonmail.com>\n> ---\n>  src/libcamera/base/log.cpp | 23 ++++++++++-------------\n>  1 file changed, 10 insertions(+), 13 deletions(-)\n>\n> diff --git a/src/libcamera/base/log.cpp b/src/libcamera/base/log.cpp\n> index 69aa16c4c..003e2cf3a 100644\n> --- a/src/libcamera/base/log.cpp\n> +++ b/src/libcamera/base/log.cpp\n> @@ -407,7 +407,7 @@ private:\n>  \tstatic bool destroyed_;\n>\n>  \tMutex mutex_;\n> -\tstd::vector<LogCategory *> categories_;\n> +\tstd::list<LogCategory> categories_;\n\nlooks like the file didn't include <vector> !\n\n>  \tstd::list<std::pair<std::string, LogSeverity>> levels_;\n>\n>  \tstd::shared_ptr<LogOutput> output_;\n> @@ -524,9 +524,6 @@ void logSetLevel(const char *category, const char *level)\n>  Logger::~Logger()\n>  {\n>  \tdestroyed_ = true;\n> -\n> -\tfor (LogCategory *category : categories_)\n> -\t\tdelete category;\n>  }\n>\n>  /**\n> @@ -659,9 +656,9 @@ void Logger::logSetLevel(const char *category, const char *level)\n>\n>  \tMutexLocker locker(mutex_);\n>\n> -\tfor (LogCategory *c : categories_) {\n> -\t\tif (c->name() == category) {\n> -\t\t\tc->setSeverity(severity);\n> +\tfor (LogCategory &c : categories_) {\n> +\t\tif (c.name() == category) {\n> +\t\t\tc.setSeverity(severity);\n>  \t\t\tbreak;\n>  \t\t}\n>  \t}\n> @@ -717,12 +714,12 @@ LogCategory *Logger::findOrCreateCategory(std::string_view name)\n>  {\n>  \tMutexLocker locker(mutex_);\n>\n> -\tfor (LogCategory *category : categories_) {\n> -\t\tif (category->name() == name)\n> -\t\t\treturn category;\n> +\tfor (LogCategory &category : categories_) {\n> +\t\tif (category.name() == name)\n> +\t\t\treturn &category;\n>  \t}\n>\n> -\tLogCategory *category = categories_.emplace_back(new LogCategory(name));\n> +\tLogCategory &category = categories_.emplace_back(name);\n>\n>  \tfor (const std::pair<std::string, LogSeverity> &level : levels_) {\n>  \t\tbool match = true;\n> @@ -739,12 +736,12 @@ LogCategory *Logger::findOrCreateCategory(std::string_view name)\n>  \t\t}\n>\n>  \t\tif (match) {\n> -\t\t\tcategory->setSeverity(level.second);\n> +\t\t\tcategory.setSeverity(level.second);\n>  \t\t\tbreak;\n>  \t\t}\n>  \t}\n>\n> -\treturn category;\n> +\treturn &category;\n\nReviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>\n\nThanks\n  j\n\n>  }\n>\n>  /**\n> --\n> 2.48.1\n>\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 0DF39BD80A\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon,  3 Feb 2025 18:43:34 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 108F5685A4;\n\tMon,  3 Feb 2025 19:43:33 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id A24C661876\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon,  3 Feb 2025 19:43:31 +0100 (CET)","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 F1B35497;\n\tMon,  3 Feb 2025 19:42:19 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"WSOphAjv\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1738608140;\n\tbh=FbnOXLuKZJWruDs+KPxsre1j5pyXwicEec9LfeCG1iw=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=WSOphAjvsK7NQIO4IBCaEE/eL3+Qm6o+1jL+TOyH/CXgJwFY/0rz2PxEqrBF+eZ3g\n\tqF1mlTvPN//SD8jb1PBwvD4H2yg0aJIOizUvUFAiJPd8YeMA3p8l5ekFoxiGNQVM2U\n\tJiy5vjiXjg5qIq0hdkLbrwuk2t0mMf0vSzKbG7M4=","Date":"Mon, 3 Feb 2025 19:43:28 +0100","From":"Jacopo Mondi <jacopo.mondi@ideasonboard.com>","To":"=?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= <pobrn@protonmail.com>","Cc":"libcamera-devel@lists.libcamera.org","Subject":"Re: [RFC PATCH v2 9/9] libcamera: base: log: Store categories in\n\tlist","Message-ID":"<ck7wz2cpykenqof4fbvd7kncxynjkdhm7tpixkwdcytxzneybw@yr25qez6sdjo>","References":"<20250130195811.1230581-1-pobrn@protonmail.com>\n\t<20250203175936.206161-1-pobrn@protonmail.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","Content-Transfer-Encoding":"8bit","In-Reply-To":"<20250203175936.206161-1-pobrn@protonmail.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>"}},{"id":33310,"web_url":"https://patchwork.libcamera.org/comment/33310/","msgid":"<20250206171630.GC12400@pendragon.ideasonboard.com>","date":"2025-02-06T17:16:30","subject":"Re: [RFC PATCH v2 9/9] libcamera: base: log: Store categories in\n\tlist","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Barnabás,\n\nThank you for the patch.\n\nOn Mon, Feb 03, 2025 at 05:59:39PM +0000, Barnabás Pőcze wrote:\n> Store the `LogCategory` objects in an `std::list`. This eliminates\n> the need for manually deleting them in the destructor while guaranteeing\n> address stability.\n> \n> Signed-off-by: Barnabás Pőcze <pobrn@protonmail.com>\n> ---\n>  src/libcamera/base/log.cpp | 23 ++++++++++-------------\n>  1 file changed, 10 insertions(+), 13 deletions(-)\n> \n> diff --git a/src/libcamera/base/log.cpp b/src/libcamera/base/log.cpp\n> index 69aa16c4c..003e2cf3a 100644\n> --- a/src/libcamera/base/log.cpp\n> +++ b/src/libcamera/base/log.cpp\n> @@ -407,7 +407,7 @@ private:\n>  \tstatic bool destroyed_;\n>  \n>  \tMutex mutex_;\n> -\tstd::vector<LogCategory *> categories_;\n> +\tstd::list<LogCategory> categories_;\n\nWill this cause two allocations, one for the list node, and one for the\nLogCategory instance ?\n\nIterating over a vector should also be (slightly) more efficient than\niterating over a list. What is the gola of this patch ? If it is only to\navoid manual deletion, we can also store unique pointers in the vector.\n\n>  \tstd::list<std::pair<std::string, LogSeverity>> levels_;\n>  \n>  \tstd::shared_ptr<LogOutput> output_;\n> @@ -524,9 +524,6 @@ void logSetLevel(const char *category, const char *level)\n>  Logger::~Logger()\n>  {\n>  \tdestroyed_ = true;\n> -\n> -\tfor (LogCategory *category : categories_)\n> -\t\tdelete category;\n>  }\n>  \n>  /**\n> @@ -659,9 +656,9 @@ void Logger::logSetLevel(const char *category, const char *level)\n>  \n>  \tMutexLocker locker(mutex_);\n>  \n> -\tfor (LogCategory *c : categories_) {\n> -\t\tif (c->name() == category) {\n> -\t\t\tc->setSeverity(severity);\n> +\tfor (LogCategory &c : categories_) {\n> +\t\tif (c.name() == category) {\n> +\t\t\tc.setSeverity(severity);\n>  \t\t\tbreak;\n>  \t\t}\n>  \t}\n> @@ -717,12 +714,12 @@ LogCategory *Logger::findOrCreateCategory(std::string_view name)\n>  {\n>  \tMutexLocker locker(mutex_);\n>  \n> -\tfor (LogCategory *category : categories_) {\n> -\t\tif (category->name() == name)\n> -\t\t\treturn category;\n> +\tfor (LogCategory &category : categories_) {\n> +\t\tif (category.name() == name)\n> +\t\t\treturn &category;\n>  \t}\n>  \n> -\tLogCategory *category = categories_.emplace_back(new LogCategory(name));\n> +\tLogCategory &category = categories_.emplace_back(name);\n>  \n>  \tfor (const std::pair<std::string, LogSeverity> &level : levels_) {\n>  \t\tbool match = true;\n> @@ -739,12 +736,12 @@ LogCategory *Logger::findOrCreateCategory(std::string_view name)\n>  \t\t}\n>  \n>  \t\tif (match) {\n> -\t\t\tcategory->setSeverity(level.second);\n> +\t\t\tcategory.setSeverity(level.second);\n>  \t\t\tbreak;\n>  \t\t}\n>  \t}\n>  \n> -\treturn category;\n> +\treturn &category;\n>  }\n>  \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 CE6AFC32EF\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu,  6 Feb 2025 17:16:37 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 93344685F9;\n\tThu,  6 Feb 2025 18:16:36 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 52DFE685EB\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu,  6 Feb 2025 18:16:35 +0100 (CET)","from pendragon.ideasonboard.com (81-175-209-231.bb.dnainternet.fi\n\t[81.175.209.231])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 6889F1198;\n\tThu,  6 Feb 2025 18:15:21 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"OSL2oXIA\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1738862121;\n\tbh=LusQoMeG1sUv917KyGJREMvlAShl5O5Ezrq0+stm4is=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=OSL2oXIApUJmD1KVo0vDooOidM3m8YJAaWb4x+UJ74scgHiwIpJGJDSwyPqERycam\n\t/iRvQWKf6WoaP9m6n287/TCFmFqRYip5bAbhg8DSzcvG8soz5+FjuBx0NOF0UsLOxt\n\tphX4Q9Wq5ystiTLEq83BJMBzFAvoeW+Hs3ERXsP8=","Date":"Thu, 6 Feb 2025 19:16:30 +0200","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"=?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= <pobrn@protonmail.com>","Cc":"libcamera-devel@lists.libcamera.org","Subject":"Re: [RFC PATCH v2 9/9] libcamera: base: log: Store categories in\n\tlist","Message-ID":"<20250206171630.GC12400@pendragon.ideasonboard.com>","References":"<20250130195811.1230581-1-pobrn@protonmail.com>\n\t<20250203175936.206161-1-pobrn@protonmail.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","Content-Transfer-Encoding":"8bit","In-Reply-To":"<20250203175936.206161-1-pobrn@protonmail.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>"}},{"id":33318,"web_url":"https://patchwork.libcamera.org/comment/33318/","msgid":"<gAnKWM8jpgoFIXRZWXM9fbuBsG1KsFbxz2uCoBIivyW-aMNVyMNuMCDeAP-FEzFR-Y4j-gpNV0dhC4SZ0s4wSMuZ2d8Ihc6rx7VnS3dKvGo=@protonmail.com>","date":"2025-02-06T17:47:59","subject":"Re: [RFC PATCH v2 9/9] libcamera: base: log: Store categories in\n\tlist","submitter":{"id":133,"url":"https://patchwork.libcamera.org/api/people/133/","name":"Pőcze Barnabás","email":"pobrn@protonmail.com"},"content":"Hi\n\n\n2025. február 6., csütörtök 18:16 keltezéssel, Laurent Pinchart <laurent.pinchart@ideasonboard.com> írta:\n\n> Hi Barnabás,\n> \n> Thank you for the patch.\n> \n> On Mon, Feb 03, 2025 at 05:59:39PM +0000, Barnabás Pőcze wrote:\n> > Store the `LogCategory` objects in an `std::list`. This eliminates\n> > the need for manually deleting them in the destructor while guaranteeing\n> > address stability.\n> >\n> > Signed-off-by: Barnabás Pőcze <pobrn@protonmail.com>\n> > ---\n> >  src/libcamera/base/log.cpp | 23 ++++++++++-------------\n> >  1 file changed, 10 insertions(+), 13 deletions(-)\n> >\n> > diff --git a/src/libcamera/base/log.cpp b/src/libcamera/base/log.cpp\n> > index 69aa16c4c..003e2cf3a 100644\n> > --- a/src/libcamera/base/log.cpp\n> > +++ b/src/libcamera/base/log.cpp\n> > @@ -407,7 +407,7 @@ private:\n> >  \tstatic bool destroyed_;\n> >\n> >  \tMutex mutex_;\n> > -\tstd::vector<LogCategory *> categories_;\n> > +\tstd::list<LogCategory> categories_;\n> \n> Will this cause two allocations, one for the list node, and one for the\n> LogCategory instance ?\n\nWith std::list there will be a single allocation for each category.\n\n\n> \n> Iterating over a vector should also be (slightly) more efficient than\n> iterating over a list. What is the gola of this patch ? If it is only to\n> avoid manual deletion, we can also store unique pointers in the vector.\n\nTo avoid manual deletion. A vector was another option that I have considered,\nbut in the end I went with a list because it's less code and the types look cleaner.\nI'm not sure if there is much difference between std::list<T> and\nstd::vector<std::unique_ptr<T>> since both options kind of require one indirection\nwhen going to the next element.\n\n\n> \n> >  \tstd::list<std::pair<std::string, LogSeverity>> levels_;\n> >\n> >  \tstd::shared_ptr<LogOutput> output_;\n> > @@ -524,9 +524,6 @@ void logSetLevel(const char *category, const char *level)\n> >  Logger::~Logger()\n> >  {\n> >  \tdestroyed_ = true;\n> > -\n> > -\tfor (LogCategory *category : categories_)\n> > -\t\tdelete category;\n> >  }\n> >\n> >  /**\n> > @@ -659,9 +656,9 @@ void Logger::logSetLevel(const char *category, const char *level)\n> >\n> >  \tMutexLocker locker(mutex_);\n> >\n> > -\tfor (LogCategory *c : categories_) {\n> > -\t\tif (c->name() == category) {\n> > -\t\t\tc->setSeverity(severity);\n> > +\tfor (LogCategory &c : categories_) {\n> > +\t\tif (c.name() == category) {\n> > +\t\t\tc.setSeverity(severity);\n> >  \t\t\tbreak;\n> >  \t\t}\n> >  \t}\n> > @@ -717,12 +714,12 @@ LogCategory *Logger::findOrCreateCategory(std::string_view name)\n> >  {\n> >  \tMutexLocker locker(mutex_);\n> >\n> > -\tfor (LogCategory *category : categories_) {\n> > -\t\tif (category->name() == name)\n> > -\t\t\treturn category;\n> > +\tfor (LogCategory &category : categories_) {\n> > +\t\tif (category.name() == name)\n> > +\t\t\treturn &category;\n> >  \t}\n> >\n> > -\tLogCategory *category = categories_.emplace_back(new LogCategory(name));\n> > +\tLogCategory &category = categories_.emplace_back(name);\n> >\n> >  \tfor (const std::pair<std::string, LogSeverity> &level : levels_) {\n> >  \t\tbool match = true;\n> > @@ -739,12 +736,12 @@ LogCategory *Logger::findOrCreateCategory(std::string_view name)\n> >  \t\t}\n> >\n> >  \t\tif (match) {\n> > -\t\t\tcategory->setSeverity(level.second);\n> > +\t\t\tcategory.setSeverity(level.second);\n> >  \t\t\tbreak;\n> >  \t\t}\n> >  \t}\n> >\n> > -\treturn category;\n> > +\treturn &category;\n> >  }\n> >\n> >  /**\n> \n> --\n> Regards,\n> \n> Laurent Pinchart\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 5C801C32EF\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu,  6 Feb 2025 17:48:08 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 0EAC868602;\n\tThu,  6 Feb 2025 18:48:08 +0100 (CET)","from mail-40131.protonmail.ch (mail-40131.protonmail.ch\n\t[185.70.40.131])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id E0D88685F6\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu,  6 Feb 2025 18:48:06 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (2048-bit key;\n\tunprotected) header.d=protonmail.com header.i=@protonmail.com\n\theader.b=\"kTE00dRi\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com;\n\ts=protonmail3; t=1738864086; x=1739123286;\n\tbh=1CgSOIPuRC3yxDLeLX7zEg+U8ZSmvH9lS+7ihQUMHrM=;\n\th=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References:\n\tFeedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID:\n\tMessage-ID:BIMI-Selector:List-Unsubscribe:List-Unsubscribe-Post;\n\tb=kTE00dRi4am87oAd5Vf8cGrFN+l/6/nQL68hsnjO9Zvtk+heYhR7j3+dTCYkRh1ai\n\t2R75BQGGvPZjm6Ej3mBfoy4xBh27yeXZ0vjjwkx7LWTdwS9GkvvKekHllnl2DJMRor\n\tteYrpOHmpfu753thOonqsYU8oyI454pzYVsWM5yP/hgLJORWiMupIKbiIcbHZbIGgg\n\thXvWGqUfgrArPMYqx+GK41Xt+fBMzD6fgyyNm58nZBlcX/EGDe+AiTJrRm3SsUDFPW\n\t5RsWBTHgpliqXHKTyupZLMr0E7QjWrT+/UgqNr2nKo8EI3AGRZxFjauOaZKDTIDucG\n\tETHT962GzRTug==","Date":"Thu, 06 Feb 2025 17:47:59 +0000","To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","From":"=?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= <pobrn@protonmail.com>","Cc":"libcamera-devel@lists.libcamera.org","Subject":"Re: [RFC PATCH v2 9/9] libcamera: base: log: Store categories in\n\tlist","Message-ID":"<gAnKWM8jpgoFIXRZWXM9fbuBsG1KsFbxz2uCoBIivyW-aMNVyMNuMCDeAP-FEzFR-Y4j-gpNV0dhC4SZ0s4wSMuZ2d8Ihc6rx7VnS3dKvGo=@protonmail.com>","In-Reply-To":"<20250206171630.GC12400@pendragon.ideasonboard.com>","References":"<20250130195811.1230581-1-pobrn@protonmail.com>\n\t<20250203175936.206161-1-pobrn@protonmail.com>\n\t<20250206171630.GC12400@pendragon.ideasonboard.com>","Feedback-ID":"20568564:user:proton","X-Pm-Message-ID":"1fa32919b91759bb37fafa104d42d293fd5fd606","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Transfer-Encoding":"quoted-printable","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":33321,"web_url":"https://patchwork.libcamera.org/comment/33321/","msgid":"<20250207094200.GG24886@pendragon.ideasonboard.com>","date":"2025-02-07T09:42:00","subject":"Re: [RFC PATCH v2 9/9] libcamera: base: log: Store categories in\n\tlist","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"On Thu, Feb 06, 2025 at 05:47:59PM +0000, Barnabás Pőcze wrote:\n> 2025. február 6., csütörtök 18:16 keltezéssel, Laurent Pinchart írta:\n> > On Mon, Feb 03, 2025 at 05:59:39PM +0000, Barnabás Pőcze wrote:\n> > > Store the `LogCategory` objects in an `std::list`. This eliminates\n> > > the need for manually deleting them in the destructor while guaranteeing\n> > > address stability.\n> > >\n> > > Signed-off-by: Barnabás Pőcze <pobrn@protonmail.com>\n> > > ---\n> > >  src/libcamera/base/log.cpp | 23 ++++++++++-------------\n> > >  1 file changed, 10 insertions(+), 13 deletions(-)\n> > >\n> > > diff --git a/src/libcamera/base/log.cpp b/src/libcamera/base/log.cpp\n> > > index 69aa16c4c..003e2cf3a 100644\n> > > --- a/src/libcamera/base/log.cpp\n> > > +++ b/src/libcamera/base/log.cpp\n> > > @@ -407,7 +407,7 @@ private:\n> > >  \tstatic bool destroyed_;\n> > >\n> > >  \tMutex mutex_;\n> > > -\tstd::vector<LogCategory *> categories_;\n> > > +\tstd::list<LogCategory> categories_;\n> > \n> > Will this cause two allocations, one for the list node, and one for the\n> > LogCategory instance ?\n> \n> With std::list there will be a single allocation for each category.\n\nI haven't found this being documented in\nhttps://en.cppreference.com/w/cpp/container/list but I agree that's how\nthe libstdc++ and libc++ implementations behave.\n\n> > Iterating over a vector should also be (slightly) more efficient than\n> > iterating over a list. What is the gola of this patch ? If it is only to\n> > avoid manual deletion, we can also store unique pointers in the vector.\n> \n> To avoid manual deletion. A vector was another option that I have considered,\n> but in the end I went with a list because it's less code and the types look cleaner.\n> I'm not sure if there is much difference between std::list<T> and\n> std::vector<std::unique_ptr<T>> since both options kind of require one indirection\n> when going to the next element.\n\nIt probably won't make much of a difference indeed.\n\nI'm OK with this patch, but I think you'll need to call\ncategories_.clear() explicitly in the destructor with the mutex locked,\nor TSA should complain.\n\n> > >  \tstd::list<std::pair<std::string, LogSeverity>> levels_;\n> > >\n> > >  \tstd::shared_ptr<LogOutput> output_;\n> > > @@ -524,9 +524,6 @@ void logSetLevel(const char *category, const char *level)\n> > >  Logger::~Logger()\n> > >  {\n> > >  \tdestroyed_ = true;\n> > > -\n> > > -\tfor (LogCategory *category : categories_)\n> > > -\t\tdelete category;\n> > >  }\n> > >\n> > >  /**\n> > > @@ -659,9 +656,9 @@ void Logger::logSetLevel(const char *category, const char *level)\n> > >\n> > >  \tMutexLocker locker(mutex_);\n> > >\n> > > -\tfor (LogCategory *c : categories_) {\n> > > -\t\tif (c->name() == category) {\n> > > -\t\t\tc->setSeverity(severity);\n> > > +\tfor (LogCategory &c : categories_) {\n> > > +\t\tif (c.name() == category) {\n> > > +\t\t\tc.setSeverity(severity);\n> > >  \t\t\tbreak;\n> > >  \t\t}\n> > >  \t}\n> > > @@ -717,12 +714,12 @@ LogCategory *Logger::findOrCreateCategory(std::string_view name)\n> > >  {\n> > >  \tMutexLocker locker(mutex_);\n> > >\n> > > -\tfor (LogCategory *category : categories_) {\n> > > -\t\tif (category->name() == name)\n> > > -\t\t\treturn category;\n> > > +\tfor (LogCategory &category : categories_) {\n> > > +\t\tif (category.name() == name)\n> > > +\t\t\treturn &category;\n> > >  \t}\n> > >\n> > > -\tLogCategory *category = categories_.emplace_back(new LogCategory(name));\n> > > +\tLogCategory &category = categories_.emplace_back(name);\n> > >\n> > >  \tfor (const std::pair<std::string, LogSeverity> &level : levels_) {\n> > >  \t\tbool match = true;\n> > > @@ -739,12 +736,12 @@ LogCategory *Logger::findOrCreateCategory(std::string_view name)\n> > >  \t\t}\n> > >\n> > >  \t\tif (match) {\n> > > -\t\t\tcategory->setSeverity(level.second);\n> > > +\t\t\tcategory.setSeverity(level.second);\n> > >  \t\t\tbreak;\n> > >  \t\t}\n> > >  \t}\n> > >\n> > > -\treturn category;\n> > > +\treturn &category;\n> > >  }\n> > >\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 D0E40C32F5\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri,  7 Feb 2025 09:42:09 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id B990E68600;\n\tFri,  7 Feb 2025 10:42:08 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id A2B18685BE\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri,  7 Feb 2025 10:42:06 +0100 (CET)","from pendragon.ideasonboard.com (81-175-209-231.bb.dnainternet.fi\n\t[81.175.209.231])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 47AF42BA;\n\tFri,  7 Feb 2025 10:40:52 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"Ov6vjHo3\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1738921252;\n\tbh=Fnt0C9Ng94jsoxzR7oTEn65BIe/NKnr0qe+5bMBxAQo=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=Ov6vjHo3X3055zD1YUh8LFCiW+kwjhq+IMroNMoHQZlesYhu3SGU/gY/y4fhskZXR\n\tJoML2VuQuba8LZu3L8vThL9Y4nKYoZk6QEYLAX4WeuBBxS7GBQRh1ETrPZGFRCoiLA\n\tgTr3lgxPaOKAvv54moUnvzfYdQrO6KcTNP8M1+Y0=","Date":"Fri, 7 Feb 2025 11:42:00 +0200","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"=?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= <pobrn@protonmail.com>","Cc":"libcamera-devel@lists.libcamera.org","Subject":"Re: [RFC PATCH v2 9/9] libcamera: base: log: Store categories in\n\tlist","Message-ID":"<20250207094200.GG24886@pendragon.ideasonboard.com>","References":"<20250130195811.1230581-1-pobrn@protonmail.com>\n\t<20250203175936.206161-1-pobrn@protonmail.com>\n\t<20250206171630.GC12400@pendragon.ideasonboard.com>\n\t<gAnKWM8jpgoFIXRZWXM9fbuBsG1KsFbxz2uCoBIivyW-aMNVyMNuMCDeAP-FEzFR-Y4j-gpNV0dhC4SZ0s4wSMuZ2d8Ihc6rx7VnS3dKvGo=@protonmail.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","Content-Transfer-Encoding":"8bit","In-Reply-To":"<gAnKWM8jpgoFIXRZWXM9fbuBsG1KsFbxz2uCoBIivyW-aMNVyMNuMCDeAP-FEzFR-Y4j-gpNV0dhC4SZ0s4wSMuZ2d8Ihc6rx7VnS3dKvGo=@protonmail.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>"}}]