调整分配器成员:

移除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` * ````使`allocator`
* *
* *
* `kernel/memm/allocator/` * `kernel/memm/allocator/`
* *
* **** * ****
* *
* **`memm_allocate_t`** * **`memm_allocate_t`**
@ -78,7 +78,11 @@ typedef void (*memm_free_t)(void *allocator, void *mem);
* ****`MEMM_ALLOC_ONLY_MEMORY``` * ****`MEMM_ALLOC_ONLY_MEMORY```
* *
* ****使 * ****使
* *
* @internal magic
*
* `MEMM_ALLOCATOR_MAGIC_NUM`
*
* @internal full * @internal full
* *
* `allocate``nullptr``true` * `allocate``nullptr``true`
@ -88,6 +92,10 @@ typedef void (*memm_free_t)(void *allocator, void *mem);
* *
* 0 * 0
* *
* @internal userspace
*
*
*
* @internal type * @internal type
* *
* `include/kernel/memm/allocator` * `include/kernel/memm/allocator`
@ -107,17 +115,21 @@ typedef void (*memm_free_t)(void *allocator, void *mem);
*/ */
typedef struct __allocator_t 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; u32 magic;
bool initialized;
// 调用分配器的`allocate`方法后,在返回`nullptr`时会设为`true`。 // 调用分配器的`allocate`方法后,在返回`nullptr`时会设为`true`。
// 调用分配器的`free`方法时设为`false`。 // 调用分配器的`free`方法时设为`false`。
bool full; bool full;
// 进程标志服表示此分配器所属的进程为0代表属于内核。 // 进程标志服表示此分配器所属的进程为0代表属于内核。
usize pid; usize pid;
// 若分配器不属于内核,此成员储存此分配器的用户空间地址。
void *userspace;
// 分配器类型。在目录`include/kernel/memm/allocator`中对每个分配器分别定义一个唯一值。 // 分配器类型。在目录`include/kernel/memm/allocator`中对每个分配器分别定义一个唯一值。
usize type; usize type;
usize size; usize size;
@ -290,18 +302,18 @@ void *memm_user_allocate(usize size, usize pid);
/** /**
* @name memm_free * @name memm_free
* *
* ```c * ```c
* void memm_free(void *mem); * void memm_free(void *mem);
* ``` * ```
* *
* *
*/ */
void memm_free(void *mem); void memm_free(void *mem);
/** /**
* @name find_fitable_pages * @name find_fitable_pages
* *
* ```c * ```c
* usize find_fitable_pages(usize page_count); * 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 *memm_allocator_new(void *start, usize length, usize type, usize pid)
{ {
allocator_t *allocator = start; allocator_t *allocator = start;
allocator->magic = MEMM_ALLOCATOR_MAGIC_NUM; allocator->magic = MEMM_ALLOCATOR_MAGIC;
allocator->initialized = true;
allocator->full = false; allocator->full = false;
allocator->pid = 0; allocator->pid = 0;
allocator->size = length; 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; allocator->free = (memm_free_t)raw_allocator_free;
break; break;
default: default:
allocator->initialized = false; allocator->magic = 0;
break; break;
} }
} }
void memm_allocator_destruct(allocator_t *allocator) void memm_allocator_destruct(allocator_t *allocator)
{ {
allocator->initialized = false; allocator->magic = 0;
// TODO 从分配器树中删除这个分配器 // TODO 从分配器树中删除这个分配器
KERNEL_TODO(); KERNEL_TODO();
} }
@ -226,7 +225,7 @@ void *memm_user_allocate(usize size, usize pid)
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);
if (allocator->magic != MEMM_ALLOCATOR_MAGIC_NUM) if (allocator->magic != MEMM_ALLOCATOR_MAGIC)
return; return;
if (is_user_address((u64)mem)) if (is_user_address((u64)mem))
{ // TODO 对于用户空间的地址需要先转换到内核地址后释放 { // TODO 对于用户空间的地址需要先转换到内核地址后释放