@@ -25,6 +25,13 @@ use sgx_types::error::OsResult;
25
25
26
26
use super :: alloc:: AllocType ;
27
27
28
+ const BYTE_SIZE : usize = 8 ;
29
+ macro_rules! bytes_num {
30
+ ( $num: expr) => {
31
+ ( $num + BYTE_SIZE - 1 ) / BYTE_SIZE
32
+ } ;
33
+ }
34
+
28
35
#[ repr( C ) ]
29
36
#[ derive( Debug ) ]
30
37
pub struct BitArray {
@@ -37,7 +44,7 @@ pub struct BitArray {
37
44
impl BitArray {
38
45
/// Init BitArray with all zero bits
39
46
pub fn new ( bits : usize , alloc : AllocType ) -> OsResult < Self > {
40
- let bytes = ( bits + 7 ) / 8 ;
47
+ let bytes = bytes_num ! ( bits) ;
41
48
42
49
// FIXME: return error if OOM
43
50
let data = match alloc {
@@ -66,8 +73,8 @@ impl BitArray {
66
73
return Err ( EACCES ) ;
67
74
}
68
75
69
- let byte_index = index / 8 ;
70
- let bit_index = index % 8 ;
76
+ let byte_index = index / BYTE_SIZE ;
77
+ let bit_index = index % BYTE_SIZE ;
71
78
let bit_mask = 1 << bit_index;
72
79
let data = unsafe { core:: slice:: from_raw_parts_mut ( self . data , self . bytes ) } ;
73
80
Ok ( ( data. get ( byte_index) . unwrap ( ) & bit_mask) != 0 )
@@ -88,8 +95,8 @@ impl BitArray {
88
95
if index >= self . bits {
89
96
return Err ( EACCES ) ;
90
97
}
91
- let byte_index = index / 8 ;
92
- let bit_index = index % 8 ;
98
+ let byte_index = index / BYTE_SIZE ;
99
+ let bit_index = index % BYTE_SIZE ;
93
100
let bit_mask = 1 << bit_index;
94
101
95
102
let data = unsafe { core:: slice:: from_raw_parts_mut ( self . data , self . bytes ) } ;
@@ -120,33 +127,33 @@ impl BitArray {
120
127
pub fn split ( & mut self , pos : usize ) -> OsResult < BitArray > {
121
128
assert ! ( pos > 0 && pos < self . bits) ;
122
129
123
- let byte_index = pos / 8 ;
124
- let bit_index = pos % 8 ;
130
+ let byte_index = pos / BYTE_SIZE ;
131
+ let bit_index = pos % BYTE_SIZE ;
125
132
126
- let l_bits = pos;
127
- let l_bytes = ( l_bits + 7 ) / 8 ;
133
+ let lbits = pos;
134
+ let lbytes = bytes_num ! ( lbits ) ;
128
135
129
- let r_bits = self . bits - l_bits ;
130
- let r_bytes = ( r_bits + 7 ) / 8 ;
136
+ let rbits = self . bits - lbits ;
137
+ let rbytes = bytes_num ! ( rbits ) ;
131
138
132
- let r_array = Self :: new ( r_bits , self . alloc ) ?;
139
+ let rarray = Self :: new ( rbits , self . alloc ) ?;
133
140
134
- let r_data = unsafe { core:: slice:: from_raw_parts_mut ( r_array . data , r_array . bytes ) } ;
135
- let l_data = unsafe { core:: slice:: from_raw_parts_mut ( self . data , self . bytes ) } ;
141
+ let rdata = unsafe { core:: slice:: from_raw_parts_mut ( rarray . data , rarray . bytes ) } ;
142
+ let ldata = unsafe { core:: slice:: from_raw_parts_mut ( self . data , self . bytes ) } ;
136
143
137
- for ( idx, item) in r_data [ ..( r_bytes - 1 ) ] . iter_mut ( ) . enumerate ( ) {
144
+ for ( idx, item) in rdata [ ..( rbytes - 1 ) ] . iter_mut ( ) . enumerate ( ) {
138
145
// current byte index in previous bit_array
139
146
let curr_idx = idx + byte_index;
140
- let low_bits = l_data [ curr_idx] >> bit_index;
141
- let high_bits = l_data [ curr_idx + 1 ] << ( 8 - bit_index) ;
147
+ let low_bits = ldata [ curr_idx] >> bit_index;
148
+ let high_bits = ldata [ curr_idx + 1 ] << ( 8 - bit_index) ;
142
149
* item = high_bits | low_bits;
143
150
}
144
- r_data [ r_bytes - 1 ] = l_data [ self . bytes - 1 ] >> bit_index;
151
+ rdata [ rbytes - 1 ] = ldata [ self . bytes - 1 ] >> bit_index;
145
152
146
- self . bits = l_bits ;
147
- self . bytes = l_bytes ;
153
+ self . bits = lbits ;
154
+ self . bytes = lbytes ;
148
155
149
- Ok ( r_array )
156
+ Ok ( rarray )
150
157
}
151
158
}
152
159
0 commit comments