给allocator增加魔数,用于在调用free时确认内存所属allocator是否是一个有效的allocator

This commit is contained in:
pointer-to-bios 2024-02-13 02:01:23 +08:00
parent 7e22dd604f
commit 197c50ff5d
3 changed files with 7 additions and 3 deletions

View File

@ -32,6 +32,9 @@ typedef void (*memm_free_t)(void *allocator, void *mem);
*/ */
typedef struct __allocator_t typedef struct __allocator_t
{ {
#define MEMM_ALLOCATOR_MAGIC_NUM 0x271fe441
u32 magic;
bool initialized; bool initialized;
// 在本分配器中调用allocate返回nullptr后为true // 在本分配器中调用allocate返回nullptr后为true
@ -47,7 +50,6 @@ typedef struct __allocator_t
// 分配器实例的allocate函数 // 分配器实例的allocate函数
// 无法分配空间返回nullptr // 无法分配空间返回nullptr
// 在size参数为0时保证不可以分配空间但是如果空间已满依然返回nullptr // 在size参数为0时保证不可以分配空间但是如果空间已满依然返回nullptr
// 当参数align=0时表示不需要对齐
memm_allocate_t allocate; memm_allocate_t allocate;
// 分配器实例的free函数 // 分配器实例的free函数
@ -129,7 +131,6 @@ void memm_allocator_destruct(allocator_t *allocator);
/* /*
使
pid=0 pid=0
线 线

View File

@ -36,7 +36,7 @@ typedef struct __raw_allocator_t
void raw_allocator_new(raw_allocator_t *allocator, usize size); 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); void raw_allocator_free(raw_allocator_t *allocator, void *mem);
#endif #endif

View File

@ -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 *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->initialized = true; allocator->initialized = true;
allocator->full = false; allocator->full = false;
allocator->pid = 0; allocator->pid = 0;
@ -220,6 +221,8 @@ 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)
return;
if (is_user_address((u64)mem)) if (is_user_address((u64)mem))
{ // TODO 对于用户空间的地址需要先转换到内核地址后释放 { // TODO 对于用户空间的地址需要先转换到内核地址后释放
} }