Skip to content

Commit 72cca90

Browse files
committed
kernel: simplify type in Allocator
Rather than use an option and wait for something to use the allocator to panic, just panic if an allocator is created for a nonexistent process.
1 parent 06c03ca commit 72cca90

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

kernel/src/grant.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,13 @@ impl<T> AppliedGrant<T> {
2626
F: FnOnce(&mut Owned<T>, &mut Allocator) -> R,
2727
R: Copy,
2828
{
29+
let proc = unsafe {
30+
process::PROCS[self.appid]
31+
.as_mut()
32+
.expect("Request to allocate in nonexistent app")
33+
};
2934
let mut allocator = Allocator {
30-
app: unsafe { process::PROCS[self.appid].as_mut() },
35+
app: proc,
3136
app_id: self.appid,
3237
};
3338
let mut root = unsafe { Owned::new(self.grant, self.appid) };
@@ -36,7 +41,7 @@ impl<T> AppliedGrant<T> {
3641
}
3742

3843
pub struct Allocator<'a> {
39-
app: Option<&'a mut &'a mut process::Process<'a>>,
44+
app: &'a mut process::Process<'a>,
4045
app_id: usize,
4146
}
4247

@@ -90,18 +95,13 @@ impl Allocator<'a> {
9095
pub fn alloc<T>(&mut self, data: T) -> Result<Owned<T>, Error> {
9196
unsafe {
9297
let app_id = self.app_id;
93-
match self.app.as_mut() {
94-
Some(app) => app
95-
.alloc(size_of::<T>())
96-
.map_or(Err(Error::OutOfMemory), |arr| {
97-
let mut owned = Owned::new(arr.as_mut_ptr() as *mut T, app_id);
98-
*owned = data;
99-
Ok(owned)
100-
}),
101-
None => {
102-
panic!("Request to allocate in kernel grant");
103-
}
104-
}
98+
self.app
99+
.alloc(size_of::<T>())
100+
.map_or(Err(Error::OutOfMemory), |arr| {
101+
let mut owned = Owned::new(arr.as_mut_ptr() as *mut T, app_id);
102+
*owned = data;
103+
Ok(owned)
104+
})
105105
}
106106
}
107107
}
@@ -181,7 +181,7 @@ impl<T: Default> Grant<T> {
181181
move |root_ptr| {
182182
let mut root = Borrowed::new(&mut *root_ptr, app_id);
183183
let mut allocator = Allocator {
184-
app: Some(app),
184+
app: app,
185185
app_id: app_id,
186186
};
187187
let res = fun(&mut root, &mut allocator);

0 commit comments

Comments
 (0)