multi_heap.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 <stdbool.h>
  17. /* multi_heap is a heap implementation for handling multiple
  18. heterogenous heaps in a single program.
  19. Any contiguous block of memory can be registered as a heap.
  20. */
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. /** @brief Opaque handle to a registered heap */
  25. typedef struct multi_heap_info *multi_heap_handle_t;
  26. /**
  27. * @brief allocate a chunk of memory with specific alignment
  28. *
  29. * @param heap Handle to a registered heap.
  30. * @param size size in bytes of memory chunk
  31. * @param alignment how the memory must be aligned
  32. *
  33. * @return pointer to the memory allocated, NULL on failure
  34. */
  35. void *multi_heap_aligned_alloc(multi_heap_handle_t heap, size_t size, size_t alignment);
  36. /** @brief malloc() a buffer in a given heap
  37. *
  38. * Semantics are the same as standard malloc(), only the returned buffer will be allocated in the specified heap.
  39. *
  40. * @param heap Handle to a registered heap.
  41. * @param size Size of desired buffer.
  42. *
  43. * @return Pointer to new memory, or NULL if allocation fails.
  44. */
  45. void *multi_heap_malloc(multi_heap_handle_t heap, size_t size);
  46. /** @brief free() a buffer aligned in a given heap.
  47. *
  48. * @param heap Handle to a registered heap.
  49. * @param p NULL, or a pointer previously returned from multi_heap_aligned_alloc() for the same heap.
  50. * @note This function is deprecated, consider using multi_heap_free() instead
  51. */
  52. void __attribute__((deprecated)) multi_heap_aligned_free(multi_heap_handle_t heap, void *p);
  53. /** @brief free() a buffer in a given heap.
  54. *
  55. * Semantics are the same as standard free(), only the argument 'p' must be NULL or have been allocated in the specified heap.
  56. *
  57. * @param heap Handle to a registered heap.
  58. * @param p NULL, or a pointer previously returned from multi_heap_malloc() or multi_heap_realloc() for the same heap.
  59. */
  60. void multi_heap_free(multi_heap_handle_t heap, void *p);
  61. /** @brief realloc() a buffer in a given heap.
  62. *
  63. * Semantics are the same as standard realloc(), only the argument 'p' must be NULL or have been allocated in the specified heap.
  64. *
  65. * @param heap Handle to a registered heap.
  66. * @param p NULL, or a pointer previously returned from multi_heap_malloc() or multi_heap_realloc() for the same heap.
  67. * @param size Desired new size for buffer.
  68. *
  69. * @return New buffer of 'size' containing contents of 'p', or NULL if reallocation failed.
  70. */
  71. void *multi_heap_realloc(multi_heap_handle_t heap, void *p, size_t size);
  72. /** @brief Return the size that a particular pointer was allocated with.
  73. *
  74. * @param heap Handle to a registered heap.
  75. * @param p Pointer, must have been previously returned from multi_heap_malloc() or multi_heap_realloc() for the same heap.
  76. *
  77. * @return Size of the memory allocated at this block. May be more than the original size argument, due
  78. * to padding and minimum block sizes.
  79. */
  80. size_t multi_heap_get_allocated_size(multi_heap_handle_t heap, void *p);
  81. /** @brief Register a new heap for use
  82. *
  83. * This function initialises a heap at the specified address, and returns a handle for future heap operations.
  84. *
  85. * There is no equivalent function for deregistering a heap - if all blocks in the heap are free, you can immediately start using the memory for other purposes.
  86. *
  87. * @param start Start address of the memory to use for a new heap.
  88. * @param size Size (in bytes) of the new heap.
  89. *
  90. * @return Handle of a new heap ready for use, or NULL if the heap region was too small to be initialised.
  91. */
  92. multi_heap_handle_t multi_heap_register(void *start, size_t size);
  93. /** @brief Associate a private lock pointer with a heap
  94. *
  95. * The lock argument is supplied to the MULTI_HEAP_LOCK() and MULTI_HEAP_UNLOCK() macros, defined in multi_heap_platform.h.
  96. *
  97. * The lock in question must be recursive.
  98. *
  99. * When the heap is first registered, the associated lock is NULL.
  100. *
  101. * @param heap Handle to a registered heap.
  102. * @param lock Optional pointer to a locking structure to associate with this heap.
  103. */
  104. void multi_heap_set_lock(multi_heap_handle_t heap, void* lock);
  105. /** @brief Dump heap information to stdout
  106. *
  107. * For debugging purposes, this function dumps information about every block in the heap to stdout.
  108. *
  109. * @param heap Handle to a registered heap.
  110. */
  111. void multi_heap_dump(multi_heap_handle_t heap);
  112. /** @brief Check heap integrity
  113. *
  114. * Walks the heap and checks all heap data structures are valid. If any errors are detected, an error-specific message
  115. * can be optionally printed to stderr. Print behaviour can be overriden at compile time by defining
  116. * MULTI_CHECK_FAIL_PRINTF in multi_heap_platform.h.
  117. *
  118. * @param heap Handle to a registered heap.
  119. * @param print_errors If true, errors will be printed to stderr.
  120. * @return true if heap is valid, false otherwise.
  121. */
  122. bool multi_heap_check(multi_heap_handle_t heap, bool print_errors);
  123. /** @brief Return free heap size
  124. *
  125. * Returns the number of bytes available in the heap.
  126. *
  127. * Equivalent to the total_free_bytes member returned by multi_heap_get_heap_info().
  128. *
  129. * Note that the heap may be fragmented, so the actual maximum size for a single malloc() may be lower. To know this
  130. * size, see the largest_free_block member returned by multi_heap_get_heap_info().
  131. *
  132. * @param heap Handle to a registered heap.
  133. * @return Number of free bytes.
  134. */
  135. size_t multi_heap_free_size(multi_heap_handle_t heap);
  136. /** @brief Return the lifetime minimum free heap size
  137. *
  138. * Equivalent to the minimum_free_bytes member returned by multi_heap_get_info().
  139. *
  140. * Returns the lifetime "low water mark" of possible values returned from multi_free_heap_size(), for the specified
  141. * heap.
  142. *
  143. * @param heap Handle to a registered heap.
  144. * @return Number of free bytes.
  145. */
  146. size_t multi_heap_minimum_free_size(multi_heap_handle_t heap);
  147. /** @brief Structure to access heap metadata via multi_heap_get_info */
  148. typedef struct {
  149. size_t total_free_bytes; ///< Total free bytes in the heap. Equivalent to multi_free_heap_size().
  150. size_t total_allocated_bytes; ///< Total bytes allocated to data in the heap.
  151. size_t largest_free_block; ///< Size of largest free block in the heap. This is the largest malloc-able size.
  152. size_t minimum_free_bytes; ///< Lifetime minimum free heap size. Equivalent to multi_minimum_free_heap_size().
  153. size_t allocated_blocks; ///< Number of (variable size) blocks allocated in the heap.
  154. size_t free_blocks; ///< Number of (variable size) free blocks in the heap.
  155. size_t total_blocks; ///< Total number of (variable size) blocks in the heap.
  156. } multi_heap_info_t;
  157. /** @brief Return metadata about a given heap
  158. *
  159. * Fills a multi_heap_info_t structure with information about the specified heap.
  160. *
  161. * @param heap Handle to a registered heap.
  162. * @param info Pointer to a structure to fill with heap metadata.
  163. */
  164. void multi_heap_get_info(multi_heap_handle_t heap, multi_heap_info_t *info);
  165. #ifdef __cplusplus
  166. }
  167. #endif