调整分配器成员:

移除initialized,增加userspace
This commit is contained in:
pointer-to-bios 2024-02-19 01:47:16 +08:00
parent aedcb8749e
commit 2aaa592636
2 changed files with 25 additions and 14 deletions

View File

@ -35,9 +35,9 @@
*
* ````使`allocator`
*
*
*
* `kernel/memm/allocator/`
*
*
* ****
*
* **`memm_allocate_t`**
@ -78,7 +78,11 @@ typedef void (*memm_free_t)(void *allocator, void *mem);
* ****`MEMM_ALLOC_ONLY_MEMORY```
*
* ****使
*
*
* @internal magic
*
* `MEMM_ALLOCATOR_MAGIC_NUM`
*
* @internal full
*
* `allocate``nullptr``true`
@ -88,6 +92,10 @@ typedef void (*memm_free_t)(void *allocator, void *mem);
*
* 0
*
* @internal userspace
*
*
*
* @internal type
*
* `include/kernel/memm/allocator`
@ -107,17 +115,21 @@ typedef void (*memm_free_t)(void *allocator, void *mem);
*/
typedef struct __allocator_t
{
#define MEMM_ALLOCATOR_MAGIC_NUM 0x271fe441
#ifndef MEMM_ALLOCATOR_MAGIC
#define MEMM_ALLOCATOR_MAGIC 0x271fe441
#endif
// 分配器有效性由此检验,不为`MEMM_ALLOCATOR_MAGIC_NUM`说明获得了一个错误的分配器地址。
// 此值在编译时通过各种方式确定,若
u32 magic;
bool initialized;
// 调用分配器的`allocate`方法后,在返回`nullptr`时会设为`true`。
// 调用分配器的`free`方法时设为`false`。
bool full;
// 进程标志服表示此分配器所属的进程为0代表属于内核。
usize pid;
// 若分配器不属于内核,此成员储存此分配器的用户空间地址。
void *userspace;
// 分配器类型。在目录`include/kernel/memm/allocator`中对每个分配器分别定义一个唯一值。
usize type;
usize size;
@ -290,18 +302,18 @@ void *memm_user_allocate(usize size, usize pid);
/**
* @name memm_free
*
*
* ```c
* void memm_free(void *mem);
* ```
*
*
*
*/
void memm_free(void *mem);
/**
* @name find_fitable_pages
*
*
* ```c
* usize find_fitable_pages(usize page_count);
* ```

View File

@ -77,8 +77,7 @@ memory_manager_t *memm_get_manager()
allocator_t *memm_allocator_new(void *start, usize length, usize type, usize pid)
{
allocator_t *allocator = start;
allocator->magic = MEMM_ALLOCATOR_MAGIC_NUM;
allocator->initialized = true;
allocator->magic = MEMM_ALLOCATOR_MAGIC;
allocator->full = false;
allocator->pid = 0;
allocator->size = length;
@ -91,14 +90,14 @@ allocator_t *memm_allocator_new(void *start, usize length, usize type, usize pid
allocator->free = (memm_free_t)raw_allocator_free;
break;
default:
allocator->initialized = false;
allocator->magic = 0;
break;
}
}
void memm_allocator_destruct(allocator_t *allocator)
{
allocator->initialized = false;
allocator->magic = 0;
// TODO 从分配器树中删除这个分配器
KERNEL_TODO();
}
@ -226,7 +225,7 @@ void *memm_user_allocate(usize size, usize pid)
void memm_free(void *mem)
{
allocator_t *allocator = memm_addr_get_allocator(mem);
if (allocator->magic != MEMM_ALLOCATOR_MAGIC_NUM)
if (allocator->magic != MEMM_ALLOCATOR_MAGIC)
return;
if (is_user_address((u64)mem))
{ // TODO 对于用户空间的地址需要先转换到内核地址后释放