mem_alloc.rst 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. Memory allocation
  2. =================
  3. Overview
  4. --------
  5. 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
  6. possible to connect external SPI flash to the ESP32; it's memory can be integrated into the ESP32s memory map using
  7. the flash cache.
  8. In order to make use of all this memory, esp-idf has a capabilities-based memory allocator. Basically, if you want to have
  9. memory with certain properties (for example, DMA-capable, accessible by a certain PID, or capable of executing code), you
  10. can create an OR-mask of the required capabilities and pass that to pvPortMallocCaps. For instance, the normal malloc
  11. code internally allocates memory with ```pvPortMallocCaps(size, MALLOC_CAP_8BIT)``` in order to get data memory that is
  12. byte-addressable.
  13. Because malloc uses this allocation system as well, memory allocated using pvPortMallocCaps can be freed by calling
  14. the standard ```free()``` function.
  15. Internally, this allocator is split in two pieces. The allocator in the FreeRTOS directory can allocate memory from
  16. tagged regions: a tag is an integer value and every region of free memory has one of these tags. The esp32-specific
  17. code initializes these regions with specific tags, and contains the logic to select applicable tags from the
  18. capabilities given by the user. While shown in the public API, tags are used in the communication between the two parts
  19. and should not be used directly.
  20. Special Uses
  21. ------------
  22. If a certain memory structure is only addressed in 32-bit units, for example an array of ints or pointers, it can be
  23. useful to allocate it with the MALLOC_CAP_32BIT flag. This also allows the allocator to give out IRAM memory; something
  24. which it can't do for a normal malloc() call. This can help to use all the available memory in the ESP32.
  25. API Reference - Heap Allocation
  26. -------------------------------
  27. .. include:: /_build/inc/esp_heap_alloc_caps.inc
  28. API Reference - Heap Regions
  29. ----------------------------
  30. .. include:: /_build/inc/heap_regions.inc