posix_memalign.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. int err;
  13. void *p, *ps[NITER];
  14. malloc_printf("Test begin\n");
  15. /* Test error conditions. */
  16. for (alignment = 0; alignment < sizeof(void *); alignment++) {
  17. err = posix_memalign(&p, alignment, 1);
  18. if (err != EINVAL) {
  19. malloc_printf(
  20. "Expected error for invalid alignment %zu\n",
  21. alignment);
  22. }
  23. }
  24. for (alignment = sizeof(size_t); alignment < MAXALIGN;
  25. alignment <<= 1) {
  26. err = posix_memalign(&p, alignment + 1, 1);
  27. if (err == 0) {
  28. malloc_printf(
  29. "Expected error for invalid alignment %zu\n",
  30. alignment + 1);
  31. }
  32. }
  33. #if LG_SIZEOF_PTR == 3
  34. alignment = UINT64_C(0x8000000000000000);
  35. size = UINT64_C(0x8000000000000000);
  36. #else
  37. alignment = 0x80000000LU;
  38. size = 0x80000000LU;
  39. #endif
  40. err = posix_memalign(&p, alignment, size);
  41. if (err == 0) {
  42. malloc_printf(
  43. "Expected error for posix_memalign(&p, %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. err = posix_memalign(&p, alignment, size);
  54. if (err == 0) {
  55. malloc_printf(
  56. "Expected error for posix_memalign(&p, %zu, %zu)\n",
  57. alignment, size);
  58. }
  59. alignment = 0x10LU;
  60. #if LG_SIZEOF_PTR == 3
  61. size = UINT64_C(0xfffffffffffffff0);
  62. #else
  63. size = 0xfffffff0LU;
  64. #endif
  65. err = posix_memalign(&p, alignment, size);
  66. if (err == 0) {
  67. malloc_printf(
  68. "Expected error for posix_memalign(&p, %zu, %zu)\n",
  69. alignment, size);
  70. }
  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. malloc_printf("Alignment: %zu\n", alignment);
  78. for (size = 1;
  79. size < 3 * alignment && size < (1U << 31);
  80. size += (alignment >> (LG_SIZEOF_PTR-1)) - 1) {
  81. for (i = 0; i < NITER; i++) {
  82. err = posix_memalign(&ps[i],
  83. alignment, size);
  84. if (err) {
  85. malloc_printf(
  86. "Error for size %zu (%#zx): %s\n",
  87. size, size, strerror(err));
  88. exit(1);
  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. malloc_printf("Test end\n");
  103. return (0);
  104. }