Skip to content

Commit d784363

Browse files
committed
remove unnecessary allocator lock on VEXos
1 parent e0ec89e commit d784363

File tree

1 file changed

+8
-32
lines changed

1 file changed

+8
-32
lines changed

library/std/src/sys/alloc/vexos.rs

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -62,57 +62,33 @@ unsafe impl dlmalloc::Allocator for Vexos {
6262
unsafe impl GlobalAlloc for System {
6363
#[inline]
6464
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
65-
// SAFETY: DLMALLOC access is guaranteed to be safe because the lock gives us unique and non-reentrant access.
65+
// SAFETY: DLMALLOC access is guaranteed to be safe because we are a single-threaded target, which
66+
// guarantees unique and non-reentrant access to the allocator.
6667
// Calling malloc() is safe because preconditions on this function match the trait method preconditions.
67-
let _lock = lock::lock();
6868
unsafe { DLMALLOC.malloc(layout.size(), layout.align()) }
6969
}
7070

7171
#[inline]
7272
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
73-
// SAFETY: DLMALLOC access is guaranteed to be safe because the lock gives us unique and non-reentrant access.
73+
// SAFETY: DLMALLOC access is guaranteed to be safe because we are a single-threaded target, which
74+
// guarantees unique and non-reentrant access to the allocator.
7475
// Calling calloc() is safe because preconditions on this function match the trait method preconditions.
75-
let _lock = lock::lock();
7676
unsafe { DLMALLOC.calloc(layout.size(), layout.align()) }
7777
}
7878

7979
#[inline]
8080
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
81-
// SAFETY: DLMALLOC access is guaranteed to be safe because the lock gives us unique and non-reentrant access.
81+
// SAFETY: DLMALLOC access is guaranteed to be safe because we are a single-threaded target, which
82+
// guarantees unique and non-reentrant access to the allocator.
8283
// Calling free() is safe because preconditions on this function match the trait method preconditions.
83-
let _lock = lock::lock();
8484
unsafe { DLMALLOC.free(ptr, layout.size(), layout.align()) }
8585
}
8686

8787
#[inline]
8888
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
89-
// SAFETY: DLMALLOC access is guaranteed to be safe because the lock gives us unique and non-reentrant access.
89+
// SAFETY: DLMALLOC access is guaranteed to be safe because we are a single-threaded target, which
90+
// guarantees unique and non-reentrant access to the allocator.
9091
// Calling realloc() is safe because preconditions on this function match the trait method preconditions.
91-
let _lock = lock::lock();
9292
unsafe { DLMALLOC.realloc(ptr, layout.size(), layout.align(), new_size) }
9393
}
9494
}
95-
96-
mod lock {
97-
use crate::sync::atomic::Ordering::{Acquire, Release};
98-
use crate::sync::atomic::{Atomic, AtomicI32};
99-
100-
static LOCKED: Atomic<i32> = AtomicI32::new(0);
101-
102-
pub struct DropLock;
103-
104-
pub fn lock() -> DropLock {
105-
loop {
106-
if LOCKED.swap(1, Acquire) == 0 {
107-
return DropLock;
108-
}
109-
}
110-
}
111-
112-
impl Drop for DropLock {
113-
fn drop(&mut self) {
114-
let r = LOCKED.swap(0, Release);
115-
debug_assert_eq!(r, 1);
116-
}
117-
}
118-
}

0 commit comments

Comments
 (0)