mallocx.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include "test/jemalloc_test.h"
  2. #define CHUNK 0x400000
  3. #define MAXALIGN (((size_t)1) << 25)
  4. #define NITER 4
  5. TEST_BEGIN(test_basic)
  6. {
  7. size_t nsz, rsz, sz;
  8. void *p;
  9. sz = 42;
  10. nsz = nallocx(sz, 0);
  11. assert_zu_ne(nsz, 0, "Unexpected nallocx() error");
  12. p = mallocx(sz, 0);
  13. assert_ptr_not_null(p, "Unexpected mallocx() error");
  14. rsz = sallocx(p, 0);
  15. assert_zu_ge(rsz, sz, "Real size smaller than expected");
  16. assert_zu_eq(nsz, rsz, "nallocx()/sallocx() size mismatch");
  17. dallocx(p, 0);
  18. p = mallocx(sz, 0);
  19. assert_ptr_not_null(p, "Unexpected mallocx() error");
  20. dallocx(p, 0);
  21. nsz = nallocx(sz, MALLOCX_ZERO);
  22. assert_zu_ne(nsz, 0, "Unexpected nallocx() error");
  23. p = mallocx(sz, MALLOCX_ZERO);
  24. assert_ptr_not_null(p, "Unexpected mallocx() error");
  25. rsz = sallocx(p, 0);
  26. assert_zu_eq(nsz, rsz, "nallocx()/sallocx() rsize mismatch");
  27. dallocx(p, 0);
  28. }
  29. TEST_END
  30. TEST_BEGIN(test_alignment_and_size)
  31. {
  32. size_t nsz, rsz, sz, alignment, total;
  33. unsigned i;
  34. void *ps[NITER];
  35. for (i = 0; i < NITER; i++)
  36. ps[i] = NULL;
  37. for (alignment = 8;
  38. alignment <= MAXALIGN;
  39. alignment <<= 1) {
  40. total = 0;
  41. for (sz = 1;
  42. sz < 3 * alignment && sz < (1U << 31);
  43. sz += (alignment >> (LG_SIZEOF_PTR-1)) - 1) {
  44. for (i = 0; i < NITER; i++) {
  45. nsz = nallocx(sz, MALLOCX_ALIGN(alignment) |
  46. MALLOCX_ZERO);
  47. assert_zu_ne(nsz, 0,
  48. "nallocx() error for alignment=%zu, "
  49. "size=%zu (%#zx)", alignment, sz, sz);
  50. ps[i] = mallocx(sz, MALLOCX_ALIGN(alignment) |
  51. MALLOCX_ZERO);
  52. assert_ptr_not_null(ps[i],
  53. "mallocx() error for alignment=%zu, "
  54. "size=%zu (%#zx)", alignment, sz, sz);
  55. rsz = sallocx(ps[i], 0);
  56. assert_zu_ge(rsz, sz,
  57. "Real size smaller than expected for "
  58. "alignment=%zu, size=%zu", alignment, sz);
  59. assert_zu_eq(nsz, rsz,
  60. "nallocx()/sallocx() size mismatch for "
  61. "alignment=%zu, size=%zu", alignment, sz);
  62. assert_ptr_null(
  63. (void *)((uintptr_t)ps[i] & (alignment-1)),
  64. "%p inadequately aligned for"
  65. " alignment=%zu, size=%zu", ps[i],
  66. alignment, sz);
  67. total += rsz;
  68. if (total >= (MAXALIGN << 1))
  69. break;
  70. }
  71. for (i = 0; i < NITER; i++) {
  72. if (ps[i] != NULL) {
  73. dallocx(ps[i], 0);
  74. ps[i] = NULL;
  75. }
  76. }
  77. }
  78. }
  79. }
  80. TEST_END
  81. int
  82. main(void)
  83. {
  84. return (test(
  85. test_basic,
  86. test_alignment_and_size));
  87. }