Patch Detail
Show a patch.
GET /api/1.1/patches/19825/?format=api
{ "id": 19825, "url": "https://patchwork.libcamera.org/api/1.1/patches/19825/?format=api", "web_url": "https://patchwork.libcamera.org/patch/19825/", "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": "<20240404084657.353464-7-mzamazal@redhat.com>", "date": "2024-04-04T08:46:43", "name": "[v7,06/18] libcamera: shared_mem_object: reorganize the code and document the SharedMemObject class", "commit_ref": null, "pull_url": null, "state": "superseded", "archived": false, "hash": "a10c8141a51c3f2cc50a8911aad43df8121c3925", "submitter": { "id": 177, "url": "https://patchwork.libcamera.org/api/1.1/people/177/?format=api", "name": "Milan Zamazal", "email": "mzamazal@redhat.com" }, "delegate": null, "mbox": "https://patchwork.libcamera.org/patch/19825/mbox/", "series": [ { "id": 4246, "url": "https://patchwork.libcamera.org/api/1.1/series/4246/?format=api", "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=4246", "date": "2024-04-04T08:46:37", "name": "libcamera: introduce Software ISP and Software IPA", "version": 7, "mbox": "https://patchwork.libcamera.org/series/4246/mbox/" } ], "comments": "https://patchwork.libcamera.org/api/patches/19825/comments/", "check": "pending", "checks": "https://patchwork.libcamera.org/api/patches/19825/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 ED0BAC32CB\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 4 Apr 2024 08:47:43 +0000 (UTC)", "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 4B78F63382;\n\tThu, 4 Apr 2024 10:47:43 +0200 (CEST)", "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 2FA6063382\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 4 Apr 2024 10:47:40 +0200 (CEST)", "from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73])\n\tby relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3,\n\tcipher=TLS_AES_256_GCM_SHA384) id us-mta-42-wKV4Cie0NAemGYoQvfWqSw-1;\n\tThu, 04 Apr 2024 04:47:36 -0400", "from smtp.corp.redhat.com\n\t(int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5])\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 4B1063816B59;\n\tThu, 4 Apr 2024 08:47:35 +0000 (UTC)", "from nuthatch.brq.redhat.com (unknown [10.43.17.39])\n\tby smtp.corp.redhat.com (Postfix) with ESMTP id E2A188173;\n\tThu, 4 Apr 2024 08:47:33 +0000 (UTC)" ], "Authentication-Results": "lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=redhat.com header.i=@redhat.com\n\theader.b=\"I5sgsVZc\"; dkim-atps=neutral", "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n\ts=mimecast20190719; t=1712220459;\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=rKFfXk4G2g2DKDJv7PPMOxATyZ7/0EzPfMRFyWraXbk=;\n\tb=I5sgsVZcD02USVeV8SmxfEBT9QAa1Ptvx7TolGVVUTZu7ub7JSug5IH164LGkWKWAaTsI9\n\tK95Kft+bp0ht7Lnx44JWbIAPCmk8oWaPgCIqMl7L7H7MfUCwisAoH2Wn+bdgdLEmjui4wq\n\tH7z8alsrTG4wJav3j2P+H3U0H9DuYrY=", "X-MC-Unique": "wKV4Cie0NAemGYoQvfWqSw-1", "From": "Milan Zamazal <mzamazal@redhat.com>", "To": "libcamera-devel@lists.libcamera.org", "Cc": "Andrei Konovalov <andrey.konovalov.ynk@gmail.com>,\n\tBryan O'Donoghue <bryan.odonoghue@linaro.org>,\n\tMaxime Ripard <mripard@redhat.com>, Milan Zamazal <mzamazal@redhat.com>, \n\tPavel Machek <pavel@ucw.cz>, Hans de Goede <hdegoede@redhat.com>,\n\tKieran Bingham <kieran.bingham@ideasonboard.com>,\n\tLaurent Pinchart <laurent.pinchart@ideasonboard.com>,\n\tDennis Bonke <admin@dennisbonke.com>", "Subject": "[PATCH v7 06/18] libcamera: shared_mem_object: reorganize the code\n\tand document the SharedMemObject class", "Date": "Thu, 4 Apr 2024 10:46:43 +0200", "Message-ID": "<20240404084657.353464-7-mzamazal@redhat.com>", "In-Reply-To": "<20240404084657.353464-1-mzamazal@redhat.com>", "References": "<20240404084657.353464-1-mzamazal@redhat.com>", "MIME-Version": "1.0", "X-Scanned-By": "MIMEDefang 3.4.1 on 10.11.54.5", "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>", "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\nThe SharedMemObject class template contains a fair amount of inline code that\ndoes not depend on the template types T. To avoid duplicating it in every\ntemplate specialization, split that code to a separate base SharedMem class.\n\nWe don't define copy semantics for the classes (we don't need one at the moment)\nand we make them non-copyable since the default copy constructor would lead to\nuse-after-unmap.\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>\nReviewed-by: Milan Zamazal <mzamazal@redhat.com>\n---\n .../libcamera/internal/shared_mem_object.h | 108 ++++----\n src/libcamera/meson.build | 1 +\n src/libcamera/shared_mem_object.cpp | 244 ++++++++++++++++++\n 3 files changed, 303 insertions(+), 50 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 8f2e7120..b2ab9ad1 100644\n--- a/include/libcamera/internal/shared_mem_object.h\n+++ b/include/libcamera/internal/shared_mem_object.h\n@@ -1,85 +1,103 @@\n /* SPDX-License-Identifier: LGPL-2.1-or-later */\n /*\n- * Copyright (C) 2023, Raspberry Pi Ltd\n+ * Copyright (C) 2023 Raspberry Pi Ltd\n+ * Copyright (C) 2024 Andrei Konovalov\n+ * Copyright (C) 2024 Dennis Bonke\n *\n- * shared_mem_object.h - Helper class for shared memory allocations\n+ * shared_mem_object.h - Helpers for shared memory allocations\n */\n #pragma once\n \n-#include <fcntl.h>\n #include <stddef.h>\n+#include <stdint.h>\n #include <string>\n #include <sys/mman.h>\n-#include <sys/stat.h>\n-#include <unistd.h>\n+#include <type_traits>\n #include <utility>\n \n #include <libcamera/base/class.h>\n #include <libcamera/base/shared_fd.h>\n+#include <libcamera/base/span.h>\n \n namespace libcamera {\n \n-template<class T>\n-class SharedMemObject\n+class SharedMem\n {\n public:\n-\tstatic constexpr std::size_t size = sizeof(T);\n+\tusing span = Span<uint8_t>;\n \n-\tSharedMemObject()\n-\t\t: obj_(nullptr)\n+\tSharedMem()\n+\t\t: mem_(span())\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+\tSharedMem(const std::string &name, std::size_t size);\n+\tSharedMem(SharedMem &&rhs);\n+\n+\tvirtual ~SharedMem();\n+\n+\tSharedMem &operator=(SharedMem &&rhs);\n+\n+\tconst SharedFD &fd() const\n \t{\n-\t\tvoid *mem;\n-\t\tint ret;\n+\t\treturn fd_;\n+\t}\n \n-\t\tret = memfd_create(name_.c_str(), MFD_CLOEXEC);\n-\t\tif (ret < 0)\n-\t\t\treturn;\n+\tspan mem() const\n+\t{\n+\t\treturn mem_;\n+\t}\n \n-\t\tfd_ = SharedFD(std::move(ret));\n-\t\tif (!fd_.isValid())\n-\t\t\treturn;\n+\texplicit operator bool() const\n+\t{\n+\t\treturn !mem_.empty();\n+\t}\n \n-\t\tret = ftruncate(fd_.get(), size);\n-\t\tif (ret < 0)\n-\t\t\treturn;\n+private:\n+\tLIBCAMERA_DISABLE_COPY(SharedMem)\n+\n+\tSharedFD fd_;\n+\n+\tspan mem_;\n+};\n+\n+template<class T, typename = std::enable_if_t<std::is_standard_layout<T>::value>>\n+class SharedMemObject : public SharedMem\n+{\n+public:\n+\tstatic constexpr std::size_t size = sizeof(T);\n+\n+\tSharedMemObject()\n+\t\t: SharedMem(), obj_(nullptr)\n+\t{\n+\t}\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+\ttemplate<class... Args>\n+\tSharedMemObject(const std::string &name, Args &&...args)\n+\t\t: SharedMem(name, size), obj_(nullptr)\n+\t{\n+\t\tif (mem().empty())\n \t\t\treturn;\n \n-\t\tobj_ = new (mem) T(std::forward<Args>(args)...);\n+\t\tobj_ = new (mem().data()) 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-\tLIBCAMERA_DISABLE_COPY(SharedMemObject)\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 +123,9 @@ 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+\tLIBCAMERA_DISABLE_COPY(SharedMemObject)\n+\n \tT *obj_;\n };\n \ndiff --git a/src/libcamera/meson.build b/src/libcamera/meson.build\nindex dd8107fa..ce31180b 100644\n--- a/src/libcamera/meson.build\n+++ b/src/libcamera/meson.build\n@@ -39,6 +39,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..210a7a37\n--- /dev/null\n+++ b/src/libcamera/shared_mem_object.cpp\n@@ -0,0 +1,244 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2023 Raspberry Pi Ltd\n+ * Copyright (C) 2024 Andrei Konovalov\n+ * Copyright (C) 2024 Dennis Bonke\n+ * Copyright (C) 2024 Ideas on Board Oy\n+ *\n+ * shared_mem_object.cpp - Helpers for shared memory allocations\n+ */\n+\n+#include \"libcamera/internal/shared_mem_object.h\"\n+\n+#include <stddef.h>\n+#include <stdint.h>\n+#include <sys/types.h>\n+#include <unistd.h>\n+\n+#include <libcamera/base/shared_fd.h>\n+\n+/**\n+ * \\file shared_mem_object.cpp\n+ * \\brief Helpers for shared memory allocations\n+ */\n+\n+namespace libcamera {\n+\n+/**\n+ * \\class SharedMem\n+ * \\brief Helper class to allocate and manage memory shareable between processes\n+ *\n+ * SharedMem manages memory suitable for sharing between processes. When an\n+ * instance is constructed, it allocates a memory buffer of the requested size\n+ * backed by an anonymous file, using the memfd API.\n+ *\n+ * The allocated memory is exposed by the mem() function. If memory allocation\n+ * fails, the function returns an empty Span. This can be also checked using the\n+ * bool() operator.\n+ *\n+ * The file descriptor for the backing file is exposed as a SharedFD by the fd()\n+ * function. It can be shared with other processes across IPC boundaries, which\n+ * can then map the memory with 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+ * \\typedef SharedMem::span\n+ * \\brief Type of the Span wrapping the memory pointer\n+ */\n+\n+/**\n+ * \\brief Construct a SharedMem with memory of the given \\a size\n+ * \\param[in] name Name of the SharedMem\n+ * \\param[in] size Size of the shared memory to allocate and map\n+ *\n+ * The \\a name is used for debugging purpose only. Multiple SharedMem instances\n+ * can have the same name.\n+ */\n+SharedMem::SharedMem(const std::string &name, std::size_t size)\n+\t: mem_(span())\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\tfd_ = SharedFD();\n+\t\treturn;\n+\t}\n+\n+\tmem_ = span{\n+\t\tstatic_cast<uint8_t *>(\n+\t\t\tmmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED,\n+\t\t\t fd_.get(), 0)),\n+\t\tsize\n+\t};\n+\tif (mem_.data() == MAP_FAILED) {\n+\t\tmem_ = span();\n+\t\tfd_ = SharedFD();\n+\t}\n+}\n+\n+/**\n+ * \\brief Move constructor for SharedMem\n+ * \\param[in] rhs The object to move\n+ */\n+SharedMem::SharedMem(SharedMem &&rhs)\n+{\n+\tthis->fd_ = std::move(rhs.fd_);\n+\tthis->mem_ = rhs.mem_;\n+\trhs.mem_ = span();\n+}\n+\n+/**\n+ * \\brief Destroy the SharedMem instance\n+ *\n+ * Destroying an instance invalidates the memory mapping exposed with mem().\n+ * Other mappings of the backing file, created in this or other processes with\n+ * mmap(), remain valid.\n+ *\n+ * Similarly, other references to the backing file descriptor created by copying\n+ * the SharedFD returned by fd() remain valid. The underlying memory will be\n+ * freed only when all file descriptors that reference the anonymous file get\n+ * closed.\n+ */\n+SharedMem::~SharedMem()\n+{\n+\tif (!mem_.empty())\n+\t\tmunmap(mem_.data(), mem_.size_bytes());\n+}\n+\n+/**\n+ * \\brief Move assignment operator for SharedMem\n+ * \\param[in] rhs The object to move\n+ */\n+SharedMem &SharedMem::operator=(SharedMem &&rhs)\n+{\n+\tthis->fd_ = std::move(rhs.fd_);\n+\tthis->mem_ = rhs.mem_;\n+\trhs.mem_ = span();\n+\treturn *this;\n+}\n+\n+/**\n+ * \\fn const SharedFD &SharedMem::fd() const\n+ * \\brief Retrieve the file descriptor for the underlying shared memory\n+ * \\return The file descriptor, or an invalid SharedFD if allocation failed\n+ */\n+\n+/**\n+ * \\fn span SharedMem::mem() const\n+ * \\brief Retrieve the underlying shared memory\n+ * \\return The memory buffer, or an empty span if allocation failed\n+ */\n+\n+/**\n+ * \\fn SharedMem::operator bool()\n+ * \\brief Check if the shared memory allocation succeeded\n+ * \\return True if allocation of the shared memory succeeded, false otherwise\n+ */\n+\n+/**\n+ * \\class SharedMemObject\n+ * \\brief Helper class to allocate an object in shareable memory\n+ * \\tparam The object type\n+ *\n+ * The SharedMemObject class is a specialization of the SharedMem class that\n+ * wraps an object of type \\a T and constructs it in shareable memory. It uses\n+ * the same underlying memory allocation and sharing mechanism as the SharedMem\n+ * class.\n+ *\n+ * The wrapped object is constructed at the same time as the SharedMemObject\n+ * instance, by forwarding the arguments passed to the SharedMemObject\n+ * constructor. The underlying memory allocation is sized to the object \\a T\n+ * size. The bool() operator should be used to check the allocation was\n+ * successful. The object can be accessed using the dereference operators\n+ * operator*() and operator->().\n+ *\n+ * While no restriction on the type \\a T is enforced, not all types are suitable\n+ * for sharing between multiple processes. Most notably, any object type that\n+ * contains pointer or reference members will likely cause issues. Even if those\n+ * members refer to other members of the same object, the shared memory will be\n+ * mapped at different addresses in different processes, and the pointers will\n+ * not be valid.\n+ *\n+ * A new anonymous file is created for every SharedMemObject instance. If there\n+ * is a need to share a large number of small objects, these objects should be\n+ * grouped into a single larger object to limit the number of file descriptors.\n+ *\n+ * To share the object with other processes, see the SharedMem documentation.\n+ */\n+\n+/**\n+ * \\var SharedMemObject::size\n+ * \\brief The size of the object stored in shared memory\n+ */\n+\n+/**\n+ * \\fn SharedMemObject::SharedMemObject(const std::string &name, Args &&...args)\n+ * \\brief Construct a SharedMemObject\n+ * \\param[in] name Name of the SharedMemObject\n+ * \\param[in] args Arguments to pass to the constructor of the object T\n+ *\n+ * The \\a name is used for debugging purpose only. Multiple SharedMem instances\n+ * can have the same name.\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 Destroy the SharedMemObject instance\n+ *\n+ * Destroying a SharedMemObject calls the wrapped T object's destructor. While\n+ * the underlying memory may not be freed immediately if other mappings have\n+ * been created manually (see SharedMem::~SharedMem() for more information), the\n+ * stored object may be modified. Depending on the ~T() destructor, accessing\n+ * the object after destruction of the SharedMemObject causes undefined\n+ * behaviour. It is the responsibility of the user of this class to synchronize\n+ * with other users who have access to the shared object.\n+ */\n+\n+/**\n+ * \\fn SharedMemObject::operator=(SharedMemObject<T> &&rhs)\n+ * \\brief Move assignment operator for SharedMemObject\n+ * \\param[in] rhs The SharedMemObject object to take the data from\n+ *\n+ * Moving a SharedMemObject does not affect the stored object.\n+ */\n+\n+/**\n+ * \\fn SharedMemObject::operator->()\n+ * \\brief Dereference the stored object\n+ * \\return Pointer to the stored object\n+ */\n+\n+/**\n+ * \\fn const T *SharedMemObject::operator->() const\n+ * \\copydoc SharedMemObject::operator->\n+ */\n+\n+/**\n+ * \\fn SharedMemObject::operator*()\n+ * \\brief Dereference the stored object\n+ * \\return Reference to the stored object\n+ */\n+\n+/**\n+ * \\fn const T &SharedMemObject::operator*() const\n+ * \\copydoc SharedMemObject::operator*\n+ */\n+\n+} /* namespace libcamera */\n", "prefixes": [ "v7", "06/18" ] }