Skip to content

Commit 05182d2

Browse files
Remove the use of RefCell in File
The previous `RefCell` was only dealing with a pointer and the data was always held by the lfs side so the checking from the refcell was useless anyways This also contains a breaking change because the `unsafe` `open` function did not take the correct lifetime. It think it's an acceptable breaking change because the previous behaviour was buggy and any code that depends on it likely has a use after free.
1 parent b3a7371 commit 05182d2

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/fs.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Experimental Filesystem version using closures.
22
33
use core::ffi::{c_int, c_void};
4+
use core::marker::PhantomData;
45
use core::ptr::addr_of;
56
use core::ptr::addr_of_mut;
67
use core::{
@@ -612,7 +613,8 @@ impl<S: driver::Storage> FileAllocation<S> {
612613
pub struct File<'a, 'b, S: driver::Storage> {
613614
// We must store a raw pointer here since the FFI retains a copy of a pointer
614615
// to the field alloc.state, so we cannot assert unique mutable access.
615-
alloc: RefCell<*mut FileAllocation<S>>,
616+
alloc: *mut FileAllocation<S>,
617+
phantom: PhantomData<&'b mut FileAllocation<S>>,
616618
fs: &'b Filesystem<'a, S>,
617619
}
618620

@@ -687,7 +689,7 @@ impl<'a, 'b, Storage: driver::Storage> File<'a, 'b, Storage> {
687689
// We need to use addr_of_mut! here instead of & mut since
688690
// the FFI stores a copy of a pointer to the field state,
689691
// so we cannot assert unique mutable access.
690-
addr_of_mut!((*(*self.alloc.borrow_mut())).state),
692+
addr_of_mut!((*self.alloc).state),
691693
);
692694
result_from((), return_code)
693695
}
@@ -700,7 +702,7 @@ impl<'a, 'b, Storage: driver::Storage> File<'a, 'b, Storage> {
700702
// so we cannot assert unique mutable access.
701703
ll::lfs_file_sync(
702704
&mut self.fs.alloc.borrow_mut().state,
703-
addr_of_mut!((*(*self.alloc.borrow_mut())).state),
705+
addr_of_mut!((*self.alloc).state),
704706
)
705707
};
706708
result_from((), return_code)
@@ -714,7 +716,7 @@ impl<'a, 'b, Storage: driver::Storage> File<'a, 'b, Storage> {
714716
// so we cannot assert unique mutable access.
715717
ll::lfs_file_size(
716718
&mut self.fs.alloc.borrow_mut().state,
717-
addr_of_mut!((*(*self.alloc.borrow_mut())).state),
719+
addr_of_mut!((*self.alloc).state),
718720
)
719721
};
720722
u32_result(return_code).map(|n| n as usize)
@@ -736,7 +738,7 @@ impl<'a, 'b, Storage: driver::Storage> File<'a, 'b, Storage> {
736738
// so we cannot assert unique mutable access.
737739
ll::lfs_file_truncate(
738740
&mut self.fs.alloc.borrow_mut().state,
739-
addr_of_mut!((*(*self.alloc.borrow_mut())).state),
741+
addr_of_mut!((*self.alloc).state),
740742
size as u32,
741743
)
742744
};
@@ -803,7 +805,7 @@ impl OpenOptions {
803805
pub unsafe fn open<'a, 'b, S: driver::Storage>(
804806
&self,
805807
fs: &'b Filesystem<'a, S>,
806-
alloc: &mut FileAllocation<S>,
808+
alloc: &'b mut FileAllocation<S>,
807809
path: &Path,
808810
) -> Result<File<'a, 'b, S>> {
809811
alloc.config.buffer = alloc.cache.get() as *mut _;
@@ -819,7 +821,8 @@ impl OpenOptions {
819821
);
820822

821823
let file = File {
822-
alloc: RefCell::new(alloc),
824+
alloc,
825+
phantom: PhantomData,
823826
fs,
824827
};
825828

@@ -919,7 +922,7 @@ impl<S: driver::Storage> io::Read for File<'_, '_, S> {
919922
// so we cannot assert unique mutable access.
920923
ll::lfs_file_read(
921924
&mut self.fs.alloc.borrow_mut().state,
922-
addr_of_mut!((*(*self.alloc.borrow_mut())).state),
925+
addr_of_mut!((*self.alloc).state),
923926
buf.as_mut_ptr() as *mut c_void,
924927
buf.len() as u32,
925928
)
@@ -936,7 +939,7 @@ impl<S: driver::Storage> io::Seek for File<'_, '_, S> {
936939
// so we cannot assert unique mutable access.
937940
ll::lfs_file_seek(
938941
&mut self.fs.alloc.borrow_mut().state,
939-
addr_of_mut!((*(*self.alloc.borrow_mut())).state),
942+
addr_of_mut!((*self.alloc).state),
940943
pos.off(),
941944
pos.whence(),
942945
)
@@ -953,7 +956,7 @@ impl<S: driver::Storage> io::Write for File<'_, '_, S> {
953956
// so we cannot assert unique mutable access.
954957
ll::lfs_file_write(
955958
&mut self.fs.alloc.borrow_mut().state,
956-
addr_of_mut!((*(*self.alloc.borrow_mut())).state),
959+
addr_of_mut!((*self.alloc).state),
957960
buf.as_ptr() as *const c_void,
958961
buf.len() as u32,
959962
)

0 commit comments

Comments
 (0)