From 65273ea898fcdd2328ef9d5b4fd56f0513c0a79d Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sun, 26 Oct 2025 18:58:07 +0100 Subject: [PATCH] Replace all Arc::try_new with Arc::new The global allocator panics when out of memory rather than returning an errors. In addition there are plenty of other places where we don't handle allocation failure anyway. At some point in the future we should systematically handle out of memory conditions, but until then let's not pretend we do and get rid of the usage of the unstable allocator_api feature that is unlikely to get stabilized any time soon. --- src/context/memory.rs | 5 ++--- src/context/mod.rs | 6 ++---- src/main.rs | 1 - src/ptrace.rs | 5 ++--- src/scheme/proc.rs | 3 +-- 5 files changed, 7 insertions(+), 13 deletions(-) diff --git a/src/context/memory.rs b/src/context/memory.rs index 7c24add14f..ecc8ec5f5e 100644 --- a/src/context/memory.rs +++ b/src/context/memory.rs @@ -87,11 +87,10 @@ pub struct AddrSpaceWrapper { } impl AddrSpaceWrapper { pub fn new() -> Result> { - Arc::try_new(Self { + Ok(Arc::new(Self { inner: RwLock::new(AddrSpace::new()?), tlb_ack: AtomicU32::new(0), - }) - .map_err(|_| Error::new(ENOMEM)) + })) } pub fn acquire_read(&self) -> RwLockReadGuard<'_, AddrSpace> { let my_percpu = PercpuBlock::current(); diff --git a/src/context/mod.rs b/src/context/mod.rs index 9d3242cde1..2990d50e08 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -4,7 +4,6 @@ use alloc::{collections::BTreeSet, sync::Arc}; use core::num::NonZeroUsize; -use syscall::ENOMEM; use crate::{ context::memory::AddrSpaceWrapper, @@ -15,7 +14,7 @@ use crate::{ ArcRwLockWriteGuard, CleanLockToken, LockToken, RwLock, RwLockReadGuard, RwLockWriteGuard, L0, L1, L2, }, - syscall::error::{Error, Result}, + syscall::error::Result, }; use self::context::Kstack; @@ -159,8 +158,7 @@ pub fn spawn( ) -> Result> { let stack = Kstack::new()?; - let context_lock = Arc::try_new(ContextLock::new(Context::new(owner_proc_id)?)) - .map_err(|_| Error::new(ENOMEM))?; + let context_lock = Arc::new(ContextLock::new(Context::new(owner_proc_id)?)); contexts_mut(token.token()).insert(ContextRef(Arc::clone(&context_lock))); diff --git a/src/main.rs b/src/main.rs index 54c3b066a3..c2e4adcb30 100644 --- a/src/main.rs +++ b/src/main.rs @@ -40,7 +40,6 @@ // Ensure that all must_use results are used #![deny(unused_must_use)] #![warn(static_mut_refs)] // FIXME deny once all occurences are fixed -#![feature(allocator_api)] #![feature(if_let_guard)] #![feature(int_roundings)] #![feature(iter_next_chunk)] diff --git a/src/ptrace.rs b/src/ptrace.rs index 187b2b374f..755448ed27 100644 --- a/src/ptrace.rs +++ b/src/ptrace.rs @@ -87,8 +87,8 @@ impl Session { .as_ref()? .upgrade() } - pub fn try_new(file_id: usize) -> Result> { - Arc::try_new(Session { + pub fn new(file_id: usize) -> Arc { + Arc::new(Session { data: Mutex::new(SessionData { breakpoint: None, events: VecDeque::new(), @@ -97,7 +97,6 @@ impl Session { tracee: WaitCondition::new(), tracer: WaitCondition::new(), }) - .map_err(|_| Error::new(ENOMEM)) } } diff --git a/src/scheme/proc.rs b/src/scheme/proc.rs index 21ba16fe09..a9076e53a0 100644 --- a/src/scheme/proc.rs +++ b/src/scheme/proc.rs @@ -729,8 +729,7 @@ impl KernelScheme for ProcScheme { } let filetable = filetable.upgrade().ok_or(Error::new(EOWNERDEAD))?; - let new_filetable = Arc::try_new(spin::RwLock::new(filetable.read().clone())) - .map_err(|_| Error::new(ENOMEM))?; + let new_filetable = Arc::new(spin::RwLock::new(filetable.read().clone())); handle( Handle {