forked from haileys/rustboot
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Heap. Allocation with type arguments; overflow
Created kernel::heap. Allocation functions take one type parameter. Used `mul_with_overflow` to safely allocate arrays (compare calloc.)
- Loading branch information
Showing
6 changed files
with
86 additions
and
71 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use core::fail::out_of_memory; | ||
use core::mem::size_of; | ||
use core::uint::mul_with_overflow; | ||
|
||
use kernel::memory; | ||
use kernel::memory::Allocator; | ||
|
||
pub static mut heap: memory::Alloc = memory::Alloc { | ||
base: 0x110_000 as *mut u8, | ||
el_size: 0, | ||
parent: memory::BuddyAlloc { | ||
order: 17, | ||
tree: memory::Bitv { storage: 0x100_000 as memory::BitvStorage } | ||
} | ||
}; | ||
|
||
pub fn init() { | ||
unsafe { | ||
heap.parent.tree.init(); | ||
} | ||
} | ||
|
||
#[lang = "exchange_malloc"] | ||
#[inline] | ||
pub unsafe fn malloc_raw(size: uint) -> *mut u8 { | ||
match heap.alloc(size) { | ||
(_, 0) => out_of_memory(), | ||
(ptr, _) => ptr | ||
} | ||
} | ||
|
||
#[lang = "exchange_free"] | ||
#[inline] | ||
pub unsafe fn free<T>(ptr: *mut T) { | ||
heap.free(ptr as *mut u8); | ||
} | ||
|
||
#[inline] | ||
pub unsafe fn alloc<T = u8>(count: uint) -> *mut T { | ||
match mul_with_overflow(count, size_of::<T>()) { | ||
(_, true) => out_of_memory(), | ||
(size, _) => malloc_raw(size) as *mut T | ||
} | ||
} | ||
|
||
#[inline] | ||
pub unsafe fn zero_alloc<T = u8>(count: uint) -> *mut T { | ||
match mul_with_overflow(count, size_of::<T>()) { | ||
(_, true) => out_of_memory(), | ||
(size, _) => match heap.zero_alloc(size) { | ||
(_, 0) => out_of_memory(), | ||
(ptr, _) => ptr as *mut T | ||
} | ||
} | ||
} | ||
|
||
#[inline] | ||
pub unsafe fn realloc_raw<T>(ptr: *mut T, count: uint) -> *mut T { | ||
match mul_with_overflow(count, size_of::<T>()) { | ||
(_, true) => out_of_memory(), | ||
(0, _) => { | ||
free(ptr); | ||
0 as *mut T | ||
} | ||
(size, _) => match heap.realloc(ptr as *mut u8, size) { | ||
(_, 0) => out_of_memory(), | ||
(ptr, _) => ptr as *mut T | ||
} | ||
} | ||
} |
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
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
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
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
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