diff --git a/include/kernel/memm.h b/include/kernel/memm.h index 8c40127..432c72c 100644 --- a/include/kernel/memm.h +++ b/include/kernel/memm.h @@ -32,6 +32,9 @@ typedef void (*memm_free_t)(void *allocator, void *mem); */ typedef struct __allocator_t { + #define MEMM_ALLOCATOR_MAGIC_NUM 0x271fe441 + u32 magic; + bool initialized; // 在本分配器中调用allocate返回nullptr后为true @@ -47,7 +50,6 @@ typedef struct __allocator_t // 分配器实例的allocate函数 // 无法分配空间返回nullptr // 在size参数为0时,保证不可以分配空间,但是如果空间已满依然返回nullptr - // 当参数align=0时表示不需要对齐 memm_allocate_t allocate; // 分配器实例的free函数 @@ -129,7 +131,6 @@ void memm_allocator_destruct(allocator_t *allocator); /* 申请内存 -第三个参数也是返回值,表示申请内存使用的分配器 pid=0时为内核分配 所有内存在内核空间都有对物理内存空间的直接映射,也就是线性地址与物理地址相同,称为内核地址 diff --git a/include/kernel/memm/allocator/raw.h b/include/kernel/memm/allocator/raw.h index 96fbe90..9dc1784 100644 --- a/include/kernel/memm/allocator/raw.h +++ b/include/kernel/memm/allocator/raw.h @@ -36,7 +36,7 @@ typedef struct __raw_allocator_t void raw_allocator_new(raw_allocator_t *allocator, usize size); -void *raw_allocator_allocate(raw_allocator_t *allocator, usize size, usize align); +void *raw_allocator_allocate(raw_allocator_t *allocator, usize size); void raw_allocator_free(raw_allocator_t *allocator, void *mem); #endif diff --git a/src/kernel/memm/memm.c b/src/kernel/memm/memm.c index db66100..9afab18 100644 --- a/src/kernel/memm/memm.c +++ b/src/kernel/memm/memm.c @@ -72,6 +72,7 @@ mem_manager_t *memm_new(usize mem_size) 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->full = false; allocator->pid = 0; @@ -220,6 +221,8 @@ 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) + return; if (is_user_address((u64)mem)) { // TODO 对于用户空间的地址需要先转换到内核地址后释放 }