tst-memalign-2.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* Test for memalign chunk reuse.
  2. Copyright (C) 2022-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <malloc.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. #include <array_length.h>
  21. #include <libc-pointer-arith.h>
  22. #include <support/check.h>
  23. typedef struct TestCase {
  24. size_t size;
  25. size_t alignment;
  26. void *ptr1;
  27. void *ptr2;
  28. } TestCase;
  29. static TestCase tcache_allocs[] = {
  30. { 24, 32, NULL, NULL },
  31. { 24, 64, NULL, NULL },
  32. { 128, 128, NULL, NULL },
  33. { 500, 128, NULL, NULL }
  34. };
  35. #define TN array_length (tcache_allocs)
  36. static TestCase large_allocs[] = {
  37. { 23450, 64, NULL, NULL },
  38. { 23450, 64, NULL, NULL },
  39. { 23550, 64, NULL, NULL },
  40. { 23550, 64, NULL, NULL },
  41. { 23650, 64, NULL, NULL },
  42. { 23650, 64, NULL, NULL },
  43. { 33650, 64, NULL, NULL },
  44. { 33650, 64, NULL, NULL }
  45. };
  46. #define LN array_length (large_allocs)
  47. void *p;
  48. /* Sanity checks, ancillary to the actual test. */
  49. #define CHECK(p,a) \
  50. if (p == NULL || !PTR_IS_ALIGNED (p, a)) \
  51. FAIL_EXIT1 ("NULL or misaligned memory detected.\n");
  52. static int
  53. do_test (void)
  54. {
  55. int i, j;
  56. int count;
  57. void *ptr[10];
  58. void *p;
  59. /* TCache test. */
  60. for (i = 0; i < TN; ++ i)
  61. {
  62. size_t sz2;
  63. tcache_allocs[i].ptr1 = memalign (tcache_allocs[i].alignment, tcache_allocs[i].size);
  64. CHECK (tcache_allocs[i].ptr1, tcache_allocs[i].alignment);
  65. sz2 = malloc_usable_size (tcache_allocs[i].ptr1);
  66. free (tcache_allocs[i].ptr1);
  67. /* This should return the same chunk as was just free'd. */
  68. tcache_allocs[i].ptr2 = memalign (tcache_allocs[i].alignment, sz2);
  69. CHECK (tcache_allocs[i].ptr2, tcache_allocs[i].alignment);
  70. free (tcache_allocs[i].ptr2);
  71. TEST_VERIFY (tcache_allocs[i].ptr1 == tcache_allocs[i].ptr2);
  72. }
  73. /* Test for non-head tcache hits. This exercises the memalign
  74. scanning code to find matching allocations. */
  75. for (i = 0; i < array_length (ptr); ++ i)
  76. {
  77. if (i == 4)
  78. {
  79. ptr[i] = memalign (64, 256);
  80. CHECK (ptr[i], 64);
  81. }
  82. else
  83. {
  84. ptr[i] = malloc (256);
  85. CHECK (ptr[i], 4);
  86. }
  87. }
  88. for (i = 0; i < array_length (ptr); ++ i)
  89. free (ptr[i]);
  90. p = memalign (64, 256);
  91. CHECK (p, 64);
  92. count = 0;
  93. for (i = 0; i < 10; ++ i)
  94. if (ptr[i] == p)
  95. ++ count;
  96. free (p);
  97. TEST_VERIFY (count > 0);
  98. /* Large bins test. This verifies that the over-allocated parts
  99. that memalign releases for future allocations can be reused by
  100. memalign itself at least in some cases. */
  101. for (i = 0; i < LN; ++ i)
  102. {
  103. large_allocs[i].ptr1 = memalign (large_allocs[i].alignment, large_allocs[i].size);
  104. CHECK (large_allocs[i].ptr1, large_allocs[i].alignment);
  105. /* Keep chunks from combining by fragmenting the heap. */
  106. p = malloc (512);
  107. CHECK (p, 4);
  108. }
  109. for (i = 0; i < LN; ++ i)
  110. free (large_allocs[i].ptr1);
  111. /* Force the unsorted bins to be scanned and moved to small/large
  112. bins. */
  113. p = malloc (60000);
  114. for (i = 0; i < LN; ++ i)
  115. {
  116. large_allocs[i].ptr2 = memalign (large_allocs[i].alignment, large_allocs[i].size);
  117. CHECK (large_allocs[i].ptr2, large_allocs[i].alignment);
  118. }
  119. count = 0;
  120. for (i = 0; i < LN; ++ i)
  121. {
  122. int ok = 0;
  123. for (j = 0; j < LN; ++ j)
  124. if (large_allocs[i].ptr1 == large_allocs[j].ptr2)
  125. ok = 1;
  126. if (ok == 1)
  127. count ++;
  128. }
  129. /* The allocation algorithm is complicated outside of the memalign
  130. logic, so just make sure it's working for most of the
  131. allocations. This avoids possible boundary conditions with
  132. empty/full heaps. */
  133. TEST_VERIFY (count > LN / 2);
  134. return 0;
  135. }
  136. #include <support/test-driver.c>