zero_realloc_alloc.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include "test/jemalloc_test.h"
  2. static uint64_t
  3. allocated() {
  4. if (!config_stats) {
  5. return 0;
  6. }
  7. uint64_t allocated;
  8. size_t sz = sizeof(allocated);
  9. expect_d_eq(mallctl("thread.allocated", (void *)&allocated, &sz, NULL,
  10. 0), 0, "Unexpected mallctl failure");
  11. return allocated;
  12. }
  13. static uint64_t
  14. deallocated() {
  15. if (!config_stats) {
  16. return 0;
  17. }
  18. uint64_t deallocated;
  19. size_t sz = sizeof(deallocated);
  20. expect_d_eq(mallctl("thread.deallocated", (void *)&deallocated, &sz,
  21. NULL, 0), 0, "Unexpected mallctl failure");
  22. return deallocated;
  23. }
  24. TEST_BEGIN(test_realloc_alloc) {
  25. void *ptr = mallocx(1, 0);
  26. expect_ptr_not_null(ptr, "Unexpected mallocx error");
  27. uint64_t allocated_before = allocated();
  28. uint64_t deallocated_before = deallocated();
  29. ptr = realloc(ptr, 0);
  30. uint64_t allocated_after = allocated();
  31. uint64_t deallocated_after = deallocated();
  32. if (config_stats) {
  33. expect_u64_lt(allocated_before, allocated_after,
  34. "Unexpected stats change");
  35. expect_u64_lt(deallocated_before, deallocated_after,
  36. "Unexpected stats change");
  37. }
  38. dallocx(ptr, 0);
  39. }
  40. TEST_END
  41. int
  42. main(void) {
  43. return test(
  44. test_realloc_alloc);
  45. }