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 <andypython@protonmail.com>
This commit is contained in:
Anhad Singh
2026-07-15 18:26:27 +10:00
parent f45e6096d1
commit 6697881222
5 changed files with 7 additions and 53 deletions
-2
View File
@@ -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")]
{
+1 -4
View File
@@ -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<Linker>,
/// pointer to rust memory allocator structure
pub mspace: *const Mutex<Dlmalloc>,
/// 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(),
+6 -20
View File
@@ -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<sys::System>;
#[allow(clippy::declare_interior_mutable_const)]
pub const NEWALLOCATOR: Allocator = Allocator::new();
pub struct Allocator {
inner: SyncUnsafeCell<Mutex<Dlmalloc>>,
pub ptr: AtomicPtr<Mutex<Dlmalloc>>,
}
pub struct Allocator(SyncUnsafeCell<Mutex<Dlmalloc>>);
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<Dlmalloc> {
let ptr = self.ptr.load(Ordering::Acquire);
if !ptr.is_null() {
return ptr;
}
self.inner.get()
}
pub fn set(&self, mspace: *const Mutex<Dlmalloc>) {
self.ptr.store(mspace.cast_mut(), Ordering::Release);
self.0.get()
}
}
-1
View File
@@ -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::<usize>();
-26
View File
@@ -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);