- 2.4 内存管理
- 2.4.1 动态内存
- 概述
- API讲解
- 编程实例
- 运行效果
- 2.4.2 静态内存
- 概述
- API讲解
- 编程实例
- 运行效果
- 2.4.1 动态内存
2.4 内存管理
2.4.1 动态内存
概述
动态内存管理模块,提供了一套动态管理系统内存的机制,支持用户动态的申请、释放不定长内存块。
API讲解
编程实例
1、在tos_config.h中,配置动态内存组件开关TOS_CFG_MMHEAP_EN:
#define TOS_CFG_MMHEAP_EN 1u
2、在tos_config.h中,配置动态内存池大小:
#define TOS_CFG_MMHEAP_POOL_SIZE 0x2000
3、编写main.c示例代码:
- #include "tos.h"
- #include "mcu_init.h"
- #define STK_SIZE_TASK_DEMO 512
- k_stack_t stack_task_demo[STK_SIZE_TASK_DEMO];
- k_task_t task_demo;
- extern void entry_task_demo(void *arg);
- void entry_task_demo(void *arg)
- {
- void *p = K_NULL, *p_aligned = K_NULL;
- int i = 0;
- while (K_TRUE) {
- if (i == 1) {
- p = tos_mmheap_alloc(0x30);
- if (p) {
- printf("alloc: %x\n", (cpu_addr_t)p);
- }
- } else if (i == 2) {
- if (p) {
- printf("free: %x\n", p);
- tos_mmheap_free(p);
- }
- } else if (i == 3) {
- p = tos_mmheap_alloc(0x30);
- if (p) {
- printf("alloc: %x\n", (cpu_addr_t)p);
- }
- } else if (i == 4) {
- p_aligned = tos_mmheap_aligned_alloc(0x50, 16);
- if (p_aligned) {
- printf("aligned alloc: %x\n", (cpu_addr_t)p_aligned);
- if ((cpu_addr_t)p_aligned % 16 == 0) {
- printf("%x is 16 aligned\n", (cpu_addr_t)p_aligned);
- } else {
- printf("should not happen\n");
- }
- }
- } else if (i == 5) {
- p = tos_mmheap_realloc(p, 0x40);
- if (p) {
- printf("realloc: %x\n", (cpu_addr_t)p);
- }
- } else if (i == 6) {
- if (p) {
- tos_mmheap_free(p);
- }
- if (p_aligned) {
- tos_mmheap_free(p_aligned);
- }
- }
- tos_task_delay(1000);
- ++i;
- }
- }
- int main(void)
- {
- board_init();
- tos_knl_init();
- (void)tos_task_create(&task_demo, "receiver_higher_prio", entry_task_demo, NULL,
- 4, stack_task_demo, STK_SIZE_TASK_DEMO, 0);
- tos_knl_start();
- }
运行效果
alloc: 20000c8cfree: 20000c8calloc: 20000c8caligned alloc: 20000cc020000cc0 is 16 alignedrealloc: 20000d14
实例代码
2.4.2 静态内存
概述
静态内存管理模块,提供了一套管理静态内存块的机制,支持用户申请、释放定长的内存块。
API讲解
创建静态内存池接口:
- k_err_t tos_mmblk_pool_create(k_mmblk_pool_t *mbp, void *pool_start, size_t blk_num, size_t blk_size);
这里详细讲解此api参数意义:
- mbp
静态内存池句柄。
- pool_start
静态内存池起始地址。
- blk_num
内存池将要划分的内存块个数。
- blk_size
每个内存块的大小。
编程实例
1、在tos_config.h中,配置静态内存组件开关TOS_CFG_MMBLK_EN:
#define TOS_CFG_MMBLK_EN 1u
2、编写main.c示例代码:
- #include "tos.h"
- #include "mcu_init.h"
- #define STK_SIZE_TASK_DEMO 512
- k_stack_t stack_task_demo[STK_SIZE_TASK_DEMO];
- k_task_t task_demo;
- #define MMBLK_BLK_NUM 5
- #define MMBLK_BLK_SIZE 0x20
- k_mmblk_pool_t mmblk_pool;
- // 需要管理的静态内存池
- uint8_t mmblk_pool_buffer[MMBLK_BLK_NUM * MMBLK_BLK_SIZE];
- // 记录从内存池中分配到的地址
- void *p[MMBLK_BLK_NUM] = { K_NULL };
- extern void entry_task_demo(void *arg);
- void entry_task_demo(void *arg)
- {
- void *p_dummy = K_NULL;
- k_err_t err;
- int i = 0;
- printf("mmblk_pool has %d blocks, size of each block is 0x%x\n", 5, 0x20);
- // 从内存池中获取所有的block
- for (; i < MMBLK_BLK_NUM; ++i) {
- err = tos_mmblk_alloc(&mmblk_pool, &p[i]);
- if (err == K_ERR_NONE) {
- printf("%d block alloced: 0x%x\n", i, p[i]);
- } else {
- printf("should not happen\n");
- }
- }
- // 前文逻辑已经将所有可用block分配完毕,继续分配会返回K_ERR_MMBLK_POOL_EMPTY错误码
- err = tos_mmblk_alloc(&mmblk_pool, &p_dummy);
- if (err == K_ERR_MMBLK_POOL_EMPTY) {
- printf("blocks exhausted, all blocks is alloced\n");
- } else {
- printf("should not happen\n");
- }
- // 将前文分配得到的所有block归还到池中
- for (i = 0; i < MMBLK_BLK_NUM; ++i) {
- err = tos_mmblk_free(&mmblk_pool, p[i]);
- if (err != K_ERR_NONE) {
- printf("should not happen\n");
- }
- }
- // 前文的归还动作中已经将所有的block归还到池中,继续规范会返回K_ERR_MMBLK_POOL_FULL错误码
- err = tos_mmblk_free(&mmblk_pool, p[0]);
- if (err == K_ERR_MMBLK_POOL_FULL) {
- printf("pool is full\n");
- } else {
- printf("should not happen\n");
- }
- }
- int main(void)
- {
- board_init();
- tos_knl_init();
- // 创建静态内存池
- tos_mmblk_pool_create(&mmblk_pool, mmblk_pool_buffer, MMBLK_BLK_NUM, MMBLK_BLK_SIZE);
- (void)tos_task_create(&task_demo, "receiver_higher_prio", entry_task_demo, NULL,
- 4, stack_task_demo, STK_SIZE_TASK_DEMO, 0);
- tos_knl_start();
- }
运行效果
mmblk_pool has 5 blocks, size of each block is 0x200 block alloced: 0x200009741 block alloced: 0x200009942 block alloced: 0x200009b43 block alloced: 0x200009d44 block alloced: 0x200009f4blocks exhausted, all blocks is allocedpool is full
实例代码