Patch Detail
Show a patch.
GET /api/1.1/patches/19762/?format=api
{ "id": 19762, "url": "https://patchwork.libcamera.org/api/1.1/patches/19762/?format=api", "web_url": "https://patchwork.libcamera.org/patch/19762/", "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": "<20240319123622.675599-6-mzamazal@redhat.com>", "date": "2024-03-19T12:35:52", "name": "[v6,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": "8cf1bdb22c933320d9e381a88d704939b298cbe4", "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/19762/mbox/", "series": [ { "id": 4231, "url": "https://patchwork.libcamera.org/api/1.1/series/4231/?format=api", "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=4231", "date": "2024-03-19T12:35:47", "name": "libcamera: introduce Software ISP and Software IPA", "version": 6, "mbox": "https://patchwork.libcamera.org/series/4231/mbox/" } ], "comments": "https://patchwork.libcamera.org/api/patches/19762/comments/", "check": "pending", "checks": "https://patchwork.libcamera.org/api/patches/19762/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 7A441C3274\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 19 Mar 2024 12:37:17 +0000 (UTC)", "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id E734D62CB0;\n\tTue, 19 Mar 2024 13:37:16 +0100 (CET)", "from us-smtp-delivery-124.mimecast.com\n\t(us-smtp-delivery-124.mimecast.com [170.10.129.124])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 7E00762CB0\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 19 Mar 2024 13:37:14 +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-563-mrwWM3VaM1q2qOGvES67_g-1; Tue, 19 Mar 2024 08:37:10 -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 A2251186E123;\n\tTue, 19 Mar 2024 12:37:09 +0000 (UTC)", "from nuthatch.redhat.com (unknown [10.45.226.119])\n\tby smtp.corp.redhat.com (Postfix) with ESMTP id 551D11C060A6;\n\tTue, 19 Mar 2024 12:37:08 +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=\"CeATCWUi\"; dkim-atps=neutral", "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n\ts=mimecast20190719; t=1710851833;\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=+U3fZupmgEV+NXN5HgwWh32kdIy52Th1rYplyYtySdY=;\n\tb=CeATCWUidoYd7evuOEbW/v+JlvOhdytFIjyKHU/2lgxXgTo3/PasLnwS1YhdCtZJS8Tnap\n\tHZaWFleBac4NAkTLYzBi27a9mO5svStzWjXZHYiQYXkmh0LSDNTGe+a32KW71GPkdqwv/R\n\tdmjEidjpkM8ZJNA7oRrS4JrSaRzF6eM=", "X-MC-Unique": "mrwWM3VaM1q2qOGvES67_g-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\tDennis Bonke <admin@dennisbonke.com>", "Subject": "[PATCH v6 05/18] libcamera: shared_mem_object: reorganize the code\n\tand document the SharedMemObject class", "Date": "Tue, 19 Mar 2024 13:35:52 +0100", "Message-ID": "<20240319123622.675599-6-mzamazal@redhat.com>", "In-Reply-To": "<20240319123622.675599-1-mzamazal@redhat.com>", "References": "<20240319123622.675599-1-mzamazal@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>", "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>\nReviewed-by: Milan Zamazal <mzamazal@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\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\tif (mem_ == nullptr)\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 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..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": [ "v6", "05/18" ] }