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