Show a patch.

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

{
    "id": 19668,
    "url": "https://patchwork.libcamera.org/api/patches/19668/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/19668/",
    "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": "<20240311141524.27192-6-hdegoede@redhat.com>",
    "date": "2024-03-11T14:15:09",
    "name": "[v5,05/18] libcamera: shared_mem_object: reorganize the code and document the SharedMemObject class",
    "commit_ref": null,
    "pull_url": null,
    "state": "superseded",
    "archived": false,
    "hash": "05621bae718c7b46aaebdf6b7855c3dddc8f8b95",
    "submitter": {
        "id": 102,
        "url": "https://patchwork.libcamera.org/api/people/102/?format=api",
        "name": "Hans de Goede",
        "email": "hdegoede@redhat.com"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/19668/mbox/",
    "series": [
        {
            "id": 4214,
            "url": "https://patchwork.libcamera.org/api/series/4214/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=4214",
            "date": "2024-03-11T14:15:04",
            "name": "libcamera: introduce Software ISP and Software IPA",
            "version": 5,
            "mbox": "https://patchwork.libcamera.org/series/4214/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/19668/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/19668/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 D8EA0C32A3\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 11 Mar 2024 14:15:42 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 8885B62C8E;\n\tMon, 11 Mar 2024 15:15:42 +0100 (CET)",
            "from us-smtp-delivery-124.mimecast.com\n\t(us-smtp-delivery-124.mimecast.com [170.10.133.124])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id C895061C7C\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 11 Mar 2024 15:15:39 +0100 (CET)",
            "from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com\n\t[66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS\n\t(version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id\n\tus-mta-230-YXF719q_PMuaJ0lldz4zuw-1; Mon, 11 Mar 2024 10:15:35 -0400",
            "from smtp.corp.redhat.com\n\t(int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\tkey-exchange X25519 server-signature RSA-PSS (2048 bits)\n\tserver-digest SHA256) (No client certificate requested)\n\tby mimecast-mx02.redhat.com (Postfix) with ESMTPS id AE2D380026B;\n\tMon, 11 Mar 2024 14:15:34 +0000 (UTC)",
            "from x1.localdomain.com (unknown [10.39.195.37])\n\tby smtp.corp.redhat.com (Postfix) with ESMTP id 8EE9E1C060A6;\n\tMon, 11 Mar 2024 14:15:33 +0000 (UTC)"
        ],
        "Authentication-Results": "lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=redhat.com header.i=@redhat.com\n\theader.b=\"RS2dR3jA\"; dkim-atps=neutral",
        "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n\ts=mimecast20190719; t=1710166538;\n\th=from:from:reply-to:subject:subject:date:date:message-id:message-id:\n\tto:to:cc:cc:mime-version:mime-version:content-type:content-type:\n\tcontent-transfer-encoding:content-transfer-encoding:\n\tin-reply-to:in-reply-to:references:references;\n\tbh=KiGRlxx84YSvo2u8xBYk3K8dzq++aHIsYqgi7Hpmr1M=;\n\tb=RS2dR3jAl5pKHKBsqiy/QohouR+2QDJaiItaZwiXjhTvBJr4uiAzmhVvele8IZi+k+h/EB\n\tlcNv6SZw6YzUgLkxvMx0i+x8NJaoT1hbX0BSLZcN7BZeVfWJNJo+Wl764RWhElRhVXU1BE\n\thJVXyWMjRW7OP2zWfgAFgf71M2Q58Dw=",
        "X-MC-Unique": "YXF719q_PMuaJ0lldz4zuw-1",
        "From": "Hans de Goede <hdegoede@redhat.com>",
        "To": "libcamera-devel@lists.libcamera.org",
        "Subject": "[PATCH v5 05/18] libcamera: shared_mem_object: reorganize the code\n\tand document the SharedMemObject class",
        "Date": "Mon, 11 Mar 2024 15:15:09 +0100",
        "Message-ID": "<20240311141524.27192-6-hdegoede@redhat.com>",
        "In-Reply-To": "<20240311141524.27192-1-hdegoede@redhat.com>",
        "References": "<20240311141524.27192-1-hdegoede@redhat.com>",
        "MIME-Version": "1.0",
        "X-Scanned-By": "MIMEDefang 3.4.1 on 10.11.54.7",
        "X-Mimecast-Spam-Score": "0",
        "X-Mimecast-Originator": "redhat.com",
        "Content-Transfer-Encoding": "8bit",
        "Content-Type": "text/plain; charset=\"US-ASCII\"; x-default=true",
        "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>",
        "Cc": "Maxime Ripard <mripard@redhat.com>, Pavel Machek <pavel@ucw.cz>,\n\tBryan O'Donoghue <bryan.odonoghue@linaro.org>,\n\tDennis Bonke <admin@dennisbonke.com>",
        "Errors-To": "libcamera-devel-bounces@lists.libcamera.org",
        "Sender": "\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"
    },
    "content": "From: Andrei Konovalov <andrey.konovalov.ynk@gmail.com>\n\nSplit the parts which doesn't otherwise depend on the type T or\narguments Args out of the SharedMemObject class into a new\nSharedMem class.\n\nDoxygen documentation by Dennis Bonke and Andrei Konovalov.\n\nReviewed-by: Pavel Machek <pavel@ucw.cz>\nCo-developed-by: Dennis Bonke <admin@dennisbonke.com>\nSigned-off-by: Dennis Bonke <admin@dennisbonke.com>\nSigned-off-by: Andrei Konovalov <andrey.konovalov.ynk@gmail.com>\nSigned-off-by: Hans de Goede <hdegoede@redhat.com>\n---\n .../libcamera/internal/shared_mem_object.h    | 101 ++++++----\n src/libcamera/meson.build                     |   1 +\n src/libcamera/shared_mem_object.cpp           | 190 ++++++++++++++++++\n 3 files changed, 253 insertions(+), 39 deletions(-)\n create mode 100644 src/libcamera/shared_mem_object.cpp",
    "diff": "diff --git a/include/libcamera/internal/shared_mem_object.h b/include/libcamera/internal/shared_mem_object.h\nindex 98636b44..43b07c9d 100644\n--- a/include/libcamera/internal/shared_mem_object.h\n+++ b/include/libcamera/internal/shared_mem_object.h\n@@ -6,12 +6,9 @@\n  */\n #pragma once\n \n-#include <fcntl.h>\n #include <stddef.h>\n #include <string>\n #include <sys/mman.h>\n-#include <sys/stat.h>\n-#include <unistd.h>\n #include <utility>\n \n #include <libcamera/base/class.h>\n@@ -19,58 +16,92 @@\n \n namespace libcamera {\n \n+class SharedMem\n+{\n+public:\n+\tSharedMem()\n+\t\t: mem_(nullptr)\n+\t{\n+\t}\n+\n+\tSharedMem(const std::string &name, std::size_t size);\n+\n+\tSharedMem(SharedMem &&rhs)\n+\t{\n+\t\tthis->name_ = std::move(rhs.name_);\n+\t\tthis->fd_ = std::move(rhs.fd_);\n+\t\tthis->mem_ = rhs.mem_;\n+\t\trhs.mem_ = nullptr;\n+\t}\n+\n+\tvirtual ~SharedMem()\n+\t{\n+\t\tif (mem_)\n+\t\t\tmunmap(mem_, size_);\n+\t}\n+\n+\t/* Make SharedMem non-copyable for now. */\n+\tLIBCAMERA_DISABLE_COPY(SharedMem)\n+\n+\tSharedMem &operator=(SharedMem &&rhs)\n+\t{\n+\t\tthis->name_ = std::move(rhs.name_);\n+\t\tthis->fd_ = std::move(rhs.fd_);\n+\t\tthis->mem_ = rhs.mem_;\n+\t\trhs.mem_ = nullptr;\n+\t\treturn *this;\n+\t}\n+\n+\tconst SharedFD &fd() const\n+\t{\n+\t\treturn fd_;\n+\t}\n+\n+\tvoid *mem() const\n+\t{\n+\t\treturn mem_;\n+\t}\n+\n+private:\n+\tstd::string name_;\n+\tSharedFD fd_;\n+\tsize_t size_;\n+protected:\n+\tvoid *mem_;\n+};\n+\n template<class T>\n-class SharedMemObject\n+class SharedMemObject : public SharedMem\n {\n public:\n \tstatic constexpr std::size_t SIZE = sizeof(T);\n \n \tSharedMemObject()\n-\t\t: obj_(nullptr)\n+\t\t: SharedMem(), obj_(nullptr)\n \t{\n \t}\n \n \ttemplate<class... Args>\n \tSharedMemObject(const std::string &name, Args &&...args)\n-\t\t: name_(name), obj_(nullptr)\n+\t\t: SharedMem(name, SIZE), obj_(nullptr)\n \t{\n-\t\tvoid *mem;\n-\t\tint ret;\n-\n-\t\tret = memfd_create(name_.c_str(), MFD_CLOEXEC);\n-\t\tif (ret < 0)\n+\t\tif (mem_ == nullptr)\n \t\t\treturn;\n \n-\t\tfd_ = SharedFD(std::move(ret));\n-\t\tif (!fd_.isValid())\n-\t\t\treturn;\n-\n-\t\tret = ftruncate(fd_.get(), SIZE);\n-\t\tif (ret < 0)\n-\t\t\treturn;\n-\n-\t\tmem = mmap(nullptr, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED,\n-\t\t\t   fd_.get(), 0);\n-\t\tif (mem == MAP_FAILED)\n-\t\t\treturn;\n-\n-\t\tobj_ = new (mem) T(std::forward<Args>(args)...);\n+\t\tobj_ = new (mem_) T(std::forward<Args>(args)...);\n \t}\n \n \tSharedMemObject(SharedMemObject<T> &&rhs)\n+\t\t: SharedMem(std::move(rhs))\n \t{\n-\t\tthis->name_ = std::move(rhs.name_);\n-\t\tthis->fd_ = std::move(rhs.fd_);\n \t\tthis->obj_ = rhs.obj_;\n \t\trhs.obj_ = nullptr;\n \t}\n \n \t~SharedMemObject()\n \t{\n-\t\tif (obj_) {\n+\t\tif (obj_)\n \t\t\tobj_->~T();\n-\t\t\tmunmap(obj_, SIZE);\n-\t\t}\n \t}\n \n \t/* Make SharedMemObject non-copyable for now. */\n@@ -78,8 +109,7 @@ public:\n \n \tSharedMemObject<T> &operator=(SharedMemObject<T> &&rhs)\n \t{\n-\t\tthis->name_ = std::move(rhs.name_);\n-\t\tthis->fd_ = std::move(rhs.fd_);\n+\t\tSharedMem::operator=(std::move(rhs));\n \t\tthis->obj_ = rhs.obj_;\n \t\trhs.obj_ = nullptr;\n \t\treturn *this;\n@@ -105,19 +135,12 @@ public:\n \t\treturn *obj_;\n \t}\n \n-\tconst SharedFD &fd() const\n-\t{\n-\t\treturn fd_;\n-\t}\n-\n \texplicit operator bool() const\n \t{\n \t\treturn !!obj_;\n \t}\n \n private:\n-\tstd::string name_;\n-\tSharedFD fd_;\n \tT *obj_;\n };\n \ndiff --git a/src/libcamera/meson.build b/src/libcamera/meson.build\nindex 3c5e43df..94a95ae3 100644\n--- a/src/libcamera/meson.build\n+++ b/src/libcamera/meson.build\n@@ -41,6 +41,7 @@ libcamera_sources = files([\n     'process.cpp',\n     'pub_key.cpp',\n     'request.cpp',\n+    'shared_mem_object.cpp',\n     'source_paths.cpp',\n     'stream.cpp',\n     'sysfs.cpp',\ndiff --git a/src/libcamera/shared_mem_object.cpp b/src/libcamera/shared_mem_object.cpp\nnew file mode 100644\nindex 00000000..44fe74c2\n--- /dev/null\n+++ b/src/libcamera/shared_mem_object.cpp\n@@ -0,0 +1,190 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2023, Raspberry Pi Ltd\n+ *\n+ * shared_mem_object.cpp - Helper class for shared memory allocations\n+ */\n+\n+#include \"libcamera/internal/shared_mem_object.h\"\n+\n+#include <sys/types.h>\n+#include <unistd.h>\n+\n+/**\n+ * \\file shared_mem_object.cpp\n+ * \\brief Helper class for shared memory allocations\n+ */\n+\n+namespace libcamera {\n+\n+/**\n+ * \\class SharedMem\n+ * \\brief Helper class for allocating shared memory\n+ *\n+ * Memory is allocated and exposed as a SharedFD for use across IPC boundaries.\n+ *\n+ * SharedMem allocates the shared memory of the given size and maps it.\n+ * To check that the shared memory was allocated and mapped successfully, one\n+ * needs to verify that the pointer to the shared memory returned by SharedMem::mem()\n+ * is not nullptr.\n+ *\n+ * To access the shared memory from another process the SharedFD should be passed\n+ * to that process, and then the shared memory should be mapped into that process\n+ * address space by calling mmap().\n+ *\n+ * A single memfd is created for every SharedMem. If there is a need to allocate\n+ * a large number of objects in shared memory, these objects should be grouped\n+ * together and use the shared memory allocated by a single SharedMem object if\n+ * possible. This will help to minimize the number of created memfd's.\n+ */\n+\n+/**\n+ * \\fn SharedMem::SharedMem(const std::string &name, std::size_t size)\n+ * \\brief Constructor for the SharedMem\n+ * \\param[in] name Name of the SharedMem\n+ * \\param[in] size Size of the shared memory to allocate and map\n+ */\n+\n+/**\n+ * \\fn SharedMem::SharedMem(SharedMem &&rhs)\n+ * \\brief Move constructor for SharedMem\n+ * \\param[in] rhs The object to move\n+ */\n+\n+/**\n+ * \\fn SharedMem::~SharedMem()\n+ * \\brief SharedMem destructor\n+ *\n+ * Unmaps the allocated shared memory. Decrements the shared memory descriptor use\n+ * count.\n+ */\n+\n+/**\n+ * \\fn SharedMem &SharedMem::operator=(SharedMem &&rhs)\n+ * \\brief Move constructor for SharedMem\n+ * \\param[in] rhs The object to move\n+ */\n+\n+/**\n+ * \\fn const SharedFD &SharedMem::fd() const\n+ * \\brief Gets the file descriptor for the underlying shared memory\n+ * \\return The file descriptor\n+ */\n+\n+/**\n+ * \\fn void *SharedMem::mem() const\n+ * \\brief Gets the pointer to the underlying shared memory\n+ * \\return The pointer to the shared memory\n+ */\n+\n+SharedMem::SharedMem(const std::string &name, std::size_t size)\n+\t: name_(name), size_(size), mem_(nullptr)\n+{\n+\tint fd = memfd_create(name_.c_str(), MFD_CLOEXEC);\n+\tif (fd < 0)\n+\t\treturn;\n+\n+\tfd_ = SharedFD(std::move(fd));\n+\tif (!fd_.isValid())\n+\t\treturn;\n+\n+\tif (ftruncate(fd_.get(), size_) < 0)\n+\t\treturn;\n+\n+\tmem_ = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED,\n+\t\t    fd_.get(), 0);\n+\tif (mem_ == MAP_FAILED)\n+\t\tmem_ = nullptr;\n+}\n+\n+/**\n+ * \\var SharedMem::mem_\n+ * \\brief Pointer to the shared memory allocated\n+ */\n+\n+/**\n+ * \\class SharedMemObject\n+ * \\brief Helper class for allocating objects in shared memory\n+ *\n+ * Memory is allocated and exposed as a SharedFD for use across IPC boundaries.\n+ *\n+ * Given the type of the object to be created in shared memory and the arguments\n+ * to pass to this object's constructor, SharedMemObject allocates the shared memory\n+ * of the size of the object and constructs the object in this memory. To ensure\n+ * that the SharedMemObject was created successfully, one needs to verify that the\n+ * overloaded bool() operator returns true. The object created in the shared memory\n+ * can be accessed using the SharedMemObject::operator*() indirection operator. Its\n+ * members can be accessed with the SharedMemObject::operator->() member of pointer\n+ * operator.\n+ *\n+ * To access the object from another process the SharedFD should be passed to that\n+ * process, and the shared memory should be mapped by calling mmap().\n+ *\n+ * A single memfd is created for every SharedMemObject. If there is a need to allocate\n+ * a large number of objects in shared memory, these objects should be grouped into a\n+ * single large object to keep the number of created memfd's reasonably small.\n+ */\n+\n+/**\n+ * \\var SharedMemObject::SIZE\n+ * \\brief The size of the object that is going to be stored here\n+ */\n+\n+/**\n+ * \\fn SharedMemObject< T >::SharedMemObject(const std::string &name, Args &&...args)\n+ * \\brief Constructor for the SharedMemObject\n+ * \\param[in] name Name of the SharedMemObject\n+ * \\param[in] args Args to pass to the constructor of the object in shared memory\n+ */\n+\n+/**\n+ * \\fn SharedMemObject::SharedMemObject(SharedMemObject<T> &&rhs)\n+ * \\brief Move constructor for SharedMemObject\n+ * \\param[in] rhs The object to move\n+ */\n+\n+/**\n+ * \\fn SharedMemObject::~SharedMemObject()\n+ * \\brief SharedMemObject destructor\n+ *\n+ * Destroys the object created in the shared memory and then unmaps the shared memory.\n+ * Decrements the shared memory descriptor use count.\n+ */\n+\n+/**\n+ * \\fn SharedMemObject::operator=(SharedMemObject<T> &&rhs)\n+ * \\brief Operator= for SharedMemObject\n+ * \\param[in] rhs The SharedMemObject object to take the data from\n+ */\n+\n+/**\n+ * \\fn SharedMemObject::operator->()\n+ * \\brief Operator-> for SharedMemObject\n+ * \\return The pointer to the object\n+ */\n+\n+/**\n+ * \\fn const T *SharedMemObject::operator->() const\n+ * \\brief Operator-> for SharedMemObject\n+ * \\return The pointer to the const object\n+ */\n+\n+/**\n+ * \\fn SharedMemObject::operator*()\n+ * \\brief Operator* for SharedMemObject\n+ * \\return The reference to the object\n+ */\n+\n+/**\n+ * \\fn const T &SharedMemObject::operator*() const\n+ * \\brief Operator* for SharedMemObject\n+ * \\return Const reference to the object\n+ */\n+\n+/**\n+ * \\fn SharedMemObject::operator bool()\n+ * \\brief Operator bool() for SharedMemObject\n+ * \\return True if the object was created OK in the shared memory, false otherwise\n+ */\n+\n+} // namespace libcamera\n",
    "prefixes": [
        "v5",
        "05/18"
    ]
}