|
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}; |
| 8 | +use core::{marker, mem, ptr}; |
9 | 9 |
|
10 | 10 | use alloc::boxed::Box; |
11 | | -use alloc::sync::Arc; |
12 | 11 |
|
13 | 12 | use crate::{ |
14 | 13 | bindings, c_types, |
15 | 14 | error::{Error, KernelResult}, |
16 | 15 | file::File, |
17 | 16 | io_buffer::{IoBufferReader, IoBufferWriter}, |
18 | 17 | iov_iter::IovIter, |
19 | | - sync::{CondVar, Ref, RefCounted}, |
| 18 | + sync::CondVar, |
| 19 | + types::PointerWrapper, |
20 | 20 | user_ptr::{UserSlicePtr, UserSlicePtrReader, UserSlicePtrWriter}, |
21 | 21 | }; |
22 | 22 |
|
@@ -555,7 +555,7 @@ pub trait FileOperations: Send + Sync + Sized { |
555 | 555 | const TO_USE: ToUse; |
556 | 556 |
|
557 | 557 | /// The pointer type that will be used to hold ourselves. |
558 | | - type Wrapper: PointerWrapper<Self> = Box<Self>; |
| 558 | + type Wrapper: PointerWrapper = Box<Self>; |
559 | 559 |
|
560 | 560 | /// Cleans up after the last reference to the file goes away. |
561 | 561 | /// |
@@ -633,64 +633,3 @@ pub trait FileOperations: Send + Sync + Sized { |
633 | 633 | Ok(bindings::POLLIN | bindings::POLLOUT | bindings::POLLRDNORM | bindings::POLLWRNORM) |
634 | 634 | } |
635 | 635 | } |
636 | | - |
637 | | -/// Used to convert an object into a raw pointer that represents it. |
638 | | -/// |
639 | | -/// It can eventually be converted back into the object. This is used to store objects as pointers |
640 | | -/// in kernel data structures, for example, an implementation of [`FileOperations`] in `struct |
641 | | -/// file::private_data`. |
642 | | -pub trait PointerWrapper<T> { |
643 | | - /// Returns the raw pointer. |
644 | | - fn into_pointer(self) -> *const T; |
645 | | - |
646 | | - /// Returns the instance back from the raw pointer. |
647 | | - /// |
648 | | - /// # Safety |
649 | | - /// |
650 | | - /// The passed pointer must come from a previous call to [`PointerWrapper::into_pointer()`]. |
651 | | - unsafe fn from_pointer(ptr: *const T) -> Self; |
652 | | -} |
653 | | - |
654 | | -impl<T> PointerWrapper<T> for Box<T> { |
655 | | - fn into_pointer(self) -> *const T { |
656 | | - Box::into_raw(self) |
657 | | - } |
658 | | - |
659 | | - unsafe fn from_pointer(ptr: *const T) -> Self { |
660 | | - Box::from_raw(ptr as _) |
661 | | - } |
662 | | -} |
663 | | - |
664 | | -impl<T: RefCounted> PointerWrapper<T> for Ref<T> { |
665 | | - fn into_pointer(self) -> *const T { |
666 | | - Ref::into_raw(self) |
667 | | - } |
668 | | - |
669 | | - unsafe fn from_pointer(ptr: *const T) -> Self { |
670 | | - Ref::from_raw(ptr as _) |
671 | | - } |
672 | | -} |
673 | | - |
674 | | -impl<T> PointerWrapper<T> for Arc<T> { |
675 | | - fn into_pointer(self) -> *const T { |
676 | | - Arc::into_raw(self) |
677 | | - } |
678 | | - |
679 | | - unsafe fn from_pointer(ptr: *const T) -> Self { |
680 | | - Arc::from_raw(ptr) |
681 | | - } |
682 | | -} |
683 | | - |
684 | | -impl<T, W: PointerWrapper<T> + Deref> PointerWrapper<T> for Pin<W> { |
685 | | - fn into_pointer(self) -> *const T { |
686 | | - // SAFETY: We continue to treat the pointer as pinned by returning just a pointer to it to |
687 | | - // the caller. |
688 | | - let inner = unsafe { Pin::into_inner_unchecked(self) }; |
689 | | - inner.into_pointer() |
690 | | - } |
691 | | - |
692 | | - unsafe fn from_pointer(p: *const T) -> Self { |
693 | | - // SAFETY: The object was originally pinned. |
694 | | - Pin::new_unchecked(W::from_pointer(p)) |
695 | | - } |
696 | | -} |
0 commit comments