Show a patch.

GET /api/patches/3805/?format=api
HTTP 200 OK
Allow: GET, PUT, PATCH, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 3805,
    "url": "https://patchwork.libcamera.org/api/patches/3805/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/3805/",
    "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": "<20200518164804.10088-1-laurent.pinchart@ideasonboard.com>",
    "date": "2020-05-18T16:48:02",
    "name": "[libcamera-devel,1/3] libcamera: file_descriptor: Implement move semantics for constructor",
    "commit_ref": "206fada99d8774fb7a9b4f1924f6caeccafdb9a1",
    "pull_url": null,
    "state": "accepted",
    "archived": false,
    "hash": "a0ef87cdf4473444ac8bfcc87884281e41d454b7",
    "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/3805/mbox/",
    "series": [
        {
            "id": 909,
            "url": "https://patchwork.libcamera.org/api/series/909/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=909",
            "date": "2020-05-18T16:48:02",
            "name": "[libcamera-devel,1/3] libcamera: file_descriptor: Implement move semantics for constructor",
            "version": 1,
            "mbox": "https://patchwork.libcamera.org/series/909/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/3805/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/3805/checks/",
    "tags": {},
    "headers": {
        "Return-Path": "<laurent.pinchart@ideasonboard.com>",
        "Received": [
            "from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id BCC47603DA\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 18 May 2020 18:48:17 +0200 (CEST)",
            "from pendragon.bb.dnainternet.fi (81-175-216-236.bb.dnainternet.fi\n\t[81.175.216.236])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id C167F258;\n\tMon, 18 May 2020 18:48:16 +0200 (CEST)"
        ],
        "Authentication-Results": "lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"ReGqlHL4\"; dkim-atps=neutral",
        "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1589820497;\n\tbh=zAFLOHWnn5srav3fpVQ3HYS9JhDTKDEIQv3T0po+EQU=;\n\th=From:To:Cc:Subject:Date:From;\n\tb=ReGqlHL4RRwIN+CiJvUWWg4nXmaYS4yfIMhsGQRhAYUzRIVh5P1JJX4/8Z5Dqyk/U\n\t1ZnpDtWGfNY8tBVTzH2O3N+oTdNHiTulen9Tx7YyYSY+6VF/qjmIDk9b0QUqqmCzjV\n\tWuZ08sQdC8QqetSmc3WEitO+5Yw7szMEyuLSLNaA=",
        "From": "Laurent Pinchart <laurent.pinchart@ideasonboard.com>",
        "To": "libcamera-devel@lists.libcamera.org",
        "Date": "Mon, 18 May 2020 19:48:02 +0300",
        "Message-Id": "<20200518164804.10088-1-laurent.pinchart@ideasonboard.com>",
        "X-Mailer": "git-send-email 2.26.2",
        "MIME-Version": "1.0",
        "Content-Transfer-Encoding": "8bit",
        "Subject": "[libcamera-devel] [PATCH 1/3] libcamera: file_descriptor: Implement\n\tmove semantics for constructor",
        "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": "Mon, 18 May 2020 16:48:18 -0000"
    },
    "content": "The FileDescriptor class, when constructed from a numerical file\ndescriptor, duplicates the file descriptor and takes ownership of the\ncopy. The caller has to close the original file descriptor manually if\nneeded. This is inefficient as the dup() and close() calls could be\navoided, but can also lead to resource leakage, as recently shown by\ncommit 353fc4c22322 (\"libcamera: v4l2_videodevice: Fix dangling file\ndescriptor\").\n\nIn an attempt to solve this problem, implement move semantics for the\nFileDescriptor constructor. The constructor taking a numerical file\ndescriptor is split in two variants:\n\n- A \"fd copy\" constructor that takes a const lvalue reference to a\n  numerical file descriptor and duplicates it (corresponding to the\n  current behaviour).\n- A \"fd move\" constructor that takes a rvalue reference to a numerical\n  file descriptor and takes ownership of it.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n include/libcamera/file_descriptor.h |  5 +-\n src/libcamera/file_descriptor.cpp   | 85 ++++++++++++++++++++++-------\n 2 files changed, 67 insertions(+), 23 deletions(-)",
    "diff": "diff --git a/include/libcamera/file_descriptor.h b/include/libcamera/file_descriptor.h\nindex 8612f86511a1..d514aac7697b 100644\n--- a/include/libcamera/file_descriptor.h\n+++ b/include/libcamera/file_descriptor.h\n@@ -14,7 +14,8 @@ namespace libcamera {\n class FileDescriptor final\n {\n public:\n-\texplicit FileDescriptor(int fd = -1);\n+\texplicit FileDescriptor(const int &fd = -1);\n+\texplicit FileDescriptor(int &&fd);\n \tFileDescriptor(const FileDescriptor &other);\n \tFileDescriptor(FileDescriptor &&other);\n \t~FileDescriptor();\n@@ -30,7 +31,7 @@ private:\n \tclass Descriptor\n \t{\n \tpublic:\n-\t\tDescriptor(int fd);\n+\t\tDescriptor(int fd, bool duplicate);\n \t\t~Descriptor();\n \n \t\tint fd() const { return fd_; }\ndiff --git a/src/libcamera/file_descriptor.cpp b/src/libcamera/file_descriptor.cpp\nindex ee60064bce6e..640e66e6d168 100644\n--- a/src/libcamera/file_descriptor.cpp\n+++ b/src/libcamera/file_descriptor.cpp\n@@ -33,43 +33,81 @@ LOG_DEFINE_CATEGORY(FileDescriptor)\n  * shared with all FileDescriptor instances constructed as copies.\n  *\n  * When constructed from a numerical file descriptor, the FileDescriptor\n- * instance duplicates the file descriptor and wraps the duplicate as a\n- * Descriptor. The copy constructor and assignment operator create copies that\n- * share the Descriptor, while the move versions of those methods additionally\n- * make the other FileDescriptor invalid. When the last FileDescriptor that\n- * references a Descriptor is destroyed, the file descriptor is closed.\n- *\n- * The numerical file descriptor is available through the fd() method. As\n- * constructing a FileDescriptor from a numerical file descriptor duplicates\n- * the file descriptor, the value returned by fd() will be different than the\n- * value passed to the constructor. All FileDescriptor instances created as\n- * copies of a FileDescriptor will report the same fd() value. Callers can\n- * perform operations on the fd(), but shall never close it manually.\n+ * instance either duplicates or takes over the file descriptor:\n+ *\n+ * - The FileDescriptor(const int &) constructor duplicates the numerical file\n+ *   descriptor and wraps the duplicate in a Descriptor. The caller is\n+ *   responsible for closing the original file descriptor, and the value\n+ *   returned by fd() will be different from the value passed to the\n+ *   constructor.\n+ *\n+ * - The FileDescriptor(int &&) constructor takes over the numerical file\n+ *   descriptor and wraps it in a Descriptor. The caller is shall not touch the\n+ *   original file descriptor once the function returns, and the value returned\n+ *   by fd() will be identical to the value passed to the constructor.\n+ *\n+ * The copy constructor and assignment operator create copies that share the\n+ * Descriptor, while the move versions of those methods additionally make the\n+ * other FileDescriptor invalid. When the last FileDescriptor that references a\n+ * Descriptor is destroyed, the file descriptor is closed.\n+ *\n+ * The numerical file descriptor is available through the fd() method. All\n+ * FileDescriptor instances created as copies of a FileDescriptor will report\n+ * the same fd() value. Callers can perform operations on the fd(), but shall\n+ * never close it manually.\n  */\n \n /**\n- * \\brief Create a FileDescriptor wrapping a copy of a given \\a fd\n+ * \\brief Create a FileDescriptor copying a given \\a fd\n  * \\param[in] fd File descriptor\n  *\n- * Constructing a FileDescriptor from a numerical file descriptor duplicates the\n- * \\a fd and takes ownership of the copy. The original \\a fd is left untouched,\n- * and the caller is responsible for closing it when appropriate. The duplicated\n- * file descriptor will be closed automatically when all FileDescriptor\n- * instances that reference it are destroyed.\n+ * Construct a FileDescriptor from a numerical file descriptor by duplicating\n+ * the \\a fd, and take ownership of the copy. The original \\a fd is left\n+ * untouched, and the caller is responsible for closing it when appropriate.\n+ * The duplicated file descriptor will be closed automatically when all\n+ * FileDescriptor instances that reference it are destroyed.\n  *\n  * If the \\a fd is negative, the FileDescriptor is constructed as invalid and\n  * the fd() method will return -1.\n  */\n-FileDescriptor::FileDescriptor(int fd)\n+FileDescriptor::FileDescriptor(const int &fd)\n {\n \tif (fd < 0)\n \t\treturn;\n \n-\tfd_ = std::make_shared<Descriptor>(fd);\n+\tfd_ = std::make_shared<Descriptor>(fd, true);\n \tif (fd_->fd() < 0)\n \t\tfd_.reset();\n }\n \n+/**\n+ * \\brief Create a FileDescriptor taking ownership of a given \\a fd\n+ * \\param[in] fd File descriptor\n+ *\n+ * Construct a FileDescriptor from a numerical file descriptor by taking\n+ * ownership of the \\a fd. The original \\a fd is set to -1 and shall not be\n+ * touched by the caller anymore. In particular, the caller shall not close the\n+ * original \\a fd manually. The duplicated file descriptor will be closed\n+ * automatically when all FileDescriptor instances that reference it are\n+ * destroyed.\n+ *\n+ * If the \\a fd is negative, the FileDescriptor is constructed as invalid and\n+ * the fd() method will return -1.\n+ */\n+FileDescriptor::FileDescriptor(int &&fd)\n+{\n+\tif (fd < 0)\n+\t\treturn;\n+\n+\tfd_ = std::make_shared<Descriptor>(fd, false);\n+\t/*\n+\t * The Descriptor constructor can't have failed here, as it took over\n+\t * the fd without duplicating it. Just set the original fd to -1 to\n+\t * implement move semantics.\n+\t */\n+\tfd = -1;\n+}\n+\n /**\n  * \\brief Copy constructor, create a FileDescriptor from a copy of \\a other\n  * \\param[in] other The other FileDescriptor\n@@ -183,8 +221,13 @@ FileDescriptor FileDescriptor::dup() const\n \treturn FileDescriptor(fd());\n }\n \n-FileDescriptor::Descriptor::Descriptor(int fd)\n+FileDescriptor::Descriptor::Descriptor(int fd, bool duplicate)\n {\n+\tif (!duplicate) {\n+\t\tfd_ = fd;\n+\t\treturn;\n+\t}\n+\n \t/* Failing to dup() a fd should not happen and is fatal. */\n \tfd_ = ::dup(fd);\n \tif (fd_ == -1) {\n",
    "prefixes": [
        "libcamera-devel",
        "1/3"
    ]
}