mirror of
https://github.com/italicsjenga/agb.git
synced 2025-01-11 17:41:33 +11:00
split blocks
This commit is contained in:
parent
63d880f083
commit
3d269c98fd
|
@ -54,14 +54,32 @@ unsafe impl GlobalAlloc for BlockAllocator {
|
|||
// find a block that this current request fits in
|
||||
let full_layout = Block::either_layout(layout);
|
||||
|
||||
let (block_after_layout, block_after_layout_offset) =
|
||||
full_layout.extend(Layout::new::<Block>()).unwrap();
|
||||
|
||||
{
|
||||
let mut state = self.state.lock();
|
||||
let mut current_block = state.first_free_block;
|
||||
let mut list_ptr = &mut state.first_free_block;
|
||||
while let Some(mut curr) = current_block {
|
||||
let curr_block = curr.as_mut();
|
||||
if curr_block.size >= full_layout.size() {
|
||||
if curr_block.size == full_layout.size() {
|
||||
*list_ptr = curr_block.next;
|
||||
return curr.as_ptr().cast();
|
||||
} else if curr_block.size >= block_after_layout.size() {
|
||||
// can split block
|
||||
let split_block = Block {
|
||||
size: curr_block.size - block_after_layout_offset,
|
||||
next: curr_block.next,
|
||||
};
|
||||
let split_ptr = curr
|
||||
.as_ptr()
|
||||
.cast::<u8>()
|
||||
.add(block_after_layout_offset)
|
||||
.cast();
|
||||
*split_ptr = split_block;
|
||||
*list_ptr = NonNull::new(split_ptr);
|
||||
|
||||
return curr.as_ptr().cast();
|
||||
}
|
||||
current_block = curr_block.next;
|
||||
|
|
Loading…
Reference in a new issue