mem_manager.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* Copyright (c) 2014 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. /** @file
  13. *
  14. * @defgroup mem_manager Memory Manager
  15. * @{
  16. * @ingroup app_common
  17. * @brief Memory Manager for the \nRFXX SDK
  18. *
  19. * @details This module allows for dynamic use of memory. Currently,
  20. * this module can be used only to allocate and free memory in the RAM.
  21. *
  22. * The Memory Manager manages static memory blocks of fixed sizes. These blocks can be requested for
  23. * usage, and freed when the application no longer needs them. A maximum of seven block categories
  24. * can be managed by the module. These block categories are identified by xxsmall, xmall, small,
  25. * medium, large, xlarge, and xxlarge. They are ordered in increasing block sizes.
  26. * The size and the count of each of the block categories can be configured based on the application
  27. * requirements in the configuration file @c sdk_config.h.
  28. * To use fewer than seven buffer pools, do not define the count for the unwanted block
  29. * or explicitly set it to zero. At least one block category must be configured
  30. * for this module to function as expected.
  31. */
  32. #ifndef MEM_MANAGER_H__
  33. #define MEM_MANAGER_H__
  34. #include "sdk_common.h"
  35. /**@brief Initializes Memory Manager.
  36. *
  37. * @details API to initialize the Memory Manager. Always call this API before using any of the other
  38. * APIs of the module. This API should be called only once.
  39. *
  40. * @retval NRF_SUCCESS If initialization was successful.
  41. * Otherwise, an error code that indicates the reason for the failure is returned.
  42. *
  43. * @warning If this API fails, the application shall not proceed with using other APIs of this
  44. * module.
  45. */
  46. uint32_t nrf_mem_init(void);
  47. /**@brief Reserves a block of memory for the application.
  48. *
  49. * @details API to request a contiguous memory block of the given length. If
  50. * the memory allocation succeeds, pp_buffer points to the memory block. If
  51. * the memory allocation fails, pp_buffer points to NULL and the return value of
  52. * the API indicates the reason for the failure. The memory block reserved using this API can
  53. * be freed using the @ref nrf_free function.
  54. *
  55. * @param[out] pp_buffer Pointer to the allocated memory block if memory allocation
  56. * succeeds; otherwise points to NULL.
  57. * @param[inout] p_size Requested memory size. This parameter returns the actual size
  58. * allocated. If the procedure was successful, the actual size
  59. * returned is always greater than or equal to requested size,
  60. * never less.
  61. *
  62. * @retval NRF_SUCCESS If memory was successfully allocated.
  63. * Otherwise, an error code indicating the reason for failure.
  64. * @retval NRF_ERROR_INVALID_PARAM If the requested memory size is zero or greater than the
  65. * largest memory block that the module is configured to
  66. * support.
  67. * @retval NRF_ERROR_NO_MEM If there is no memory available of the requested size.
  68. */
  69. uint32_t nrf_mem_reserve(uint8_t ** pp_buffer, uint32_t * p_size);
  70. /**@brief 'malloc' styled memory allocation function.
  71. *
  72. * @details API to allocate memory, same as nrf_mem_reserve but uses malloc signature.
  73. *
  74. * @param[in] size Requested memory size.
  75. *
  76. * @retval Valid memory location if the procedure was successful, else, NULL.
  77. */
  78. void * nrf_malloc(uint32_t size);
  79. /**@brief 'calloc' styled memory allocation function.
  80. *
  81. * @details API to allocate zero-initialized memory of size count*size.
  82. *
  83. * @param[in] nmemb Number of elements of 'size' bytes.
  84. * @param[in] size Size of each element allocated.
  85. *
  86. * @retval Valid, zero-initialized memory location if the procedure was successful, else, NULL.
  87. */
  88. void * nrf_calloc(uint32_t nmemb, uint32_t size);
  89. /**@brief Free allocated memory - standard 'free' styles API.
  90. *
  91. * @details API to resubmit memory allocated, same in functionality nrf_free.
  92. *
  93. * @param[out] p_buffer Pointer to the memory block that is being freed.
  94. */
  95. void nrf_free(void * p_buffer);
  96. /**@brief Memory reallocation (trim) function.
  97. *
  98. * @details API to reallocate memory or to trim it. Trim is mentioned here to avoid use of API to
  99. * request memory size larger than original memory allocated.
  100. *
  101. * @param[in] p_buffer Pointer to the memory block that needs to be trimmed.
  102. * @param[in] size Size of memory at the beginning of the buffer to be left untrimmed.
  103. *
  104. * @retval Pointer to memory location with trimmed size, else, NULL.
  105. */
  106. void * nrf_realloc(void *p_buffer, uint32_t size);
  107. #ifdef MEM_MANAGER_ENABLE_DIAGNOSTICS
  108. /**@brief Function to print statstics related to memory blocks managed by memory manager.
  109. *
  110. * @details This API prints information with respects to each block function, including size, total
  111. * block count, number of blocks in use at the time of printing, smallest memory size
  112. * allocated in the block and the largest one. This API is intended to help developers
  113. * tune the block sizes to make optimal use of memory for the application.
  114. * This functionality is never needed in final application and therefore, is disabled by
  115. * default.
  116. */
  117. void nrf_mem_diagnose(void);
  118. #endif // MEM_MANAGER_ENABLE_DIAGNOSTICS
  119. #endif // MEM_MANAGER_H__
  120. /** @} */