xkk6663/mcu_static_pool

0.1.0

Latest
uploaded 6 hours ago
Compile-time static internal-RAM pool for fixed-size objects that must not fragment the heap

Readme

# mcu_static_pool

## 安装

```powershell
idf.py add-dependency "xkk6663/mcu_static_pool^0.1.0"
```

编译期静态内部 RAM 池,解决“内部堆总量够、最大连续块不够”的问题。

## 特性

- 池在 `.bss` 中预留,不经过 `malloc`,不会产生堆碎片
- bump 分配、终生不回收,适合固定尺寸、反复创建/销毁的短任务栈
- 16 B 对齐,满足 FreeRTOS 栈和 esp-dl 缓冲的最低对齐要求
- 分配瞬间用临界区保护,任务间可安全调用

## 使用边界

**不是通用 malloc 替代品。** 只放“必须内部 RAM、尺寸固定、反复创建”的对象;不要放动态尺寸缓冲、模型、图像。每个调用点应缓存返回指针,只申请一次。

## 拖入别的工程

1. 复制 `components/mcu_static_pool/`
2. 声明依赖:
   ```cmake
   idf_component_register(... PRIV_REQUIRES mcu_static_pool)
   ```
3. 容量二选一:
   - `menuconfig` → `MCU_STATIC_POOL_TOTAL`
   - 编译宏 `-DSTATIC_POOL_TOTAL=...`
4. include:
   ```c
   #include "static_pool.h"

   static void *s_stack;
   if (!s_stack) s_stack = static_pool_alloc(4096, "my_task");
   ```

## 平台说明

当前实现依赖 ESP-IDF 的 `esp_log.h` 与 FreeRTOS 的 `portMUX_TYPE`,属于 ESP-IDF/FreeRTOS 组件;裸机项目使用需替换临界区与日志宏。

## 测试

`test/test_static_pool.c` 用 `test/stubs/` 里的最小 `esp_log` / FreeRTOS 临界区 stub,在 host 上验证:

```powershell
gcc -std=c11 -Wall -Wextra `
  -I include -I test/stubs `
  test/test_static_pool.c src/static_pool.c -o $env:TEMP\test_static_pool.exe
& $env:TEMP\test_static_pool.exe
```

覆盖 16 B 对齐、bump 累加、0 字节拒绝、超池容量拒绝和 `used/free` 计数。池不可回收,所以每个进程只跑一轮。

Links

Supports all targets

To add this component to your project, run:

idf.py add-dependency "xkk6663/mcu_static_pool^0.1.0"

download archive

Stats

  • Archive size
    Archive size ~ 7.16 KB
  • Downloaded in total
    Downloaded in total 0 times
  • Downloaded this version
    This version: 0 times

Badge

xkk6663/mcu_static_pool version: 0.1.0
|