base: Use trait objects for ioctls

Having these function be generic over F: AsRawDescriptor with a &F
parameter means that we cannot use trait objects with them because the
compiler complains that `dyn AsRawDescriptor` does not have a
compile-time size.

Instead change the generic parameter to be a trait object.  We're
already doing this in a few other places and we're about to make a
syscall so the cost of dynamic dispatch is probably overshadowed by the
cost of syscall itself.

BUG=b:180565632
TEST=unit tests

Change-Id: I38f696b621411e7cf3e13af71e426865b6509f6f
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2706369
Tested-by: Chirantan Ekbote <chirantan@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Auto-Submit: Chirantan Ekbote <chirantan@chromium.org>
Commit-Queue: Stephen Barber <smbarber@chromium.org>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Stephen Barber <smbarber@chromium.org>
Reviewed-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
This commit is contained in:
Chirantan Ekbote 2021-02-19 18:19:35 +09:00 committed by Commit Bot
parent 8ef81401dd
commit 90163ceb41

View file

@ -11,16 +11,12 @@ pub unsafe fn ioctl<F: AsRawDescriptor>(descriptor: &F, nr: IoctlNr) -> c_int {
}
/// Run an ioctl with a single value argument.
pub unsafe fn ioctl_with_val<F: AsRawDescriptor>(
descriptor: &F,
nr: IoctlNr,
arg: c_ulong,
) -> c_int {
pub unsafe fn ioctl_with_val(descriptor: &dyn AsRawDescriptor, nr: IoctlNr, arg: c_ulong) -> c_int {
libc::ioctl(descriptor.as_raw_descriptor(), nr, arg)
}
/// Run an ioctl with an immutable reference.
pub unsafe fn ioctl_with_ref<F: AsRawDescriptor, T>(descriptor: &F, nr: IoctlNr, arg: &T) -> c_int {
pub unsafe fn ioctl_with_ref<T>(descriptor: &dyn AsRawDescriptor, nr: IoctlNr, arg: &T) -> c_int {
libc::ioctl(
descriptor.as_raw_descriptor(),
nr,
@ -29,8 +25,8 @@ pub unsafe fn ioctl_with_ref<F: AsRawDescriptor, T>(descriptor: &F, nr: IoctlNr,
}
/// Run an ioctl with a mutable reference.
pub unsafe fn ioctl_with_mut_ref<F: AsRawDescriptor, T>(
descriptor: &F,
pub unsafe fn ioctl_with_mut_ref<T>(
descriptor: &dyn AsRawDescriptor,
nr: IoctlNr,
arg: &mut T,
) -> c_int {
@ -42,8 +38,8 @@ pub unsafe fn ioctl_with_mut_ref<F: AsRawDescriptor, T>(
}
/// Run an ioctl with a raw pointer.
pub unsafe fn ioctl_with_ptr<F: AsRawDescriptor, T>(
descriptor: &F,
pub unsafe fn ioctl_with_ptr<T>(
descriptor: &dyn AsRawDescriptor,
nr: IoctlNr,
arg: *const T,
) -> c_int {
@ -51,8 +47,8 @@ pub unsafe fn ioctl_with_ptr<F: AsRawDescriptor, T>(
}
/// Run an ioctl with a mutable raw pointer.
pub unsafe fn ioctl_with_mut_ptr<F: AsRawDescriptor, T>(
descriptor: &F,
pub unsafe fn ioctl_with_mut_ptr<T>(
descriptor: &dyn AsRawDescriptor,
nr: IoctlNr,
arg: *mut T,
) -> c_int {