From patchwork Wed Mar 29 22:33:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: y86-dev X-Patchwork-Id: 76851 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp745735vqo; Wed, 29 Mar 2023 16:13:32 -0700 (PDT) X-Google-Smtp-Source: AKy350atvBJMgNMfh7irgBNmPxhm8vno5US8qcrAT+bmytBy/1/jalQkYjxOSX0uRdc/6Uj/ZnoH X-Received: by 2002:a17:906:c791:b0:8f2:62a9:6159 with SMTP id cw17-20020a170906c79100b008f262a96159mr23277900ejb.2.1680131612012; Wed, 29 Mar 2023 16:13:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680131611; cv=none; d=google.com; s=arc-20160816; b=HABBo0ud0wg61qvveJWteg/8Yn/X7Wx6iyL9Y4XJvNo/GhCspFWoQmv/6D9VZTXClF WXAAGLQIgJyqAPhwcISNawwCBmJJRcT8TCuRewWDMA5JypRrNrO6PjrbnR2RVhhERt20 IY5bTsapmaE+1eMtn/V5QM4KDmA4UJnSy8fcKmgIDAWbsEZDU41DnSIoAZUw4mJDtTJp c9wmJ581IAjzKt8PvAf+ZRZLpGVMr9CMxfT9IBBo6w8D4mCniEnuAuWFsv9nhKoUYEEw /Xw4FjH+PFz+I0qXbZWJ3Tr3GBAmODDWJrUNHOztjZlssb7HJEDD8p9JgmClhHSvuPGR bdIw== 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=YF0KQdg01n5qzHolAR22LM31sPRu0U9EL2Fgy7BvhWY=; b=x3LUhPaIfZd2WXZeQL/86bR7Nxx5q2+XsjwVeTpP5WzC0BOOCRdXR7XdAYricLe3pw /k1+CbgFeT9gRLc8SC+vrZzrJp8dFNKJNzU8E/We66paBsX4BB7rfPJ7eb6dmVhOCH3W iEls5r4avM5W0riOSLE6lalg48kok0AITwZm1S3UpL5CbqJv5+kJomwSwYw58PQxcJHn H056T/ATGJ+RWOoufqYWsmcuO/V6msyUOUmRJVwsLos/jrgkTTsnyoKOtCgtj1rtnyIA NNubwTpMHbfSz2noomGs/RMYsaVCy5NFCx8KtCZ1NklzypWIW+ZLKrTwjdVD25PDd4v+ CcDQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@protonmail.com header.s=protonmail3 header.b=K9Mcc2lg; 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 go31-20020a1709070d9f00b0093999e387dcsi17045254ejc.84.2023.03.29.16.13.07; Wed, 29 Mar 2023 16:13:31 -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=K9Mcc2lg; 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 S231167AbjC2Wew (ORCPT + 99 others); Wed, 29 Mar 2023 18:34:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50882 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230459AbjC2Wed (ORCPT ); Wed, 29 Mar 2023 18:34:33 -0400 Received: from mail-4316.protonmail.ch (mail-4316.protonmail.ch [185.70.43.16]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0D69F6A41 for ; Wed, 29 Mar 2023 15:34:01 -0700 (PDT) Date: Wed, 29 Mar 2023 22:33:53 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1680129237; x=1680388437; bh=YF0KQdg01n5qzHolAR22LM31sPRu0U9EL2Fgy7BvhWY=; h=Date:To:From:Cc:Subject:Message-ID:Feedback-ID:From:To:Cc:Date: Subject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector; b=K9Mcc2lgyAmLApQ1FKC5qh6AYqyIwn0z69T0ipfKpv9PzSGlJ6nZCEm9X8+lCGn+5 ThpO2yyOmgEQmeLNXPSMIvWa0B7ZjDqiVcH6AK8Uvxjvo2hzHpzeL94RkO1dfQ5/oC M5teL0DzerQKN4/EXPZWJm9VyfhbrfSQjdfPBtoHWEw7Uv9+VRgxnbgMhONhPTuARe PKmjwAfroRqXsSzwqLMTr3sTESGEUwacs6jny3JlQkdgKR3IP9ifxXrRQoMj3xVpbW Tw/DVhXp51T0Kr41EfIK8addT277VjnnnH9JAFjFgfA7JAxdWD14CsB2P7edEUDRLJ vQ4MIN7biMu4w== 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 Subject: [PATCH v3 13/13] rust: sync: add functions for initializing `UniqueArc>` Message-ID: <20230329223239.138757-14-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?1761745685007484438?= X-GMAIL-MSGID: =?utf-8?q?1761745685007484438?= 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 4ed6329a5e5f..64b0e9d67025 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> {