allocm.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 = 0;
  11. assert_d_eq(nallocm(&nsz, sz, 0), ALLOCM_SUCCESS,
  12. "Unexpected nallocm() error");
  13. rsz = 0;
  14. assert_d_eq(allocm(&p, &rsz, sz, 0), ALLOCM_SUCCESS,
  15. "Unexpected allocm() error");
  16. assert_zu_ge(rsz, sz, "Real size smaller than expected");
  17. assert_zu_eq(nsz, rsz, "nallocm()/allocm() rsize mismatch");
  18. assert_d_eq(dallocm(p, 0), ALLOCM_SUCCESS,
  19. "Unexpected dallocm() error");
  20. assert_d_eq(allocm(&p, NULL, sz, 0), ALLOCM_SUCCESS,
  21. "Unexpected allocm() error");
  22. assert_d_eq(dallocm(p, 0), ALLOCM_SUCCESS,
  23. "Unexpected dallocm() error");
  24. nsz = 0;
  25. assert_d_eq(nallocm(&nsz, sz, ALLOCM_ZERO), ALLOCM_SUCCESS,
  26. "Unexpected nallocm() error");
  27. rsz = 0;
  28. assert_d_eq(allocm(&p, &rsz, sz, ALLOCM_ZERO), ALLOCM_SUCCESS,
  29. "Unexpected allocm() error");
  30. assert_zu_eq(nsz, rsz, "nallocm()/allocm() rsize mismatch");
  31. assert_d_eq(dallocm(p, 0), ALLOCM_SUCCESS,
  32. "Unexpected dallocm() error");
  33. }
  34. TEST_END
  35. TEST_BEGIN(test_alignment_and_size)
  36. {
  37. int r;
  38. size_t nsz, rsz, sz, alignment, total;
  39. unsigned i;
  40. void *ps[NITER];
  41. for (i = 0; i < NITER; i++)
  42. ps[i] = NULL;
  43. for (alignment = 8;
  44. alignment <= MAXALIGN;
  45. alignment <<= 1) {
  46. total = 0;
  47. for (sz = 1;
  48. sz < 3 * alignment && sz < (1U << 31);
  49. sz += (alignment >> (LG_SIZEOF_PTR-1)) - 1) {
  50. for (i = 0; i < NITER; i++) {
  51. nsz = 0;
  52. r = nallocm(&nsz, sz, ALLOCM_ALIGN(alignment) |
  53. ALLOCM_ZERO);
  54. assert_d_eq(r, ALLOCM_SUCCESS,
  55. "nallocm() error for alignment=%zu, "
  56. "size=%zu (%#zx): %d",
  57. alignment, sz, sz, r);
  58. rsz = 0;
  59. r = allocm(&ps[i], &rsz, sz,
  60. ALLOCM_ALIGN(alignment) | ALLOCM_ZERO);
  61. assert_d_eq(r, ALLOCM_SUCCESS,
  62. "allocm() error for alignment=%zu, "
  63. "size=%zu (%#zx): %d",
  64. alignment, sz, sz, r);
  65. assert_zu_ge(rsz, sz,
  66. "Real size smaller than expected for "
  67. "alignment=%zu, size=%zu", alignment, sz);
  68. assert_zu_eq(nsz, rsz,
  69. "nallocm()/allocm() rsize mismatch for "
  70. "alignment=%zu, size=%zu", alignment, sz);
  71. assert_ptr_null(
  72. (void *)((uintptr_t)ps[i] & (alignment-1)),
  73. "%p inadequately aligned for"
  74. " alignment=%zu, size=%zu", ps[i],
  75. alignment, sz);
  76. sallocm(ps[i], &rsz, 0);
  77. total += rsz;
  78. if (total >= (MAXALIGN << 1))
  79. break;
  80. }
  81. for (i = 0; i < NITER; i++) {
  82. if (ps[i] != NULL) {
  83. dallocm(ps[i], 0);
  84. ps[i] = NULL;
  85. }
  86. }
  87. }
  88. }
  89. }
  90. TEST_END
  91. int
  92. main(void)
  93. {
  94. return (test(
  95. test_basic,
  96. test_alignment_and_size));
  97. }