2
0

aligned_alloc.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include "test/jemalloc_test.h"
  2. #define MAXALIGN (((size_t)1) << 23)
  3. /*
  4. * On systems which can't merge extents, tests that call this function generate
  5. * a lot of dirty memory very quickly. Purging between cycles mitigates
  6. * potential OOM on e.g. 32-bit Windows.
  7. */
  8. static void
  9. purge(void) {
  10. assert_d_eq(mallctl("arena.0.purge", NULL, NULL, NULL, 0), 0,
  11. "Unexpected mallctl error");
  12. }
  13. TEST_BEGIN(test_alignment_errors) {
  14. size_t alignment;
  15. void *p;
  16. alignment = 0;
  17. set_errno(0);
  18. p = aligned_alloc(alignment, 1);
  19. assert_false(p != NULL || get_errno() != EINVAL,
  20. "Expected error for invalid alignment %zu", alignment);
  21. for (alignment = sizeof(size_t); alignment < MAXALIGN;
  22. alignment <<= 1) {
  23. set_errno(0);
  24. p = aligned_alloc(alignment + 1, 1);
  25. assert_false(p != NULL || get_errno() != EINVAL,
  26. "Expected error for invalid alignment %zu",
  27. alignment + 1);
  28. }
  29. }
  30. TEST_END
  31. TEST_BEGIN(test_oom_errors) {
  32. size_t alignment, size;
  33. void *p;
  34. #if LG_SIZEOF_PTR == 3
  35. alignment = UINT64_C(0x8000000000000000);
  36. size = UINT64_C(0x8000000000000000);
  37. #else
  38. alignment = 0x80000000LU;
  39. size = 0x80000000LU;
  40. #endif
  41. set_errno(0);
  42. p = aligned_alloc(alignment, size);
  43. assert_false(p != NULL || get_errno() != ENOMEM,
  44. "Expected error for aligned_alloc(%zu, %zu)",
  45. alignment, size);
  46. #if LG_SIZEOF_PTR == 3
  47. alignment = UINT64_C(0x4000000000000000);
  48. size = UINT64_C(0xc000000000000001);
  49. #else
  50. alignment = 0x40000000LU;
  51. size = 0xc0000001LU;
  52. #endif
  53. set_errno(0);
  54. p = aligned_alloc(alignment, size);
  55. assert_false(p != NULL || get_errno() != ENOMEM,
  56. "Expected error for aligned_alloc(%zu, %zu)",
  57. alignment, size);
  58. alignment = 0x10LU;
  59. #if LG_SIZEOF_PTR == 3
  60. size = UINT64_C(0xfffffffffffffff0);
  61. #else
  62. size = 0xfffffff0LU;
  63. #endif
  64. set_errno(0);
  65. p = aligned_alloc(alignment, size);
  66. assert_false(p != NULL || get_errno() != ENOMEM,
  67. "Expected error for aligned_alloc(&p, %zu, %zu)",
  68. alignment, size);
  69. }
  70. TEST_END
  71. TEST_BEGIN(test_alignment_and_size) {
  72. #define NITER 4
  73. size_t alignment, size, total;
  74. unsigned i;
  75. void *ps[NITER];
  76. for (i = 0; i < NITER; i++) {
  77. ps[i] = NULL;
  78. }
  79. for (alignment = 8;
  80. alignment <= MAXALIGN;
  81. alignment <<= 1) {
  82. total = 0;
  83. for (size = 1;
  84. size < 3 * alignment && size < (1U << 31);
  85. size += (alignment >> (LG_SIZEOF_PTR-1)) - 1) {
  86. for (i = 0; i < NITER; i++) {
  87. ps[i] = aligned_alloc(alignment, size);
  88. if (ps[i] == NULL) {
  89. char buf[BUFERROR_BUF];
  90. buferror(get_errno(), buf, sizeof(buf));
  91. test_fail(
  92. "Error for alignment=%zu, "
  93. "size=%zu (%#zx): %s",
  94. alignment, size, size, buf);
  95. }
  96. total += malloc_usable_size(ps[i]);
  97. if (total >= (MAXALIGN << 1)) {
  98. break;
  99. }
  100. }
  101. for (i = 0; i < NITER; i++) {
  102. if (ps[i] != NULL) {
  103. free(ps[i]);
  104. ps[i] = NULL;
  105. }
  106. }
  107. }
  108. purge();
  109. }
  110. #undef NITER
  111. }
  112. TEST_END
  113. int
  114. main(void) {
  115. return test(
  116. test_alignment_errors,
  117. test_oom_errors,
  118. test_alignment_and_size);
  119. }