From patchwork Fri Dec 2 16:14:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miguel Ojeda X-Patchwork-Id: 28970 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp947836wrr; Fri, 2 Dec 2022 08:18:38 -0800 (PST) X-Google-Smtp-Source: AA0mqf4zzIWjNpZvYJpff0Du1542I9ziRqDUX/yNQMPEf4+c1kKeZbYfagqwITH4TA3Z7ITXV5gK X-Received: by 2002:a17:906:158f:b0:78d:45e9:97be with SMTP id k15-20020a170906158f00b0078d45e997bemr58446000ejd.565.1669997918193; Fri, 02 Dec 2022 08:18:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669997918; cv=none; d=google.com; s=arc-20160816; b=tdOCJWOmlRcB1nxTQiuWcTjKYS4qjJvmQparGdalxIT3mdufgbenOWUn70F+4D4BAQ jvA8IHyDV+X7LSA7UpH631nRZguMYgsTXBRie5ySokIr0Rmf+aE9+rTrKgLkz/+AGKUL XK0hFhOyLCqrNz7fQS/cy+U41PFcZM5cwML1SxNg4wrfqEijcPbtumfM+n+GJVu/lZbl /4Y9qvOMl5enjJgvrUdE/gfl2sgLlgWP2u39kZM5TO6ytOT1PWlWH6AfO52XuWaLOecw ShHO3Ak4mtFyUcATTipESxG7hBIHZGhYg41pvbgyDUsokIgXcp9IfXb6h76bb01LKLfX 2yzA== 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=v9FYS2cltDjwMDceAGbVK0nzIJuGiSPpmfouroMSSaE=; b=kMOLj3apshhOHcDQQk+dfIYilH7E8YnjGQFtY7jHkYciircFhM5KF+nyf1H6Mllp5G movk56nocO9hYFfIDNvs+cRZyRy+wuQvU2al39uEcOt/dkgxc2Zh63d9lnNKad5/ywKo OCpehxNXZCAYbDymANEoKZ5yp3DQop3bhst9Z/Vxcoi6BQW6BAFwth4FeG36TY+tiGD/ Or6+lO4fx2PSHowCjyV1ijZJnIn9tDeXp4EnH3CINj+/4+S/QOtltNtkibztZM7h3dwo wGngMhaqiQsnjX58sfGmbsgwXjD/ALdo/pgAXcUQ8dUhUYKDEsuB+bn+B+GUiZhegpNp 4IIg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="CiJAL/O5"; 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 z11-20020a056402274b00b004673015ab88si6610018edd.19.2022.12.02.08.18.13; Fri, 02 Dec 2022 08:18:38 -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="CiJAL/O5"; 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 S233920AbiLBQRL (ORCPT + 99 others); Fri, 2 Dec 2022 11:17:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53836 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233763AbiLBQQa (ORCPT ); Fri, 2 Dec 2022 11:16:30 -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 7CF9DE0741; Fri, 2 Dec 2022 08:16:04 -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 1B1A06232F; Fri, 2 Dec 2022 16:16:04 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7045DC433D6; Fri, 2 Dec 2022 16:16:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1669997763; bh=BWxOvrWLIfwcfu7Jhuioqwg9jJ3x2xImME+v7sDyUfw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CiJAL/O5nYOueC3kMzcu/WJ9tWK+syLi//zvidOY+ZbCvng0QEwaqhkIBAOkYZ6tF YL7NSmDYhSNwX7NKIdzoWnnnLOayWhLxk5LTnuaailbdpw10e8uOH78PhaK2smqIS4 5O6UYG3YS36WY07VHR5UNR+/9chYtNBmz1vcrQ1X5fnQHX+efxhXGZaXwp0Xw6Rt4h Axq2gozL7rgL2B48BeL/4z44odbI4wGlHRgvS34zlMaD6xYL98ph7cOV/awkD30FfK C828xpj1c+5fimCjg/KdsMZuiszP93w3mdSfsQFlXETPlLbcQiLxJ8sOJVbgPziFlH aFWxShjqHeZKQ== From: ojeda@kernel.org 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 v2 12/28] rust: alloc: add `RawVec::try_with_capacity_in()` constructor Date: Fri, 2 Dec 2022 17:14:43 +0100 Message-Id: <20221202161502.385525-13-ojeda@kernel.org> In-Reply-To: <20221202161502.385525-1-ojeda@kernel.org> References: <20221202161502.385525-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?1751119736686657200?= X-GMAIL-MSGID: =?utf-8?q?1751119736686657200?= From: Miguel Ojeda 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. Reviewed-by: Gary Guo 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