From 66978812229d739ae33c697c9f676e5c96c1d2b1 Mon Sep 17 00:00:00 2001 From: Anhad Singh Date: Wed, 15 Jul 2026 18:26:27 +1000 Subject: [PATCH] feat(alloc): cleanup * Storing a pointer to the allocator in TCB is no longer necessary. * `AtomicPtr` to store the allocator is no longer required as the allocator is not swapped on init if the program was dynamically linked. Signed-off-by: Anhad Singh --- src/ld_so/linker.rs | 2 -- src/ld_so/tcb.rs | 5 +---- src/platform/allocator/mod.rs | 26 ++++++-------------------- src/pthread/mod.rs | 1 - src/start.rs | 26 -------------------------- 5 files changed, 7 insertions(+), 53 deletions(-) diff --git a/src/ld_so/linker.rs b/src/ld_so/linker.rs index dd3a18505a..ab9727451c 100644 --- a/src/ld_so/linker.rs +++ b/src/ld_so/linker.rs @@ -14,7 +14,6 @@ use object::{ use core::ptr; use crate::{ - ALLOCATOR, c_str::{CStr, CString}, error::Errno, header::{ @@ -700,7 +699,6 @@ impl Linker { #[cfg(target_os = "redox")] Some(thr_fd), ); - tcb.mspace = ALLOCATOR.get(); #[cfg(target_os = "redox")] { diff --git a/src/ld_so/tcb.rs b/src/ld_so/tcb.rs index 73c37709e0..7bffec76d0 100644 --- a/src/ld_so/tcb.rs +++ b/src/ld_so/tcb.rs @@ -11,7 +11,7 @@ use generic_rt::GenericTcb; use crate::{ header::sys_mman, ld_so::linker::Linker, - platform::{Dlmalloc, Pal, Sys}, + platform::{Pal, Sys}, pthread::{OsTid, Pthread}, sync::{mutex::Mutex, waitval::Waitval}, }; @@ -56,8 +56,6 @@ pub struct Tcb { pub num_copied_masters: usize, /// Pointer to dynamic linker pub linker_ptr: *const Mutex, - /// pointer to rust memory allocator structure - pub mspace: *const Mutex, /// Underlying pthread_t struct, pthread_self() returns &self.pthread pub pthread: Pthread, @@ -98,7 +96,6 @@ impl Tcb { masters_len: 0, num_copied_masters: 0, linker_ptr: ptr::null(), - mspace: ptr::null(), pthread: Pthread { waitval: Waitval::new(), flags: Default::default(), diff --git a/src/platform/allocator/mod.rs b/src/platform/allocator/mod.rs index acdb643787..e10ddd0d94 100644 --- a/src/platform/allocator/mod.rs +++ b/src/platform/allocator/mod.rs @@ -3,8 +3,7 @@ use core::{ cell::SyncUnsafeCell, cmp, mem::align_of, - ptr::{self, copy_nonoverlapping, write_bytes}, - sync::atomic::{AtomicPtr, Ordering}, + ptr::{copy_nonoverlapping, write_bytes}, }; mod sys; @@ -17,31 +16,18 @@ pub type Dlmalloc = DlmallocCApi; #[allow(clippy::declare_interior_mutable_const)] pub const NEWALLOCATOR: Allocator = Allocator::new(); -pub struct Allocator { - inner: SyncUnsafeCell>, - pub ptr: AtomicPtr>, -} +pub struct Allocator(SyncUnsafeCell>); impl Allocator { #[allow(clippy::new_without_default)] pub const fn new() -> Self { - Allocator { - inner: SyncUnsafeCell::new(Mutex::new(Dlmalloc::new(sys::System::new()))), - ptr: AtomicPtr::new(ptr::null_mut()), - } + Self(SyncUnsafeCell::new(Mutex::new(Dlmalloc::new( + sys::System::new(), + )))) } pub fn get(&self) -> *const Mutex { - let ptr = self.ptr.load(Ordering::Acquire); - if !ptr.is_null() { - return ptr; - } - - self.inner.get() - } - - pub fn set(&self, mspace: *const Mutex) { - self.ptr.store(mspace.cast_mut(), Ordering::Release); + self.0.get() } } diff --git a/src/pthread/mod.rs b/src/pthread/mod.rs index f0a016b5e4..81f2389a31 100644 --- a/src/pthread/mod.rs +++ b/src/pthread/mod.rs @@ -172,7 +172,6 @@ pub(crate) unsafe fn create( new_tcb.masters_ptr = current_tcb.masters_ptr; new_tcb.masters_len = current_tcb.masters_len; new_tcb.linker_ptr = current_tcb.linker_ptr; - new_tcb.mspace = current_tcb.mspace; let stack_end = unsafe { stack_base.add(stack_size) }; let mut stack = stack_end.cast::(); diff --git a/src/start.rs b/src/start.rs index ca07ee32b8..4d82878837 100644 --- a/src/start.rs +++ b/src/start.rs @@ -4,7 +4,6 @@ use alloc::vec::Vec; use core::{intrinsics, ptr}; use crate::{ - ALLOCATOR, header::{libgen, stdio, stdlib}, ld_so::{self}, platform::{self, Pal, Sys, get_auxvs, types::*}, @@ -88,21 +87,6 @@ static mut INIT_COMPLETE: bool = false; #[unsafe(no_mangle)] static mut __relibc_init_environ: *mut *mut c_char = ptr::null_mut(); -fn alloc_init() { - unsafe { - if INIT_COMPLETE { - return; - } - } - unsafe { - if let Some(tcb) = ld_so::tcb::Tcb::current() - && !tcb.mspace.is_null() - { - ALLOCATOR.set(tcb.mspace); - } - } -} - extern "C" fn init_array() { // The thing is that we cannot guarantee if // init_array runs first or if relibc_start runs first @@ -115,7 +99,6 @@ extern "C" fn init_array() { } } - alloc_init(); io_init(); unsafe { @@ -202,16 +185,7 @@ pub unsafe extern "C" fn relibc_start_v1( redox_rt::TLS_ACTIVATED.store(true, core::sync::atomic::Ordering::Relaxed); } - // Set up the right allocator... - // if any memory rust based memory allocation happen before this step .. we are doomed. - alloc_init(); - let is_dynamically_linked = if let Some(tcb) = unsafe { ld_so::tcb::Tcb::current() } { - // Update TCB mspace - if tcb.mspace.is_null() { - tcb.mspace = ALLOCATOR.get(); - } - #[cfg(target_os = "redox")] redox_rt::signal::setup_sighandler(&tcb.os_specific, true);