2
0

posix_memalign.c 2.5 KB

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