1
- use alloc:: { string:: String , vec, vec:: Vec } ;
2
- use core:: ops:: Range ;
1
+ use core:: {
2
+ fmt:: { Display , Formatter , Result } ,
3
+ ops:: Range ,
4
+ } ;
3
5
4
- # [ derive ( Debug ) ]
6
+ /// 从设备树采集的板信息。
5
7
pub ( crate ) struct BoardInfo {
6
8
pub dtb : Range < usize > ,
7
- pub model : String ,
9
+ pub model : StringInline < 128 > ,
8
10
pub smp : usize ,
9
- pub mem : Vec < Range < usize > > ,
11
+ pub mem : Range < usize > ,
10
12
pub uart : Range < usize > ,
11
13
pub test : Range < usize > ,
12
14
pub clint : Range < usize > ,
13
15
}
14
16
17
+ /// 在栈上存储有限长度字符串。
18
+ pub ( crate ) struct StringInline < const N : usize > ( usize , [ u8 ; N ] ) ;
19
+
20
+ impl < const N : usize > Display for StringInline < N > {
21
+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> Result {
22
+ write ! ( f, "{}" , unsafe {
23
+ core:: str :: from_utf8_unchecked( & self . 1 [ ..self . 0 ] )
24
+ } )
25
+ }
26
+ }
27
+
28
+ /// 解析设备树。
15
29
pub ( crate ) fn parse ( opaque : usize ) -> BoardInfo {
16
30
use dtb_walker:: { Dtb , DtbObj , Property , WalkOperation :: * } ;
17
31
const CPUS : & [ u8 ] = b"cpus" ;
@@ -23,9 +37,9 @@ pub(crate) fn parse(opaque: usize) -> BoardInfo {
23
37
24
38
let mut ans = BoardInfo {
25
39
dtb : opaque..opaque,
26
- model : String :: new ( ) ,
40
+ model : StringInline ( 0 , [ 0u8 ; 128 ] ) ,
27
41
smp : 0 ,
28
- mem : vec ! [ ] ,
42
+ mem : 0 .. 0 ,
29
43
uart : 0 ..0 ,
30
44
test : 0 ..0 ,
31
45
clint : 0 ..0 ,
@@ -55,9 +69,8 @@ pub(crate) fn parse(opaque: usize) -> BoardInfo {
55
69
}
56
70
}
57
71
DtbObj :: Property ( Property :: Model ( model) ) if path. last ( ) . is_empty ( ) => {
58
- if let Ok ( model) = model. as_str ( ) {
59
- ans. model = model. into ( ) ;
60
- }
72
+ ans. model . 0 = model. as_bytes ( ) . len ( ) ;
73
+ ans. model . 1 [ ..ans. model . 0 ] . copy_from_slice ( model. as_bytes ( ) ) ;
61
74
StepOver
62
75
}
63
76
DtbObj :: Property ( Property :: Reg ( mut reg) ) => {
@@ -72,7 +85,7 @@ pub(crate) fn parse(opaque: usize) -> BoardInfo {
72
85
ans. clint = reg. next ( ) . unwrap ( ) ;
73
86
StepOut
74
87
} else if node. starts_with ( MEMORY ) {
75
- ans. mem = reg. into_iter ( ) . collect ( ) ;
88
+ ans. mem = reg. next ( ) . unwrap ( ) ;
76
89
StepOut
77
90
} else {
78
91
StepOver
0 commit comments