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
1
use super :: { BlockDevice , BLOCK_SZ } ;
2
+ use alloc:: boxed:: Box ;
2
3
use alloc:: collections:: VecDeque ;
3
4
use alloc:: sync:: Arc ;
4
- use alloc:: vec ;
5
- use alloc :: vec :: Vec ;
5
+ use core :: alloc:: Layout ;
6
+ use core :: mem :: ManuallyDrop ;
6
7
use core:: ptr:: { addr_of, addr_of_mut} ;
7
8
use core:: slice;
8
9
use lazy_static:: * ;
9
10
use spin:: Mutex ;
10
11
11
- /// use `Vec<u64> ` to ensure the alignment of addr is `8 `
12
- struct CacheData ( Vec < u64 > ) ;
12
+ /// Use `ManuallyDrop ` to ensure data is deallocated with an alignment of `BLOCK_SZ `
13
+ struct CacheData ( ManuallyDrop < Box < [ u8 ; BLOCK_SZ ] > > ) ;
13
14
14
15
impl CacheData {
15
- fn new ( ) -> Self {
16
- Self ( vec ! [ 0u64 ; BLOCK_SZ / 8 ] )
16
+ pub fn new ( ) -> Self {
17
+ let data = unsafe {
18
+ let raw = alloc:: alloc:: alloc ( Self :: layout ( ) ) ;
19
+ Box :: from_raw ( raw as * mut [ u8 ; BLOCK_SZ ] )
20
+ } ;
21
+ Self ( ManuallyDrop :: new ( data) )
22
+ }
23
+
24
+ fn layout ( ) -> Layout {
25
+ Layout :: from_size_align ( BLOCK_SZ , BLOCK_SZ ) . unwrap ( )
26
+ }
27
+ }
28
+
29
+ impl Drop for CacheData {
30
+ fn drop ( & mut self ) {
31
+ let ptr = self . 0 . as_mut_ptr ( ) ;
32
+ unsafe { alloc:: alloc:: dealloc ( ptr, Self :: layout ( ) ) } ;
17
33
}
18
34
}
19
35
You can’t perform that action at this time.
0 commit comments