Skip to content

Commit de87ae7

Browse files
author
chansuke
committed
Add documents for DLMALLOC
1 parent eed4510 commit de87ae7

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

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

+8-4
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,32 @@ static mut DLMALLOC: dlmalloc::Dlmalloc = dlmalloc::DLMALLOC_INIT;
2424
unsafe impl GlobalAlloc for System {
2525
#[inline]
2626
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
27-
// SAFETY: DLMALLOC.malloc() is guranteed to be safe since lock::lock() aqcuire a globl lock
27+
// SAFETY: DLMALLOC access is guranteed to be safe because the lock gives us unique and non-reentrant access.
28+
// Calling malloc() is safe because preconditions on this function match the trait method preconditions.
2829
let _lock = lock::lock();
2930
unsafe { DLMALLOC.malloc(layout.size(), layout.align()) }
3031
}
3132

3233
#[inline]
3334
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
34-
// SAFETY: DLMALLOC.calloc() is guranteed to be safe since lock::lock() aqcuire a globl lock
35+
// SAFETY: DLMALLOC access is guranteed to be safe because the lock gives us unique and non-reentrant access.
36+
// Calling calloc() is safe because preconditions on this function match the trait method preconditions.
3537
let _lock = lock::lock();
3638
unsafe { DLMALLOC.calloc(layout.size(), layout.align()) }
3739
}
3840

3941
#[inline]
4042
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
41-
// SAFETY: DLMALLOC.free() is guranteed to be safe since lock::lock() aqcuire a globl lock
43+
// SAFETY: DLMALLOC access is guranteed to be safe because the lock gives us unique and non-reentrant access.
44+
// Calling free() is safe because preconditions on this function match the trait method preconditions.
4245
let _lock = lock::lock();
4346
unsafe { DLMALLOC.free(ptr, layout.size(), layout.align()) }
4447
}
4548

4649
#[inline]
4750
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
48-
// SAFETY: DLMALLOC.realloc() is guranteed to be safe since lock::lock() aqcuire a globl lock
51+
// SAFETY: DLMALLOC access is guranteed to be safe because the lock gives us unique and non-reentrant access.
52+
// Calling realloc() is safe because preconditions on this function match the trait method preconditions.
4953
let _lock = lock::lock();
5054
unsafe { DLMALLOC.realloc(ptr, layout.size(), layout.align(), new_size) }
5155
}

0 commit comments

Comments
 (0)