From 197c50ff5dc5c11638bb18f402d437c77333c043 Mon Sep 17 00:00:00 2001 From: pointer-to-bios Date: Tue, 13 Feb 2024 02:01:23 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=99allocator=E5=A2=9E=E5=8A=A0=E9=AD=94?= =?UTF-8?q?=E6=95=B0=EF=BC=8C=E7=94=A8=E4=BA=8E=E5=9C=A8=E8=B0=83=E7=94=A8?= =?UTF-8?q?free=E6=97=B6=E7=A1=AE=E8=AE=A4=E5=86=85=E5=AD=98=E6=89=80?= =?UTF-8?q?=E5=B1=9Eallocator=E6=98=AF=E5=90=A6=E6=98=AF=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E6=9C=89=E6=95=88=E7=9A=84allocator?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/kernel/memm.h | 5 +++-- include/kernel/memm/allocator/raw.h | 2 +- src/kernel/memm/memm.c | 3 +++ 3 files changed, 7 insertions(+), 3 deletions(-) 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 对于用户空间的地址需要先转换到内核地址后释放 }