From patchwork Fri Oct 13 07:48:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Naushir Patuck X-Patchwork-Id: 19127 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 89BCDC32B7 for ; Fri, 13 Oct 2023 07:48:54 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id D053D6298E; Fri, 13 Oct 2023 09:48:52 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1697183332; bh=U+LuW5XpM1TGIphdq0IiFVOuYqOc16ZDNqA+E0cEDeA=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=x+K3bRBjpW+qKtWNWdqb5DpAzHfH3rgdubvjLMAThWt8nNjzJF4KNGyzcN+67UVUA 495MEzczzGO9DcHGHGr7gpUxorW3Ve3bPL2IOteJL1J9p+KYjq1K3Nwv8ykix3giId KXsRtFeFMtg13iqdnA97fPuNS8i0bVKNN2xMAN7VGhIC8v2+c6VXPAYwoDqx4Y4pPF i6CCxpPnTKi8xbM5c0NWNuUPR8ahZdDbGbLCbdJHllnxNdiOMEexjFpzmDOc0kPtF6 WVFml3fs01+dL1gKmrIvv1HrEMCuCm1Dpk+HFE6i0EUZcIMek2YZoXYzb16j5trXf1 j7JCcsHrKn6qQ== Received: from mail-wr1-x42f.google.com (mail-wr1-x42f.google.com [IPv6:2a00:1450:4864:20::42f]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 5D67162980 for ; Fri, 13 Oct 2023 09:48:49 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=raspberrypi.com header.i=@raspberrypi.com header.b="IbewTnly"; dkim-atps=neutral Received: by mail-wr1-x42f.google.com with SMTP id ffacd0b85a97d-3232be274a0so1920952f8f.1 for ; Fri, 13 Oct 2023 00:48:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=raspberrypi.com; s=google; t=1697183329; x=1697788129; 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=Tf9cMYwANGl6wI2KKAEoNBPrx/L8GaWXI40cn6J1ToE=; b=IbewTnlyU1GW04W+JKOYc0F6Yd5LWxF/tXOYlQalNgBz1tCwosfa1tZWNDA1/F/f0y ByI2tw+WheeKplMjrJ42mGQQSDBPTAelxlGSVgOAgiqrIED1zaqfoVEtQQtNT528Pv6T zSejb6ygt7ajRnlHQ1BM805qDAGq5mWEritn6c9CDcz3E2BIy3lsyK1Ogq4Dxmp/N9so j+dk94C4QapqMIRfDNYIANoUTBeeXJ9Yx3giLVHsPO1hAq5PlpzDlkAIl2aS3xf4DLhi KWMaqddt4kdKbPVuUvnnIvyiLKHgxu4twxniGgie+7imLnWzjYjBPYlSUUqkjzmXdRaz tq8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697183329; x=1697788129; 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=Tf9cMYwANGl6wI2KKAEoNBPrx/L8GaWXI40cn6J1ToE=; b=I1CaeXozWcG7bma9ecaNF29IihpsvzcJt0tXqZX4n0AoWfJ+HNhVITzsGBtFUdRz7/ Tk+EflBVz1F6N9/xCCcYy26s7MhFUoe/hUTZ9aQz6YG6bjjQ83ydh7V9dhc/zfYNcUc6 tq5QxGg0cIovYNoGV7blX2f/VkZiNGfL8r/57DixpcG+JCm5mBGJeqgUhoK9X0h9+j25 0JoPlXaeht8HrjJ2kgBmnGxTtRP/0WYzudtvXmKpF+CPxBbec6yyJxwYMvH1to5UTdjK WLeH6buYGH/VDzV6/AqTriyQQyvBEbd+ACGHcoHmKFJkoIhKXEht5h7jI1AHIrvhDkeP xuNA== X-Gm-Message-State: AOJu0Yxe5tukCKXaQQMsaLgC4Gh5pnvAoKc3Puv+xmlDLM78VQwEc2gZ QRM3UoMku+7/HN2TVRGN2jjvrhaeUb8zlGYWzfk5KQ== X-Google-Smtp-Source: AGHT+IGrV7VQi2p4m1VWoVxVHepXo60OLME3mGMYohrmv+Y2prtOitBULGxypvWjVjl73hx5nUO7SQ== X-Received: by 2002:a5d:6904:0:b0:31f:fc9a:a03 with SMTP id t4-20020a5d6904000000b0031ffc9a0a03mr18498993wru.20.1697183328798; Fri, 13 Oct 2023 00:48:48 -0700 (PDT) Received: from localhost.localdomain ([93.93.133.154]) by smtp.gmail.com with ESMTPSA id m16-20020a7bca50000000b003fee6e170f9sm1791890wml.45.2023.10.13.00.48.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Oct 2023 00:48:48 -0700 (PDT) To: libcamera-devel@lists.libcamera.org Date: Fri, 13 Oct 2023 08:48:24 +0100 Message-Id: <20231013074841.16972-4-naush@raspberrypi.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231013074841.16972-1-naush@raspberrypi.com> References: <20231013074841.16972-1-naush@raspberrypi.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 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 Cc: Jacopo Mondi 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 | 128 ++++++++++++++++++ 1 file changed, 128 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..aa56c2209201 --- /dev/null +++ b/src/libcamera/pipeline/rpi/common/shared_mem_object.h @@ -0,0 +1,128 @@ +/* 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 + +#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(SharedMemObject &&rhs) + { + this->name_ = std::move(rhs.name_); + this->fd_ = std::move(rhs.fd_); + this->obj_ = rhs.obj_; + rhs.obj_ = nullptr; + } + + ~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 */