Patch Detail
Show a patch.
GET /api/patches/14819/?format=api
{ "id": 14819, "url": "https://patchwork.libcamera.org/api/patches/14819/?format=api", "web_url": "https://patchwork.libcamera.org/patch/14819/", "project": { "id": 1, "url": "https://patchwork.libcamera.org/api/projects/1/?format=api", "name": "libcamera", "link_name": "libcamera", "list_id": "libcamera_core", "list_email": "libcamera-devel@lists.libcamera.org", "web_url": "", "scm_url": "", "webscm_url": "" }, "msgid": "<20211128235752.10836-5-laurent.pinchart@ideasonboard.com>", "date": "2021-11-28T23:57:39", "name": "[libcamera-devel,v3,04/17] libcamera: base: Introduce UniqueFD", "commit_ref": null, "pull_url": null, "state": "superseded", "archived": false, "hash": "9f4cd34a46f46adcb946eaf0c24ca736d200cd70", "submitter": { "id": 2, "url": "https://patchwork.libcamera.org/api/people/2/?format=api", "name": "Laurent Pinchart", "email": "laurent.pinchart@ideasonboard.com" }, "delegate": null, "mbox": "https://patchwork.libcamera.org/patch/14819/mbox/", "series": [ { "id": 2768, "url": "https://patchwork.libcamera.org/api/series/2768/?format=api", "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=2768", "date": "2021-11-28T23:57:35", "name": "libcamera: Introduce UniqueFD", "version": 3, "mbox": "https://patchwork.libcamera.org/series/2768/mbox/" } ], "comments": "https://patchwork.libcamera.org/api/patches/14819/comments/", "check": "pending", "checks": "https://patchwork.libcamera.org/api/patches/14819/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 2A362BF415\n\tfor <parsemail@patchwork.libcamera.org>;\n\tSun, 28 Nov 2021 23:58:30 +0000 (UTC)", "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id BD17F605A4;\n\tMon, 29 Nov 2021 00:58:28 +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 1BE6F6058C\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 29 Nov 2021 00:58:23 +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 A0623A15;\n\tMon, 29 Nov 2021 00:58:22 +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=\"vjzY+uSW\"; dkim-atps=neutral", "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1638143902;\n\tbh=8FU//GerFGy0XOrloVcixQN3cv9UfE5ViIjIgSSbUrU=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=vjzY+uSWRIa/UOS1XhTkUNkpIg313Dc+en5E7GLpT1wmaSYk+s8Dtd97ZmwGF5UrV\n\tm2DZdW8g+E/kdFEz1+FN7HGgW4A7mQpP28gTkNBUtYr+FNkYb3drbd9FsA38uyw7n0\n\tDATWjOAAgN7CzNuwP66Oud8VlruGK8zLqBdnpbgo=", "From": "Laurent Pinchart <laurent.pinchart@ideasonboard.com>", "To": "libcamera-devel@lists.libcamera.org", "Date": "Mon, 29 Nov 2021 01:57:39 +0200", "Message-Id": "<20211128235752.10836-5-laurent.pinchart@ideasonboard.com>", "X-Mailer": "git-send-email 2.32.0", "In-Reply-To": "<20211128235752.10836-1-laurent.pinchart@ideasonboard.com>", "References": "<20211128235752.10836-1-laurent.pinchart@ideasonboard.com>", "MIME-Version": "1.0", "Content-Transfer-Encoding": "8bit", "Subject": "[libcamera-devel] [PATCH v3 04/17] 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>\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", "v3", "04/17" ] }