-
-
Save eddyb/7886876 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub unsafe fn allocMegaGroup(mblocks: nat) -> ~Block { | |
let n = MBLOCK_GROUP_BLOCKS(mblocks); | |
enum SearchResult<'a> { | |
PerfectMatch(~Block), | |
BestMatch(&'a mut BlockData, &'a mut BlockMeta), | |
NoMatch | |
} | |
fn go<'a>(n: uint, | |
prev_link: &'a mut Option<~Block>, | |
best: Option<(&'a mut BlockData, &'a mut BlockMeta)>) | |
-> SearchResult<'a> { | |
match prev_link { | |
&None => match best { | |
None => NoMatch, | |
Some((data, meta)) => BestMatch(data, meta) | |
}, | |
&Some(_) if bd.meta.blocks as uint == n => PerfectMatch(prev_link.take().unwrap()), | |
&Some(ref mut bd) => if (bd.meta.blocks as uint > n && match best { | |
None => true, | |
Some((_, ref best_meta)) => bd.meta.blocks < best_meta.blocks | |
}) { | |
go(n, &mut bd.link.p, Some((&mut bd.data, &mut bd.meta))) | |
} else { | |
go(n, &mut bd.link.p, best) | |
} | |
} | |
} | |
match go(n, &mut free_mblock_list.p, None) { | |
// We found a perfect match and removed it from the list | |
PerfectMatch(p) => return p, | |
// Take our chunk off the end of the best match | |
BestMatch(_, _) => fail!("ni"), | |
// Nothing in the free list was big enough, so allocate | |
NoMatch => fail!("ni"), | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment