mirror of
https://github.com/iceHtwoO/novaOS.git
synced 2026-04-17 04:32:27 +00:00
Re organize
This commit is contained in:
19
src/heap.rs
19
src/heap.rs
@@ -99,7 +99,7 @@ impl Heap {
|
|||||||
|
|
||||||
pub fn malloc(&self, mut size: usize) -> Result<*mut u8, NovaError> {
|
pub fn malloc(&self, mut size: usize) -> Result<*mut u8, NovaError> {
|
||||||
if size == 0 {
|
if size == 0 {
|
||||||
return Err(NovaError::EmptyHeapNotAllowed);
|
return Err(NovaError::EmptyHeapSegmentNotAllowed);
|
||||||
}
|
}
|
||||||
|
|
||||||
if size < MIN_BLOCK_SIZE {
|
if size < MIN_BLOCK_SIZE {
|
||||||
@@ -120,6 +120,15 @@ impl Heap {
|
|||||||
return Ok(current.byte_add(HEAP_HEADER_SIZE) as *mut u8);
|
return Ok(current.byte_add(HEAP_HEADER_SIZE) as *mut u8);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Self::fragment_segment(current, size);
|
||||||
|
|
||||||
|
let data_start_address = current.byte_add(HEAP_HEADER_SIZE);
|
||||||
|
|
||||||
|
Ok(data_start_address as *mut u8)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe fn fragment_segment(current: *mut HeapHeader, size: usize) {
|
||||||
let byte_offset = HEAP_HEADER_SIZE + size;
|
let byte_offset = HEAP_HEADER_SIZE + size;
|
||||||
let new_address = current.byte_add(byte_offset);
|
let new_address = current.byte_add(byte_offset);
|
||||||
|
|
||||||
@@ -141,11 +150,6 @@ impl Heap {
|
|||||||
(*current).next = new_address;
|
(*current).next = new_address;
|
||||||
(*current).free = false;
|
(*current).free = false;
|
||||||
(*current).size = size;
|
(*current).size = size;
|
||||||
|
|
||||||
let data_start_address = current.byte_add(HEAP_HEADER_SIZE);
|
|
||||||
|
|
||||||
Ok(data_start_address as *mut u8)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn free(&self, pointer: *mut u8) -> Result<(), NovaError> {
|
pub fn free(&self, pointer: *mut u8) -> Result<(), NovaError> {
|
||||||
@@ -167,6 +171,7 @@ impl Heap {
|
|||||||
(*segment).size += (*next_head).size + HEAP_HEADER_SIZE;
|
(*segment).size += (*next_head).size + HEAP_HEADER_SIZE;
|
||||||
delete_header(next_head);
|
delete_header(next_head);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Neither: Set free
|
// Neither: Set free
|
||||||
(*segment).free = true;
|
(*segment).free = true;
|
||||||
}
|
}
|
||||||
@@ -182,7 +187,7 @@ impl Heap {
|
|||||||
println!("free: {}", head.free);
|
println!("free: {}", head.free);
|
||||||
println!("size: {}", head.size);
|
println!("size: {}", head.size);
|
||||||
println!("hasNext: {}", !head.next.is_null());
|
println!("hasNext: {}", !head.next.is_null());
|
||||||
println!();
|
println!("");
|
||||||
if !head.next.is_null() {
|
if !head.next.is_null() {
|
||||||
pointer_address = head.next;
|
pointer_address = head.next;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -40,5 +40,5 @@ pub fn mmio_write(address: u32, data: u32) {
|
|||||||
pub enum NovaError {
|
pub enum NovaError {
|
||||||
Mailbox,
|
Mailbox,
|
||||||
HeapFull,
|
HeapFull,
|
||||||
EmptyHeapNotAllowed,
|
EmptyHeapSegmentNotAllowed,
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user