2
0

aligned_alloc.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include "test/jemalloc_test.h"
  2. #define CHUNK 0x400000
  3. /* #define MAXALIGN ((size_t)UINT64_C(0x80000000000)) */
  4. #define MAXALIGN ((size_t)0x2000000LU)
  5. #define NITER 4
  6. TEST_BEGIN(test_alignment_errors)
  7. {
  8. size_t alignment;
  9. void *p;
  10. alignment = 0;
  11. set_errno(0);
  12. p = aligned_alloc(alignment, 1);
  13. assert_false(p != NULL || get_errno() != EINVAL,
  14. "Expected error for invalid alignment %zu", alignment);
  15. for (alignment = sizeof(size_t); alignment < MAXALIGN;
  16. alignment <<= 1) {
  17. set_errno(0);
  18. p = aligned_alloc(alignment + 1, 1);
  19. assert_false(p != NULL || get_errno() != EINVAL,
  20. "Expected error for invalid alignment %zu",
  21. alignment + 1);
  22. }
  23. }
  24. TEST_END
  25. TEST_BEGIN(test_oom_errors)
  26. {
  27. size_t alignment, size;
  28. void *p;
  29. #if LG_SIZEOF_PTR == 3
  30. alignment = UINT64_C(0x8000000000000000);
  31. size = UINT64_C(0x8000000000000000);
  32. #else
  33. alignment = 0x80000000LU;
  34. size = 0x80000000LU;
  35. #endif
  36. set_errno(0);
  37. p = aligned_alloc(alignment, size);
  38. assert_false(p != NULL || get_errno() != ENOMEM,
  39. "Expected error for aligned_alloc(%zu, %zu)",
  40. alignment, size);
  41. #if LG_SIZEOF_PTR == 3
  42. alignment = UINT64_C(0x4000000000000000);
  43. size = UINT64_C(0xc000000000000001);
  44. #else
  45. alignment = 0x40000000LU;
  46. size = 0xc0000001LU;
  47. #endif
  48. set_errno(0);
  49. p = aligned_alloc(alignment, size);
  50. assert_false(p != NULL || get_errno() != ENOMEM,
  51. "Expected error for aligned_alloc(%zu, %zu)",
  52. alignment, size);
  53. alignment = 0x10LU;
  54. #if LG_SIZEOF_PTR == 3
  55. size = UINT64_C(0xfffffffffffffff0);
  56. #else
  57. size = 0xfffffff0LU;
  58. #endif
  59. set_errno(0);
  60. p = aligned_alloc(alignment, size);
  61. assert_false(p != NULL || get_errno() != ENOMEM,
  62. "Expected error for aligned_alloc(&p, %zu, %zu)",
  63. alignment, size);
  64. }
  65. TEST_END
  66. TEST_BEGIN(test_alignment_and_size)
  67. {
  68. size_t alignment, size, total;
  69. unsigned i;
  70. void *ps[NITER];
  71. for (i = 0; i < NITER; i++)
  72. ps[i] = NULL;
  73. for (alignment = 8;
  74. alignment <= MAXALIGN;
  75. alignment <<= 1) {
  76. total = 0;
  77. for (size = 1;
  78. size < 3 * alignment && size < (1U << 31);
  79. size += (alignment >> (LG_SIZEOF_PTR-1)) - 1) {
  80. for (i = 0; i < NITER; i++) {
  81. ps[i] = aligned_alloc(alignment, size);
  82. if (ps[i] == NULL) {
  83. char buf[BUFERROR_BUF];
  84. buferror(get_errno(), buf, sizeof(buf));
  85. test_fail(
  86. "Error for alignment=%zu, "
  87. "size=%zu (%#zx): %s",
  88. alignment, size, size, buf);
  89. }
  90. total += malloc_usable_size(ps[i]);
  91. if (total >= (MAXALIGN << 1))
  92. break;
  93. }
  94. for (i = 0; i < NITER; i++) {
  95. if (ps[i] != NULL) {
  96. free(ps[i]);
  97. ps[i] = NULL;
  98. }
  99. }
  100. }
  101. }
  102. }
  103. TEST_END
  104. int
  105. main(void)
  106. {
  107. return (test(
  108. test_alignment_errors,
  109. test_oom_errors,
  110. test_alignment_and_size));
  111. }