diff --git a/include/kernel/memm.h b/include/kernel/memm.h index c686151..27557a7 100644 --- a/include/kernel/memm.h +++ b/include/kernel/memm.h @@ -64,9 +64,6 @@ typedef struct __allocator_iterator_t { allocator_t *allocator; struct __allocator_iterator_t *left, *right; - - // 这个节点的内存空间处于的那个分配器 - allocator_t *owned_allocator; } allocator_iterator_t; /* @@ -137,13 +134,19 @@ pid=0时为内核分配 所有内存在内核空间都有对物理内存空间的直接映射,也就是线性地址与物理地址相同,称为内核地址 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); /* 寻找大小合适的一组页 diff --git a/include/kernel/memm/allocator/raw.h b/include/kernel/memm/allocator/raw.h index b2f00b3..96fbe90 100644 --- a/include/kernel/memm/allocator/raw.h +++ b/include/kernel/memm/allocator/raw.h @@ -2,13 +2,16 @@ #define RAW_H 1 #include +#include #define MEMM_RAW_ALLOCATOR 1 typedef struct __raw_allocator_cell { - usize capacity; // 是content的长度 - usize length; // 是实际使用的长度 + usize capacity; // 是content的长度 + usize length; // 是实际使用的长度 + allocator_t *allocator; // 所在的分配器 + usize reserved; u8 content[0]; } raw_allocator_cell; #define raw_allocator_next_cell(cell) (raw_allocator_cell *)((void *)((cell)->content) + (cell)->capacity) diff --git a/include/kernel/tty.h b/include/kernel/tty.h index 5d2ef22..d68689f 100644 --- a/include/kernel/tty.h +++ b/include/kernel/tty.h @@ -59,8 +59,6 @@ typedef struct __tty tty_typeinfo typeinfo; tty_mode mode; tty_text_state text; - - allocator_t *allocator; } tty; // tty控制器 diff --git a/include/libk/lst.h b/include/libk/lst.h index b57a9df..4cc7e4d 100644 --- a/include/libk/lst.h +++ b/include/libk/lst.h @@ -16,10 +16,6 @@ typedef struct __lst_line_t typedef struct __lst_iterator_t { lst_line_t line; - // 这个指针是kernel/memm.h中的allocator_t * - // 代表当前lst_line_index_t对象在allocate时使用的allocator - // 用的话会造成递归include - void *allocator; struct __lst_iterator_t *next; } lst_iterator_t; diff --git a/src/kernel/memm/memm.c b/src/kernel/memm/memm.c index d722fe4..aecfe24 100644 --- a/src/kernel/memm/memm.c +++ b/src/kernel/memm/memm.c @@ -29,7 +29,6 @@ mem_manager_t *memm_new(usize mem_size) alcatr_ind->allocator = allocator0; alcatr_ind->left = nullptr; alcatr_ind->right = nullptr; - alcatr_ind->owned_allocator = allocator0; 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; // 从分配器树中分配内存 - 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) goto after_allocation; @@ -190,11 +190,9 @@ void *memm_allocate(usize size, usize pid, allocator_t **allocator) allocator_t *new_allocator = memm_allocator_new((void *)(allocator_start * MEMM_PAGE_SIZE), size * MEMM_PAGE_SIZE, MEMM_RAW_ALLOCATOR, pid); - *allocator = new_allocator; + allocator = new_allocator; - allocator_t *all; - allocator_iterator_t *allind = memm_allocate(sizeof(allocator_iterator_t), 0, &all); - allind->owned_allocator = all; + allocator_iterator_t *allind = memm_allocate(sizeof(allocator_iterator_t), 0); allind->allocator = new_allocator; allind->left = 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); after_allocation: + memm_addr_set_allocator(ptr, allocator); if (pid != 0) { // TODO 进程管理中应该有一个用户地址-内核地址映射表 // 在进程分配时将页映射到用户空间中,并将这个映射关系记录进这个表中 @@ -210,8 +209,9 @@ after_allocation: 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)) { // TODO 对于用户空间的地址需要先转换到内核地址后释放 } diff --git a/src/kernel/tty/tty.c b/src/kernel/tty/tty.c index 7c208d2..86a1b46 100644 --- a/src/kernel/tty/tty.c +++ b/src/kernel/tty/tty.c @@ -16,12 +16,10 @@ tty_controller_t *tty_controller_new() tty *tty_new(tty_type type, tty_mode mode) { - allocator_t *allocator; - tty *__tty = memm_allocate(sizeof(tty), 0, &allocator); + tty *__tty = memm_allocate(sizeof(tty), 0); memset(__tty, 0, sizeof(tty)); __tty->type = type; __tty->mode = mode; - __tty->allocator = allocator; tty *res = nullptr; for (usize i = 0; i < TTY_MAX_NUM; ++i) { diff --git a/src/libk/lst.c b/src/libk/lst.c index 262ceb9..cbfcce0 100644 --- a/src/libk/lst.c +++ b/src/libk/lst.c @@ -7,11 +7,9 @@ lst_iterator_t *lst_new(usize start, usize end) { - allocator_t *allocator; - lst_iterator_t *lst = memm_allocate(sizeof(lst_iterator_t), 0, &allocator); + lst_iterator_t *lst = memm_allocate(sizeof(lst_iterator_t), 0); lst->line.left = start; lst->line.right = end; - lst->allocator = allocator; lst->next = nullptr; return lst; } @@ -130,9 +128,7 @@ bool lst_add(lst_iterator_t *lst, usize left, usize right, bool force) lst->line.left = left; else { - allocator_t *alloctr; - lst_iterator_t *new_node = memm_allocate(sizeof(lst_iterator_t), 0, &alloctr); - new_node->allocator = alloctr; + lst_iterator_t *new_node = memm_allocate(sizeof(lst_iterator_t), 0); new_node->line = line; new_node->next = lst; if (last != nullptr) @@ -169,7 +165,7 @@ bool lst_add(lst_iterator_t *lst, usize left, usize right, bool force) } tmplast = *tmpnode; lst_iterator_t *t = lst_next(tmpnode); - memm_free(tmpnode->allocator, tmpnode); + memm_free(tmpnode); tmpnode = t; } lst->line.right = max(tmplast.line.right, right);