aligned_alloc.c 2.6 KB

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