Skip to content

Commit a629f05

Browse files
authored
Merge pull request #159 from wedsonaf/vtable
Move creation of `struct file_operations` to a const function.
2 parents 101e008 + 0af7715 commit a629f05

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

rust/kernel/chrdev.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ impl<const N: usize> Registration<{ N }> {
112112
// SAFETY: Calling unsafe functions and manipulating `MaybeUninit`
113113
// pointer.
114114
unsafe {
115-
bindings::cdev_init(cdev, &file_operations::FileOperationsVtable::<T>::VTABLE);
115+
bindings::cdev_init(cdev, file_operations::FileOperationsVtable::<T>::build());
116116
(*cdev).owner = this.this_module.0;
117117
let rc = bindings::cdev_add(cdev, inner.dev + inner.used as bindings::dev_t, 1);
118118
if rc != 0 {

rust/kernel/file_operations.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ unsafe extern "C" fn fsync_callback<T: FileOperations>(
201201
pub(crate) struct FileOperationsVtable<T>(marker::PhantomData<T>);
202202

203203
impl<T: FileOperations> FileOperationsVtable<T> {
204-
pub(crate) const VTABLE: bindings::file_operations = bindings::file_operations {
204+
const VTABLE: bindings::file_operations = bindings::file_operations {
205205
open: Some(open_callback::<T>),
206206
release: Some(release_callback::<T>),
207207
read: if T::TO_USE.read {
@@ -260,6 +260,11 @@ impl<T: FileOperations> FileOperationsVtable<T> {
260260
},
261261
write_iter: None,
262262
};
263+
264+
/// Builds an instance of [`struct file_operations`].
265+
pub(crate) const fn build() -> &'static bindings::file_operations {
266+
&Self::VTABLE
267+
}
263268
}
264269

265270
/// Represents which fields of [`struct file_operations`] should be populated with pointers.

rust/kernel/miscdev.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl Registration {
6161

6262
this.mdev = Some(bindings::miscdevice::default());
6363
let dev = this.mdev.as_mut().unwrap();
64-
dev.fops = &FileOperationsVtable::<T>::VTABLE;
64+
dev.fops = FileOperationsVtable::<T>::build();
6565
dev.name = name.as_ptr() as *const c_types::c_char;
6666
dev.minor = minor.unwrap_or(bindings::MISC_DYNAMIC_MINOR as i32);
6767
let ret = unsafe { bindings::misc_register(dev) };

0 commit comments

Comments
 (0)