File tree Expand file tree Collapse file tree 1 file changed +22
-6
lines changed Expand file tree Collapse file tree 1 file changed +22
-6
lines changed Original file line number Diff line number Diff line change 1+ use core:: alloc:: Layout ;
2+ use core:: mem:: ManuallyDrop ;
13use core:: ptr:: { addr_of, addr_of_mut} ;
24use core:: slice;
35
46use super :: { BlockDevice , BLOCK_SZ } ;
7+ use alloc:: boxed:: Box ;
58use alloc:: collections:: VecDeque ;
69use alloc:: sync:: Arc ;
7- use alloc:: vec;
8- use alloc:: vec:: Vec ;
910use lazy_static:: * ;
1011use spin:: Mutex ;
1112
12- /// use `Vec<u64> ` to ensure the alignment of addr is `8 `
13- struct CacheData ( Vec < u64 > ) ;
13+ /// Use `ManuallyDrop ` to ensure data is deallocated with an alignment of `BLOCK_SZ `
14+ struct CacheData ( ManuallyDrop < Box < [ u8 ; BLOCK_SZ ] > > ) ;
1415
1516impl CacheData {
16- fn new ( ) -> Self {
17- Self ( vec ! [ 0u64 ; BLOCK_SZ / 8 ] )
17+ pub fn new ( ) -> Self {
18+ let data = unsafe {
19+ let raw = alloc:: alloc:: alloc ( Self :: layout ( ) ) ;
20+ Box :: from_raw ( raw as * mut [ u8 ; BLOCK_SZ ] )
21+ } ;
22+ Self ( ManuallyDrop :: new ( data) )
23+ }
24+
25+ fn layout ( ) -> Layout {
26+ Layout :: from_size_align ( BLOCK_SZ , BLOCK_SZ ) . unwrap ( )
27+ }
28+ }
29+
30+ impl Drop for CacheData {
31+ fn drop ( & mut self ) {
32+ let ptr = self . 0 . as_mut_ptr ( ) ;
33+ unsafe { alloc:: alloc:: dealloc ( ptr, Self :: layout ( ) ) } ;
1834 }
1935}
2036
You can’t perform that action at this time.
0 commit comments