优化内存分配函数的调用方法

This commit is contained in:
pointer-to-bios 2024-01-16 21:38:05 +08:00
parent 85e0d52783
commit b5a65fb341
7 changed files with 25 additions and 31 deletions

View File

@ -64,9 +64,6 @@ typedef struct __allocator_iterator_t
{ {
allocator_t *allocator; allocator_t *allocator;
struct __allocator_iterator_t *left, *right; struct __allocator_iterator_t *left, *right;
// 这个节点的内存空间处于的那个分配器
allocator_t *owned_allocator;
} allocator_iterator_t; } allocator_iterator_t;
/* /*
@ -137,13 +134,19 @@ pid=0时为内核分配
线 线
allocator对象在进程与内核之间传递时一律使用内核空间的映射地址 allocator对象在进程与内核之间传递时一律使用内核空间的映射地址
## 要求在返回的地址前16字节处保留8字节空间作为它所在的allocator地址并且不需要分配器的具体实现做这个事
*/ */
void *memm_allocate(usize size, usize pid, allocator_t **allocator); void *memm_allocate(usize size, usize pid);
#define memm_addr_set_allocator(mem, allocator) \
*(allocator_t **)(mem - 16) = allocator;
#define memm_addr_get_allocator(mem) \
((*(allocator_t **)(mem - 16)))
/* /*
*/ */
void memm_free(allocator_t *allocator, void *mem); void memm_free(void *mem);
/* /*

View File

@ -2,6 +2,7 @@
#define RAW_H 1 #define RAW_H 1
#include <types.h> #include <types.h>
#include <kernel/memm.h>
#define MEMM_RAW_ALLOCATOR 1 #define MEMM_RAW_ALLOCATOR 1
@ -9,6 +10,8 @@ typedef struct __raw_allocator_cell
{ {
usize capacity; // 是content的长度 usize capacity; // 是content的长度
usize length; // 是实际使用的长度 usize length; // 是实际使用的长度
allocator_t *allocator; // 所在的分配器
usize reserved;
u8 content[0]; u8 content[0];
} raw_allocator_cell; } raw_allocator_cell;
#define raw_allocator_next_cell(cell) (raw_allocator_cell *)((void *)((cell)->content) + (cell)->capacity) #define raw_allocator_next_cell(cell) (raw_allocator_cell *)((void *)((cell)->content) + (cell)->capacity)

View File

@ -59,8 +59,6 @@ typedef struct __tty
tty_typeinfo typeinfo; tty_typeinfo typeinfo;
tty_mode mode; tty_mode mode;
tty_text_state text; tty_text_state text;
allocator_t *allocator;
} tty; } tty;
// tty控制器 // tty控制器

View File

@ -16,10 +16,6 @@ typedef struct __lst_line_t
typedef struct __lst_iterator_t typedef struct __lst_iterator_t
{ {
lst_line_t line; lst_line_t line;
// 这个指针是kernel/memm.h中的allocator_t *
// 代表当前lst_line_index_t对象在allocate时使用的allocator
// 用的话会造成递归include
void *allocator;
struct __lst_iterator_t *next; struct __lst_iterator_t *next;
} lst_iterator_t; } lst_iterator_t;

View File

@ -29,7 +29,6 @@ mem_manager_t *memm_new(usize mem_size)
alcatr_ind->allocator = allocator0; alcatr_ind->allocator = allocator0;
alcatr_ind->left = nullptr; alcatr_ind->left = nullptr;
alcatr_ind->right = nullptr; alcatr_ind->right = nullptr;
alcatr_ind->owned_allocator = allocator0;
memory_manager.allocators = alcatr_ind; memory_manager.allocators = alcatr_ind;
@ -159,11 +158,12 @@ static void insert_allocator(allocator_iterator_t *iter, allocator_iterator_t *i
} }
} }
void *memm_allocate(usize size, usize pid, allocator_t **allocator) void *memm_allocate(usize size, usize pid)
{ {
usize orgsize = size; usize orgsize = size;
// 从分配器树中分配内存 // 从分配器树中分配内存
void *ptr = memm_find_and_allocate(memory_manager.allocators, size, pid, allocator); allocator_t *allocator;
void *ptr = memm_find_and_allocate(memory_manager.allocators, size, pid, &allocator);
if (ptr != nullptr) if (ptr != nullptr)
goto after_allocation; goto after_allocation;
@ -190,11 +190,9 @@ void *memm_allocate(usize size, usize pid, allocator_t **allocator)
allocator_t *new_allocator = allocator_t *new_allocator =
memm_allocator_new((void *)(allocator_start * MEMM_PAGE_SIZE), size * MEMM_PAGE_SIZE, memm_allocator_new((void *)(allocator_start * MEMM_PAGE_SIZE), size * MEMM_PAGE_SIZE,
MEMM_RAW_ALLOCATOR, pid); MEMM_RAW_ALLOCATOR, pid);
*allocator = new_allocator; allocator = new_allocator;
allocator_t *all; allocator_iterator_t *allind = memm_allocate(sizeof(allocator_iterator_t), 0);
allocator_iterator_t *allind = memm_allocate(sizeof(allocator_iterator_t), 0, &all);
allind->owned_allocator = all;
allind->allocator = new_allocator; allind->allocator = new_allocator;
allind->left = nullptr; allind->left = nullptr;
allind->right = nullptr; allind->right = nullptr;
@ -202,6 +200,7 @@ void *memm_allocate(usize size, usize pid, allocator_t **allocator)
ptr = new_allocator->allocate(&new_allocator->allocator_instance, orgsize, 0); ptr = new_allocator->allocate(&new_allocator->allocator_instance, orgsize, 0);
after_allocation: after_allocation:
memm_addr_set_allocator(ptr, allocator);
if (pid != 0) if (pid != 0)
{ // TODO 进程管理中应该有一个用户地址-内核地址映射表 { // TODO 进程管理中应该有一个用户地址-内核地址映射表
// 在进程分配时将页映射到用户空间中,并将这个映射关系记录进这个表中 // 在进程分配时将页映射到用户空间中,并将这个映射关系记录进这个表中
@ -210,8 +209,9 @@ after_allocation:
return ptr; return ptr;
} }
void memm_free(allocator_t *allocator, void *mem) void memm_free(void *mem)
{ {
allocator_t *allocator = memm_addr_get_allocator(mem);
if (is_user_address((u64)mem)) if (is_user_address((u64)mem))
{ // TODO 对于用户空间的地址需要先转换到内核地址后释放 { // TODO 对于用户空间的地址需要先转换到内核地址后释放
} }

View File

@ -16,12 +16,10 @@ tty_controller_t *tty_controller_new()
tty *tty_new(tty_type type, tty_mode mode) tty *tty_new(tty_type type, tty_mode mode)
{ {
allocator_t *allocator; tty *__tty = memm_allocate(sizeof(tty), 0);
tty *__tty = memm_allocate(sizeof(tty), 0, &allocator);
memset(__tty, 0, sizeof(tty)); memset(__tty, 0, sizeof(tty));
__tty->type = type; __tty->type = type;
__tty->mode = mode; __tty->mode = mode;
__tty->allocator = allocator;
tty *res = nullptr; tty *res = nullptr;
for (usize i = 0; i < TTY_MAX_NUM; ++i) for (usize i = 0; i < TTY_MAX_NUM; ++i)
{ {

View File

@ -7,11 +7,9 @@
lst_iterator_t *lst_new(usize start, usize end) lst_iterator_t *lst_new(usize start, usize end)
{ {
allocator_t *allocator; lst_iterator_t *lst = memm_allocate(sizeof(lst_iterator_t), 0);
lst_iterator_t *lst = memm_allocate(sizeof(lst_iterator_t), 0, &allocator);
lst->line.left = start; lst->line.left = start;
lst->line.right = end; lst->line.right = end;
lst->allocator = allocator;
lst->next = nullptr; lst->next = nullptr;
return lst; return lst;
} }
@ -130,9 +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
{ {
allocator_t *alloctr; lst_iterator_t *new_node = memm_allocate(sizeof(lst_iterator_t), 0);
lst_iterator_t *new_node = memm_allocate(sizeof(lst_iterator_t), 0, &alloctr);
new_node->allocator = alloctr;
new_node->line = line; new_node->line = line;
new_node->next = lst; new_node->next = lst;
if (last != nullptr) if (last != nullptr)
@ -169,7 +165,7 @@ bool lst_add(lst_iterator_t *lst, usize left, usize right, bool force)
} }
tmplast = *tmpnode; tmplast = *tmpnode;
lst_iterator_t *t = lst_next(tmpnode); lst_iterator_t *t = lst_next(tmpnode);
memm_free(tmpnode->allocator, tmpnode); memm_free(tmpnode);
tmpnode = t; tmpnode = t;
} }
lst->line.right = max(tmplast.line.right, right); lst->line.right = max(tmplast.line.right, right);