2023-12-13 02:24:25 +08:00
|
|
|
|
#ifndef RAW_H
|
|
|
|
|
#define RAW_H 1
|
|
|
|
|
|
|
|
|
|
#include <types.h>
|
2024-01-16 21:38:05 +08:00
|
|
|
|
#include <kernel/memm.h>
|
2023-12-13 02:24:25 +08:00
|
|
|
|
|
|
|
|
|
#define MEMM_RAW_ALLOCATOR 1
|
|
|
|
|
|
|
|
|
|
typedef struct __raw_allocator_cell
|
|
|
|
|
{
|
2024-01-16 21:38:05 +08:00
|
|
|
|
usize capacity; // 是content的长度
|
|
|
|
|
usize length; // 是实际使用的长度
|
2023-12-13 02:24:25 +08:00
|
|
|
|
u8 content[0];
|
|
|
|
|
} raw_allocator_cell;
|
2024-01-15 19:58:01 +08:00
|
|
|
|
#define raw_allocator_next_cell(cell) (raw_allocator_cell *)((void *)((cell)->content) + (cell)->capacity)
|
2023-12-13 02:24:25 +08:00
|
|
|
|
|
2024-02-18 03:53:54 +08:00
|
|
|
|
/**
|
|
|
|
|
* @name raw_allocator_t
|
|
|
|
|
*
|
|
|
|
|
* 原始分配器。包括至少一个cell,分配时像cell的分裂一样将空白的一段分成两段,释放时,只把length归零,并不将cell合并。
|
|
|
|
|
*
|
|
|
|
|
* `length`为0的cell称为空cell。
|
|
|
|
|
*
|
|
|
|
|
* 统计从上次细胞合并以来free的调用次数,当调用次数达到`RAW_ALLOCATOR_FREE_MAX`或无可用空间时触发细胞合并。
|
|
|
|
|
*
|
2024-03-01 01:47:06 +08:00
|
|
|
|
* 使用建议:只在少量allocate和free的情况下使用。使用大量allocate时效率低下并难以得到内存安全保证。
|
|
|
|
|
*
|
2024-02-18 03:53:54 +08:00
|
|
|
|
* @internal free_count
|
|
|
|
|
*
|
|
|
|
|
* free方法的调用次数,达到`RAW_ALLOCATOR_FREE_MAX`时归零。
|
|
|
|
|
*/
|
2023-12-13 02:24:25 +08:00
|
|
|
|
typedef struct __raw_allocator_t
|
|
|
|
|
{
|
2024-01-15 19:58:01 +08:00
|
|
|
|
usize size;
|
|
|
|
|
usize free_count; // free调用计数
|
|
|
|
|
#define RAW_ALLOCATOR_FREE_MAX 64
|
|
|
|
|
usize rest_memory;
|
2023-12-13 02:24:25 +08:00
|
|
|
|
raw_allocator_cell cells[0];
|
|
|
|
|
} raw_allocator_t;
|
2024-01-15 19:58:01 +08:00
|
|
|
|
#define raw_allocator_end(allocator) ((void *)(allocator) + (allocator)->size)
|
2023-12-13 02:24:25 +08:00
|
|
|
|
|
2024-02-18 03:53:54 +08:00
|
|
|
|
/**
|
|
|
|
|
* @name raw_allocator_new
|
|
|
|
|
*
|
|
|
|
|
* ```c
|
|
|
|
|
* void raw_allocator_new(raw_allocator_t *allocator, usize size);
|
|
|
|
|
* ```
|
|
|
|
|
*
|
|
|
|
|
* 初始化一个`raw_allocator`。
|
|
|
|
|
*/
|
2023-12-13 02:24:25 +08:00
|
|
|
|
void raw_allocator_new(raw_allocator_t *allocator, usize size);
|
|
|
|
|
|
2024-02-18 03:53:54 +08:00
|
|
|
|
/**
|
|
|
|
|
* @name raw_allocator_allocate, raw_allocator_free
|
|
|
|
|
*
|
|
|
|
|
* `raw_allocator`的一对allocate, free方法。
|
|
|
|
|
*/
|
2024-02-13 02:01:23 +08:00
|
|
|
|
void *raw_allocator_allocate(raw_allocator_t *allocator, usize size);
|
2023-12-13 02:24:25 +08:00
|
|
|
|
void raw_allocator_free(raw_allocator_t *allocator, void *mem);
|
|
|
|
|
|
|
|
|
|
#endif
|