unity_test_utils_memory.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <stddef.h>
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. /**
  12. * @brief Adjust the allowed memory leak thresholds for unit tests.
  13. *
  14. * Usually, unit tests will check if memory is leaked. Some functionality used by unit tests may unavoidably
  15. * leak memory. This function allows to adjust that memory leak threshold.
  16. *
  17. * @param leak_level Maximum allowed memory leak which will not trigger a unit test failure.
  18. */
  19. void unity_utils_set_leak_level(size_t leak_level);
  20. /**
  21. * @brief Start/Restart memory leak checking.
  22. *
  23. * Records the current free memory values at time of calling. After the test case, it may be checked with
  24. * \c unity_utils_finish_and_evaluate_leaks.
  25. *
  26. * If this function is called repeatedly, only the free memory values at the last time of calling will prevail
  27. * as reference.
  28. */
  29. void unity_utils_record_free_mem(void);
  30. /**
  31. * @brief Calculate leaks and check they are below against a threshold
  32. *
  33. * This function is for internal use, users shouldn't have a reason to call this.
  34. *
  35. * Calculates the leak from \c before_free and \c after_free and checks that the difference does not exceed
  36. * \c threshold. It uses a unity assert to to the check and report in case of failure.
  37. * A summary of the leaked data will be printed in all cases.
  38. */
  39. void unity_utils_check_leak(unsigned int before_free,
  40. unsigned int after_free,
  41. const char *type,
  42. unsigned int threshold);
  43. /**
  44. * @brief Evaluate memory leak checking according to the provided thresholds.
  45. *
  46. * If the current memory leak level (counted from the last time calling \c unity_utils_record_free_mem() ) exceeds
  47. * \c threshold, a unit test failure will be triggered.
  48. */
  49. void unity_utils_evaluate_leaks_direct(size_t threshold);
  50. /**
  51. * @brief Evaluate memory leaks.
  52. *
  53. * If the current memory leak level (counted from the last time calling \c unity_utils_record_free_mem() ) exceeds
  54. * the threshold set before via \c unity_utils_set_leak_level(), a unit test failure will be triggered.
  55. *
  56. * @note The user MUST set the allowed leak threshold before via \c unity_utils_set_leak_level(), otherwise the
  57. * allowed leak threshold is undefined.
  58. */
  59. void unity_utils_evaluate_leaks(void);
  60. /**
  61. * @brief Helper function to setup and initialize heap tracing.
  62. *
  63. * @param num_heap_records the size of the heap record butter,
  64. * counted in number of heap record elements (heap_trace_record_t).
  65. * Use a default value of 80 if no special requirements need to be met.
  66. */
  67. void unity_utils_setup_heap_record(size_t num_heap_records);
  68. #ifdef __cplusplus
  69. }
  70. #endif