Skip to content

Commit

Permalink
kernel/vtpm/wrapper: reduce indentation in some wrappers
Browse files Browse the repository at this point in the history
In several places we use `if let Ok(foo) = func() {` creating large
indented blocks, let's replace with `let Ok(foo) = func() else {` by
immediately returning a null pointer and reducing the indentation
to make the code more readable.

Suggested-by: Carlos López <[email protected]>
Signed-off-by: Stefano Garzarella <[email protected]>
  • Loading branch information
stefano-garzarella committed Jan 7, 2025
1 parent d350ae4 commit 73efb3d
Showing 1 changed file with 48 additions and 42 deletions.
90 changes: 48 additions & 42 deletions kernel/src/vtpm/tcgtpm/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,31 @@ pub extern "C" fn malloc(size: c_ulong) -> *mut c_void {
return ptr::null_mut();
}

if let Ok(layout) = layout_from_size(size as usize) {
// SAFETY: layout is guaranteed to be non-zero size. Memory may not be
// initiatlized, but that's what the caller expects.
return unsafe { alloc(layout).cast() };
}
ptr::null_mut()
let Ok(layout) = layout_from_size(size as usize) else {
return ptr::null_mut();
};

// SAFETY: layout is guaranteed to be non-zero size. Memory may not be
// initiatlized, but that's what the caller expects.
unsafe { alloc(layout).cast() }
}

#[no_mangle]
pub extern "C" fn calloc(items: c_ulong, size: c_ulong) -> *mut c_void {
if let Some(new_size) = items.checked_mul(size) {
if new_size == 0 {
return ptr::null_mut();
}

if let Ok(layout) = layout_from_size(new_size as usize) {
// SAFETY: layout is guaranteed to be non-zero size.
return unsafe { alloc_zeroed(layout).cast() };
}
let Some(new_size) = items.checked_mul(size) else {
return ptr::null_mut();
};

if new_size == 0 {
return ptr::null_mut();
}
ptr::null_mut()

let Ok(layout) = layout_from_size(new_size as usize) else {
return ptr::null_mut();
};

// SAFETY: layout is guaranteed to be non-zero size.
unsafe { alloc_zeroed(layout).cast() }
}

#[no_mangle]
Expand All @@ -64,27 +68,28 @@ pub unsafe extern "C" fn realloc(p: *mut c_void, size: c_ulong) -> *mut c_void {
return malloc(size);
}

if let Some(layout) = layout_from_ptr(ptr) {
if new_size == 0 {
// SAFETY: layout_from_ptr() call ensures that `ptr` was allocated
// with this allocator and we are using the same `layout` used to
// allocate `ptr`.
unsafe { dealloc(ptr, layout) };
return ptr::null_mut();
}

// This will fail if `new_size` rounded value exceeds `isize::MAX`
if Layout::from_size_align(new_size, layout.align()).is_err() {
return ptr::null_mut();
}

// SAFETY: layout_from_ptr() call ensures that `ptr` was allocated with
// this allocator and we are using the same `layout` used to allocate
// `ptr`. We also checked that `new_size` aligned does not overflow and
// it is not 0.
return unsafe { _realloc(ptr, layout, new_size).cast() };
let Some(layout) = layout_from_ptr(ptr) else {
return ptr::null_mut();
};

if new_size == 0 {
// SAFETY: layout_from_ptr() call ensures that `ptr` was allocated
// with this allocator and we are using the same `layout` used to
// allocate `ptr`.
unsafe { dealloc(ptr, layout) };
return ptr::null_mut();
}

// This will fail if `new_size` rounded value exceeds `isize::MAX`
if Layout::from_size_align(new_size, layout.align()).is_err() {
return ptr::null_mut();
}
ptr::null_mut()

// SAFETY: layout_from_ptr() call ensures that `ptr` was allocated with
// this allocator and we are using the same `layout` used to allocate
// `ptr`. We also checked that `new_size` aligned does not overflow and
// it is not 0.
unsafe { _realloc(ptr, layout, new_size).cast() }
}

#[no_mangle]
Expand All @@ -93,12 +98,13 @@ pub unsafe extern "C" fn free(p: *mut c_void) {
return;
}
let ptr = p as *mut u8;
if let Some(layout) = layout_from_ptr(ptr.cast()) {
// SAFETY: layout_from_ptr() call ensures that `ptr` was allocated
// with this allocator and we are using the same `layout` used to
// allocate `ptr`.
unsafe { dealloc(ptr, layout) }
}
let Some(layout) = layout_from_ptr(ptr.cast()) else {
return;
};
// SAFETY: layout_from_ptr() call ensures that `ptr` was allocated
// with this allocator and we are using the same `layout` used to
// allocate `ptr`.
unsafe { dealloc(ptr, layout) }
}

#[no_mangle]
Expand Down

0 comments on commit 73efb3d

Please sign in to comment.