From patchwork Wed Apr 5 19:36:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: y86-dev X-Patchwork-Id: 79881 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp547049vqo; Wed, 5 Apr 2023 12:39:49 -0700 (PDT) X-Google-Smtp-Source: AKy350aSi8q38kelKIT/OMalgf/rCug4yGjqHxfGqkoUOTVfYKYGenxPT/MEjPOnrLi3CCV/RE5S X-Received: by 2002:a05:6a20:4913:b0:e6:fd73:e79b with SMTP id ft19-20020a056a20491300b000e6fd73e79bmr393934pzb.14.1680723588894; Wed, 05 Apr 2023 12:39:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680723588; cv=none; d=google.com; s=arc-20160816; b=VqhdWgwM3Q2FNJJd89NnzSnZsqRsAWP7IgK2zUpZ4cgn3eHdMJlZw9qLQ1Sw0lbQ+w tpSvh/yqEdmGwXgaqSK8anEqH3HW6f5HI05/Qiyy2GKuq7nrOb300QlFJ9zAYRltf/TI pohceDA0Pv5602ImzM3J7d/tINQNqDe9qYkjUf6fkqQo6JnwbUHw/dfDN2FLIh6fU7Lj ZdnI8ziIZIVfZNpEC0qDjlIwcQqcWlV3Av2WKK+/ouwj9n1DnIVdyhJzx0iU44Bd7X6G jvow9DKDXnDdyUWxDmaHYa5qifa7C9WbLuhfp2L8izygtlbXJWWNEMHxie7OrYHuUOBX aLWA== 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 :feedback-id:references:in-reply-to:message-id:subject:cc:from:to :dkim-signature:date; bh=S5ndiOTk444R587cz6eQKsLf+C57xcupLszBquLvesM=; b=l9TCybHzx564EGAlf0Kc3Kp5V1ElbMlh5lCSQ2XMirZifpeKZvI4flSAB/lsKkjVWd 2yR/2graEUCc5y4hirKGBBaitAVb91JSOlh8iNi4WcKkb95l+Aff4+/DAJ7ZiGYrpIWd FmGFON3n9b2vHxdGMqEaUurgc+z77kk1mGpIeTw428bWlozPX4WOHH/ToePvGtzxnPvi VEN3JqxgBWvEGCPQa/78DerOOwabmz5xwQzap8Se3K3R/5LdGTlxVNkuqxLR2TV+jEkb 7KupakHX/ips8RmUN6x1s+56TzNSsvzyFplLd7c9EGZFtCDWRut1i3E0lVZMiE1nbb3W eBeA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@protonmail.com header.s=protonmail3 header.b=XjtAfMfE; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=protonmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id m2-20020a656a02000000b004fb7e7ef77fsi13504422pgu.536.2023.04.05.12.39.36; Wed, 05 Apr 2023 12:39:48 -0700 (PDT) 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=@protonmail.com header.s=protonmail3 header.b=XjtAfMfE; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=protonmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233811AbjDETi2 (ORCPT + 99 others); Wed, 5 Apr 2023 15:38:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56928 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233157AbjDETiP (ORCPT ); Wed, 5 Apr 2023 15:38:15 -0400 Received: from mail-4322.protonmail.ch (mail-4322.protonmail.ch [185.70.43.22]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E73DF7DB0 for ; Wed, 5 Apr 2023 12:37:44 -0700 (PDT) Date: Wed, 05 Apr 2023 19:36:57 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1680723429; x=1680982629; bh=S5ndiOTk444R587cz6eQKsLf+C57xcupLszBquLvesM=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=XjtAfMfEFar0bfYi2prsjdoGPaslwrXi0qgDfn7bUnCxLHi96I22hFVxcZxD6a5nj ECWMccW60rBuYaRf394obLRxMwkEG6Ct9WKp6+VPlzKB/NUWLgB3DU/pvAblffnj7G kSrMeClDp/hbBB68bJlbiIP7VqkSqC9+TS9dkXtZQzEu640wPZeYI1cMp24w0pApjL ZcTcOG0Ug1I3b0zi3YlzxeWzQR4ZGKOUhJKmCYpGpRePOe7s7zj8auBUOMstj1e3QU C0F3hyhNVlPPE5aWhIzFr/KMeigk/cL92qOdGFWBSXl6D6E4sMIpVxCN4v01F19U3w 8lDna6LXWeTrg== To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Alice Ryhl , Andreas Hindborg From: Benno Lossin Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev, Benno Lossin , Alice Ryhl , Andreas Hindborg Subject: [PATCH v6 15/15] rust: sync: add functions for initializing `UniqueArc>` Message-ID: <20230405193445.745024-16-y86-dev@protonmail.com> In-Reply-To: <20230405193445.745024-1-y86-dev@protonmail.com> References: <20230405193445.745024-1-y86-dev@protonmail.com> Feedback-ID: 40624463:user:proton MIME-Version: 1.0 X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,RCVD_IN_MSPIKE_H2, SPF_HELO_PASS,SPF_PASS autolearn=unavailable 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?1762366417889853327?= X-GMAIL-MSGID: =?utf-8?q?1762366417889853327?= Add two functions `init_with` and `pin_init_with` to `UniqueArc>` to initialize the memory of already allocated `UniqueArc`s. This is useful when you want to allocate memory check some condition inside of a context where allocation is forbidden and then conditionally initialize an object. Signed-off-by: Benno Lossin Reviewed-by: Gary Guo Reviewed-by: Alice Ryhl Reviewed-by: Andreas Hindborg --- rust/kernel/sync/arc.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) -- 2.39.2 diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs index d05caa723718..d1a79cce5e54 100644 --- a/rust/kernel/sync/arc.rs +++ b/rust/kernel/sync/arc.rs @@ -541,6 +541,30 @@ impl UniqueArc> { inner: unsafe { Arc::from_inner(inner.cast()) }, } } + + /// Initialize `self` using the given initializer. + pub fn init_with(mut self, init: impl Init) -> core::result::Result, E> { + // SAFETY: The supplied pointer is valid for initialization. + match unsafe { init.__init(self.as_mut_ptr()) } { + // SAFETY: Initialization completed successfully. + Ok(()) => Ok(unsafe { self.assume_init() }), + Err(err) => Err(err), + } + } + + /// Pin-initialize `self` using the given pin-initializer. + pub fn pin_init_with( + mut self, + init: impl PinInit, + ) -> core::result::Result>, E> { + // SAFETY: The supplied pointer is valid for initialization and we will later pin the value + // to ensure it does not move. + match unsafe { init.__pinned_init(self.as_mut_ptr()) } { + // SAFETY: Initialization completed successfully. + Ok(()) => Ok(unsafe { self.assume_init() }.into()), + Err(err) => Err(err), + } + } } impl From> for Pin> {