From patchwork Sat Apr 8 12:26:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: y86-dev X-Patchwork-Id: 81185 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp845351vqo; Sat, 8 Apr 2023 05:30:49 -0700 (PDT) X-Google-Smtp-Source: AKy350amZLAwigV9MDgyfJucDxW1A/q6wxfa9cjYaZWqsMB3GijJ+40FzgC5uQixmr0BJQBXFELb X-Received: by 2002:a17:902:d506:b0:1a2:749:5f1a with SMTP id b6-20020a170902d50600b001a207495f1amr15150108plg.26.1680957048885; Sat, 08 Apr 2023 05:30:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680957048; cv=none; d=google.com; s=arc-20160816; b=znorSYGWmdQCYSSxP/bsKt+Xr4nMCJvPHyVZpiCh0XWffHdHMcC1EmJHOMrMQjKCoM ngkSP63znZEXH97eZZjP+sYmXzK8NLed3i+3Ske9iYRKvv2Zi1lYbXgf63qFQXGhcgCr PmZKCHo19Vu9QRQyTWv/ufv67E5n6fZ23uaqV7UYC+HtAZhghaVu6rs1YK5vE4t9/85X qzNsNLzASqyxbgo8ZTEzYcIx8KDMt8frpzJLzGcQEzK5yVGxGDsBqHQ31tUSsvDKWU5B X3UGShmb+xvHjEABkxxqRc/P2AGOXFrqeI2pW5aGUVpcu4nuTwZFkn2k3CQhlbFSFw63 tuxA== 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=qETQq/3DfMKxhDV1HqqEBpporvl0P5eeFvZCO9ipqTANpNMegjUrjNjRtAkqCZTvfE SRIHqgNLzm/WQY/FIi70XwPNKmF1qY7b1gdGbiVfYMqqguV4+biOl+1Je2oEGTOEPf9E Ch942eZBfg4qbH7QIsG1h3xVtNsXJ36qnM4HHG9ya043jNFdAg1zW72jAyFHr++4CCWi x/+SXo54UnZRXNO2wTpHK5oZYptkDduSq6gfpxXOQLuz9gHNN/DrlDbaPhaMNNubWPjK lGbMLrpD71Eret6gTl/91HtE80ubXKQBMoAnoRA/eOXQZMuInYFrajtXdPc11MO5SCHr FH2g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@protonmail.com header.s=protonmail3 header.b="OOrubc/L"; 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 l9-20020a170902f68900b0019c354055d0si6586425plg.304.2023.04.08.05.30.36; Sat, 08 Apr 2023 05:30: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="OOrubc/L"; 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 S231455AbjDHM1y (ORCPT + 99 others); Sat, 8 Apr 2023 08:27:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34956 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231372AbjDHM1Z (ORCPT ); Sat, 8 Apr 2023 08:27:25 -0400 Received: from mail-4322.protonmail.ch (mail-4322.protonmail.ch [185.70.43.22]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C3AF811678 for ; Sat, 8 Apr 2023 05:26:57 -0700 (PDT) Date: Sat, 08 Apr 2023 12:26:33 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1680956808; x=1681216008; 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=OOrubc/LRQIdYFRctgkFbcNDLiGSXGD0U+/c1+PBFxg5roifhVsFIopbaVNtgQRce 3bNmW7rO4LCRdZaqzSGGI/6Muy+hQ36+5DBOFxNIIRvfAEqk007iyVmWvp9u76zWga VHTHg2hU7/Lonqx0eZeNfhytztlyZXkdOV0DV0fNtq8y9rxLUp3x92Zsr1CxWdlMXm IhznCMtgGp/B8cEIvFL00NjW7t0AEhD4+ZZMjpX32+TtHqtVOxPT+9YUCYH5snDFN+ zhm74iTuqbJGUy4Y+zIfAVAcYvjyQjnzkDkubwF46ysWgAM2km640rDebmaWQlO3Y5 wu81/QyW3onBw== 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 v7 15/15] rust: sync: add functions for initializing `UniqueArc>` Message-ID: <20230408122429.1103522-16-y86-dev@protonmail.com> In-Reply-To: <20230408122429.1103522-1-y86-dev@protonmail.com> References: <20230408122429.1103522-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?1762611218869123961?= X-GMAIL-MSGID: =?utf-8?q?1762611218869123961?= 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> {