forked from metaverse/kernel-dev
优化内存管理模块的内存分配函数
This commit is contained in:
parent
3cd40e00e8
commit
831c7a689f
|
@ -143,6 +143,10 @@ void *memm_allocate(usize size, usize pid);
|
||||||
#define memm_addr_get_allocator(mem) \
|
#define memm_addr_get_allocator(mem) \
|
||||||
((*(allocator_t **)(mem - 16)))
|
((*(allocator_t **)(mem - 16)))
|
||||||
|
|
||||||
|
void *memm_kernel_allocate(usize size);
|
||||||
|
|
||||||
|
void *memm_user_allocate(usize size, usize pid);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
释放内存
|
释放内存
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -192,7 +192,7 @@ void *memm_allocate(usize size, usize pid)
|
||||||
MEMM_RAW_ALLOCATOR, pid);
|
MEMM_RAW_ALLOCATOR, pid);
|
||||||
allocator = new_allocator;
|
allocator = new_allocator;
|
||||||
|
|
||||||
allocator_iterator_t *allind = memm_allocate(sizeof(allocator_iterator_t), 0);
|
allocator_iterator_t *allind = memm_kernel_allocate(sizeof(allocator_iterator_t));
|
||||||
allind->allocator = new_allocator;
|
allind->allocator = new_allocator;
|
||||||
allind->left = nullptr;
|
allind->left = nullptr;
|
||||||
allind->right = nullptr;
|
allind->right = nullptr;
|
||||||
|
@ -201,14 +201,21 @@ void *memm_allocate(usize size, usize pid)
|
||||||
|
|
||||||
after_allocation:
|
after_allocation:
|
||||||
memm_addr_set_allocator(ptr, allocator);
|
memm_addr_set_allocator(ptr, allocator);
|
||||||
if (pid != 0)
|
|
||||||
{ // TODO 进程管理中应该有一个用户地址-内核地址映射表
|
|
||||||
// 在进程分配时将页映射到用户空间中,并将这个映射关系记录进这个表中
|
|
||||||
// 需要返回的是用户空间的地址
|
|
||||||
}
|
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void *memm_kernel_allocate(usize size)
|
||||||
|
{
|
||||||
|
return memm_allocate(size, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void *memm_user_allocate(usize size, usize pid)
|
||||||
|
{
|
||||||
|
void *res = memm_allocate(size, pid);
|
||||||
|
// TODO 将内存空间映射到用户空间
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
void memm_free(void *mem)
|
void memm_free(void *mem)
|
||||||
{
|
{
|
||||||
allocator_t *allocator = memm_addr_get_allocator(mem);
|
allocator_t *allocator = memm_addr_get_allocator(mem);
|
||||||
|
|
|
@ -3,7 +3,8 @@ extern crate core;
|
||||||
use core::alloc::{GlobalAlloc, Layout};
|
use core::alloc::{GlobalAlloc, Layout};
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
pub fn memm_allocate(size: usize, pid: usize) -> *mut u8;
|
pub fn memm_kernel_allocate(size: usize) -> *mut u8;
|
||||||
|
pub fn memm_user_allocate(size: usize, pid: usize);
|
||||||
pub fn memm_free(mem: *mut u8);
|
pub fn memm_free(mem: *mut u8);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,7 +12,7 @@ pub struct KernelAllocator {}
|
||||||
|
|
||||||
unsafe impl GlobalAlloc for KernelAllocator {
|
unsafe impl GlobalAlloc for KernelAllocator {
|
||||||
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
|
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
|
||||||
memm_allocate(layout.size(), 0)
|
memm_kernel_allocate(layout.size())
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
|
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
|
|
||||||
lst_iterator_t *lst_new(usize start, usize end)
|
lst_iterator_t *lst_new(usize start, usize end)
|
||||||
{
|
{
|
||||||
lst_iterator_t *lst = memm_allocate(sizeof(lst_iterator_t), 0);
|
lst_iterator_t *lst = memm_kernel_allocate(sizeof(lst_iterator_t), 0);
|
||||||
lst->line.left = start;
|
lst->line.left = start;
|
||||||
lst->line.right = end;
|
lst->line.right = end;
|
||||||
lst->next = nullptr;
|
lst->next = nullptr;
|
||||||
|
@ -128,7 +128,7 @@ bool lst_add(lst_iterator_t *lst, usize left, usize right, bool force)
|
||||||
lst->line.left = left;
|
lst->line.left = left;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
lst_iterator_t *new_node = memm_allocate(sizeof(lst_iterator_t), 0);
|
lst_iterator_t *new_node = memm_kernel_allocate(sizeof(lst_iterator_t));
|
||||||
new_node->line = line;
|
new_node->line = line;
|
||||||
new_node->next = lst;
|
new_node->next = lst;
|
||||||
if (last != nullptr)
|
if (last != nullptr)
|
||||||
|
|
Loading…
Reference in New Issue