From patchwork Fri Oct 6 13:19:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Naushir Patuck X-Patchwork-Id: 19105 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 51EF1C32B7 for ; Fri, 6 Oct 2023 13:20:03 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 9812C6296F; Fri, 6 Oct 2023 15:20:02 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1696598402; bh=MrbguUUhWoJ3mvBKCag4gOOpqwweJsmOEDY+C/5I+lA=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=C+Xw2CN3DDx4MBd30F0D49A3hV1oH4eynB05XZfX8RRkhKbWGyPh4OpMT6sxtWJui A8Q0Oyks7x4rYNOsGVfnRwNoc/1a1fXK1MNqZOC/KF//PnkIyqFAgcdNnss5KjgrPy QW3CKAs3aT895hxtvmpP7dKSniYXbUlHMAiKe3huDvx0dTnOTn1lccI3Nx9S9kb9kU OiRsrL7Z0AUV937Wr+3Erhu8ZQrh+z1F3b/gY/L87ydCE4NRU2HEtRPehtqHBND7rU +WriGWwFWpzGt/2HgDn7HZ9G7Tufb1VsaEehJ/l5WxDKDv+T6RZhb3XVAE8o2V+Moo UlSiOVx2WiOdQ== Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 3815461DDF for ; Fri, 6 Oct 2023 15:20:00 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=raspberrypi.com header.i=@raspberrypi.com header.b="b1/TcgLS"; dkim-atps=neutral Received: by mail-wr1-x433.google.com with SMTP id ffacd0b85a97d-3231dff4343so1329432f8f.0 for ; Fri, 06 Oct 2023 06:20:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=raspberrypi.com; s=google; t=1696598399; x=1697203199; darn=lists.libcamera.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Mfz7taO4zHPB6ZKI/hoU5uhlW/mJ3kymDVPfQGARFmo=; b=b1/TcgLS+Vv7ucOnboZ0A2yKMEj+JAyirPZ5jpn71lj0cEMDhxy/p/vi7XzyAfT+TB eyKpo2VXOFtPUpEMXE4eSKuTdqqNBXeq/EiMpECJ7dt5ok15ZwFB9isSyeQGaxYggsy7 nIicaYlX5cF5ZEGdtdNMOzs9/viZ2AVrDsoj4vzJbDu0X9IsEJ3KOu4ETEkQO0R8aGOu 4CCEI9VnOdYQzU4h//xhrhAmPg4f/aT/0JfAMawLgfh7i7cipI190WItBBNisJboOKHA PdqvqPLKBeYRMFwdD9XV6YJC5EsGxAtiaKt3p90BJvW7Jygz7MMpOPbd5+sYf9U/M3tE VnuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696598399; x=1697203199; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Mfz7taO4zHPB6ZKI/hoU5uhlW/mJ3kymDVPfQGARFmo=; b=ClgR4U330U+QfftNuUCVPklb/PUlkh69hqx9e4dupXgXQSXPyQTHacunousnckEPLs 7sPrtSbBafpWhIS3xU+vim2EsIBlg7hPkpv7buHzASjHDWh4vP/7cGAwJyo4PPlsDhPQ IshLtKv24D4SIRPsOlCR8tbMNlrYlsKSlZooSVt95rCy6tuKka+ys9KDJNEeH1nKt2dn b52/6eMwQIkNVsTiHoRoNNznxLwlE44xmE66F3lKR/XjA+GoDNaCo+kiwK2OO5KqTVT9 15c5IUkedZ4ylq0eTACXRFs8EcgqZNTXYYDrOUM7/UquVrrD4bcptpfC5F450Nisq/9g kiHA== X-Gm-Message-State: AOJu0YzPFghpix+r6DcEnOBDWlLaQOC9++1bgH9jjGwJoGh5H+g9W9PH 44QdKfZ/3Mgol9ADiOTXHNYb0zJNsu0NcNZ17SLrLQ== X-Google-Smtp-Source: AGHT+IEabilBkvqh33c9tOG7dqXMMyZgFS3aPGRW/p3+IQiBm59gL01aN1cnkXG+miD43C3VqqpxTQ== X-Received: by 2002:a5d:55c7:0:b0:321:6339:f523 with SMTP id i7-20020a5d55c7000000b003216339f523mr4762413wrw.22.1696598399692; Fri, 06 Oct 2023 06:19:59 -0700 (PDT) Received: from localhost.localdomain ([93.93.133.154]) by smtp.gmail.com with ESMTPSA id m8-20020a056000180800b00321773bb933sm1639478wrh.77.2023.10.06.06.19.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 Oct 2023 06:19:58 -0700 (PDT) To: libcamera-devel@lists.libcamera.org Date: Fri, 6 Oct 2023 14:19:43 +0100 Message-Id: <20231006132000.23504-4-naush@raspberrypi.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231006132000.23504-1-naush@raspberrypi.com> References: <20231006132000.23504-1-naush@raspberrypi.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 03/20] pipeline: rpi: Add SharedMemObject class X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Naushir Patuck via libcamera-devel From: Naushir Patuck Reply-To: Naushir Patuck Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Add new SharedMemObject class that wraps a memfd memory allocation and constructs a templated object in the memory. With appropriate locking, this object can then be shared across different processes using the associated allocation file handle. Signed-off-by: Naushir Patuck Reviewed-by: David Plowman Reviewed-by: Jacopo Mondi --- .../pipeline/rpi/common/shared_mem_object.h | 118 ++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 src/libcamera/pipeline/rpi/common/shared_mem_object.h diff --git a/src/libcamera/pipeline/rpi/common/shared_mem_object.h b/src/libcamera/pipeline/rpi/common/shared_mem_object.h new file mode 100644 index 000000000000..69051ecc3f52 --- /dev/null +++ b/src/libcamera/pipeline/rpi/common/shared_mem_object.h @@ -0,0 +1,118 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2023, Raspberry Pi Ltd + * + * shared_mem_object.h - Helper class for shared memory allocations + */ +#pragma once + +#include +#include +#include +#include +#include + +#include +#include + +namespace libcamera { + +namespace RPi { + +template +class SharedMemObject +{ +public: + static constexpr std::size_t SIZE = sizeof(T); + + SharedMemObject() + : obj_(nullptr) + { + } + + template + SharedMemObject(const std::string &name, Args &&...args) + : name_(name), obj_(nullptr) + { + void *mem; + int ret; + + ret = memfd_create(name_.c_str(), MFD_CLOEXEC); + if (ret < 0) + return; + + fd_ = SharedFD(std::move(ret)); + if (!fd_.isValid()) + return; + + ret = ftruncate(fd_.get(), SIZE); + if (ret < 0) + return; + + mem = mmap(nullptr, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, + fd_.get(), 0); + if (mem == MAP_FAILED) + return; + + obj_ = new (mem) T(std::forward(args)...); + } + + ~SharedMemObject() + { + if (obj_) { + obj_->~T(); + munmap(obj_, SIZE); + } + } + + /* Make SharedMemObject non-copyable for now. */ + LIBCAMERA_DISABLE_COPY(SharedMemObject) + + SharedMemObject &operator=(SharedMemObject &&rhs) + { + this->name_ = std::move(rhs.name_); + this->fd_ = std::move(rhs.fd_); + this->obj_ = rhs.obj_; + rhs.obj_ = nullptr; + return *this; + } + + T *operator->() + { + return obj_; + } + + const T *operator->() const + { + return obj_; + } + + T &operator*() + { + return *obj_; + } + + const T &operator*() const + { + return *obj_; + } + + const SharedFD &fd() const + { + return fd_; + } + + explicit operator bool() const + { + return !!obj_; + } + +private: + std::string name_; + SharedFD fd_; + T *obj_; +}; + +} /* namespace RPi */ + +} /* namespace libcamera */