esp_heap_caps.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*
  2. * SPDX-FileCopyrightText: 2019-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <stdint.h>
  8. #include <stdlib.h>
  9. #include "multi_heap.h"
  10. #include <sdkconfig.h>
  11. #include "esp_err.h"
  12. #include "esp_attr.h"
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. #if CONFIG_HEAP_PLACE_FUNCTION_INTO_FLASH
  17. #define HEAP_IRAM_ATTR
  18. #else
  19. #define HEAP_IRAM_ATTR IRAM_ATTR
  20. #endif
  21. /**
  22. * @brief Flags to indicate the capabilities of the various memory systems
  23. */
  24. #define MALLOC_CAP_EXEC (1<<0) ///< Memory must be able to run executable code
  25. #define MALLOC_CAP_32BIT (1<<1) ///< Memory must allow for aligned 32-bit data accesses
  26. #define MALLOC_CAP_8BIT (1<<2) ///< Memory must allow for 8/16/...-bit data accesses
  27. #define MALLOC_CAP_DMA (1<<3) ///< Memory must be able to accessed by DMA
  28. #define MALLOC_CAP_PID2 (1<<4) ///< Memory must be mapped to PID2 memory space (PIDs are not currently used)
  29. #define MALLOC_CAP_PID3 (1<<5) ///< Memory must be mapped to PID3 memory space (PIDs are not currently used)
  30. #define MALLOC_CAP_PID4 (1<<6) ///< Memory must be mapped to PID4 memory space (PIDs are not currently used)
  31. #define MALLOC_CAP_PID5 (1<<7) ///< Memory must be mapped to PID5 memory space (PIDs are not currently used)
  32. #define MALLOC_CAP_PID6 (1<<8) ///< Memory must be mapped to PID6 memory space (PIDs are not currently used)
  33. #define MALLOC_CAP_PID7 (1<<9) ///< Memory must be mapped to PID7 memory space (PIDs are not currently used)
  34. #define MALLOC_CAP_SPIRAM (1<<10) ///< Memory must be in SPI RAM
  35. #define MALLOC_CAP_INTERNAL (1<<11) ///< Memory must be internal; specifically it should not disappear when flash/spiram cache is switched off
  36. #define MALLOC_CAP_DEFAULT (1<<12) ///< Memory can be returned in a non-capability-specific memory allocation (e.g. malloc(), calloc()) call
  37. #define MALLOC_CAP_IRAM_8BIT (1<<13) ///< Memory must be in IRAM and allow unaligned access
  38. #define MALLOC_CAP_RETENTION (1<<14) ///< Memory must be able to accessed by retention DMA
  39. #define MALLOC_CAP_RTCRAM (1<<15) ///< Memory must be in RTC fast memory
  40. #define MALLOC_CAP_TCM (1<<16) ///< Memory must be in TCM memory
  41. #define MALLOC_CAP_INVALID (1<<31) ///< Memory can't be used / list end marker
  42. /**
  43. * @brief callback called when an allocation operation fails, if registered
  44. * @param size in bytes of failed allocation
  45. * @param caps capabilities requested of failed allocation
  46. * @param function_name function which generated the failure
  47. */
  48. typedef void (*esp_alloc_failed_hook_t) (size_t size, uint32_t caps, const char * function_name);
  49. /**
  50. * @brief registers a callback function to be invoked if a memory allocation operation fails
  51. * @param callback caller defined callback to be invoked
  52. * @return ESP_OK if callback was registered.
  53. */
  54. esp_err_t heap_caps_register_failed_alloc_callback(esp_alloc_failed_hook_t callback);
  55. #ifdef CONFIG_HEAP_USE_HOOKS
  56. /**
  57. * @brief callback called after every allocation
  58. * @param ptr the allocated memory
  59. * @param size in bytes of the allocation
  60. * @param caps Bitwise OR of MALLOC_CAP_* flags indicating the type of memory allocated.
  61. * @note this hook is called on the same thread as the allocation, which may be within a low level operation.
  62. * You should refrain from doing heavy work, logging, flash writes, or any locking.
  63. */
  64. __attribute__((weak)) HEAP_IRAM_ATTR void esp_heap_trace_alloc_hook(void* ptr, size_t size, uint32_t caps);
  65. /**
  66. * @brief callback called after every free
  67. * @param ptr the memory that was freed
  68. * @note this hook is called on the same thread as the allocation, which may be within a low level operation.
  69. * You should refrain from doing heavy work, logging, flash writes, or any locking.
  70. */
  71. __attribute__((weak)) HEAP_IRAM_ATTR void esp_heap_trace_free_hook(void* ptr);
  72. #endif
  73. /**
  74. * @brief Allocate a chunk of memory which has the given capabilities
  75. *
  76. * Equivalent semantics to libc malloc(), for capability-aware memory.
  77. *
  78. * @param size Size, in bytes, of the amount of memory to allocate
  79. * @param caps Bitwise OR of MALLOC_CAP_* flags indicating the type
  80. * of memory to be returned
  81. *
  82. * @return A pointer to the memory allocated on success, NULL on failure
  83. */
  84. void *heap_caps_malloc(size_t size, uint32_t caps);
  85. /**
  86. * @brief Free memory previously allocated via heap_caps_malloc() or heap_caps_realloc().
  87. *
  88. * Equivalent semantics to libc free(), for capability-aware memory.
  89. *
  90. * In IDF, ``free(p)`` is equivalent to ``heap_caps_free(p)``.
  91. *
  92. * @param ptr Pointer to memory previously returned from heap_caps_malloc() or heap_caps_realloc(). Can be NULL.
  93. */
  94. void heap_caps_free( void *ptr);
  95. /**
  96. * @brief Reallocate memory previously allocated via heap_caps_malloc() or heap_caps_realloc().
  97. *
  98. * Equivalent semantics to libc realloc(), for capability-aware memory.
  99. *
  100. * In IDF, ``realloc(p, s)`` is equivalent to ``heap_caps_realloc(p, s, MALLOC_CAP_8BIT)``.
  101. *
  102. * 'caps' parameter can be different to the capabilities that any original 'ptr' was allocated with. In this way,
  103. * realloc can be used to "move" a buffer if necessary to ensure it meets a new set of capabilities.
  104. *
  105. * @param ptr Pointer to previously allocated memory, or NULL for a new allocation.
  106. * @param size Size of the new buffer requested, or 0 to free the buffer.
  107. * @param caps Bitwise OR of MALLOC_CAP_* flags indicating the type
  108. * of memory desired for the new allocation.
  109. *
  110. * @return Pointer to a new buffer of size 'size' with capabilities 'caps', or NULL if allocation failed.
  111. */
  112. void *heap_caps_realloc( void *ptr, size_t size, uint32_t caps);
  113. /**
  114. * @brief Allocate an aligned chunk of memory which has the given capabilities
  115. *
  116. * Equivalent semantics to libc aligned_alloc(), for capability-aware memory.
  117. * @param alignment How the pointer received needs to be aligned
  118. * must be a power of two
  119. * @param size Size, in bytes, of the amount of memory to allocate
  120. * @param caps Bitwise OR of MALLOC_CAP_* flags indicating the type
  121. * of memory to be returned
  122. *
  123. * @return A pointer to the memory allocated on success, NULL on failure
  124. *
  125. *
  126. */
  127. void *heap_caps_aligned_alloc(size_t alignment, size_t size, uint32_t caps);
  128. /**
  129. * @brief Used to deallocate memory previously allocated with heap_caps_aligned_alloc
  130. *
  131. * @param ptr Pointer to the memory allocated
  132. * @note This function is deprecated, please consider using heap_caps_free() instead
  133. */
  134. void __attribute__((deprecated)) heap_caps_aligned_free(void *ptr);
  135. /**
  136. * @brief Allocate an aligned chunk of memory which has the given capabilities. The initialized value in the memory is set to zero.
  137. *
  138. * @param alignment How the pointer received needs to be aligned
  139. * must be a power of two
  140. * @param n Number of continuing chunks of memory to allocate
  141. * @param size Size, in bytes, of a chunk of memory to allocate
  142. * @param caps Bitwise OR of MALLOC_CAP_* flags indicating the type
  143. * of memory to be returned
  144. *
  145. * @return A pointer to the memory allocated on success, NULL on failure
  146. *
  147. */
  148. void *heap_caps_aligned_calloc(size_t alignment, size_t n, size_t size, uint32_t caps);
  149. /**
  150. * @brief Allocate a chunk of memory which has the given capabilities. The initialized value in the memory is set to zero.
  151. *
  152. * Equivalent semantics to libc calloc(), for capability-aware memory.
  153. *
  154. * In IDF, ``calloc(p)`` is equivalent to ``heap_caps_calloc(p, MALLOC_CAP_8BIT)``.
  155. *
  156. * @param n Number of continuing chunks of memory to allocate
  157. * @param size Size, in bytes, of a chunk of memory to allocate
  158. * @param caps Bitwise OR of MALLOC_CAP_* flags indicating the type
  159. * of memory to be returned
  160. *
  161. * @return A pointer to the memory allocated on success, NULL on failure
  162. */
  163. void *heap_caps_calloc(size_t n, size_t size, uint32_t caps);
  164. /**
  165. * @brief Get the total size of all the regions that have the given capabilities
  166. *
  167. * This function takes all regions capable of having the given capabilities allocated in them
  168. * and adds up the total space they have.
  169. *
  170. * @param caps Bitwise OR of MALLOC_CAP_* flags indicating the type
  171. * of memory
  172. *
  173. * @return total size in bytes
  174. */
  175. size_t heap_caps_get_total_size(uint32_t caps);
  176. /**
  177. * @brief Get the total free size of all the regions that have the given capabilities
  178. *
  179. * This function takes all regions capable of having the given capabilities allocated in them
  180. * and adds up the free space they have.
  181. *
  182. * @note Note that because of heap fragmentation it is probably not possible to allocate a single block of memory
  183. * of this size. Use heap_caps_get_largest_free_block() for this purpose.
  184. * @param caps Bitwise OR of MALLOC_CAP_* flags indicating the type
  185. * of memory
  186. *
  187. * @return Amount of free bytes in the regions
  188. */
  189. size_t heap_caps_get_free_size( uint32_t caps );
  190. /**
  191. * @brief Get the total minimum free memory of all regions with the given capabilities
  192. *
  193. * This adds all the low watermarks of the regions capable of delivering the memory
  194. * with the given capabilities.
  195. *
  196. * @note Note the result may be less than the global all-time minimum available heap of this kind, as "low watermarks" are
  197. * tracked per-region. Individual regions' heaps may have reached their "low watermarks" at different points in time. However,
  198. * this result still gives a "worst case" indication for all-time minimum free heap.
  199. *
  200. * @param caps Bitwise OR of MALLOC_CAP_* flags indicating the type
  201. * of memory
  202. *
  203. * @return Amount of free bytes in the regions
  204. */
  205. size_t heap_caps_get_minimum_free_size( uint32_t caps );
  206. /**
  207. * @brief Get the largest free block of memory able to be allocated with the given capabilities.
  208. *
  209. * Returns the largest value of ``s`` for which ``heap_caps_malloc(s, caps)`` will succeed.
  210. *
  211. * @param caps Bitwise OR of MALLOC_CAP_* flags indicating the type
  212. * of memory
  213. *
  214. * @return Size of the largest free block in bytes.
  215. */
  216. size_t heap_caps_get_largest_free_block( uint32_t caps );
  217. /**
  218. * @brief Get heap info for all regions with the given capabilities.
  219. *
  220. * Calls multi_heap_info() on all heaps which share the given capabilities. The information returned is an aggregate
  221. * across all matching heaps. The meanings of fields are the same as defined for multi_heap_info_t, except that
  222. * ``minimum_free_bytes`` has the same caveats described in heap_caps_get_minimum_free_size().
  223. *
  224. * @param info Pointer to a structure which will be filled with relevant
  225. * heap metadata.
  226. * @param caps Bitwise OR of MALLOC_CAP_* flags indicating the type
  227. * of memory
  228. *
  229. */
  230. void heap_caps_get_info( multi_heap_info_t *info, uint32_t caps );
  231. /**
  232. * @brief Print a summary of all memory with the given capabilities.
  233. *
  234. * Calls multi_heap_info on all heaps which share the given capabilities, and
  235. * prints a two-line summary for each, then a total summary.
  236. *
  237. * @param caps Bitwise OR of MALLOC_CAP_* flags indicating the type
  238. * of memory
  239. *
  240. */
  241. void heap_caps_print_heap_info( uint32_t caps );
  242. /**
  243. * @brief Check integrity of all heap memory in the system.
  244. *
  245. * Calls multi_heap_check on all heaps. Optionally print errors if heaps are corrupt.
  246. *
  247. * Calling this function is equivalent to calling heap_caps_check_integrity
  248. * with the caps argument set to MALLOC_CAP_INVALID.
  249. *
  250. * @param print_errors Print specific errors if heap corruption is found.
  251. *
  252. * @note Please increase the value of `CONFIG_ESP_INT_WDT_TIMEOUT_MS` when using this API
  253. * with PSRAM enabled.
  254. *
  255. * @return True if all heaps are valid, False if at least one heap is corrupt.
  256. */
  257. bool heap_caps_check_integrity_all(bool print_errors);
  258. /**
  259. * @brief Check integrity of all heaps with the given capabilities.
  260. *
  261. * Calls multi_heap_check on all heaps which share the given capabilities. Optionally
  262. * print errors if the heaps are corrupt.
  263. *
  264. * See also heap_caps_check_integrity_all to check all heap memory
  265. * in the system and heap_caps_check_integrity_addr to check memory
  266. * around a single address.
  267. *
  268. * @param caps Bitwise OR of MALLOC_CAP_* flags indicating the type
  269. * of memory
  270. * @param print_errors Print specific errors if heap corruption is found.
  271. *
  272. * @note Please increase the value of `CONFIG_ESP_INT_WDT_TIMEOUT_MS` when using this API
  273. * with PSRAM capability flag.
  274. *
  275. * @return True if all heaps are valid, False if at least one heap is corrupt.
  276. */
  277. bool heap_caps_check_integrity(uint32_t caps, bool print_errors);
  278. /**
  279. * @brief Check integrity of heap memory around a given address.
  280. *
  281. * This function can be used to check the integrity of a single region of heap memory,
  282. * which contains the given address.
  283. *
  284. * This can be useful if debugging heap integrity for corruption at a known address,
  285. * as it has a lower overhead than checking all heap regions. Note that if the corrupt
  286. * address moves around between runs (due to timing or other factors) then this approach
  287. * won't work, and you should call heap_caps_check_integrity or
  288. * heap_caps_check_integrity_all instead.
  289. *
  290. * @note The entire heap region around the address is checked, not only the adjacent
  291. * heap blocks.
  292. *
  293. * @param addr Address in memory. Check for corruption in region containing this address.
  294. * @param print_errors Print specific errors if heap corruption is found.
  295. *
  296. * @return True if the heap containing the specified address is valid,
  297. * False if at least one heap is corrupt or the address doesn't belong to a heap region.
  298. */
  299. bool heap_caps_check_integrity_addr(intptr_t addr, bool print_errors);
  300. /**
  301. * @brief Enable malloc() in external memory and set limit below which
  302. * malloc() attempts are placed in internal memory.
  303. *
  304. * When external memory is in use, the allocation strategy is to initially try to
  305. * satisfy smaller allocation requests with internal memory and larger requests
  306. * with external memory. This sets the limit between the two, as well as generally
  307. * enabling allocation in external memory.
  308. *
  309. * @param limit Limit, in bytes.
  310. */
  311. void heap_caps_malloc_extmem_enable(size_t limit);
  312. /**
  313. * @brief Allocate a chunk of memory as preference in decreasing order.
  314. *
  315. * @attention The variable parameters are bitwise OR of MALLOC_CAP_* flags indicating the type of memory.
  316. * This API prefers to allocate memory with the first parameter. If failed, allocate memory with
  317. * the next parameter. It will try in this order until allocating a chunk of memory successfully
  318. * or fail to allocate memories with any of the parameters.
  319. *
  320. * @param size Size, in bytes, of the amount of memory to allocate
  321. * @param num Number of variable parameters
  322. *
  323. * @return A pointer to the memory allocated on success, NULL on failure
  324. */
  325. void *heap_caps_malloc_prefer( size_t size, size_t num, ... );
  326. /**
  327. * @brief Reallocate a chunk of memory as preference in decreasing order.
  328. *
  329. * @param ptr Pointer to previously allocated memory, or NULL for a new allocation.
  330. * @param size Size of the new buffer requested, or 0 to free the buffer.
  331. * @param num Number of variable paramters
  332. *
  333. * @return Pointer to a new buffer of size 'size', or NULL if allocation failed.
  334. */
  335. void *heap_caps_realloc_prefer( void *ptr, size_t size, size_t num, ... );
  336. /**
  337. * @brief Allocate a chunk of memory as preference in decreasing order.
  338. *
  339. * @param n Number of continuing chunks of memory to allocate
  340. * @param size Size, in bytes, of a chunk of memory to allocate
  341. * @param num Number of variable paramters
  342. *
  343. * @return A pointer to the memory allocated on success, NULL on failure
  344. */
  345. void *heap_caps_calloc_prefer( size_t n, size_t size, size_t num, ... );
  346. /**
  347. * @brief Dump the full structure of all heaps with matching capabilities.
  348. *
  349. * Prints a large amount of output to serial (because of locking limitations,
  350. * the output bypasses stdout/stderr). For each (variable sized) block
  351. * in each matching heap, the following output is printed on a single line:
  352. *
  353. * - Block address (the data buffer returned by malloc is 4 bytes after this
  354. * if heap debugging is set to Basic, or 8 bytes otherwise).
  355. * - Data size (the data size may be larger than the size requested by malloc,
  356. * either due to heap fragmentation or because of heap debugging level).
  357. * - Address of next block in the heap.
  358. * - If the block is free, the address of the next free block is also printed.
  359. *
  360. * @param caps Bitwise OR of MALLOC_CAP_* flags indicating the type
  361. * of memory
  362. */
  363. void heap_caps_dump(uint32_t caps);
  364. /**
  365. * @brief Dump the full structure of all heaps.
  366. *
  367. * Covers all registered heaps. Prints a large amount of output to serial.
  368. *
  369. * Output is the same as for heap_caps_dump.
  370. *
  371. */
  372. void heap_caps_dump_all(void);
  373. /**
  374. * @brief Return the size that a particular pointer was allocated with.
  375. *
  376. * @param ptr Pointer to currently allocated heap memory. Must be a pointer value previously
  377. * returned by heap_caps_malloc, malloc, calloc, etc. and not yet freed.
  378. *
  379. * @note The app will crash with an assertion failure if the pointer is not valid.
  380. *
  381. * @return Size of the memory allocated at this block.
  382. *
  383. */
  384. size_t heap_caps_get_allocated_size( void *ptr );
  385. #ifdef __cplusplus
  386. }
  387. #endif