Skip to content

Commit

Permalink
Fix error (shared reference to mutable static)
Browse files Browse the repository at this point in the history
  • Loading branch information
byeongkeunahn committed Sep 21, 2024
1 parent e05a93b commit 0ecfa79
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 47 deletions.
6 changes: 4 additions & 2 deletions basm-std/src/platform/malloc/dlmalloc_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ impl Default for System {
unsafe impl DlmallocAllocator for System {
fn alloc(&self, size: usize) -> (*mut u8, usize, u32) {
let addr = unsafe {
super::super::os::windows::WINAPI.VirtualAlloc(
let winapi = &*core::ptr::addr_of_mut!(super::super::os::windows::WINAPI);
winapi.VirtualAlloc(
ptr::null_mut(),
size,
0x00003000, /* MEM_COMMIT | MEM_RESERVE */
Expand All @@ -49,7 +50,8 @@ unsafe impl DlmallocAllocator for System {
#[allow(unused)]
fn free(&self, ptr: *mut u8, size: usize) -> bool {
unsafe {
super::super::os::windows::WINAPI.VirtualFree(ptr, 0, 0x00008000 /* MEM_RELEASE */) != 0
let winapi = &*core::ptr::addr_of_mut!(super::super::os::windows::WINAPI);
winapi.VirtualFree(ptr, 0, 0x00008000 /* MEM_RELEASE */) != 0
}
}

Expand Down
22 changes: 14 additions & 8 deletions basm-std/src/platform/os/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,20 +334,25 @@ pub mod syscall {
static mut DLMALLOC: dlmalloc::Dlmalloc<dlmalloc_linux::System> =
dlmalloc::Dlmalloc::new(dlmalloc_linux::System::new());
unsafe fn dlmalloc_alloc(size: usize, align: usize) -> *mut u8 {
unsafe { DLMALLOC.memalign(align, size) }
unsafe {
let dlmalloc = &mut *core::ptr::addr_of_mut!(DLMALLOC);
dlmalloc.memalign(align, size)
}
}
unsafe fn dlmalloc_alloc_zeroed(size: usize, align: usize) -> *mut u8 {
unsafe {
let ptr = DLMALLOC.memalign(align, size);
if !ptr.is_null() && DLMALLOC.calloc_must_clear(ptr) {
let dlmalloc = &mut *core::ptr::addr_of_mut!(DLMALLOC);
let ptr = dlmalloc.memalign(align, size);
if !ptr.is_null() && dlmalloc.calloc_must_clear(ptr) {
core::ptr::write_bytes(ptr, 0, size);
}
ptr
}
}
unsafe fn dlmalloc_dealloc(ptr: *mut u8, _size: usize, _align: usize) {
unsafe {
DLMALLOC.free(ptr);
let dlmalloc = &mut *core::ptr::addr_of_mut!(DLMALLOC);
dlmalloc.free(ptr);
}
}
unsafe fn dlmalloc_realloc(
Expand All @@ -357,13 +362,14 @@ unsafe fn dlmalloc_realloc(
new_size: usize,
) -> *mut u8 {
unsafe {
if old_align <= DLMALLOC.malloc_alignment() {
DLMALLOC.realloc(ptr, new_size)
let dlmalloc = &mut *core::ptr::addr_of_mut!(DLMALLOC);
if old_align <= dlmalloc.malloc_alignment() {
dlmalloc.realloc(ptr, new_size)
} else {
let ptr_new = DLMALLOC.memalign(old_align, new_size);
let ptr_new = dlmalloc.memalign(old_align, new_size);
if !ptr_new.is_null() {
core::ptr::copy_nonoverlapping(ptr, ptr_new, core::cmp::min(old_size, new_size));
DLMALLOC.free(ptr);
dlmalloc.free(ptr);
}
ptr_new
}
Expand Down
22 changes: 14 additions & 8 deletions basm-std/src/platform/os/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,25 @@ pub mod syscall {
static mut DLMALLOC: dlmalloc::Dlmalloc<dlmalloc_macos::System> =
dlmalloc::Dlmalloc::new(dlmalloc_macos::System::new());
unsafe fn dlmalloc_alloc(size: usize, align: usize) -> *mut u8 {
unsafe { DLMALLOC.memalign(align, size) }
unsafe {
let dlmalloc = &mut *core::ptr::addr_of_mut!(DLMALLOC);
dlmalloc.memalign(align, size)
}
}
unsafe fn dlmalloc_alloc_zeroed(size: usize, align: usize) -> *mut u8 {
unsafe {
let ptr = DLMALLOC.memalign(align, size);
if !ptr.is_null() && DLMALLOC.calloc_must_clear(ptr) {
let dlmalloc = &mut *core::ptr::addr_of_mut!(DLMALLOC);
let ptr = dlmalloc.memalign(align, size);
if !ptr.is_null() && dlmalloc.calloc_must_clear(ptr) {
core::ptr::write_bytes(ptr, 0, size);
}
ptr
}
}
unsafe fn dlmalloc_dealloc(ptr: *mut u8, _size: usize, _align: usize) {
unsafe {
DLMALLOC.free(ptr);
let dlmalloc = &mut *core::ptr::addr_of_mut!(DLMALLOC);
dlmalloc.free(ptr);
}
}
unsafe fn dlmalloc_realloc(
Expand All @@ -63,13 +68,14 @@ unsafe fn dlmalloc_realloc(
new_size: usize,
) -> *mut u8 {
unsafe {
if old_align <= DLMALLOC.malloc_alignment() {
DLMALLOC.realloc(ptr, new_size)
let dlmalloc = &mut *core::ptr::addr_of_mut!(DLMALLOC);
if old_align <= dlmalloc.malloc_alignment() {
dlmalloc.realloc(ptr, new_size)
} else {
let ptr_new = DLMALLOC.memalign(old_align, new_size);
let ptr_new = dlmalloc.memalign(old_align, new_size);
if !ptr_new.is_null() {
core::ptr::copy_nonoverlapping(ptr, ptr_new, core::cmp::min(old_size, new_size));
DLMALLOC.free(ptr);
dlmalloc.free(ptr);
}
ptr_new
}
Expand Down
22 changes: 14 additions & 8 deletions basm-std/src/platform/os/wasm32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,25 @@ use super::super::{allocator, services};
static mut DLMALLOC: dlmalloc::Dlmalloc<dlmalloc_wasm32::System> =
dlmalloc::Dlmalloc::new(dlmalloc_wasm32::System::new());
unsafe fn dlmalloc_alloc(size: usize, align: usize) -> *mut u8 {
unsafe { DLMALLOC.memalign(align, size) }
unsafe {
let dlmalloc = &mut *core::ptr::addr_of_mut!(DLMALLOC);
dlmalloc.memalign(align, size)
}
}
unsafe fn dlmalloc_alloc_zeroed(size: usize, align: usize) -> *mut u8 {
unsafe {
let ptr = DLMALLOC.memalign(align, size);
if !ptr.is_null() && DLMALLOC.calloc_must_clear(ptr) {
let dlmalloc = &mut *core::ptr::addr_of_mut!(DLMALLOC);
let ptr = dlmalloc.memalign(align, size);
if !ptr.is_null() && dlmalloc.calloc_must_clear(ptr) {
core::ptr::write_bytes(ptr, 0, size);
}
ptr
}
}
unsafe fn dlmalloc_dealloc(ptr: *mut u8, _size: usize, _align: usize) {
unsafe {
DLMALLOC.free(ptr);
let dlmalloc = &mut *core::ptr::addr_of_mut!(DLMALLOC);
dlmalloc.free(ptr);
}
}
unsafe fn dlmalloc_realloc(
Expand All @@ -27,13 +32,14 @@ unsafe fn dlmalloc_realloc(
new_size: usize,
) -> *mut u8 {
unsafe {
if old_align <= DLMALLOC.malloc_alignment() {
DLMALLOC.realloc(ptr, new_size)
let dlmalloc = &mut *core::ptr::addr_of_mut!(DLMALLOC);
if old_align <= dlmalloc.malloc_alignment() {
dlmalloc.realloc(ptr, new_size)
} else {
let ptr_new = DLMALLOC.memalign(old_align, new_size);
let ptr_new = dlmalloc.memalign(old_align, new_size);
if !ptr_new.is_null() {
core::ptr::copy_nonoverlapping(ptr, ptr_new, core::cmp::min(old_size, new_size));
DLMALLOC.free(ptr);
dlmalloc.free(ptr);
}
ptr_new
}
Expand Down
50 changes: 29 additions & 21 deletions basm-std/src/platform/os/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,20 +169,25 @@ pub static mut WINAPI: WinApi = WinApi {
static mut DLMALLOC: dlmalloc::Dlmalloc<dlmalloc_windows::System> =
dlmalloc::Dlmalloc::new(dlmalloc_windows::System::new());
unsafe fn dlmalloc_alloc(size: usize, align: usize) -> *mut u8 {
unsafe { DLMALLOC.memalign(align, size) }
unsafe {
let dlmalloc = &mut *core::ptr::addr_of_mut!(DLMALLOC);
dlmalloc.memalign(align, size)
}
}
unsafe fn dlmalloc_alloc_zeroed(size: usize, align: usize) -> *mut u8 {
unsafe {
let ptr = DLMALLOC.memalign(align, size);
if !ptr.is_null() && DLMALLOC.calloc_must_clear(ptr) {
let dlmalloc = &mut *core::ptr::addr_of_mut!(DLMALLOC);
let ptr = dlmalloc.memalign(align, size);
if !ptr.is_null() && dlmalloc.calloc_must_clear(ptr) {
core::ptr::write_bytes(ptr, 0, size);
}
ptr
}
}
unsafe fn dlmalloc_dealloc(ptr: *mut u8, _size: usize, _align: usize) {
unsafe {
DLMALLOC.free(ptr);
let dlmalloc = &mut *core::ptr::addr_of_mut!(DLMALLOC);
dlmalloc.free(ptr);
}
}
unsafe fn dlmalloc_realloc(
Expand All @@ -192,13 +197,14 @@ unsafe fn dlmalloc_realloc(
new_size: usize,
) -> *mut u8 {
unsafe {
if old_align <= DLMALLOC.malloc_alignment() {
DLMALLOC.realloc(ptr, new_size)
let dlmalloc = &mut *core::ptr::addr_of_mut!(DLMALLOC);
if old_align <= dlmalloc.malloc_alignment() {
dlmalloc.realloc(ptr, new_size)
} else {
let ptr_new = DLMALLOC.memalign(old_align, new_size);
let ptr_new = dlmalloc.memalign(old_align, new_size);
if !ptr_new.is_null() {
core::ptr::copy_nonoverlapping(ptr, ptr_new, core::cmp::min(old_size, new_size));
DLMALLOC.free(ptr);
dlmalloc.free(ptr);
}
ptr_new
}
Expand All @@ -210,46 +216,48 @@ mod services_override {
basm_abi! {pub unsafe fn svc_read_stdio(fd: usize, buf: *mut u8, count: usize) -> usize {
unsafe {
debug_assert!(fd == 0);
let handle = WINAPI.GetStdHandle(WinApi::STD_INPUT_HANDLE);
let winapi = &mut *core::ptr::addr_of_mut!(WINAPI);
let handle = winapi.GetStdHandle(WinApi::STD_INPUT_HANDLE);
let mut bytes_read: u32 = 0;
let mut ov: Overlapped = Default::default();
ov.set_off(WINAPI.io_off[fd]);
let mut ret = WINAPI.ReadFile(handle, buf, count as u32,
ov.set_off(winapi.io_off[fd]);
let mut ret = winapi.ReadFile(handle, buf, count as u32,
&mut bytes_read as *mut u32, &mut ov as *mut Overlapped);
if ret == 0 {
let err = WINAPI.GetLastError();
let err = winapi.GetLastError();
if err == WinApi::ERROR_IO_PENDING {
ret = WINAPI.GetOverlappedResult(handle, &mut ov as *mut Overlapped,
ret = winapi.GetOverlappedResult(handle, &mut ov as *mut Overlapped,
&mut bytes_read as *mut u32, 1);
}
if ret != 0 { return 0; }
}
WINAPI.io_off[fd] += bytes_read as u64;
winapi.io_off[fd] += bytes_read as u64;
bytes_read as usize
}
}}
basm_abi! {pub unsafe fn svc_write_stdio(fd: usize, buf: *const u8, count: usize) -> usize {
unsafe {
debug_assert!(fd == 1 || fd == 2);
let winapi = &mut *core::ptr::addr_of_mut!(WINAPI);
let handle = match fd {
1 => { WINAPI.GetStdHandle(WinApi::STD_OUTPUT_HANDLE) },
2 => { WINAPI.GetStdHandle(WinApi::STD_ERROR_HANDLE) },
1 => { winapi.GetStdHandle(WinApi::STD_OUTPUT_HANDLE) },
2 => { winapi.GetStdHandle(WinApi::STD_ERROR_HANDLE) },
_ => { unreachable!(); }
};
let mut bytes_written: u32 = 0;
let mut ov: Overlapped = Default::default();
ov.set_off(WINAPI.io_off[fd]);
let mut ret = WINAPI.WriteFile(handle, buf, count as u32,
ov.set_off(winapi.io_off[fd]);
let mut ret = winapi.WriteFile(handle, buf, count as u32,
&mut bytes_written as *mut u32, &mut ov as *mut Overlapped);
if ret == 0 {
let err = WINAPI.GetLastError();
let err = winapi.GetLastError();
if err == WinApi::ERROR_IO_PENDING {
ret = WINAPI.GetOverlappedResult(handle, &mut ov as *mut Overlapped,
ret = winapi.GetOverlappedResult(handle, &mut ov as *mut Overlapped,
&mut bytes_written as *mut u32, 1);
}
if ret != 0 { return 0; }
}
WINAPI.io_off[fd] += bytes_written as u64;
winapi.io_off[fd] += bytes_written as u64;
bytes_written as usize
}
}}
Expand Down

0 comments on commit 0ecfa79

Please sign in to comment.