From patchwork Fri Dec 2 16:14:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miguel Ojeda X-Patchwork-Id: 28988 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp949243wrr; Fri, 2 Dec 2022 08:21:22 -0800 (PST) X-Google-Smtp-Source: AA0mqf5apj1sk2JXaZbiKHRlsauCzwn2100T7Iurgt+JDsB5rdiR1SgYWDJNwBILNTlUfQeAIfhf X-Received: by 2002:a17:902:b782:b0:189:a208:d13c with SMTP id e2-20020a170902b78200b00189a208d13cmr17221480pls.145.1669998082032; Fri, 02 Dec 2022 08:21:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669998082; cv=none; d=google.com; s=arc-20160816; b=tCm7Ixopn3Z5ryPBRWiumDvLM+/aCC6di6mbFuNMUnbmOTFwTzm+9i5dUFcAsbG0wL /12Q3ug2bIAz8k8wba69p9xcoMTBCDeM+fhfmPFl5XjIOxn6f7a9PNeRPVoelfS3QMiK v6rVcAdru2rY7AVCI3wKvLEJAlCk5HVIzWctYwI4PPehBAK0dDafStXzGRtHUvi6+8SY EtvfRFDkp4m8Al7tTqx33vXICMt+BAqvEIQxllcSP8axf3LmpIpmWoqG6N2WmiDG4p/R iPjVTHcBSi+hUfc6yqq8BixnvCfbEbFmJCdMAkz7kHKOLRuhc1c+Y/5k5VKn5qn449X3 ZvNA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=sxoDggStGU9gyibisVBvy0AaoTqSPzu+cnxgW3+2CgQ=; b=n71IOh93/hm3Imx0kjGh1r7SSpAPj46DLZW+TI5DivOWqkKbQ+NULg1r6kbE7AUBUQ T9tJ6fCsB9wQIDK30bp3pfzUI4i2fRUzTluhAx+cO57yOihUewUeTxFTHUWFIRRlZIrr mvu+H/2LYFgoeHzRzsjVuiGLY/TqrZmx/1fkb5OqUQmZGSbzOauJDviwseMrAkH4E4zT xaGhvBHvLexk7GX+ogpJMzO/nQKuJw6PUo4UuFh5eI2NAfQrZL1z2TvtrVvoUBt4DH2c jonm/6nPac7sxf7Ya7hSVEPjXMksTHDYqPVKLn8BXiCz4cpB9ej0uxT6zDB7KPNZ6u8e CZIw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=iUVfcvaG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k35-20020a634b63000000b004705b0ca9f1si7411486pgl.545.2022.12.02.08.21.08; Fri, 02 Dec 2022 08:21:22 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=iUVfcvaG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234030AbiLBQS4 (ORCPT + 99 others); Fri, 2 Dec 2022 11:18:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53114 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234070AbiLBQSL (ORCPT ); Fri, 2 Dec 2022 11:18:11 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DEB81DFB59; Fri, 2 Dec 2022 08:16:47 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 7B1A362322; Fri, 2 Dec 2022 16:16:47 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D6F3EC43470; Fri, 2 Dec 2022 16:16:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1669997806; bh=YyjXthwblgrT88DPLFu+XIPGLqjTiZLg+Xpj0PN4MaE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iUVfcvaGJdF/n2QAZNTbyYm5gbDZ+FvzXb1+V8Q7HwFKlarV3seVo1NnOevDs7f9A uxcx1Ce9O/9sI14ZMd98O5sFBiUUgCcucTb4+KI/zXlega/CNBZpzsuRiXAlpPUEux oPtm3kxnmuw1fAt/iUFshg9JMuKQZavSA/KJpFUAd8Rl2yUN8Hbu+6kOtRGKw3hyBp I3ZSh6nvGSQ6ddBxn2yHDHPvwNJKZDL8JoiLX3U6mkmv8zs1eWyZhXSzHNioAell2D 8+znw2S00T/44bGNXLEEUmiMlyC/YIDBT6y9MFZowF6EDXr+RVIhfRd0/qFm5ZqEUb wCkPYCWBYfpTA== From: ojeda@kernel.org To: Miguel Ojeda , Wedson Almeida Filho , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH v2 28/28] rust: types: add `Opaque` type Date: Fri, 2 Dec 2022 17:14:59 +0100 Message-Id: <20221202161502.385525-29-ojeda@kernel.org> In-Reply-To: <20221202161502.385525-1-ojeda@kernel.org> References: <20221202161502.385525-1-ojeda@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751119908643034477?= X-GMAIL-MSGID: =?utf-8?q?1751119908643034477?= From: Wedson Almeida Filho Add the `Opaque` type, which is meant to be used with FFI objects that are never interpreted by Rust code, e.g.: struct Waiter { completion: Opaque, next: *mut Waiter, } It has the advantage that the objects don't have to be zero-initialised before calling their init functions, making the code performance closer to C. Signed-off-by: Wedson Almeida Filho [Reworded, adapted for upstream and applied latest changes] Signed-off-by: Miguel Ojeda --- rust/kernel/types.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs index 3b0c44769708..e84e51ec9716 100644 --- a/rust/kernel/types.rs +++ b/rust/kernel/types.rs @@ -2,6 +2,31 @@ //! Kernel types. +use core::{cell::UnsafeCell, mem::MaybeUninit}; + +/// Stores an opaque value. +/// +/// This is meant to be used with FFI objects that are never interpreted by Rust code. +#[repr(transparent)] +pub struct Opaque(MaybeUninit>); + +impl Opaque { + /// Creates a new opaque value. + pub const fn new(value: T) -> Self { + Self(MaybeUninit::new(UnsafeCell::new(value))) + } + + /// Creates an uninitialised value. + pub const fn uninit() -> Self { + Self(MaybeUninit::uninit()) + } + + /// Returns a raw pointer to the opaque data. + pub fn get(&self) -> *mut T { + UnsafeCell::raw_get(self.0.as_ptr()) + } +} + /// A sum type that always holds either a value of type `L` or `R`. pub enum Either { /// Constructs an instance of [`Either`] containing a value of type `L`.