esp_heap_caps.h 17 KB

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