From patchwork Sat Jun 24 09:25:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Benno Lossin X-Patchwork-Id: 112434 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp6292982vqr; Sat, 24 Jun 2023 02:31:53 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5DAHVmeuiLG0SHJcgylEhxG7Mclsg1URcQz7+DUfo+LwXP4v3Vfqx2AI/5YLMJ/SogEJOp X-Received: by 2002:a05:6a20:8f0b:b0:107:1805:feea with SMTP id b11-20020a056a208f0b00b001071805feeamr31678385pzk.37.1687599113438; Sat, 24 Jun 2023 02:31:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687599113; cv=none; d=google.com; s=arc-20160816; b=iKd0e0CRfx5FUwvKT4THedt84M94l63jkh9nozvdMqljsn+sTJWInMuOxB/gSI5Q1V Yj2UFsQ0khc3HGCrd5D/TouSQ75GFScUautcXE4Pe6a8YfH2DBvVcxyW9BGE4HPyBBPJ qHHVKhyo1XdLrLvuEqOvXhw9zeKAURhW//3YfTeRJNsxpHQPndWHsB8hhW3nAOSmsPj9 W64PE3Bp9JnLCeOWvzvj/FgotZbHS4thPMC5bOexoGfkN+7Wg8+6MEdnUkNwd56SZa1O NzeRnQJF+q7Uu8Qxeo1D2Phikv4PIM/O1UKidS8aBsY4dLsyBGxx1UtebWPLwuzAFehj 2dCQ== 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=gLSKxHqYjJDu1EPRJrKPDpLlPpYDJtCGZbauNM8TqoI=; fh=Y3hGqDvLhFUfCxW08frpp1lth+cS6Xq25ZUxZll0Zv0=; b=eXEg9IQzl1oanOwbFXYwkM/dyjS/LrrbHDsynedhsg1gtkl8mGj2T4DzVZ7TEtYF0A 0nQvdNrQp4ke2tkl692oE7xnLhFSvbWs69WV40L2bCxDTz5difK8Slbci8UsS9bEwzWD 9PGX8rycYYHWMi/TJ8SqGHVw1U5I2oHQnV6NEey3Xg7UPNs6mdQ3wjwnD1WB+FGQk551 PbPHT6LLFaMuop3fOE25rwvU8JmRk4rrwbTpryohbVPJGtrqPDw1Ksw7Xm7lWd6mKa6+ dmVur+oObWRXYtt8zMrtPrUgc75G6oUTzojM2qssZfMsqtRMM9ELsutEkoepArjWKfCp hSUg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=PzF6EJxG; 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=proton.me Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id fi4-20020a056a00398400b006665df4f1f0si984100pfb.136.2023.06.24.02.31.40; Sat, 24 Jun 2023 02:31:53 -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=@proton.me header.s=protonmail header.b=PzF6EJxG; 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=proton.me Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233092AbjFXJ0S (ORCPT + 99 others); Sat, 24 Jun 2023 05:26:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52750 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233090AbjFXJ0H (ORCPT ); Sat, 24 Jun 2023 05:26:07 -0400 Received: from mail-40133.protonmail.ch (mail-40133.protonmail.ch [185.70.40.133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7286A2726 for ; Sat, 24 Jun 2023 02:25:41 -0700 (PDT) Date: Sat, 24 Jun 2023 09:25:34 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1687598739; x=1687857939; bh=gLSKxHqYjJDu1EPRJrKPDpLlPpYDJtCGZbauNM8TqoI=; 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=PzF6EJxG1JQsX9+/c2QvRBGF5Cbkpnue8an6zQc9ZiqUYDUbqRR6uVPFTdYoxa/Bm XHmJxP2+dwnwyHf3xCVMBvycPXyKDfrpaN8Bj5tmkrE7wWjqNdcgT0DqgpmPB6vDEW OGo9gZ1pYvS64q4AwSnH2I155EI8c4TqhNeZjHkl+FjUKDJtx/S5blWgJzPDYobl3p kcKJ6g3/OTVZOVXUBEFG84CoFPxfj3wPjJYttS6dDdVYjlT29F0Ggd9yxgSjmoIx7G s37c8geiKJZtxmImSKsQ7OZ8+PKwY5V37EkLz4u2s8DNqh6gwCSt8tlyZ/9iSIZknp dohrYK9kH30KA== To: Miguel Ojeda , Wedson Almeida Filho , Alex Gaynor From: Benno Lossin Cc: Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Andreas Hindborg , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev, Asahi Lina Subject: [PATCH 6/7] rust: init: Add functions to create array initializers Message-ID: <20230624092330.157338-6-benno.lossin@proton.me> In-Reply-To: <20230624092330.157338-1-benno.lossin@proton.me> References: <20230624092330.157338-1-benno.lossin@proton.me> Feedback-ID: 71780778:user:proton MIME-Version: 1.0 X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=no 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?1769575927841982358?= X-GMAIL-MSGID: =?utf-8?q?1769575927841982358?= Add two functions `pin_init_array_from_fn` and `init_array_from_fn` that take a function that generates initializers for `T` from usize, the added functions then return an initializer for `[T; N]` where every element is initialized by an element returned from the generator function. Suggested-by: Asahi Lina Signed-off-by: Benno Lossin Reviewed-by: Björn Roy Baron --- rust/kernel/init.rs | 90 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) -- 2.41.0 diff --git a/rust/kernel/init.rs b/rust/kernel/init.rs index 44bc3e77419a..c9ea4bf71987 100644 --- a/rust/kernel/init.rs +++ b/rust/kernel/init.rs @@ -867,6 +867,96 @@ pub fn uninit() -> impl Init, E> { unsafe { init_from_closure(|_| Ok(())) } } +/// Initializes an array by initializing each element via the provided initializer. +/// +/// # Examples +/// +/// ```rust +/// let array: Box<[usize; 1000_000_000]>= Box::init(init_array_from_fn(|i| i)).unwrap(); +/// println!("{array:?}"); +/// ``` +pub fn init_array_from_fn( + mut make_init: impl FnMut(usize) -> I, +) -> impl Init<[T; N], E> +where + I: Init, +{ + let init = move |slot: *mut [T; N]| { + let slot = slot.cast::(); + for i in 0..N { + let init = make_init(i); + // SAFETY: since 0 <= `i` < N, it is still in bounds of `[T; N]`. + let ptr = unsafe { slot.add(i) }; + // SAFETY: The pointer is derived from `slot` and thus satisfies the `__init` + // requirements. + match unsafe { init.__init(ptr) } { + Ok(()) => {} + Err(e) => { + // We now free every element that has been initialized before: + for j in 0..i { + let ptr = unsafe { slot.add(j) }; + // SAFETY: The value was initialized in a previous iteration of the loop + // and since we return `Err` below, the caller will consider the memory at + // `slot` as uninitialized. + unsafe { ptr::drop_in_place(ptr) }; + } + return Err(e); + } + } + } + Ok(()) + }; + // SAFETY: The initializer above initializes every element of the array. On failure it drops + // any initialized elements and returns `Err`. + unsafe { init_from_closure(init) } +} + +/// Initializes an array by initializing each element via the provided initializer. +/// +/// # Examples +/// +/// ```rust +/// let array: Arc<[Mutex; 1000_000_000]>= +/// Arc::pin_init(init_array_from_fn(|i| new_mutex!(i))).unwrap(); +/// println!("{array:?}"); +/// ``` +pub fn pin_init_array_from_fn( + mut make_init: impl FnMut(usize) -> I, +) -> impl PinInit<[T; N], E> +where + I: PinInit, +{ + let init = move |slot: *mut [T; N]| { + let slot = slot.cast::(); + for i in 0..N { + let init = make_init(i); + // SAFETY: since 0 <= `i` < N, it is still in bounds of `[T; N]`. + let ptr = unsafe { slot.add(i) }; + // SAFETY: The pointer is derived from `slot` and thus satisfies the `__pinned_init` + // requirements. + match unsafe { init.__pinned_init(ptr) } { + Ok(()) => {} + Err(e) => { + // We now have to free every element that has been initialized before, since we + // have to abide by the drop guarantee. + for j in 0..i { + let ptr = unsafe { slot.add(j) }; + // SAFETY: The value was initialized in a previous iteration of the loop + // and since we return `Err` below, the caller will consider the memory at + // `slot` as uninitialized. + unsafe { ptr::drop_in_place(ptr) }; + } + return Err(e); + } + } + } + Ok(()) + }; + // SAFETY: The initializer above initializes every element of the array. On failure it drops + // any initialized elements and returns `Err`. + unsafe { pin_init_from_closure(init) } +} + // SAFETY: Every type can be initialized by-value. unsafe impl Init for T { unsafe fn __init(self, slot: *mut T) -> Result<(), E> {