Show a patch.

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

{
    "id": 11925,
    "url": "https://patchwork.libcamera.org/api/1.1/patches/11925/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/11925/",
    "project": {
        "id": 1,
        "url": "https://patchwork.libcamera.org/api/1.1/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": "<20210414091703.3207932-1-hiroh@chromium.org>",
    "date": "2021-04-14T09:17:03",
    "name": "[libcamera-devel,WIP] libcamera: FileDescriptor: Make FileDescriptor own a fd",
    "commit_ref": null,
    "pull_url": null,
    "state": "rfc",
    "archived": false,
    "hash": "0287b0b75fb1a40296f89a1e3fe0c649e68e85e2",
    "submitter": {
        "id": 63,
        "url": "https://patchwork.libcamera.org/api/1.1/people/63/?format=api",
        "name": "Hirokazu Honda",
        "email": "hiroh@chromium.org"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/11925/mbox/",
    "series": [
        {
            "id": 1926,
            "url": "https://patchwork.libcamera.org/api/1.1/series/1926/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=1926",
            "date": "2021-04-14T09:17:03",
            "name": "[libcamera-devel,WIP] libcamera: FileDescriptor: Make FileDescriptor own a fd",
            "version": 1,
            "mbox": "https://patchwork.libcamera.org/series/1926/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/11925/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/11925/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 0D556BD1F6\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 14 Apr 2021 09:17:13 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 2FC3A6880E;\n\tWed, 14 Apr 2021 11:17:12 +0200 (CEST)",
            "from mail-pf1-x42d.google.com (mail-pf1-x42d.google.com\n\t[IPv6:2607:f8b0:4864:20::42d])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 5E127602CA\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 14 Apr 2021 11:17:11 +0200 (CEST)",
            "by mail-pf1-x42d.google.com with SMTP id n38so13339357pfv.2\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 14 Apr 2021 02:17:11 -0700 (PDT)",
            "from hiroh2.tok.corp.google.com\n\t([2401:fa00:8f:2:84f5:7981:dfbe:8f02])\n\tby smtp.gmail.com with ESMTPSA id\n\tg24sm14497298pgn.18.2021.04.14.02.17.08\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tWed, 14 Apr 2021 02:17:09 -0700 (PDT)"
        ],
        "Authentication-Results": "lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=chromium.org header.i=@chromium.org\n\theader.b=\"Nh3xrVM6\"; dkim-atps=neutral",
        "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org;\n\ts=google; h=from:to:cc:subject:date:message-id:mime-version\n\t:content-transfer-encoding;\n\tbh=pI6W/9TVrbGWD814nS76WXabkbM3bTfW1eqYhsUOaes=;\n\tb=Nh3xrVM6FXUzI+ffzKZPNNwzACF9LkPDzHjMK1WKtJgN1JsLc6KQHNe0Q95gab+eZ/\n\tA/8nxmwHF+hVlNLyD/j1xsb9gSmtppH6dhhHHuM+5YgjEVo1oDFyrmymWLpIKrJAdU4h\n\tS7GaBmoEc+/mKUsOIvxS+l0ZLPnhCTCgxsYVg=",
        "X-Google-DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20161025;\n\th=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version\n\t:content-transfer-encoding;\n\tbh=pI6W/9TVrbGWD814nS76WXabkbM3bTfW1eqYhsUOaes=;\n\tb=DatnZ0sG4o+yPQ4l3XEahJNvd8OARD6cdvOmlEaT8Df+lsrKWordXdlJu3PGAAebhR\n\td7S2E1ts7tfPw6liAeV8jXnMzOjydROaXM8k6iTuVv4mYNKyxFxAp2meuP5EaTa1YMuo\n\t2EtxwRb/VKj97yjFoYZHpKhRswSkyC7KFfLwL9Uve+cnBjObmxDjJgXUgvnIMbEMNZlv\n\tb+W2L/hCO+L1eN9jYGNfJQb9MaE+kb5oW2jMKZPlpuA12YY9+qXu+MnWMH6oJy5HZLHS\n\t4lhxFtwnT1x4hquKMstYOQ4yDgSUgYXn6GZsu7IpMBk4eembdtrukyeu//vFn3g7JKGO\n\tQNSg==",
        "X-Gm-Message-State": "AOAM533t6K5ATmtADSsHTsjyV+n6UJAzecdD8c4FKthEXTkREM6LebHx\n\tryaTLzzbJSvJzlf5Mo78Kq9FjwcZ7ylOoQ==",
        "X-Google-Smtp-Source": "ABdhPJyarJogO7eaOblcgp2I6HackxL2/yD368Lg+mr8VWARCdTz9LXBFBbqwCscrE7eI4Km5du0YQ==",
        "X-Received": "by 2002:a63:1352:: with SMTP id 18mr35297018pgt.11.1618391829600;\n\tWed, 14 Apr 2021 02:17:09 -0700 (PDT)",
        "From": "Hirokazu Honda <hiroh@chromium.org>",
        "To": "libcamera-devel@lists.libcamera.org",
        "Date": "Wed, 14 Apr 2021 18:17:03 +0900",
        "Message-Id": "<20210414091703.3207932-1-hiroh@chromium.org>",
        "X-Mailer": "git-send-email 2.31.1.295.g9ea45b61b8-goog",
        "MIME-Version": "1.0",
        "Subject": "[libcamera-devel] [PATCH] [WIP] libcamera: FileDescriptor: Make\n\tFileDescriptor own a fd",
        "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>",
        "Content-Type": "text/plain; charset=\"us-ascii\"",
        "Content-Transfer-Encoding": "7bit",
        "Errors-To": "libcamera-devel-bounces@lists.libcamera.org",
        "Sender": "\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"
    },
    "content": "Multiple FileDescriptors can share the same file descriptor. It\nleads that the ownership of a fd managed by the class is unclear.\nThis makes FileDescriptor own a fd, similar to std::unique_ptr.\n\nSigned-off-by: Hirokazu Honda <hiroh@chromium.org>\n---\n include/libcamera/file_descriptor.h |  26 ++-----\n src/libcamera/file_descriptor.cpp   | 112 ++++------------------------\n 2 files changed, 22 insertions(+), 116 deletions(-)",
    "diff": "diff --git a/include/libcamera/file_descriptor.h b/include/libcamera/file_descriptor.h\nindex d514aac7..aaaf7f10 100644\n--- a/include/libcamera/file_descriptor.h\n+++ b/include/libcamera/file_descriptor.h\n@@ -13,34 +13,22 @@ namespace libcamera {\n \n class FileDescriptor final\n {\n+\tstatic constexpr int kInvalidFd = -1;\n public:\n-\texplicit FileDescriptor(const int &fd = -1);\n-\texplicit FileDescriptor(int &&fd);\n-\tFileDescriptor(const FileDescriptor &other);\n+\texplicit FileDescriptor(const int fd = kInvalidFd);\n \tFileDescriptor(FileDescriptor &&other);\n \t~FileDescriptor();\n \n-\tFileDescriptor &operator=(const FileDescriptor &other);\n \tFileDescriptor &operator=(FileDescriptor &&other);\n+\tFileDescriptor &operator=(const FileDescriptor &other) = delete;\n+\tFileDescriptor(const FileDescriptor &other) = delete;\n \n-\tbool isValid() const { return fd_ != nullptr; }\n-\tint fd() const { return fd_ ? fd_->fd() : -1; }\n+\tbool isValid() const { return fd_ != kInvalidFd; }\n+\tint fd() const { return fd_; }\n \tFileDescriptor dup() const;\n \n private:\n-\tclass Descriptor\n-\t{\n-\tpublic:\n-\t\tDescriptor(int fd, bool duplicate);\n-\t\t~Descriptor();\n-\n-\t\tint fd() const { return fd_; }\n-\n-\tprivate:\n-\t\tint fd_;\n-\t};\n-\n-\tstd::shared_ptr<Descriptor> fd_;\n+\tint fd_;\n };\n \n } /* namespace libcamera */\ndiff --git a/src/libcamera/file_descriptor.cpp b/src/libcamera/file_descriptor.cpp\nindex 8b505ed3..5757aca0 100644\n--- a/src/libcamera/file_descriptor.cpp\n+++ b/src/libcamera/file_descriptor.cpp\n@@ -7,6 +7,7 @@\n \n #include <libcamera/file_descriptor.h>\n \n+#include <algorithm>\n #include <string.h>\n #include <unistd.h>\n #include <utility>\n@@ -35,13 +36,7 @@ LOG_DEFINE_CATEGORY(FileDescriptor)\n  * When constructed from a numerical file descriptor, the FileDescriptor\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+ * - The FileDescriptor(const int &) constructor takes over the numerical file\n  *   descriptor and wraps it in a Descriptor. The caller 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@@ -70,56 +65,9 @@ LOG_DEFINE_CATEGORY(FileDescriptor)\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(const int &fd)\n-{\n-\tif (fd < 0)\n-\t\treturn;\n-\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- *\n- * Copying a FileDescriptor implicitly shares ownership of the wrapped file\n- * descriptor. The original FileDescriptor is left untouched, and the caller is\n- * responsible for destroying it when appropriate. The wrapped file descriptor\n- * will be closed automatically when all FileDescriptor instances that\n- * reference it are destroyed.\n- */\n-FileDescriptor::FileDescriptor(const FileDescriptor &other)\n-\t: fd_(other.fd_)\n+FileDescriptor::FileDescriptor(const int fd)\n+\t: fd_(fd)\n {\n }\n \n@@ -134,8 +82,9 @@ FileDescriptor::FileDescriptor(const FileDescriptor &other)\n  * reference it are destroyed.\n  */\n FileDescriptor::FileDescriptor(FileDescriptor &&other)\n-\t: fd_(std::move(other.fd_))\n+\t: fd_(other.fd_)\n {\n+\tother.fd_ = -1;\n }\n \n /**\n@@ -147,27 +96,8 @@ FileDescriptor::FileDescriptor(FileDescriptor &&other)\n  */\n FileDescriptor::~FileDescriptor()\n {\n-}\n-\n-/**\n- * \\brief Copy assignment operator, replace the wrapped file descriptor with a\n- * copy of \\a other\n- * \\param[in] other The other FileDescriptor\n- *\n- * Copying a FileDescriptor creates a new reference to the wrapped file\n- * descriptor owner by \\a other. If \\a other is invalid, *this will also be\n- * invalid. The original FileDescriptor is left untouched, and the caller is\n- * responsible for destroying it when appropriate. The wrapped file descriptor\n- * will be closed automatically when all FileDescriptor instances that\n- * reference it are destroyed.\n- *\n- * \\return A reference to this FileDescriptor\n- */\n-FileDescriptor &FileDescriptor::operator=(const FileDescriptor &other)\n-{\n-\tfd_ = other.fd_;\n-\n-\treturn *this;\n+\tif (isValid())\n+\t\tclose(fd_);\n }\n \n /**\n@@ -186,7 +116,7 @@ FileDescriptor &FileDescriptor::operator=(const FileDescriptor &other)\n  */\n FileDescriptor &FileDescriptor::operator=(FileDescriptor &&other)\n {\n-\tfd_ = std::move(other.fd_);\n+\tstd::swap(fd_, other.fd_);\n \n \treturn *this;\n }\n@@ -218,29 +148,17 @@ FileDescriptor &FileDescriptor::operator=(FileDescriptor &&other)\n  */\n FileDescriptor FileDescriptor::dup() const\n {\n-\treturn FileDescriptor(fd());\n-}\n+\tif (!isValid())\n+\t\treturn FileDescriptor();\n \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+\tint dupFd = ::dup(fd);\n+\tif (dupFd == -1) {\n \t\tint ret = -errno;\n \t\tLOG(FileDescriptor, Fatal)\n \t\t\t<< \"Failed to dup() fd: \" << strerror(-ret);\n+\t\treturn FileDescriptor();\n \t}\n-}\n \n-FileDescriptor::Descriptor::~Descriptor()\n-{\n-\tif (fd_ != -1)\n-\t\tclose(fd_);\n+\treturn FileDescriptor(dupFd);\n }\n-\n } /* namespace libcamera */\n",
    "prefixes": [
        "libcamera-devel",
        "WIP"
    ]
}