zero_reallocs.c 927 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #include "test/jemalloc_test.h"
  2. static size_t
  3. zero_reallocs() {
  4. if (!config_stats) {
  5. return 0;
  6. }
  7. size_t count = 12345;
  8. size_t sz = sizeof(count);
  9. expect_d_eq(mallctl("stats.zero_reallocs", (void *)&count, &sz,
  10. NULL, 0), 0, "Unexpected mallctl failure");
  11. return count;
  12. }
  13. TEST_BEGIN(test_zero_reallocs) {
  14. test_skip_if(!config_stats);
  15. for (size_t i = 0; i < 100; ++i) {
  16. void *ptr = mallocx(i * i + 1, 0);
  17. expect_ptr_not_null(ptr, "Unexpected mallocx error");
  18. size_t count = zero_reallocs();
  19. expect_zu_eq(i, count, "Incorrect zero realloc count");
  20. ptr = realloc(ptr, 0);
  21. expect_ptr_null(ptr, "Realloc didn't free");
  22. count = zero_reallocs();
  23. expect_zu_eq(i + 1, count, "Realloc didn't adjust count");
  24. }
  25. }
  26. TEST_END
  27. int
  28. main(void) {
  29. /*
  30. * We expect explicit counts; reentrant tests run multiple times, so
  31. * counts leak across runs.
  32. */
  33. return test_no_reentrancy(
  34. test_zero_reallocs);
  35. }