From patchwork Mon Apr 3 16:06:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: y86-dev X-Patchwork-Id: 78622 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2419545vqo; Mon, 3 Apr 2023 09:14:44 -0700 (PDT) X-Google-Smtp-Source: AK7set+z9GjRRaHBmP/brJ10oiBZgIXLXLKabeRaJ68bNR7/9tMf8GyldzGty0I98qXKYhG9PeLP X-Received: by 2002:a05:6a20:6d04:b0:dc:76bb:b09b with SMTP id fv4-20020a056a206d0400b000dc76bbb09bmr27653842pzb.5.1680538483987; Mon, 03 Apr 2023 09:14:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680538483; cv=none; d=google.com; s=arc-20160816; b=E89pCPw/A+AFm4K/TBl/4l3iMlyk2ERzj45YMUv+ueT9UZaiokAWhy/57zY3ingsup W8X13wK1mbEL9pjEGg9f4xmaBHc/9XuxaS4c0BrtudGWBK1peFLA4yDhWii9wifJoOyD 9/7nZoxHbRa/JPYSfx5pq8UehcpCd3JZZNw6qdmmtsvXEo6u/SwDU26lt7LAllh6y63F fnch9YmJc4Qtv1L+UzJJbBP8LbYVYks4JYdEyD+l1X1CVBNqqfbnhvA8CDxofWMUWKOo Vm/rsfbPXCpzVPa675kwitFFzpUg03F2OngTW5W3MdUpjJaOejLZR/pW0s7rySf+N8Ce oNWQ== 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=q5BxB9Mpv8HDjKMIk6dHKmcyxTPvBq07fS223CBeCKu75tudyTDTPpufdsjWPHbdDF iX8D99QQuvWdT2XRWbp+s+AiT4k+tX0f3Q6+K6h1qvtATe8O5Ox06L9uvfWYBEoVWRzr lBOiIAi//cEQBxdXPZNLgOEYJUc27ou2ULh4PGpQWM9qIoFZL6OTRRHim7W9CRwCIA/c hI2sRaE6qEuuQyBjothi0Pculx6YI9FzPbXdr9/HDJRJlU41o7ZWa0457pzwMhoM9l6z Xx8FaZX0RhJvIpb2x2CoEXL1aVBTEo8WvPIE5tsxPt03GN/ZIIvjlN2jpvsjoRG3XLMw rFNA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@protonmail.com header.s=protonmail3 header.b=D1B+Y+om; 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 a27-20020aa795bb000000b00627ed0fa77csi8323705pfk.188.2023.04.03.09.14.31; Mon, 03 Apr 2023 09:14:43 -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=D1B+Y+om; 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 S233030AbjDCQGv (ORCPT + 99 others); Mon, 3 Apr 2023 12:06:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35306 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233226AbjDCQGj (ORCPT ); Mon, 3 Apr 2023 12:06:39 -0400 Received: from mail-4316.protonmail.ch (mail-4316.protonmail.ch [185.70.43.16]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DC2D53AB5; Mon, 3 Apr 2023 09:06:13 -0700 (PDT) Date: Mon, 03 Apr 2023 16:06:02 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1680537965; x=1680797165; 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=D1B+Y+omvyh0YiZ7aLHE5yXMoYPPexH0Sm74LEUhuLk7et6jQjwZBYYklUxQcKwD+ opxmhpc8HH5QM3BlB9ofIpTyuqO5g69IsjlfWjFVxLPeQ+8ZQrWf8f/OPf1m5ZNOrS QYcXGHkIsEKLWqaFhz/dK/onb0LRWEyQv5IDrMohCWkSuSKNs3RpoyiFaPuHnwqIMC vQuKijouFY8DAfTDlitS6yMOlKpxwBXzaiwPVONcqU9OqrdovPzsdZxCXOp7z5hdYf uxMFw3djYFtaAM51WSyvs+fvbz6ZudDvB11K4QsC8k02R2YqZw63PFycnh0aLg2sI5 0nC2tXzjreQUA== To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Alice Ryhl 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 v5 15/15] rust: sync: add functions for initializing `UniqueArc>` Message-ID: <20230403160511.174894-8-y86-dev@protonmail.com> In-Reply-To: <20230403154422.168633-1-y86-dev@protonmail.com> References: <20230403154422.168633-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?1762172321303332776?= X-GMAIL-MSGID: =?utf-8?q?1762172321303332776?= 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> {