Issue 3223: make unsafe scopes tighter
This commit is contained in:
@@ -496,14 +496,13 @@ impl Arena {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe fn drop_slab_in_place(value: NonNull<AllocSlab>, tag: ArenaHeaderTag) {
|
unsafe fn drop_slab_in_place(value: NonNull<AllocSlab>, tag: ArenaHeaderTag) {
|
||||||
unsafe {
|
|
||||||
macro_rules! drop_typed_slab_in_place {
|
macro_rules! drop_typed_slab_in_place {
|
||||||
($payload: ty, $value: expr) => {
|
($payload: ty, $value: expr) => {
|
||||||
<$payload as ArenaAllocated>::dealloc($value.cast::<TypedAllocSlab<$payload>>())
|
<$payload as ArenaAllocated>::dealloc($value.cast::<TypedAllocSlab<$payload>>())
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
unsafe {
|
||||||
match tag {
|
match tag {
|
||||||
ArenaHeaderTag::Integer => {
|
ArenaHeaderTag::Integer => {
|
||||||
drop_typed_slab_in_place!(Integer, value);
|
drop_typed_slab_in_place!(Integer, value);
|
||||||
|
|||||||
@@ -386,8 +386,6 @@ impl Atom {
|
|||||||
unsafe fn write_to_ptr(string: &str, ptr: *mut u8) {
|
unsafe fn write_to_ptr(string: &str, ptr: *mut u8) {
|
||||||
unsafe {
|
unsafe {
|
||||||
ptr::write(ptr as *mut _, AtomHeader::build_with(string.len() as u64));
|
ptr::write(ptr as *mut _, AtomHeader::build_with(string.len() as u64));
|
||||||
}
|
|
||||||
unsafe {
|
|
||||||
let str_ptr = ptr.add(mem::size_of::<AtomHeader>());
|
let str_ptr = ptr.add(mem::size_of::<AtomHeader>());
|
||||||
ptr::copy_nonoverlapping(string.as_ptr(), str_ptr, string.len());
|
ptr::copy_nonoverlapping(string.as_ptr(), str_ptr, string.len());
|
||||||
}
|
}
|
||||||
|
|||||||
20
src/ffi.rs
20
src/ffi.rs
@@ -56,8 +56,8 @@ impl FunctionImpl {
|
|||||||
unsafe fn call_void(&self, args: &[Arg], _: &mut Arena) -> Result<Value, FfiError> {
|
unsafe fn call_void(&self, args: &[Arg], _: &mut Arena) -> Result<Value, FfiError> {
|
||||||
unsafe {
|
unsafe {
|
||||||
self.cif.call_return_into(self.code_ptr, args, Ret::void());
|
self.cif.call_return_into(self.code_ptr, args, Ret::void());
|
||||||
Ok(Value::Number(Number::Fixnum(Fixnum::build_with(0))))
|
|
||||||
}
|
}
|
||||||
|
Ok(Value::Number(Number::Fixnum(Fixnum::build_with(0))))
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe fn call_int<T>(&self, args: &[Arg], arena: &mut Arena) -> Result<Value, FfiError>
|
unsafe fn call_int<T>(&self, args: &[Arg], arena: &mut Arena) -> Result<Value, FfiError>
|
||||||
@@ -204,14 +204,14 @@ impl StructImpl {
|
|||||||
layout: &mut Layout,
|
layout: &mut Layout,
|
||||||
val: T,
|
val: T,
|
||||||
) -> Result<(), FfiError> {
|
) -> Result<(), FfiError> {
|
||||||
|
let (new_layout, offset) = layout
|
||||||
|
.extend(Layout::new::<T>())
|
||||||
|
.map_err(|_| FfiError::LayoutError)?;
|
||||||
|
*layout = new_layout;
|
||||||
unsafe {
|
unsafe {
|
||||||
let (new_layout, offset) = layout
|
|
||||||
.extend(Layout::new::<T>())
|
|
||||||
.map_err(|_| FfiError::LayoutError)?;
|
|
||||||
*layout = new_layout;
|
|
||||||
ptr.byte_offset(offset as isize).cast::<T>().write(val);
|
ptr.byte_offset(offset as isize).cast::<T>().write(val);
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
for arg in args {
|
for arg in args {
|
||||||
@@ -267,11 +267,11 @@ impl StructImpl {
|
|||||||
ptr: *mut c_void,
|
ptr: *mut c_void,
|
||||||
layout: &mut Layout,
|
layout: &mut Layout,
|
||||||
) -> Result<T, FfiError> {
|
) -> Result<T, FfiError> {
|
||||||
|
let (new_layout, offset) = layout
|
||||||
|
.extend(Layout::new::<T>())
|
||||||
|
.map_err(|_| FfiError::LayoutError)?;
|
||||||
|
*layout = new_layout;
|
||||||
unsafe {
|
unsafe {
|
||||||
let (new_layout, offset) = layout
|
|
||||||
.extend(Layout::new::<T>())
|
|
||||||
.map_err(|_| FfiError::LayoutError)?;
|
|
||||||
*layout = new_layout;
|
|
||||||
let n = std::ptr::read::<T>(ptr.byte_offset(offset as isize).cast());
|
let n = std::ptr::read::<T>(ptr.byte_offset(offset as isize).cast());
|
||||||
Ok(n)
|
Ok(n)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,7 +58,6 @@ struct InnerHeap {
|
|||||||
|
|
||||||
impl InnerHeap {
|
impl InnerHeap {
|
||||||
unsafe fn grow(&mut self) -> bool {
|
unsafe fn grow(&mut self) -> bool {
|
||||||
unsafe {
|
|
||||||
let new_cap = if self.byte_cap == 0 {
|
let new_cap = if self.byte_cap == 0 {
|
||||||
256 * 256 * 8
|
256 * 256 * 8
|
||||||
} else {
|
} else {
|
||||||
@@ -72,7 +71,7 @@ impl InnerHeap {
|
|||||||
new_layout.size() <= isize::MAX as usize,
|
new_layout.size() <= isize::MAX as usize,
|
||||||
"Allocation too large. We should probably GC (TODO)"
|
"Allocation too large. We should probably GC (TODO)"
|
||||||
);
|
);
|
||||||
|
unsafe {
|
||||||
let new_ptr = if self.byte_cap == 0 {
|
let new_ptr = if self.byte_cap == 0 {
|
||||||
alloc::alloc(new_layout)
|
alloc::alloc(new_layout)
|
||||||
} else {
|
} else {
|
||||||
@@ -105,11 +104,12 @@ pub struct HeapStringScan<'a> {
|
|||||||
|
|
||||||
// The heap_slice should be inside the heap
|
// The heap_slice should be inside the heap
|
||||||
unsafe fn scan_slice_to_str(heap_slice: &[u8]) -> HeapStringScan<'_> {
|
unsafe fn scan_slice_to_str(heap_slice: &[u8]) -> HeapStringScan<'_> {
|
||||||
unsafe {
|
|
||||||
let string_len = heap_slice
|
let string_len = heap_slice
|
||||||
.iter()
|
.iter()
|
||||||
.position(|b| *b == 0u8)
|
.position(|b| *b == 0u8)
|
||||||
.unwrap_or(heap_slice.len());
|
.unwrap_or(heap_slice.len());
|
||||||
|
|
||||||
|
unsafe {
|
||||||
let zero_byte_addr = heap_slice.as_ptr().add(string_len);
|
let zero_byte_addr = heap_slice.as_ptr().add(string_len);
|
||||||
|
|
||||||
let sentinel_len = pstr_sentinel_length(zero_byte_addr.addr());
|
let sentinel_len = pstr_sentinel_length(zero_byte_addr.addr());
|
||||||
@@ -130,7 +130,6 @@ unsafe fn scan_slice_to_str(heap_slice: &[u8]) -> HeapStringScan<'_> {
|
|||||||
// Same as scan_slice_to_str but assumes that the slice is from the start of a string.
|
// Same as scan_slice_to_str but assumes that the slice is from the start of a string.
|
||||||
// Can be used on strings out of the heap.
|
// Can be used on strings out of the heap.
|
||||||
unsafe fn scan_slice_to_str_from_start(heap_slice: &[u8]) -> HeapStringScan<'_> {
|
unsafe fn scan_slice_to_str_from_start(heap_slice: &[u8]) -> HeapStringScan<'_> {
|
||||||
unsafe {
|
|
||||||
let string_len = heap_slice
|
let string_len = heap_slice
|
||||||
.iter()
|
.iter()
|
||||||
.position(|b| *b == 0u8)
|
.position(|b| *b == 0u8)
|
||||||
@@ -143,7 +142,8 @@ unsafe fn scan_slice_to_str_from_start(heap_slice: &[u8]) -> HeapStringScan<'_>
|
|||||||
);
|
);
|
||||||
|
|
||||||
let str_slice = &heap_slice[..string_len];
|
let str_slice = &heap_slice[..string_len];
|
||||||
|
|
||||||
|
unsafe {
|
||||||
HeapStringScan {
|
HeapStringScan {
|
||||||
string: std::str::from_utf8_unchecked(str_slice),
|
string: std::str::from_utf8_unchecked(str_slice),
|
||||||
tail_idx,
|
tail_idx,
|
||||||
|
|||||||
@@ -176,8 +176,8 @@ impl Stack {
|
|||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
unsafe fn alloc(&mut self, frame_size: usize) -> Result<NonNull<u8>, AllocError> {
|
unsafe fn alloc(&mut self, frame_size: usize) -> Result<NonNull<u8>, AllocError> {
|
||||||
unsafe {
|
|
||||||
loop {
|
loop {
|
||||||
|
unsafe {
|
||||||
let ptr = self.buf.alloc(frame_size);
|
let ptr = self.buf.alloc(frame_size);
|
||||||
if let Some(ptr) = NonNull::new(ptr) {
|
if let Some(ptr) = NonNull::new(ptr) {
|
||||||
return Ok(ptr);
|
return Ok(ptr);
|
||||||
|
|||||||
@@ -102,7 +102,7 @@ pub(crate) fn set_prompt(value: bool) {
|
|||||||
#[cfg(feature = "repl")]
|
#[cfg(feature = "repl")]
|
||||||
#[inline]
|
#[inline]
|
||||||
fn get_prompt() -> &'static str {
|
fn get_prompt() -> &'static str {
|
||||||
unsafe { if PROMPT { "?- " } else { "" } }
|
if unsafe { PROMPT } { "?- " } else { "" }
|
||||||
}
|
}
|
||||||
|
|
||||||
thread_local! {
|
thread_local! {
|
||||||
|
|||||||
Reference in New Issue
Block a user