Use unsafe blocks in platform module
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
// TODO: set this for entire crate when possible
|
||||
#![deny(unsafe_op_in_unsafe_fn)]
|
||||
|
||||
use core::{
|
||||
alloc::{GlobalAlloc, Layout},
|
||||
cell::SyncUnsafeCell,
|
||||
@@ -33,22 +36,22 @@ unsafe impl GlobalAlloc for Allocator {
|
||||
#[inline]
|
||||
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
|
||||
if layout.align() <= align_of::<max_align_t>() {
|
||||
(*self.get()).lock().malloc(layout.size())
|
||||
unsafe { (*self.get()).lock().malloc(layout.size()) }
|
||||
} else {
|
||||
(*self.get()).lock().memalign(layout.align(), layout.size())
|
||||
unsafe { (*self.get()).lock().memalign(layout.align(), layout.size()) }
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
|
||||
(*self.get()).lock().free(ptr)
|
||||
unsafe { (*self.get()).lock().free(ptr) }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
|
||||
let ptr = self.alloc(layout);
|
||||
if !ptr.is_null() && (*self.get()).lock().calloc_must_clear(ptr) {
|
||||
write_bytes(ptr, 0, layout.size());
|
||||
let ptr = unsafe { self.alloc(layout) };
|
||||
if !ptr.is_null() && unsafe { (*self.get()).lock().calloc_must_clear(ptr) } {
|
||||
unsafe { write_bytes(ptr, 0, layout.size()) };
|
||||
}
|
||||
ptr
|
||||
}
|
||||
@@ -56,19 +59,20 @@ unsafe impl GlobalAlloc for Allocator {
|
||||
#[inline]
|
||||
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
|
||||
if layout.align() <= align_of::<max_align_t>() {
|
||||
(*self.get()).lock().realloc(ptr, new_size)
|
||||
unsafe { (*self.get()).lock().realloc(ptr, new_size) }
|
||||
} else {
|
||||
let new = self.alloc(Layout::from_size_align_unchecked(new_size, layout.align()));
|
||||
let new =
|
||||
unsafe { self.alloc(Layout::from_size_align_unchecked(new_size, layout.align())) };
|
||||
let old_size = layout.size();
|
||||
let old_align = layout.align();
|
||||
|
||||
if !new.is_null() {
|
||||
let size = cmp::min(old_size, new_size);
|
||||
copy_nonoverlapping(ptr, new, size);
|
||||
unsafe { copy_nonoverlapping(ptr, new, size) };
|
||||
}
|
||||
|
||||
drop((old_size, old_align));
|
||||
(*self.get()).lock().free(ptr);
|
||||
unsafe { (*self.get()).lock().free(ptr) };
|
||||
|
||||
new
|
||||
}
|
||||
@@ -76,18 +80,18 @@ unsafe impl GlobalAlloc for Allocator {
|
||||
}
|
||||
|
||||
pub unsafe fn alloc(size: size_t) -> *mut c_void {
|
||||
(*ALLOCATOR.get()).lock().malloc(size).cast()
|
||||
unsafe { (*ALLOCATOR.get()).lock().malloc(size) }.cast()
|
||||
}
|
||||
|
||||
pub unsafe fn alloc_align(size: size_t, alignment: size_t) -> *mut c_void {
|
||||
(*ALLOCATOR.get()).lock().memalign(alignment, size).cast()
|
||||
unsafe { (*ALLOCATOR.get()).lock().memalign(alignment, size) }.cast()
|
||||
}
|
||||
|
||||
pub unsafe fn realloc(ptr: *mut c_void, size: size_t) -> *mut c_void {
|
||||
if ptr.is_null() {
|
||||
(*ALLOCATOR.get()).lock().malloc(size).cast()
|
||||
unsafe { (*ALLOCATOR.get()).lock().malloc(size) }.cast()
|
||||
} else {
|
||||
(*ALLOCATOR.get()).lock().realloc(ptr.cast(), size).cast()
|
||||
unsafe { (*ALLOCATOR.get()).lock().realloc(ptr.cast(), size) }.cast()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,12 +99,12 @@ pub unsafe fn free(ptr: *mut c_void) {
|
||||
if ptr.is_null() {
|
||||
return;
|
||||
}
|
||||
(*ALLOCATOR.get()).lock().free(ptr.cast())
|
||||
unsafe { (*ALLOCATOR.get()).lock().free(ptr.cast()) }
|
||||
}
|
||||
|
||||
pub unsafe fn alloc_usable_size(ptr: *mut c_void) -> size_t {
|
||||
if ptr.is_null() {
|
||||
return 0;
|
||||
}
|
||||
(*ALLOCATOR.get()).lock().usable_size(ptr.cast())
|
||||
unsafe { (*ALLOCATOR.get()).lock().usable_size(ptr.cast()) }
|
||||
}
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
// TODO: set this for entire crate when possible
|
||||
#![deny(unsafe_op_in_unsafe_fn)]
|
||||
|
||||
use crate::{
|
||||
header::{
|
||||
sys_mman::{self, MAP_FAILED, MREMAP_MAYMOVE},
|
||||
@@ -115,13 +118,15 @@ pub unsafe fn enable_alloc_after_fork() {
|
||||
// it will acquire the lock before any other thread,
|
||||
// protecting it from deadlock,
|
||||
// due to the child being created with only the calling thread.
|
||||
if !FORK_PROTECTED {
|
||||
pthread_atfork(
|
||||
Some(_acquire_global_lock),
|
||||
Some(_release_global_lock),
|
||||
Some(_release_global_lock),
|
||||
);
|
||||
FORK_PROTECTED = true;
|
||||
unsafe {
|
||||
if !FORK_PROTECTED {
|
||||
pthread_atfork(
|
||||
Some(_acquire_global_lock),
|
||||
Some(_release_global_lock),
|
||||
Some(_release_global_lock),
|
||||
);
|
||||
FORK_PROTECTED = true;
|
||||
}
|
||||
}
|
||||
release_global_lock();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user