multi_heap.h 6.7 KB

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