mem_manager.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /**
  2. * Copyright (c) 2014 - 2018, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. /** @file
  41. *
  42. * @defgroup mem_manager Memory Manager
  43. * @{
  44. * @ingroup app_common
  45. * @brief Memory Manager for the @nRFXX SDK
  46. *
  47. * @details This module allows for dynamic use of memory. Currently,
  48. * this module can be used only to allocate and free memory in the RAM.
  49. *
  50. * The Memory Manager manages static memory blocks of fixed sizes. These blocks can be requested for
  51. * usage, and freed when the application no longer needs them. A maximum of seven block categories
  52. * can be managed by the module. These block categories are identified by xxsmall, xmall, small,
  53. * medium, large, xlarge, and xxlarge. They are ordered in increasing block sizes.
  54. * The size and the count of each of the block categories can be configured based on the application
  55. * requirements in the configuration file @c sdk_config.h.
  56. * To use fewer than seven buffer pools, do not define the count for the unwanted block
  57. * or explicitly set it to zero. At least one block category must be configured
  58. * for this module to function as expected.
  59. */
  60. #ifndef MEM_MANAGER_H__
  61. #define MEM_MANAGER_H__
  62. #include "sdk_common.h"
  63. #ifdef __cplusplus
  64. extern "C" {
  65. #endif
  66. /**@brief Initializes Memory Manager.
  67. *
  68. * @details API to initialize the Memory Manager. Always call this API before using any of the other
  69. * APIs of the module. This API should be called only once.
  70. *
  71. * @retval NRF_SUCCESS If initialization was successful.
  72. * Otherwise, an error code that indicates the reason for the failure is returned.
  73. *
  74. * @warning If this API fails, the application shall not proceed with using other APIs of this
  75. * module.
  76. */
  77. uint32_t nrf_mem_init(void);
  78. /**@brief Reserves a block of memory for the application.
  79. *
  80. * @details API to request a contiguous memory block of the given length. If
  81. * the memory allocation succeeds, pp_buffer points to the memory block. If
  82. * the memory allocation fails, pp_buffer points to NULL and the return value of
  83. * the API indicates the reason for the failure. The memory block reserved using this API can
  84. * be freed using the @ref nrf_free function.
  85. *
  86. * @param[out] pp_buffer Pointer to the allocated memory block if memory allocation
  87. * succeeds; otherwise points to NULL.
  88. * @param[inout] p_size Requested memory size. This parameter returns the actual size
  89. * allocated. If the procedure was successful, the actual size
  90. * returned is always greater than or equal to requested size,
  91. * never less.
  92. *
  93. * @retval NRF_SUCCESS If memory was successfully allocated.
  94. * Otherwise, an error code indicating the reason for failure.
  95. * @retval NRF_ERROR_INVALID_PARAM If the requested memory size is zero or greater than the
  96. * largest memory block that the module is configured to
  97. * support.
  98. * @retval NRF_ERROR_NO_MEM If there is no memory available of the requested size.
  99. */
  100. uint32_t nrf_mem_reserve(uint8_t ** pp_buffer, uint32_t * p_size);
  101. /**@brief 'malloc' styled memory allocation function.
  102. *
  103. * @details API to allocate memory, same as nrf_mem_reserve but uses malloc signature.
  104. *
  105. * @param[in] size Requested memory size.
  106. *
  107. * @retval Valid memory location if the procedure was successful, else, NULL.
  108. */
  109. void * nrf_malloc(uint32_t size);
  110. /**@brief 'calloc' styled memory allocation function.
  111. *
  112. * @details API to allocate zero-initialized memory of size count*size.
  113. *
  114. * @param[in] nmemb Number of elements of 'size' bytes.
  115. * @param[in] size Size of each element allocated.
  116. *
  117. * @retval Valid, zero-initialized memory location if the procedure was successful, else, NULL.
  118. */
  119. void * nrf_calloc(uint32_t nmemb, uint32_t size);
  120. /**@brief Free allocated memory - standard 'free' styles API.
  121. *
  122. * @details API to resubmit memory allocated, same in functionality nrf_free.
  123. *
  124. * @param[out] p_buffer Pointer to the memory block that is being freed.
  125. */
  126. void nrf_free(void * p_buffer);
  127. /**@brief Memory reallocation (trim) function.
  128. *
  129. * @details API to reallocate memory or to trim it. Trim is mentioned here to avoid use of API to
  130. * request memory size larger than original memory allocated.
  131. *
  132. * @param[in] p_buffer Pointer to the memory block that needs to be trimmed.
  133. * @param[in] size Size of memory at the beginning of the buffer to be left untrimmed.
  134. *
  135. * @retval Pointer to memory location with trimmed size, else, NULL.
  136. */
  137. void * nrf_realloc(void *p_buffer, uint32_t size);
  138. #ifdef MEM_MANAGER_ENABLE_DIAGNOSTICS
  139. /**@brief Function to print statistics related to memory blocks managed by memory manager.
  140. *
  141. * @details This API prints information with respects to each block function, including size, total
  142. * block count, number of blocks in use at the time of printing, smallest memory size
  143. * allocated in the block and the largest one. This API is intended to help developers
  144. * tune the block sizes to make optimal use of memory for the application.
  145. * This functionality is never needed in final application and therefore, is disabled by
  146. * default.
  147. */
  148. void nrf_mem_diagnose(void);
  149. #endif // MEM_MANAGER_ENABLE_DIAGNOSTICS
  150. #ifdef __cplusplus
  151. }
  152. #endif
  153. #endif // MEM_MANAGER_H__
  154. /** @} */