From patchwork Fri Mar 31 21:54:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: y86-dev X-Patchwork-Id: 78004 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp870752vqo; Fri, 31 Mar 2023 15:12:16 -0700 (PDT) X-Google-Smtp-Source: AKy350atYHKFfvMuUtV0LQ8vvc9SP9qFd09qF2ahN8QQ8BuQv6/gKvHwRC07UaVBBhGZ/9hShY8O X-Received: by 2002:a17:907:c015:b0:947:d9b1:89a3 with SMTP id ss21-20020a170907c01500b00947d9b189a3mr1885285ejc.27.1680300736688; Fri, 31 Mar 2023 15:12:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680300736; cv=none; d=google.com; s=arc-20160816; b=G3wMJuShbRuwRyT5DA3wj84ZF6On0JGV0d2Dq/O7Z4tTDlH4LTW0nT9ulbH+nTYZnr nDwj6k88ldwDbJwyOP6uq6hcS6BuB1zizvY1k6ucA0hZcPww4lqFJ0asKafOl1bK1QOe fnlX0nhpncOBuu3Ef+B6Ak63AztgcnqSyRUss66qF94r+QqbmBEJTxE8j2sKXgWJUDD6 kxoXG/OjHHa5S++HBaICaD7m2ZDJ2KbZcD/aDv+upe18fGUTlmV+E3yvQvoy1yH/HdKQ G8b0vKXjybavnsC1RzjmIurAzmYOMMkFSw0/32BbpB0H/sMkOMA4KcmouT3+CKgQ1GLx SuIQ== 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:message-id:subject:cc:from:to:dkim-signature:date; bh=KCurfW5Y+j7R5fMbjmPJwPW9L2YVYu9q3k1eOHB2SiI=; b=TKsh8TQS1KxKBEHrM8ylq18+jqvHzV7wp76T6n3Id1HEAZIY9g+brZYybKzzPbzEZ5 G2wESmVtjs5G1aw9O4yD3keTj2GkjTa7rkQUWgXTORftKEBw8FZHpsYUjewIplRPAdMF NcaeWhRtTZdaLOmNPkFfO5oEa3uvuF7e6Y3qFEPSit9sJ6RtDVxzvVoUq1sGIg5zCnAc Oco7/telQ7GyEewNGtE8SBzEDIglREGCkM8Z8Qn+E3WeV/fnnzHv5rimN++g3z0hQBGU eHf2gL5H7DO/Ah/ywS3dnD/XBavhXEmnMyTHwEzviJHHwSq7NiNMt1rgEJa3NuNl2Vua aFaw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@protonmail.com header.s=protonmail3 header.b=wxkdvrmX; 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 jl14-20020a17090775ce00b009448ef3cfb8si2786379ejc.614.2023.03.31.15.11.52; Fri, 31 Mar 2023 15:12:16 -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=wxkdvrmX; 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 S233304AbjCaV43 (ORCPT + 99 others); Fri, 31 Mar 2023 17:56:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46988 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233316AbjCaV4S (ORCPT ); Fri, 31 Mar 2023 17:56:18 -0400 Received: from mail-40134.protonmail.ch (mail-40134.protonmail.ch [185.70.40.134]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 810472441C; Fri, 31 Mar 2023 14:55:48 -0700 (PDT) Date: Fri, 31 Mar 2023 21:54:52 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1680299697; x=1680558897; bh=KCurfW5Y+j7R5fMbjmPJwPW9L2YVYu9q3k1eOHB2SiI=; h=Date:To:From:Cc:Subject:Message-ID:Feedback-ID:From:To:Cc:Date: Subject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector; b=wxkdvrmXC/k4XiTyxLpBUK+Cqntvv+0ZjbikRImKsmvixU+eA6ryqIS9apKPmgana Ez+zL/wUyzlotdDxsXXe6U1I1SzleZvchId1yRlFa84YWxfolCXFFxyrXvHrvYdZuV Ox28mr33BMaizAA1dwxWFip6LQBuGhlFUpJmfNBNBJzlGLyc9DSlDLpJPWDp9Yju1R KZveNXe7wfAE1ukEJ8/KtRn0IxqwP7zGAaCqHhUQmPj13oI5t4hJzmhXsTUmepU3FP A0gyw27n11yAPgKQrz7gWFlfUI8QfSfhDAHm3imB5edueXzruT809K3erxWCPhlvjb iLNbQbj//iRQw== To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Alice Ryhl From: y86-dev@protonmail.com Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev, Benno Lossin , Alice Ryhl , Andreas Hindborg Subject: [PATCH v4 15/15] rust: sync: add functions for initializing `UniqueArc>` Message-ID: <20230331215053.585759-16-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?1761923025355946095?= X-GMAIL-MSGID: =?utf-8?q?1761923025355946095?= From: Benno Lossin 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 5f64affd8f33..fddb7a2b1890 100644 --- a/rust/kernel/sync/arc.rs +++ b/rust/kernel/sync/arc.rs @@ -540,6 +540,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> {