unity_utils_memory.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdio.h>
  7. #include "unity.h"
  8. #include "unity_test_utils.h"
  9. static size_t s_allowed_leak_level;
  10. void unity_utils_set_leak_level(size_t leak_level)
  11. {
  12. s_allowed_leak_level = leak_level;
  13. }
  14. void unity_utils_check_leak(unsigned int before_free,
  15. unsigned int after_free,
  16. const char *type,
  17. unsigned int threshold)
  18. {
  19. int free_delta = (int)after_free - (int)before_free;
  20. printf("MALLOC_CAP_%s usage: Free memory delta: %d Leak threshold: -%u \n",
  21. type,
  22. free_delta,
  23. threshold);
  24. if (free_delta > 0) {
  25. return; // free memory went up somehow
  26. }
  27. unsigned int leaked = (size_t)(free_delta * -1);
  28. printf("MALLOC_CAP_%s %s leak: Before %u bytes free, After %u bytes free (delta %u)\n",
  29. type,
  30. leaked <= threshold ? "potential" : "critical",
  31. before_free, after_free, leaked);
  32. fflush(stdout);
  33. TEST_ASSERT_MESSAGE(leaked <= threshold, "The test leaked too much memory");
  34. }
  35. void unity_utils_evaluate_leaks(void)
  36. {
  37. unity_utils_evaluate_leaks_direct(s_allowed_leak_level);
  38. }