memory_checks.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <stddef.h>
  8. #include "esp_err.h"
  9. #ifdef __cplusplus
  10. extern "C" {
  11. #endif
  12. /**
  13. * @brief Leak for components
  14. */
  15. typedef enum {
  16. ESP_COMP_LEAK_GENERAL = 0, /**< Leak by default */
  17. ESP_COMP_LEAK_LWIP, /**< Leak for LWIP */
  18. ESP_COMP_LEAK_NVS, /**< Leak for NVS */
  19. ESP_COMP_LEAK_ALL, /**< Use for getting the summary leak level */
  20. } esp_comp_leak_t;
  21. /**
  22. * @brief Type of a leak threshold
  23. */
  24. typedef enum {
  25. ESP_LEAK_TYPE_WARNING = 0, /**< Warning level of leak */
  26. ESP_LEAK_TYPE_CRITICAL, /**< Critical level of leak */
  27. ESP_LEAK_TYPE_MAX, /**< Max number of leak levels for all components/levels */
  28. } esp_type_leak_t;
  29. /**
  30. * @brief Adjust the memory leak thresholds for unit tests.
  31. *
  32. * Usually, unit tests will check if memory is leaked. Some functionality used by unit tests may unavoidably
  33. * leak memory. This is why there is a default threshold for memory leaks (currently 1200 bytes).
  34. * Within this range, the number of bytes leaked will be visually reported on the terminal, but no test failure will
  35. * be triggered. Any memory leak above the default threshold will trigger a unit test failure.
  36. * This function allows to adjust that memory leak threshold.
  37. *
  38. * @param leak_level Maximum allowed memory leak which will not trigger a unit test failure.
  39. * @param type_of_leak There are two types of leak thresholds: critical and warning. Only the
  40. * critical threshold will trigger a unit test failure if exceeded.
  41. * @param component Thresholds can be set in general or for specific components. Note that this argument
  42. * is not checked.
  43. *
  44. * @return ESP_OK on success, ESP_INVALID_ARG if type_of_leak is invalid. \c component is unchecked.
  45. */
  46. esp_err_t test_utils_set_leak_level(size_t leak_level, esp_type_leak_t type_of_leak, esp_comp_leak_t component);
  47. /**
  48. * @brief Return the memory leak thresholds for unit tests for a leak type and component.
  49. *
  50. * For more information, see \c test_utils_set_leak_level above.
  51. *
  52. * @param type_of_leak Warning or Critical
  53. * @param component The component for which to return the leak threshold.
  54. */
  55. size_t test_utils_get_leak_level(esp_type_leak_t type_of_leak, esp_comp_leak_t component);
  56. /**
  57. * @brief Start/Restart memory leak checking.
  58. *
  59. * Records the current free memory values at time of calling. After the test case, it may be checked with
  60. * \c test_utils_finish_and_evaluate_leaks.
  61. *
  62. * If this function is called repeatedly, only the free memory values at the last time of calling will prevail
  63. * as reference.
  64. */
  65. void test_utils_record_free_mem(void);
  66. /**
  67. * @brief Evaluate memory leak checking according to the provided thresholds.
  68. *
  69. * If the current memory leak level (counted from the last time calling \c test_utils_record_free_mem() ) exceeds
  70. * \c critical_threshold, a unit test failure will be triggered. If it exceeds only the warning,
  71. * a warning message will be issued.
  72. */
  73. void test_utils_finish_and_evaluate_leaks(size_t warn_threshold, size_t critical_threshold);
  74. /**
  75. * @brief Helper function to setup and initialize heap tracing.
  76. */
  77. void setup_heap_record(void);
  78. #ifdef __cplusplus
  79. }
  80. #endif