zero.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. assert_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. assert_u_eq(s[0], MAGIC,
  14. "Previously allocated byte %zu/%zu is corrupted",
  15. ZU(0), sz_prev);
  16. assert_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. assert_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. assert_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, SMALL_MAXCLASS-1);
  38. }
  39. TEST_END
  40. TEST_BEGIN(test_zero_large) {
  41. test_skip_if(!config_fill);
  42. test_zero(SMALL_MAXCLASS+1, (1U << (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. }