Skip to content

Instantly share code, notes, and snippets.

@eddyb
Forked from ezyang/gist:7886849
Last active December 30, 2015 21:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eddyb/7886876 to your computer and use it in GitHub Desktop.
Save eddyb/7886876 to your computer and use it in GitHub Desktop.
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