|
5 | 5 | //! C header: [`include/linux/fs.h`](../../../../include/linux/fs.h)
|
6 | 6 |
|
7 | 7 | use core::convert::{TryFrom, TryInto};
|
8 |
| -use core::{marker, mem, ops::Deref, pin::Pin, ptr}; |
9 |
| - |
10 |
| -use alloc::boxed::Box; |
11 |
| -use alloc::sync::Arc; |
| 8 | +use core::{marker, mem, ptr}; |
12 | 9 |
|
13 | 10 | use crate::bindings;
|
14 | 11 | use crate::c_types;
|
15 | 12 | use crate::error::{Error, KernelResult};
|
16 |
| -use crate::sync::{CondVar, Ref, RefCounted}; |
| 13 | +use crate::sync::CondVar; |
| 14 | +use crate::types::PointerWrapper; |
17 | 15 | use crate::user_ptr::{UserSlicePtr, UserSlicePtrReader, UserSlicePtrWriter};
|
18 | 16 |
|
19 | 17 | /// Wraps the kernel's `struct file`.
|
@@ -605,64 +603,3 @@ pub trait FileOperations: Send + Sync + Sized {
|
605 | 603 | Ok(bindings::POLLIN | bindings::POLLOUT | bindings::POLLRDNORM | bindings::POLLWRNORM)
|
606 | 604 | }
|
607 | 605 | }
|
608 |
| - |
609 |
| -/// Used to convert an object into a raw pointer that represents it. |
610 |
| -/// |
611 |
| -/// It can eventually be converted back into the object. This is used to store objects as pointers |
612 |
| -/// in kernel data structures, for example, an implementation of [`FileOperations`] in `struct |
613 |
| -/// file::private_data`. |
614 |
| -pub trait PointerWrapper<T> { |
615 |
| - /// Returns the raw pointer. |
616 |
| - fn into_pointer(self) -> *const T; |
617 |
| - |
618 |
| - /// Returns the instance back from the raw pointer. |
619 |
| - /// |
620 |
| - /// # Safety |
621 |
| - /// |
622 |
| - /// The passed pointer must come from a previous call to [`PointerWrapper::into_pointer()`]. |
623 |
| - unsafe fn from_pointer(ptr: *const T) -> Self; |
624 |
| -} |
625 |
| - |
626 |
| -impl<T> PointerWrapper<T> for Box<T> { |
627 |
| - fn into_pointer(self) -> *const T { |
628 |
| - Box::into_raw(self) |
629 |
| - } |
630 |
| - |
631 |
| - unsafe fn from_pointer(ptr: *const T) -> Self { |
632 |
| - Box::from_raw(ptr as _) |
633 |
| - } |
634 |
| -} |
635 |
| - |
636 |
| -impl<T: RefCounted> PointerWrapper<T> for Ref<T> { |
637 |
| - fn into_pointer(self) -> *const T { |
638 |
| - Ref::into_raw(self) |
639 |
| - } |
640 |
| - |
641 |
| - unsafe fn from_pointer(ptr: *const T) -> Self { |
642 |
| - Ref::from_raw(ptr as _) |
643 |
| - } |
644 |
| -} |
645 |
| - |
646 |
| -impl<T> PointerWrapper<T> for Arc<T> { |
647 |
| - fn into_pointer(self) -> *const T { |
648 |
| - Arc::into_raw(self) |
649 |
| - } |
650 |
| - |
651 |
| - unsafe fn from_pointer(ptr: *const T) -> Self { |
652 |
| - Arc::from_raw(ptr) |
653 |
| - } |
654 |
| -} |
655 |
| - |
656 |
| -impl<T, W: PointerWrapper<T> + Deref> PointerWrapper<T> for Pin<W> { |
657 |
| - fn into_pointer(self) -> *const T { |
658 |
| - // SAFETY: We continue to treat the pointer as pinned by returning just a pointer to it to |
659 |
| - // the caller. |
660 |
| - let inner = unsafe { Pin::into_inner_unchecked(self) }; |
661 |
| - inner.into_pointer() |
662 |
| - } |
663 |
| - |
664 |
| - unsafe fn from_pointer(p: *const T) -> Self { |
665 |
| - // SAFETY: The object was originally pinned. |
666 |
| - Pin::new_unchecked(W::from_pointer(p)) |
667 |
| - } |
668 |
| -} |
0 commit comments