From patchwork Thu Nov 19 16:39:44 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 29042 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 D317DC3355 for ; Thu, 19 Nov 2026 16:40:03 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 14F5E6A939; Thu, 19 Nov 2026 17:39:58 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="F9bcqrDo"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 0D3D36A923 for ; Thu, 19 Nov 2026 17:39:54 +0100 (CET) Received: from [192.168.1.7] (unknown [93.65.100.155]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id CB4211F1A; Thu, 19 Nov 2026 17:37:24 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1795106244; bh=3Li6ZKxdCZbaoX6b0BxnfDcODmfXlkiGCK892e04vmQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=F9bcqrDox1hUDzC7nqyn6YQZb/gYhe2Khy2c1UD301GCwHr3Dy6QWGdnIeoJauVB3 ffnF4cRI0PoDjyZ7XKpGRdKMVuiXn4PW85KCgBM7XL7hO0eJ/CXR4qwPGWT6fqJzcI rfGhyafLisRFZT4IyPR+X9iTxWg4ctzpMHMfr1+Y= From: Jacopo Mondi Date: Thu, 19 Nov 2026 17:39:44 +0100 Subject: [PATCH 2/6] libcamera: media_device: Align the two loops in match() MIME-Version: 1.0 Message-Id: <20261119-multicontext-prep-v1-2-f68ea7ef960f@ideasonboard.com> References: <20261119-multicontext-prep-v1-0-f68ea7ef960f@ideasonboard.com> In-Reply-To: <20261119-multicontext-prep-v1-0-f68ea7ef960f@ideasonboard.com> To: libcamera-devel@lists.libcamera.org Cc: Jacopo Mondi X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2903; i=jacopo.mondi@ideasonboard.com; h=from:subject:message-id; bh=3Li6ZKxdCZbaoX6b0BxnfDcODmfXlkiGCK892e04vmQ=; b=owEBbQKS/ZANAwAKAXI0Bo8WoVY8AcsmYgBq/yZYNey6LD+1Dm8JOmSer1guPB4A7JalRO2jA MDY4YMUd7WJAjMEAAEKAB0WIQS1xD1IgJogio9YOMByNAaPFqFWPAUCav8mWAAKCRByNAaPFqFW PKW3D/9K45cj+wuBeRoJik3R1MK1+/NkzJpui+PpdHlCbKMbhPDDwAwNgfs4hC+hTnny6H523fD Up2FQTajmhjCxrJG7A3q1l3NYdSMw9KR1LrmZsvGXgQuw8lFWuAgPeN+oBIpo6O/rrLtQpn1YEz xI2qXrGR66vYtBzBGZb4fhoKwulM1nrQmDqwTeShxrQHWzjuCFW5RelxVLP4kJlUdFMWVvFoxRx d04dZV/mI8mgD+LFKICHTwSHLab3DdUnVpD0ILI5yUi5/sDkahu4A64mgbKl71SAr/9TOcuGszm sPKpmuxjj4acsEY8d9bvs8VaiM+kugWVDBIpzJC7yfvSRMdKwj5mGETMRRbMWecELKNMtz1BdXG PHk9DMvNOYSNUoqUSsNsPcXv1mX8HrER4MWIIS2aOQ5iarjjwBXAUfJTUtuuTCbMsI2r+84xmqj Taxnc+pJ48mdIOcpXiFO6y39dJox1JAcr7o0+cPX3O4hfcN2JkuZA9sqmAA+wDNRLq7R8uLRWex BBZStr9CmzIrsKNfXuvwxjwNiw3KaiL+L3cLreBLTTadVtjdmjPracW7UcOvvByE68GKrOml5rT afoJbtSZK216Csgc6yqwjrt32Db6tfQH1WTQ+fAFlk5t4sphVmqWY6oWaGEMmZsLU0EEQlpxLbZ RPPqvtqGFz+JOnw== X-Developer-Key: i=jacopo.mondi@ideasonboard.com; a=openpgp; fpr=72392EDC88144A65C701EA9BA5826A2587AD026B 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" Align the implementations of the two search loops to be identical by using a lambda function. The only difference between the two existing implementation is that with string-based matching as soon as we find a match we can break from the loop, while regex-based matching needs to walk all entities even if a match is found to make sure no multiple matches happen. To support both behaviours pass to the lambda a boolean flag that allows to interrupt the loop as soon as a match is found. Signed-off-by: Jacopo Mondi --- src/libcamera/media_device.cpp | 57 +++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 29 deletions(-) diff --git a/src/libcamera/media_device.cpp b/src/libcamera/media_device.cpp index b50d7328e47c..a84bc9abc4e9 100644 --- a/src/libcamera/media_device.cpp +++ b/src/libcamera/media_device.cpp @@ -305,39 +305,14 @@ bool MediaDevice::match(const DeviceMatch &device) const if (driver_ != device.driver()) return false; - for (const std::string &name : device.entitiesNames()) { - bool found = false; - - for (const MediaEntity *entity : entities()) { - if (name == entity->name()) { - if (!entity->deviceNode().empty()) { - found = true; - break; - } else { - LOG(MediaDevice, Debug) - << "Skip " << entity->name() - << ": no device node"; - } - } - } - - if (!found) - return false; - } - - for (const std::regex &nameRegex : device.entitiesRegexs()) { + const auto searchEntities = [&](const auto &searchPattern, + const auto &searchFn, bool breakEarly) { bool found = false; for (const MediaEntity *entity : entities()) { - if (!std::regex_search(entity->name(), nameRegex)) + if (!searchFn(searchPattern, entity->name())) continue; - if (found) { - LOG(MediaDevice, Error) - << "Multiple entities match regex"; - return false; - } - if (entity->deviceNode().empty()) { LOG(MediaDevice, Debug) << "Skip " << entity->name() @@ -345,10 +320,34 @@ bool MediaDevice::match(const DeviceMatch &device) const continue; } + if (found) { + LOG(MediaDevice, Error) + << "Multiple entities match " + << entity->name(); + return false; + } + found = true; + if (breakEarly) + break; } - if (!found) + return found; + }; + + for (const std::string &name : device.entitiesNames()) { + if (!searchEntities(name, [](const std::string &s, + const std::string &e) { + return s == e; + }, true)) + return false; + } + + for (const std::regex &nameRegex : device.entitiesRegexs()) { + if (!searchEntities(nameRegex, [](const std::regex &r, + const std::string &e) { + return std::regex_search(e, r); + }, false)) return false; }