{"id":19487,"url":"https://patchwork.libcamera.org/api/1.1/patches/19487/?format=json","web_url":"https://patchwork.libcamera.org/patch/19487/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20240214170122.60754-6-hdegoede@redhat.com>","date":"2024-02-14T17:01:09","name":"[v3,05/16] libcamera: shared_mem_object: reorganize the code and document the SharedMemObject class","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"87ea57719adef194ad2b33d530d6ac1eb6b36057","submitter":{"id":102,"url":"https://patchwork.libcamera.org/api/1.1/people/102/?format=json","name":"Hans de Goede","email":"hdegoede@redhat.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/19487/mbox/","series":[{"id":4167,"url":"https://patchwork.libcamera.org/api/1.1/series/4167/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4167","date":"2024-02-14T17:01:04","name":"libcamera: introduce Software ISP and Software IPA","version":3,"mbox":"https://patchwork.libcamera.org/series/4167/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/19487/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/19487/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 5A7DAC3259\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 14 Feb 2024 17:01:45 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 18B2362813;\n\tWed, 14 Feb 2024 18:01:45 +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 4749C6280F\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 14 Feb 2024 18:01:42 +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-381-hIKTz6E0OZe1JJyNXQDEPQ-1; Wed, 14 Feb 2024 12:01:39 -0500","from smtp.corp.redhat.com\n\t(int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6])\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 BEE468493E4;\n\tWed, 14 Feb 2024 17:01:38 +0000 (UTC)","from localhost.localdomain (unknown [10.39.192.30])\n\tby smtp.corp.redhat.com (Postfix) with ESMTP id A723E2166B36;\n\tWed, 14 Feb 2024 17:01:37 +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=\"XLryB+uN\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n\ts=mimecast20190719; t=1707930101;\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=S8xMC7ek2z3oWz411INsDF6bjKRBEiJQQuQ8jEJlrx0=;\n\tb=XLryB+uNS7wbA0tQzTx14I+UAxiRUtfIndmouYKs4MLMxb/Tp9Iz9P2qKgS21/SuO2dyZk\n\t0hTKNnHg+EZv0EtT1gY96FU4E9UgiPqRt+rOX8gJ137A8O9iI08oQIE7OMs7JkFSGaVlCu\n\twOD6Z/NX/8GzXl1z8jDl7MCTVNJgc88=","X-MC-Unique":"hIKTz6E0OZe1JJyNXQDEPQ-1","From":"Hans de Goede <hdegoede@redhat.com>","To":"libcamera-devel@lists.libcamera.org","Subject":"[PATCH v3 05/16] libcamera: shared_mem_object: reorganize the code\n\tand document the SharedMemObject class","Date":"Wed, 14 Feb 2024 18:01:09 +0100","Message-ID":"<20240214170122.60754-6-hdegoede@redhat.com>","In-Reply-To":"<20240214170122.60754-1-hdegoede@redhat.com>","References":"<20240214170122.60754-1-hdegoede@redhat.com>","MIME-Version":"1.0","X-Scanned-By":"MIMEDefang 3.4.1 on 10.11.54.6","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>\n---\n .../libcamera/internal/shared_mem_object.h    |  92 ++++++---\n src/libcamera/meson.build                     |   1 +\n src/libcamera/shared_mem_object.cpp           | 191 ++++++++++++++++++\n 3 files changed, 253 insertions(+), 31 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 bfb639ee..a4de6500 100644\n--- a/include/libcamera/internal/shared_mem_object.h\n+++ b/include/libcamera/internal/shared_mem_object.h\n@@ -7,11 +7,8 @@\n #pragma once\n \n #include <cstddef>\n-#include <fcntl.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,6 +16,59 @@\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+\t~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+\tvoid *mem_;\n+};\n+\n template<class T>\n class SharedMemObject\n {\n@@ -32,26 +82,11 @@ public:\n \n \ttemplate<class... Args>\n \tSharedMemObject(const std::string &name, Args &&...args)\n-\t\t: name_(name), obj_(nullptr)\n+\t\t: shMem_(name, SIZE), obj_(nullptr)\n \t{\n-\t\tvoid *mem;\n-\t\tint ret;\n+\t\tvoid *mem = shMem_.mem();\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\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\tif (mem == nullptr)\n \t\t\treturn;\n \n \t\tobj_ = new (mem) T(std::forward<Args>(args)...);\n@@ -59,18 +94,15 @@ public:\n \n \tSharedMemObject(SharedMemObject<T> &&rhs)\n \t{\n-\t\tthis->name_ = std::move(rhs.name_);\n-\t\tthis->fd_ = std::move(rhs.fd_);\n+\t\tthis->shMem_ = std::move(rhs.shMem_);\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 +110,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\tthis->shMem_ = std::move(rhs.shMem_);\n \t\tthis->obj_ = rhs.obj_;\n \t\trhs.obj_ = nullptr;\n \t\treturn *this;\n@@ -107,7 +138,7 @@ public:\n \n \tconst SharedFD &fd() const\n \t{\n-\t\treturn fd_;\n+\t\treturn shMem_.fd();\n \t}\n \n \texplicit operator bool() const\n@@ -116,8 +147,7 @@ public:\n \t}\n \n private:\n-\tstd::string name_;\n-\tSharedFD fd_;\n+\tSharedMem shMem_;\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..06bbee38\n--- /dev/null\n+++ b/src/libcamera/shared_mem_object.cpp\n@@ -0,0 +1,191 @@\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 Contstructor 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 underlaying shared memory\n+ * \\return The file descriptor\n+ */\n+\n+/**\n+ * \\fn void *SharedMem::mem() const\n+ * \\brief Gets the pointer to the underlaying 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+ * \\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 check that\n+ * the SharedMemObject was created successfully, one needs to verify that the\n+ * underlying SharedFD (the reference to it is returned by SharedMemObject::fd() member\n+ * function) is valid. The object created in the shared memory can be accessed using\n+ * the SharedMemObject::operator*() indirection operator. Its members can be accessed\n+ * with the SharedMemObject::operator->() member of pointer 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 Contstructor 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::fd()\n+ * \\brief Gets the file descriptor for the underlaying storage file\n+ * \\return The shared memory file descriptor (as SharedFD)\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":["v3","05/16"]}