官方文档翻译-ESP32-Heap Memory Allocation

堆内存分配

概述

The ESP32 has multiple types of RAM. Internally, there’s IRAM, DRAM as well as RAM that can be used as both. It’s also possible to connect external SPI RAM to the ESP32 - external RAM can be integrated into the ESP32’s memory map using the flash cache.
ESP32有多种类型的RAM.在内部,有IRAM,DRAM以及可用作前两者的RAM.也可以将外部SPI RAM连接到ESP32 - 使用闪存缓存可将外部RAM集成到ESP32的存储器映射中.

For most purposes, the standard libc malloc() and free() functions can be used for heap allocation without any issues.
对于大多数用途而言,标准的libc malloc()free()函数可以用于堆分配而不会有任何问题.

However, in order to fully make use of all of the memory types and their characteristics, esp-idf also has a capabilities-based heap memory allocator. If you want to have memory with certain properties (for example, DMA-capable memory, or executable memory), you can create an OR-mask of the required capabilities and pass that to heap_caps_malloc(). For instance, the standard malloc() implementation internally allocates memory via heap_caps_malloc(size, MALLOC_CAP_8BIT) in order to get data memory that is byte-addressable.
但是,为了充分利用所有内存类型及其特性,esp-idf还具有基于功能的堆内存分配器.如果您想拥有某些属性的内存(例如,支持DMA的内存或可执行内存),则可以创建所需功能的OR掩码并将其传递给 heap_caps_malloc().例如,标准库的malloc()在内部的实现是通过heap_caps_malloc(size, MALLOC_CAP_8BIT)实现获取字节可寻址(byte-addressable)的内存的.

Because malloc uses this allocation system as well, memory allocated using heap_caps_malloc() can be freed by calling the standard free() function.
因为malloc也使用这个分配系统,所以用heap_caps_malloc()分配的内存可以通过调用标准free()函数来释放.

The “soc” component contains a list of memory regions for the chip, along with the type of each memory (aka its tag) and the associated capabilities for that memory type. On startup, a separate heap is initialised for each contiguous memory region. The capabilities-based allocator chooses the best heap for each allocation, based on the requested capabilities.
“soc”组件包含芯片的存储器区域列表,以及每个存储器的类型(也就是其标签)以及该存储器类型的关联功能.在启动时,为每个连续的内存区域初始化一个单独的堆.根据基于功能的分配器的请求能力为每个分配选择最佳的堆.

特殊用途

DMA存储器

Use the MALLOC_CAP_DMA flag to allocate memory which is suitable for use with hardware DMA engines (for example SPI and I2S). This capability flag excludes any external PSRAM.
使用 MALLOC_CAP_DMA 标志分配适用于硬件DMA控制器(例如SPI和I2S)的内存.此功能标志不包括任何外部PSRAM.

32位可访问内存

If a certain memory structure is only addressed in 32-bit units, for example an array of ints or pointers, it can be useful to allocate it with the MALLOC_CAP_32BIT flag. This also allows the allocator to give out IRAM memory; something which it can’t do for a normal malloc() call. This can help to use all the available memory in the ESP32.
如果某个存储器结构仅以32位为单位寻址,例如一个整数或指针数组,则可以使用 MALLOC_CAP_32BIT 标志来分配它.这也允许分配器给一些不能使用malloc()的地方分配IRAM内存.这可以帮助使用ESP32中的所有可用内存.

Memory allocated with MALLOC_CAP_32BIT can only be accessed via 32-bit reads and writes, any other type of access will generate a fatal LoadStoreError exception.
分配有 MALLOC_CAP_32BIT 的内存只能通过32位读写访问,任何其他类型的访问都将生成致命的LoadStoreError异常.

API参考 - 堆分配

头文件

  • heap/include/esp_heap_caps.h

本文翻译自:https://esp-idf.readthedocs.io/en/latest/api-reference/system/mem_alloc.html
翻译水平有限,如有错误欢迎指正

你可能感兴趣的:(嵌入式,esp32,单片机)