zero.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include "test/jemalloc_test.h"
  2. static void
  3. test_zero(size_t sz_min, size_t sz_max) {
  4. uint8_t *s;
  5. size_t sz_prev, sz, i;
  6. #define MAGIC ((uint8_t)0x61)
  7. sz_prev = 0;
  8. s = (uint8_t *)mallocx(sz_min, 0);
  9. expect_ptr_not_null((void *)s, "Unexpected mallocx() failure");
  10. for (sz = sallocx(s, 0); sz <= sz_max;
  11. sz_prev = sz, sz = sallocx(s, 0)) {
  12. if (sz_prev > 0) {
  13. expect_u_eq(s[0], MAGIC,
  14. "Previously allocated byte %zu/%zu is corrupted",
  15. ZU(0), sz_prev);
  16. expect_u_eq(s[sz_prev-1], MAGIC,
  17. "Previously allocated byte %zu/%zu is corrupted",
  18. sz_prev-1, sz_prev);
  19. }
  20. for (i = sz_prev; i < sz; i++) {
  21. expect_u_eq(s[i], 0x0,
  22. "Newly allocated byte %zu/%zu isn't zero-filled",
  23. i, sz);
  24. s[i] = MAGIC;
  25. }
  26. if (xallocx(s, sz+1, 0, 0) == sz) {
  27. s = (uint8_t *)rallocx(s, sz+1, 0);
  28. expect_ptr_not_null((void *)s,
  29. "Unexpected rallocx() failure");
  30. }
  31. }
  32. dallocx(s, 0);
  33. #undef MAGIC
  34. }
  35. TEST_BEGIN(test_zero_small) {
  36. test_skip_if(!config_fill);
  37. test_zero(1, SC_SMALL_MAXCLASS - 1);
  38. }
  39. TEST_END
  40. TEST_BEGIN(test_zero_large) {
  41. test_skip_if(!config_fill);
  42. test_zero(SC_SMALL_MAXCLASS + 1, 1U << (SC_LG_LARGE_MINCLASS + 1));
  43. }
  44. TEST_END
  45. int
  46. main(void) {
  47. return test(
  48. test_zero_small,
  49. test_zero_large);
  50. }