Skip to content

Commit c88e2bb

Browse files
committed
Refine code
1 parent 99ae008 commit c88e2bb

File tree

5 files changed

+302
-323
lines changed

5 files changed

+302
-323
lines changed

sgx_trts/src/capi.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ use crate::emm::ema::EmaOptions;
2121
use crate::emm::page::AllocFlags;
2222
use crate::emm::pfhandler::PfHandler;
2323
use crate::emm::vmmgr::{
24-
ALLIGNMENT_MASK, ALLIGNMENT_SHIFT, ALLOC_FLAGS_MASK, ALLOC_FLAGS_SHIFT, PAGE_TYPE_MASK,
25-
PAGE_TYPE_SHIFT, RangeType,
24+
RangeType, ALLIGNMENT_MASK, ALLIGNMENT_SHIFT, ALLOC_FLAGS_MASK, ALLOC_FLAGS_SHIFT,
25+
PAGE_TYPE_MASK, PAGE_TYPE_SHIFT,
2626
};
27-
use crate::emm::{mm_commit, mm_uncommit, mm_alloc_user, PageInfo, PageType, ProtFlags, self};
27+
use crate::emm::{self, mm_alloc_user, mm_commit, mm_uncommit, PageInfo, PageType, ProtFlags};
2828
use crate::enclave::{self, is_within_enclave, MmLayout};
2929
use crate::error;
3030
use crate::rand::rand;

sgx_trts/src/emm/alloc.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ const SIZE_MASK: usize = !(EXACT_MATCH_INCREMENT - 1);
5454
static mut STATIC_MEM: [u8; STATIC_MEM_SIZE] = [0; STATIC_MEM_SIZE];
5555

5656
/// Lowest level: Allocator for static memory
57+
///
58+
/// TODO: reimplement static allocator with monotone increasing policies
5759
static STATIC: Once<LockedHeap<32>> = Once::new();
5860

5961
/// Second level: Allocator for reserve memory
@@ -228,7 +230,9 @@ impl BlockUsed {
228230

229231
intrusive_adapter!(BlockFreeAda = UnsafeRef<BlockFree>: BlockFree { link: Link });
230232

231-
/// Interior allocator for reserve memory management,
233+
/// Interior allocator for reserve memory management
234+
///
235+
/// TODO: implement slab allocator mechanism
232236
pub struct Reserve {
233237
exact_blocks: [SinglyLinkedList<BlockFreeAda>; 256],
234238
large_blocks: SinglyLinkedList<BlockFreeAda>,
@@ -391,7 +395,7 @@ impl Reserve {
391395
return Ok(self.block_to_payload(block));
392396
};
393397

394-
// AllocType new block from chunks
398+
// Alloc new block from chunks
395399
block = self.alloc_from_chunks(bsize);
396400
if block.is_none() {
397401
let chunk_size = size_of::<Chunk>();

sgx_trts/src/emm/bitmap.rs

+28-21
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ use sgx_types::error::OsResult;
2525

2626
use super::alloc::AllocType;
2727

28+
const BYTE_SIZE: usize = 8;
29+
macro_rules! bytes_num {
30+
($num:expr) => {
31+
($num + BYTE_SIZE - 1) / BYTE_SIZE
32+
};
33+
}
34+
2835
#[repr(C)]
2936
#[derive(Debug)]
3037
pub struct BitArray {
@@ -37,7 +44,7 @@ pub struct BitArray {
3744
impl BitArray {
3845
/// Init BitArray with all zero bits
3946
pub fn new(bits: usize, alloc: AllocType) -> OsResult<Self> {
40-
let bytes = (bits + 7) / 8;
47+
let bytes = bytes_num!(bits);
4148

4249
// FIXME: return error if OOM
4350
let data = match alloc {
@@ -66,8 +73,8 @@ impl BitArray {
6673
return Err(EACCES);
6774
}
6875

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;
7178
let bit_mask = 1 << bit_index;
7279
let data = unsafe { core::slice::from_raw_parts_mut(self.data, self.bytes) };
7380
Ok((data.get(byte_index).unwrap() & bit_mask) != 0)
@@ -88,8 +95,8 @@ impl BitArray {
8895
if index >= self.bits {
8996
return Err(EACCES);
9097
}
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;
93100
let bit_mask = 1 << bit_index;
94101

95102
let data = unsafe { core::slice::from_raw_parts_mut(self.data, self.bytes) };
@@ -120,33 +127,33 @@ impl BitArray {
120127
pub fn split(&mut self, pos: usize) -> OsResult<BitArray> {
121128
assert!(pos > 0 && pos < self.bits);
122129

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;
125132

126-
let l_bits = pos;
127-
let l_bytes = (l_bits + 7) / 8;
133+
let lbits = pos;
134+
let lbytes = bytes_num!(lbits);
128135

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);
131138

132-
let r_array = Self::new(r_bits, self.alloc)?;
139+
let rarray = Self::new(rbits, self.alloc)?;
133140

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) };
136143

137-
for (idx, item) in r_data[..(r_bytes - 1)].iter_mut().enumerate() {
144+
for (idx, item) in rdata[..(rbytes - 1)].iter_mut().enumerate() {
138145
// current byte index in previous bit_array
139146
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);
142149
*item = high_bits | low_bits;
143150
}
144-
r_data[r_bytes - 1] = l_data[self.bytes - 1] >> bit_index;
151+
rdata[rbytes - 1] = ldata[self.bytes - 1] >> bit_index;
145152

146-
self.bits = l_bits;
147-
self.bytes = l_bytes;
153+
self.bits = lbits;
154+
self.bytes = lbytes;
148155

149-
Ok(r_array)
156+
Ok(rarray)
150157
}
151158
}
152159

0 commit comments

Comments
 (0)