From patchwork Thu Nov 10 16:41:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miguel Ojeda X-Patchwork-Id: 18213 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp251770wru; Thu, 10 Nov 2022 08:44:42 -0800 (PST) X-Google-Smtp-Source: AMsMyM7N7zkIDPmAwbH5dV0poFlATFWRXu/GQOPUpceZ1dU8iSRK5sHDhhmEJiuDFOs2Ov0j0aO5 X-Received: by 2002:a05:6402:b6c:b0:461:c563:defa with SMTP id cb12-20020a0564020b6c00b00461c563defamr2597033edb.72.1668098681853; Thu, 10 Nov 2022 08:44:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668098681; cv=none; d=google.com; s=arc-20160816; b=YVUb2HEeBRa6kVcH3GuN8SRGmRXlFWVDU6ce6c3DtF5W1M8+MGX464yc8DB72ydm0c Ni/MGcQwjPFsZPCKaVKArw/aKTp2m19g8oYjt0WpT5WlihlW5aqZDiL3g/E8NRqNbCHf IYrSP8k/lu+lbeghuHKvZcKIq3tjO9qHzOBwcB50ODxBymE+BfuSs7amAKkrIfJEp/hB 4JQoVr2jUAtZZREzDVCH/8xnqG9KqEwC+cf5t4hHQDgN0SyNiYgPxKhde/5nCbhbv7j4 BHs4P19l4QKV73Xwpbwu5JNUNUWdttE3Xg66XPs4A29CsCDlVKgNOEHw6lP6Fw4m+SFY jirw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=eVD7pQO0toRvJ+1A1FlIpZOJCQUXU8SyPHlX2aiuCQU=; b=KKqQiUx+oqs2a/cbas7KzpEkCy+QRspEBByHzRIo0yc+vmRtSE5YsNewoVPJ6K84D1 GzYoOiHdMVwp4gLBVSQOgTgx8qLczSeTuAZlrs5OHwT8oqgOzYOok1XClU+LtpZi9Xyr e8CpxpHcJ38CK4nCv+7zJqWmnjvP178VftRB947cThQQ5XpB9CHq/sSmVEHfR4Eb1JKR MGXveM3jPnp0q/Q2xpvMA1351DZ1fyYsd3+lDX9K3eNKZTtxsQ8AT4giQ6qqm0lsLxO9 z2CvYkJUmb1HnEwlpICYb1Snfv8X9lLMac8/hA1Uy1lzi2yG+fibW/yOLOJbasF1u5VZ F6Kg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="P4Bx/5fM"; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f12-20020a17090660cc00b007ad855bc09csi15101096ejk.595.2022.11.10.08.44.17; Thu, 10 Nov 2022 08:44:41 -0800 (PST) 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=@kernel.org header.s=k20201202 header.b="P4Bx/5fM"; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232287AbiKJQno (ORCPT + 99 others); Thu, 10 Nov 2022 11:43:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40594 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232274AbiKJQnL (ORCPT ); Thu, 10 Nov 2022 11:43:11 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8E59145ECB; Thu, 10 Nov 2022 08:42:55 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 2B36D60E75; Thu, 10 Nov 2022 16:42:55 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 852C1C433C1; Thu, 10 Nov 2022 16:42:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1668098574; bh=oHlabre48JwaQeQ82d/rrnGlLyDZ1CNALanCqEPU3dw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=P4Bx/5fMNOjtIMrYghXNmUtzIdPI4JhjRlrRWBRrhfd5cTaivD5K2wGsqAzYlSn0+ KRyBBHNuGQnBXvI5SV2wbxTs1JHzfzT4LqQUPmwJ1rrvha15ZLz8dREEP4tTirp1bn 9JDVflUqFXxCop7nlp23gmGF6+hB07TaO3xom+/StRWZErd9wLBg/iKR8fNWcrgKb0 tpBoobNEDIQ474EIgzE8d5H6Sja0sMf/FQT0dw+aOilom7LYseVVZ1HgqFI1Szc+wN bJLKrVa59oW6cGhuPBUzneF3vxk9SVfm3rcgm8KMyR1KciDIJOvDTgWRDNdaureF/w mSBDNNB25bnIA== From: Miguel Ojeda To: Miguel Ojeda , Wedson Almeida Filho , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH v1 12/28] rust: alloc: add `RawVec::try_with_capacity_in()` constructor Date: Thu, 10 Nov 2022 17:41:24 +0100 Message-Id: <20221110164152.26136-13-ojeda@kernel.org> In-Reply-To: <20221110164152.26136-1-ojeda@kernel.org> References: <20221110164152.26136-1-ojeda@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham 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?1749128243291210816?= X-GMAIL-MSGID: =?utf-8?q?1749128243291210816?= Add the `RawVec::try_with_capacity_in()` constructor as the fallible version of `RawVec::with_capacity_in()`. The implementation follows the original. The infallible constructor is implemented in terms of the private `RawVec::allocate_in()` constructor, thus also add the private `RawVec::try_allocate_in()` constructor following the other. It will be used to implement `Vec::try_with_capacity{,_in}()` in the next patch. Signed-off-by: Miguel Ojeda Reviewed-by: Gary Guo --- rust/alloc/raw_vec.rs | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/rust/alloc/raw_vec.rs b/rust/alloc/raw_vec.rs index daf5f2da7168..c342f3843972 100644 --- a/rust/alloc/raw_vec.rs +++ b/rust/alloc/raw_vec.rs @@ -20,11 +20,11 @@ use crate::collections::TryReserveErrorKind::*; #[cfg(test)] mod tests; -#[cfg(not(no_global_oom_handling))] enum AllocInit { /// The contents of the new memory are uninitialized. Uninitialized, /// The new memory is guaranteed to be zeroed. + #[allow(dead_code)] Zeroed, } @@ -133,6 +133,14 @@ impl RawVec { Self::allocate_in(capacity, AllocInit::Uninitialized, alloc) } + /// Like `try_with_capacity`, but parameterized over the choice of + /// allocator for the returned `RawVec`. + #[allow(dead_code)] + #[inline] + pub fn try_with_capacity_in(capacity: usize, alloc: A) -> Result { + Self::try_allocate_in(capacity, AllocInit::Uninitialized, alloc) + } + /// Like `with_capacity_zeroed`, but parameterized over the choice /// of allocator for the returned `RawVec`. #[cfg(not(no_global_oom_handling))] @@ -203,6 +211,30 @@ impl RawVec { } } + fn try_allocate_in(capacity: usize, init: AllocInit, alloc: A) -> Result { + // Don't allocate here because `Drop` will not deallocate when `capacity` is 0. + if mem::size_of::() == 0 || capacity == 0 { + return Ok(Self::new_in(alloc)); + } + + let layout = Layout::array::(capacity).map_err(|_| CapacityOverflow)?; + alloc_guard(layout.size())?; + let result = match init { + AllocInit::Uninitialized => alloc.allocate(layout), + AllocInit::Zeroed => alloc.allocate_zeroed(layout), + }; + let ptr = result.map_err(|_| AllocError { layout, non_exhaustive: () })?; + + // Allocators currently return a `NonNull<[u8]>` whose length + // matches the size requested. If that ever changes, the capacity + // here should change to `ptr.len() / mem::size_of::()`. + Ok(Self { + ptr: unsafe { Unique::new_unchecked(ptr.cast().as_ptr()) }, + cap: capacity, + alloc, + }) + } + /// Reconstitutes a `RawVec` from a pointer, capacity, and allocator. /// /// # Safety