Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp934727rwb; Thu, 10 Nov 2022 09:05:43 -0800 (PST) X-Google-Smtp-Source: AMsMyM6Wci84H/jT/VsyD29sM9zRHIkFtD0fMwqo5JaHtULwUS8QWdFhT/n4y0e2PIPw9MUG27FT X-Received: by 2002:a17:90b:3a8f:b0:213:74a:845c with SMTP id om15-20020a17090b3a8f00b00213074a845cmr1551642pjb.240.1668099943626; Thu, 10 Nov 2022 09:05:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668099943; cv=none; d=google.com; s=arc-20160816; b=L83Ig4CO+zwh+ENkAED1yOmDjf2WvojMUWFl8bg+WwORMs6uUoPOM1RTQlZSQ4euCG BrWsKkQoXOTSncs8ePD8sCfMyBq28oiRRr3AI9ahVXlsQoYNWofcm1t8cfX/YVcdQTSs /SszrPoHpuh2Xuc0q1/9acuDK6bC1PHQrquz4hur0druC5+zB8Dpxz6IYzg9LiIVaYSn ImF2vEkDeIi08djzUfsXEJMNQWB5+m9BEojRdsUHUgnSm+/loJVo/NHwz6BjvKneDJ1E fJSVGr/4+klueOiiTMPBmY7RD2Yg8Xo1fk3UvDviaC+ohoWMms0RKSeHkTCsa9JF9sxv T/Dw== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a193-20020a6390ca000000b004344b581925si20030205pge.879.2022.11.10.09.05.19; Thu, 10 Nov 2022 09:05:43 -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 + 93 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=20Roy=20Baron?= 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 Content-Transfer-Encoding: 8bit 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 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 --- 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 -- 2.38.1