kernel-dev/include/kernel/memm.h

235 lines
6.2 KiB
C
Raw Normal View History

2023-12-13 02:24:25 +08:00
#ifndef MEMM_H
#define MEMM_H 1
#ifdef __x86_64__
#include <kernel/arch/x86_64/memm.h>
#endif
#include <libk/lst.h>
/**
* @name MEMM_MAX_SUPPORTED_MEMORY
2023-12-13 02:24:25 +08:00
*
* `1TB`
2023-12-13 02:24:25 +08:00
*/
#define MEMM_MAX_SUPPORTED_MEMORY (1024 * (1024 * (1024 * (usize)1024)))
/**
* @name MEMM_MAX_SUPPORTED_PAGES
*
* `MEMM_MAX_SUPPORTED_MEMORY``MEMM_PAGE_SIZE`
*/
2023-12-13 02:24:25 +08:00
#define MEMM_MAX_SUPPORTED_PAGES (MEMM_MAX_SUPPORTED_MEMORY / MEMM_PAGE_SIZE)
/**
* @name MEMM_ALLOC_ONLY_MEMORY
*
2024-04-06 04:07:32 +08:00
* `64MB``0`\~`MEMM_ALLOC_ONLY_MEMORY`
*
* **1MB以内低地址******
*/
2024-04-06 04:07:32 +08:00
#define MEMM_ALLOC_ONLY_MEMORY (64 * 1024 * 1024)
#define MEMM_PAGE_TABLE_AREA_MAX (4 * 1024 * 1024)
2023-12-13 02:24:25 +08:00
/**
* @name memm_allocate_t, memm_free_t
*
* ````使`allocator`
*
*
* `kernel/memm/allocator/`
*
* ****
*
* **`memm_allocate_t`**
*
* ```c
* typedef void *(*memm_allocate_t)(void *allocator, usize size);
* ```
*
* `allocator``size`
*
* `nullptr``size`0`nullptr`
* **16**8
*
* **`memm_free_t`**
*
* ```c
* typedef void (*memm_free_t)(void *allocator, void *mem);
* ```
*
* `allocator``mem`
*
* `mem``allocator`
*/
typedef void *(*memm_allocate_t)(void *allocator, usize size);
2024-01-14 15:45:35 +08:00
typedef void (*memm_free_t)(void *allocator, void *mem);
/**
* @name allocator_t
*
* ```c
* typedef struct { } allocator_t;
* ```
*
*
*
* `memm_allocate`
*
* ****`MEMM_ALLOC_ONLY_MEMORY```
*
* ****使
*
* @internal magic
*
* `MEMM_ALLOCATOR_MAGIC_NUM`
*
* @internal full
*
* `allocate``nullptr``true`
* `free``false`
*
* @internal pid
*
* 0
*
* @internal userspace
*
*
*
* @internal type
*
* `include/kernel/memm/allocator`
*
* @internal allocate
*
* allocate函数nullptrsize参数为0时
* nullptr
*
* @internal free
*
* free函数allocate得到的地址则什么都不做
*
* @internal allocator_instance
*
*
2023-12-13 02:24:25 +08:00
*/
typedef struct __allocator_t
{
#ifndef MEMM_ALLOCATOR_MAGIC
2024-02-26 01:58:49 +08:00
#define MEMM_ALLOCATOR_MAGIC ((u32)0x271fe441)
#endif
// 分配器有效性由此检验,不为`MEMM_ALLOCATOR_MAGIC_NUM`说明获得了一个错误的分配器地址。
// 此值在编译时通过各种方式确定,若
u32 magic;
// 调用分配器的`allocate`方法后,在返回`nullptr`时会设为`true`。
// 调用分配器的`free`方法时设为`false`。
2023-12-13 02:24:25 +08:00
bool full;
// 分配器类型。在目录`include/kernel/memm/allocator`中对每个分配器分别定义一个唯一值。
2023-12-13 02:24:25 +08:00
usize type;
usize size;
// 分配器实例的allocate函数。当无法分配空间时返回nullptr。在size参数为0时
// 保证不可以分配空间但是如果空间已满依然返回nullptr。
2024-01-14 15:45:35 +08:00
memm_allocate_t allocate;
// 分配器实例的free函数。若不是allocate得到的地址则什么都不做。
2024-01-14 15:45:35 +08:00
memm_free_t free;
2023-12-13 02:24:25 +08:00
// 分配器实例。
2023-12-13 02:24:25 +08:00
u64 allocator_instance[0];
} allocator_t;
/**
* @name
*
* @internal alloc_only_memory
*
*
*/
2023-12-13 02:24:25 +08:00
typedef struct __mem_manager_t
{
usize memory_size;
usize page_amount;
usize alloc_only_memory;
2024-04-06 04:07:32 +08:00
allocator_t *kernel_base_allocator;
2023-12-13 02:24:25 +08:00
2024-04-06 04:07:32 +08:00
usize page_table_area;
} memory_manager_t;
2023-12-13 02:24:25 +08:00
/**
* @name memm_new
*
* ```c
* memory_manager_t *memm_new(usize mem_size);
* ```
*
*
*/
memory_manager_t *memm_new(usize mem_size);
2023-12-13 02:24:25 +08:00
/**
* @name memm_get_manager
*
* ```c
* memory_manager_t *memm_get_manager();
* ```
*
*
2023-12-13 02:24:25 +08:00
*/
memory_manager_t *memm_get_manager();
2023-12-13 02:24:25 +08:00
/**
* @name memm_allocator_new
*
* ```c
* allocator_t *memm_allocator_new(void *start, usize length, usize type, usize pid);
* ```
*
* `start``length``pid`0使
*/
allocator_t *memm_allocator_new(void *start, usize length, usize type, usize pid);
2023-12-13 02:24:25 +08:00
/**
* @name memm_allocator_destruct
*
* ```c
* void memm_allocator_destruct(allocator_t *allocator);
* ```
*
*
*
* ****
2023-12-13 02:24:25 +08:00
*/
void memm_allocator_destruct(allocator_t *allocator);
/**
* @name memm_kernel_allocate
*
* ```c
* void *memm_kernel_allocate(usize size);
* ```
*
*
*/
void *memm_kernel_allocate(usize size);
/**
* @name memm_free
*
* ```c
* void memm_free(void *mem);
* ```
*
*
2023-12-13 02:24:25 +08:00
*/
void memm_free(void *mem);
2023-12-13 02:24:25 +08:00
2024-04-06 04:07:32 +08:00
void *memm_allcate_pagetable();
2023-12-13 02:24:25 +08:00
#endif