zero.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include "test/jemalloc_test.h"
  2. #ifdef JEMALLOC_FILL
  3. const char *malloc_conf =
  4. "abort:false,junk:false,zero:true,redzone:false,quarantine:0";
  5. #endif
  6. static void
  7. test_zero(size_t sz_min, size_t sz_max)
  8. {
  9. char *s;
  10. size_t sz_prev, sz, i;
  11. sz_prev = 0;
  12. s = (char *)mallocx(sz_min, 0);
  13. assert_ptr_not_null((void *)s, "Unexpected mallocx() failure");
  14. for (sz = sallocx(s, 0); sz <= sz_max;
  15. sz_prev = sz, sz = sallocx(s, 0)) {
  16. if (sz_prev > 0) {
  17. assert_c_eq(s[0], 'a',
  18. "Previously allocated byte %zu/%zu is corrupted",
  19. ZU(0), sz_prev);
  20. assert_c_eq(s[sz_prev-1], 'a',
  21. "Previously allocated byte %zu/%zu is corrupted",
  22. sz_prev-1, sz_prev);
  23. }
  24. for (i = sz_prev; i < sz; i++) {
  25. assert_c_eq(s[i], 0x0,
  26. "Newly allocated byte %zu/%zu isn't zero-filled",
  27. i, sz);
  28. s[i] = 'a';
  29. }
  30. if (xallocx(s, sz+1, 0, 0) == sz) {
  31. s = (char *)rallocx(s, sz+1, 0);
  32. assert_ptr_not_null((void *)s,
  33. "Unexpected rallocx() failure");
  34. }
  35. }
  36. dallocx(s, 0);
  37. }
  38. TEST_BEGIN(test_zero_small)
  39. {
  40. test_skip_if(!config_fill);
  41. test_zero(1, SMALL_MAXCLASS-1);
  42. }
  43. TEST_END
  44. TEST_BEGIN(test_zero_large)
  45. {
  46. test_skip_if(!config_fill);
  47. test_zero(SMALL_MAXCLASS+1, large_maxclass);
  48. }
  49. TEST_END
  50. TEST_BEGIN(test_zero_huge)
  51. {
  52. test_skip_if(!config_fill);
  53. test_zero(large_maxclass+1, chunksize*2);
  54. }
  55. TEST_END
  56. int
  57. main(void)
  58. {
  59. return (test(
  60. test_zero_small,
  61. test_zero_large,
  62. test_zero_huge));
  63. }