{"id":14859,"url":"https://patchwork.libcamera.org/api/patches/14859/?format=json","web_url":"https://patchwork.libcamera.org/patch/14859/","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":"<20211130033820.18235-5-laurent.pinchart@ideasonboard.com>","date":"2021-11-30T03:38:02","name":"[libcamera-devel,v4,04/22] libcamera: base: Introduce UniqueFD","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"9f4cd34a46f46adcb946eaf0c24ca736d200cd70","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/14859/mbox/","series":[{"id":2776,"url":"https://patchwork.libcamera.org/api/series/2776/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=2776","date":"2021-11-30T03:37:58","name":"libcamera: Introduce UniqueFD","version":4,"mbox":"https://patchwork.libcamera.org/series/2776/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/14859/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/14859/checks/","tags":{},"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 46E0ABF415\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 30 Nov 2021 03:38:59 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id A6E3C60717;\n\tTue, 30 Nov 2021 04:38:58 +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 3FCE3605BA\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 30 Nov 2021 04:38:50 +0100 (CET)","from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id A43028F0;\n\tTue, 30 Nov 2021 04:38:49 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"EIDusvJE\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1638243529;\n\tbh=HilQ8jl7CA5hVx3Y5PCHmfR/5wqX0HIv0tjK5itDCxQ=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=EIDusvJE4/EQ7Fc7UcXtHF7F87bia+WVsgzPULLSg2Vlf3vGnWEWs8jpygY3JvJk9\n\t8swGXOni+SkMNHHG3HZHiIz+6PtpiZ4ZJ9o7gFK9lqTFvjkAuMXdXIFWeejtaco3Qc\n\toOhFXtldS350Pzys2RH2PuLh1grosAXMyE7jEQTM=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Tue, 30 Nov 2021 05:38:02 +0200","Message-Id":"<20211130033820.18235-5-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.32.0","In-Reply-To":"<20211130033820.18235-1-laurent.pinchart@ideasonboard.com>","References":"<20211130033820.18235-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v4 04/22] libcamera: base: Introduce\n\tUniqueFD","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>"},"content":"From: Hirokazu Honda <hiroh@chromium.org>\n\nThis introduces UniqueFD. It acts like unique_ptr to a file descriptor.\n\nSigned-off-by: Hirokazu Honda <hiroh@chromium.org>\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\nReviewed-by: Hirokazu Honda <hiroh@chromium.org>\n---\nChanges since v2:\n\n- Rename ScopedFD to UniqueFD\n- Inline most functions to allow compiler optimizations\n- Bring the API closer to unique_ptr<>\n- Add swap()\n- Documentation cleanups\n- Slip FileDescriptor constructor to separate patch\n- Fix isValid()\n---\n include/libcamera/base/meson.build |   1 +\n include/libcamera/base/unique_fd.h |  69 ++++++++++++++++\n src/libcamera/base/meson.build     |   1 +\n src/libcamera/base/unique_fd.cpp   | 123 +++++++++++++++++++++++++++++\n 4 files changed, 194 insertions(+)\n create mode 100644 include/libcamera/base/unique_fd.h\n create mode 100644 src/libcamera/base/unique_fd.cpp","diff":"diff --git a/include/libcamera/base/meson.build b/include/libcamera/base/meson.build\nindex f73b00917409..cca374a769cc 100644\n--- a/include/libcamera/base/meson.build\n+++ b/include/libcamera/base/meson.build\n@@ -22,6 +22,7 @@ libcamera_base_headers = files([\n     'span.h',\n     'thread.h',\n     'timer.h',\n+    'unique_fd.h',\n     'utils.h',\n ])\n \ndiff --git a/include/libcamera/base/unique_fd.h b/include/libcamera/base/unique_fd.h\nnew file mode 100644\nindex 000000000000..ae4d96b75797\n--- /dev/null\n+++ b/include/libcamera/base/unique_fd.h\n@@ -0,0 +1,69 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2021, Google Inc.\n+ *\n+ * unique_fd.h - File descriptor wrapper that owns a file descriptor.\n+ */\n+\n+#pragma once\n+\n+#include <utility>\n+\n+#include <libcamera/base/class.h>\n+#include <libcamera/base/compiler.h>\n+\n+namespace libcamera {\n+\n+class UniqueFD final\n+{\n+public:\n+\tUniqueFD()\n+\t\t: fd_(-1)\n+\t{\n+\t}\n+\n+\texplicit UniqueFD(int fd)\n+\t\t: fd_(fd)\n+\t{\n+\t}\n+\n+\tUniqueFD(UniqueFD &&other)\n+\t\t: fd_(other.release())\n+\t{\n+\t}\n+\n+\t~UniqueFD()\n+\t{\n+\t\treset();\n+\t}\n+\n+\tUniqueFD &operator=(UniqueFD &&other)\n+\t{\n+\t\treset(other.release());\n+\t\treturn *this;\n+\t}\n+\n+\t__nodiscard int release()\n+\t{\n+\t\tint fd = fd_;\n+\t\tfd_ = -1;\n+\t\treturn fd;\n+\t}\n+\n+\tvoid reset(int fd = -1);\n+\n+\tvoid swap(UniqueFD &other)\n+\t{\n+\t\tstd::swap(fd_, other.fd_);\n+\t}\n+\n+\tint get() const { return fd_; }\n+\tbool isValid() const { return fd_ >= 0; }\n+\n+private:\n+\tLIBCAMERA_DISABLE_COPY(UniqueFD)\n+\n+\tint fd_;\n+};\n+\n+} /* namespace libcamera */\ndiff --git a/src/libcamera/base/meson.build b/src/libcamera/base/meson.build\nindex d5254fda9cbf..b0d85bc19245 100644\n--- a/src/libcamera/base/meson.build\n+++ b/src/libcamera/base/meson.build\n@@ -17,6 +17,7 @@ libcamera_base_sources = files([\n     'signal.cpp',\n     'thread.cpp',\n     'timer.cpp',\n+    'unique_fd.cpp',\n     'utils.cpp',\n ])\n \ndiff --git a/src/libcamera/base/unique_fd.cpp b/src/libcamera/base/unique_fd.cpp\nnew file mode 100644\nindex 000000000000..83d6919cf623\n--- /dev/null\n+++ b/src/libcamera/base/unique_fd.cpp\n@@ -0,0 +1,123 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2021, Google Inc.\n+ *\n+ * unique_fd.cpp - File descriptor wrapper that owns a file descriptor\n+ */\n+\n+#include <libcamera/base/unique_fd.h>\n+\n+#include <unistd.h>\n+#include <utility>\n+\n+#include <libcamera/base/log.h>\n+\n+/**\n+ * \\file base/unique_fd.h\n+ * \\brief File descriptor wrapper that owns a file descriptor\n+ */\n+\n+namespace libcamera {\n+\n+LOG_DEFINE_CATEGORY(UniqueFD)\n+\n+/**\n+ * \\class UniqueFD\n+ * \\brief unique_ptr-like wrapper for a file descriptor\n+ *\n+ * The UniqueFD is a wrapper that owns and manages the lifetime of a file\n+ * descriptor. It is constructed from a numerical file descriptor, and takes\n+ * over its ownership. The file descriptor is closed when the UniqueFD is\n+ * destroyed, or when it is assigned another file descriptor with operator=()\n+ * or reset().\n+ */\n+\n+/**\n+ * \\fn UniqueFD::UniqueFD()\n+ * \\brief Construct a UniqueFD that owns no file descriptor\n+ */\n+\n+/**\n+ * \\fn UniqueFD::UniqueFD(int fd)\n+ * \\brief Construct a UniqueFD that owns \\a fd\n+ * \\param[in] fd A file descriptor to manage\n+ */\n+\n+/**\n+ * \\fn UniqueFD::UniqueFD(UniqueFD &&other)\n+ * \\brief Move constructor, create a UniqueFD by taking over \\a other\n+ * \\param[in] other The other UniqueFD\n+ *\n+ * Create a UniqueFD by transferring ownership of the file descriptor owned by\n+ * \\a other. Upon return, the \\a other UniqueFD is invalid.\n+ */\n+\n+/**\n+ * \\fn UniqueFD::~UniqueFD()\n+ * \\brief Destroy the UniqueFD instance\n+ *\n+ * If a file descriptor is owned, it is closed.\n+ */\n+\n+/**\n+ * \\fn UniqueFD::operator=(UniqueFD &&other)\n+ * \\brief Move assignment operator, replace a UniqueFD by taking over \\a other\n+ * \\param[in] other The other UniqueFD\n+ *\n+ * If this UniqueFD owns a file descriptor, the file descriptor is closed\n+ * first. The file descriptor is then replaced by the one of \\a other. Upon\n+ * return, \\a other is invalid.\n+ *\n+ * \\return A reference to this UniqueFD\n+ */\n+\n+/**\n+ * \\fn UniqueFD::release()\n+ * \\brief Release ownership of the file descriptor without closing it\n+ *\n+ * This function releases and returns the owned file descriptor without closing\n+ * it. The caller owns the returned value and must take care of handling its\n+ * life time to avoid file descriptor leakages. Upon return this UniqueFD is\n+ * invalid.\n+ *\n+ * \\return The managed file descriptor, or -1 if no file descriptor was owned\n+ */\n+\n+/**\n+ * \\brief Replace the managed file descriptor\n+ * \\param[in] fd The new file descriptor to manage\n+ *\n+ * Close the managed file descriptor, if any, and replace it with the new \\a fd.\n+ *\n+ * Self-resetting (passing an \\a fd already managed by this instance) is invalid\n+ * and results in undefined behaviour.\n+ */\n+void UniqueFD::reset(int fd)\n+{\n+\tASSERT(!isValid() || fd != fd_);\n+\n+\tstd::swap(fd, fd_);\n+\n+\tif (fd >= 0)\n+\t\tclose(fd);\n+}\n+\n+/**\n+ * \\fn UniqueFD::swap(UniqueFD &other)\n+ * \\brief Swap the managed file descriptors with another UniqueFD\n+ * \\param[in] other Another UniqueFD to swap the file descriptor with\n+ */\n+\n+/**\n+ * \\fn UniqueFD::get()\n+ * \\brief Retrieve the managed file descriptor\n+ * \\return The managed file descriptor, or -1 if no file descriptor is owned\n+ */\n+\n+/**\n+ * \\fn UniqueFD::isValid()\n+ * \\brief Check if the UniqueFD owns a valid file descriptor\n+ * \\return True if the UniqueFD owns a valid file descriptor, false otherwise\n+ */\n+\n+} /* namespace libcamera */\n","prefixes":["libcamera-devel","v4","04/22"]}