Skip to content

Commit

Permalink
Decrease scoping on some unsafe blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
emesare committed Sep 1, 2024
1 parent d98cf65 commit d36b962
Showing 1 changed file with 32 additions and 41 deletions.
73 changes: 32 additions & 41 deletions rust/src/callingconvention.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,27 +74,27 @@ where
where
C: CallingConventionBase,
{
ffi_wrap!("CallingConvention::free", unsafe {
let _ctxt = Box::from_raw(ctxt as *mut CustomCallingConventionContext<C>);
ffi_wrap!("CallingConvention::free", {
let _ctxt = unsafe { Box::from_raw(ctxt as *mut CustomCallingConventionContext<C>) };
})
}

extern "C" fn cb_free_register_list(_ctxt: *mut c_void, regs: *mut u32, count: usize) {
ffi_wrap!("CallingConvention::free_register_list", unsafe {
ffi_wrap!("CallingConvention::free_register_list", {
if regs.is_null() {
return;
}

let _regs = Box::from_raw(ptr::slice_from_raw_parts_mut(regs, count));
let _regs = unsafe { Box::from_raw(ptr::slice_from_raw_parts_mut(regs, count)) };
})
}

extern "C" fn cb_caller_saved<C>(ctxt: *mut c_void, count: *mut usize) -> *mut u32
where
C: CallingConventionBase,
{
ffi_wrap!("CallingConvention::caller_saved_registers", unsafe {
let ctxt = &*(ctxt as *mut CustomCallingConventionContext<C>);
ffi_wrap!("CallingConvention::caller_saved_registers", {
let ctxt = unsafe { &*(ctxt as *mut CustomCallingConventionContext<C>) };
let mut regs: Vec<_> = ctxt
.cc
.caller_saved_registers()
Expand All @@ -114,8 +114,8 @@ where
where
C: CallingConventionBase,
{
ffi_wrap!("CallingConvention::callee_saved_registers", unsafe {
let ctxt = &*(ctxt as *mut CustomCallingConventionContext<C>);
ffi_wrap!("CallingConvention::callee_saved_registers", {
let ctxt = unsafe { &*(ctxt as *mut CustomCallingConventionContext<C>) };
let mut regs: Vec<_> = ctxt
.cc
.callee_saved_registers()
Expand All @@ -135,8 +135,8 @@ where
where
C: CallingConventionBase,
{
ffi_wrap!("CallingConvention::int_arg_registers", unsafe {
let ctxt = &*(ctxt as *mut CustomCallingConventionContext<C>);
ffi_wrap!("CallingConvention::int_arg_registers", {
let ctxt = unsafe { &*(ctxt as *mut CustomCallingConventionContext<C>) };
let mut regs: Vec<_> = ctxt.cc.int_arg_registers().iter().map(|r| r.id()).collect();

// SAFETY: `count` is an out parameter
Expand All @@ -151,8 +151,8 @@ where
where
C: CallingConventionBase,
{
ffi_wrap!("CallingConvention::float_arg_registers", unsafe {
let ctxt = &*(ctxt as *mut CustomCallingConventionContext<C>);
ffi_wrap!("CallingConvention::float_arg_registers", {
let ctxt = unsafe { &*(ctxt as *mut CustomCallingConventionContext<C>) };
let mut regs: Vec<_> = ctxt
.cc
.float_arg_registers()
Expand All @@ -172,9 +172,8 @@ where
where
C: CallingConventionBase,
{
ffi_wrap!("CallingConvention::arg_registers_shared_index", unsafe {
let ctxt = &*(ctxt as *mut CustomCallingConventionContext<C>);

ffi_wrap!("CallingConvention::arg_registers_shared_index", {
let ctxt = unsafe { &*(ctxt as *mut CustomCallingConventionContext<C>) };
ctxt.cc.arg_registers_shared_index()
})
}
Expand All @@ -185,9 +184,8 @@ where
{
ffi_wrap!(
"CallingConvention::reserved_stack_space_for_arg_registers",
unsafe {
let ctxt = &*(ctxt as *mut CustomCallingConventionContext<C>);

{
let ctxt = unsafe { &*(ctxt as *mut CustomCallingConventionContext<C>) };
ctxt.cc.reserved_stack_space_for_arg_registers()
}
)
Expand All @@ -197,9 +195,8 @@ where
where
C: CallingConventionBase,
{
ffi_wrap!("CallingConvention::stack_adjusted_on_return", unsafe {
let ctxt = &*(ctxt as *mut CustomCallingConventionContext<C>);

ffi_wrap!("CallingConvention::stack_adjusted_on_return", {
let ctxt = unsafe { &*(ctxt as *mut CustomCallingConventionContext<C>) };
ctxt.cc.stack_adjusted_on_return()
})
}
Expand All @@ -208,9 +205,8 @@ where
where
C: CallingConventionBase,
{
ffi_wrap!("CallingConvention::is_eligible_for_heuristics", unsafe {
let ctxt = &*(ctxt as *mut CustomCallingConventionContext<C>);

ffi_wrap!("CallingConvention::is_eligible_for_heuristics", {
let ctxt = unsafe { &*(ctxt as *mut CustomCallingConventionContext<C>) };
ctxt.cc.is_eligible_for_heuristics()
})
}
Expand All @@ -219,9 +215,8 @@ where
where
C: CallingConventionBase,
{
ffi_wrap!("CallingConvention::return_int_reg", unsafe {
let ctxt = &*(ctxt as *mut CustomCallingConventionContext<C>);

ffi_wrap!("CallingConvention::return_int_reg", {
let ctxt = unsafe { &*(ctxt as *mut CustomCallingConventionContext<C>) };
match ctxt.cc.return_int_reg() {
Some(r) => r.id(),
_ => 0xffff_ffff,
Expand All @@ -233,9 +228,8 @@ where
where
C: CallingConventionBase,
{
ffi_wrap!("CallingConvention::return_hi_int_reg", unsafe {
let ctxt = &*(ctxt as *mut CustomCallingConventionContext<C>);

ffi_wrap!("CallingConvention::return_hi_int_reg", {
let ctxt = unsafe { &*(ctxt as *mut CustomCallingConventionContext<C>) };
match ctxt.cc.return_hi_int_reg() {
Some(r) => r.id(),
_ => 0xffff_ffff,
Expand All @@ -247,9 +241,8 @@ where
where
C: CallingConventionBase,
{
ffi_wrap!("CallingConvention::return_float_reg", unsafe {
let ctxt = &*(ctxt as *mut CustomCallingConventionContext<C>);

ffi_wrap!("CallingConvention::return_float_reg", {
let ctxt = unsafe { &*(ctxt as *mut CustomCallingConventionContext<C>) };
match ctxt.cc.return_float_reg() {
Some(r) => r.id(),
_ => 0xffff_ffff,
Expand All @@ -261,9 +254,8 @@ where
where
C: CallingConventionBase,
{
ffi_wrap!("CallingConvention::global_pointer_reg", unsafe {
let ctxt = &*(ctxt as *mut CustomCallingConventionContext<C>);

ffi_wrap!("CallingConvention::global_pointer_reg", {
let ctxt = unsafe { &*(ctxt as *mut CustomCallingConventionContext<C>) };
match ctxt.cc.global_pointer_reg() {
Some(r) => r.id(),
_ => 0xffff_ffff,
Expand All @@ -278,8 +270,8 @@ where
where
C: CallingConventionBase,
{
ffi_wrap!("CallingConvention::implicitly_defined_registers", unsafe {
let ctxt = &*(ctxt as *mut CustomCallingConventionContext<C>);
ffi_wrap!("CallingConvention::implicitly_defined_registers", {
let ctxt = unsafe { &*(ctxt as *mut CustomCallingConventionContext<C>) };
let mut regs: Vec<_> = ctxt
.cc
.implicitly_defined_registers()
Expand Down Expand Up @@ -359,9 +351,8 @@ where
{
ffi_wrap!(
"CallingConvention::are_argument_registers_used_for_var_args",
unsafe {
let ctxt = &*(ctxt as *mut CustomCallingConventionContext<C>);

{
let ctxt = unsafe { &*(ctxt as *mut CustomCallingConventionContext<C>) };
ctxt.cc.are_argument_registers_used_for_var_args()
}
)
Expand Down

0 comments on commit d36b962

Please sign in to comment.