allocm.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. int r;
  11. void *p;
  12. size_t nsz, rsz, sz, alignment, total;
  13. unsigned i;
  14. void *ps[NITER];
  15. malloc_printf("Test begin\n");
  16. sz = 42;
  17. nsz = 0;
  18. r = nallocm(&nsz, sz, 0);
  19. if (r != ALLOCM_SUCCESS) {
  20. malloc_printf("Unexpected nallocm() error\n");
  21. abort();
  22. }
  23. rsz = 0;
  24. r = allocm(&p, &rsz, sz, 0);
  25. if (r != ALLOCM_SUCCESS) {
  26. malloc_printf("Unexpected allocm() error\n");
  27. abort();
  28. }
  29. if (rsz < sz)
  30. malloc_printf("Real size smaller than expected\n");
  31. if (nsz != rsz)
  32. malloc_printf("nallocm()/allocm() rsize mismatch\n");
  33. if (dallocm(p, 0) != ALLOCM_SUCCESS)
  34. malloc_printf("Unexpected dallocm() error\n");
  35. r = allocm(&p, NULL, sz, 0);
  36. if (r != ALLOCM_SUCCESS) {
  37. malloc_printf("Unexpected allocm() error\n");
  38. abort();
  39. }
  40. if (dallocm(p, 0) != ALLOCM_SUCCESS)
  41. malloc_printf("Unexpected dallocm() error\n");
  42. nsz = 0;
  43. r = nallocm(&nsz, sz, ALLOCM_ZERO);
  44. if (r != ALLOCM_SUCCESS) {
  45. malloc_printf("Unexpected nallocm() error\n");
  46. abort();
  47. }
  48. rsz = 0;
  49. r = allocm(&p, &rsz, sz, ALLOCM_ZERO);
  50. if (r != ALLOCM_SUCCESS) {
  51. malloc_printf("Unexpected allocm() error\n");
  52. abort();
  53. }
  54. if (nsz != rsz)
  55. malloc_printf("nallocm()/allocm() rsize mismatch\n");
  56. if (dallocm(p, 0) != ALLOCM_SUCCESS)
  57. malloc_printf("Unexpected dallocm() error\n");
  58. #if LG_SIZEOF_PTR == 3
  59. alignment = UINT64_C(0x8000000000000000);
  60. sz = UINT64_C(0x8000000000000000);
  61. #else
  62. alignment = 0x80000000LU;
  63. sz = 0x80000000LU;
  64. #endif
  65. nsz = 0;
  66. r = nallocm(&nsz, sz, ALLOCM_ALIGN(alignment));
  67. if (r == ALLOCM_SUCCESS) {
  68. malloc_printf(
  69. "Expected error for nallocm(&nsz, %zu, %#x)\n",
  70. sz, ALLOCM_ALIGN(alignment));
  71. }
  72. rsz = 0;
  73. r = allocm(&p, &rsz, sz, ALLOCM_ALIGN(alignment));
  74. if (r == ALLOCM_SUCCESS) {
  75. malloc_printf(
  76. "Expected error for allocm(&p, %zu, %#x)\n",
  77. sz, ALLOCM_ALIGN(alignment));
  78. }
  79. if (nsz != rsz)
  80. malloc_printf("nallocm()/allocm() rsize mismatch\n");
  81. #if LG_SIZEOF_PTR == 3
  82. alignment = UINT64_C(0x4000000000000000);
  83. sz = UINT64_C(0x8400000000000001);
  84. #else
  85. alignment = 0x40000000LU;
  86. sz = 0x84000001LU;
  87. #endif
  88. nsz = 0;
  89. r = nallocm(&nsz, sz, ALLOCM_ALIGN(alignment));
  90. if (r != ALLOCM_SUCCESS)
  91. malloc_printf("Unexpected nallocm() error\n");
  92. rsz = 0;
  93. r = allocm(&p, &rsz, sz, ALLOCM_ALIGN(alignment));
  94. if (r == ALLOCM_SUCCESS) {
  95. malloc_printf(
  96. "Expected error for allocm(&p, %zu, %#x)\n",
  97. sz, ALLOCM_ALIGN(alignment));
  98. }
  99. alignment = 0x10LU;
  100. #if LG_SIZEOF_PTR == 3
  101. sz = UINT64_C(0xfffffffffffffff0);
  102. #else
  103. sz = 0xfffffff0LU;
  104. #endif
  105. nsz = 0;
  106. r = nallocm(&nsz, sz, ALLOCM_ALIGN(alignment));
  107. if (r == ALLOCM_SUCCESS) {
  108. malloc_printf(
  109. "Expected error for nallocm(&nsz, %zu, %#x)\n",
  110. sz, ALLOCM_ALIGN(alignment));
  111. }
  112. rsz = 0;
  113. r = allocm(&p, &rsz, sz, ALLOCM_ALIGN(alignment));
  114. if (r == ALLOCM_SUCCESS) {
  115. malloc_printf(
  116. "Expected error for allocm(&p, %zu, %#x)\n",
  117. sz, ALLOCM_ALIGN(alignment));
  118. }
  119. if (nsz != rsz)
  120. malloc_printf("nallocm()/allocm() rsize mismatch\n");
  121. for (i = 0; i < NITER; i++)
  122. ps[i] = NULL;
  123. for (alignment = 8;
  124. alignment <= MAXALIGN;
  125. alignment <<= 1) {
  126. total = 0;
  127. malloc_printf("Alignment: %zu\n", alignment);
  128. for (sz = 1;
  129. sz < 3 * alignment && sz < (1U << 31);
  130. sz += (alignment >> (LG_SIZEOF_PTR-1)) - 1) {
  131. for (i = 0; i < NITER; i++) {
  132. nsz = 0;
  133. r = nallocm(&nsz, sz,
  134. ALLOCM_ALIGN(alignment) | ALLOCM_ZERO);
  135. if (r != ALLOCM_SUCCESS) {
  136. malloc_printf(
  137. "nallocm() error for size %zu"
  138. " (%#zx): %d\n",
  139. sz, sz, r);
  140. exit(1);
  141. }
  142. rsz = 0;
  143. r = allocm(&ps[i], &rsz, sz,
  144. ALLOCM_ALIGN(alignment) | ALLOCM_ZERO);
  145. if (r != ALLOCM_SUCCESS) {
  146. malloc_printf(
  147. "allocm() error for size %zu"
  148. " (%#zx): %d\n",
  149. sz, sz, r);
  150. exit(1);
  151. }
  152. if (rsz < sz) {
  153. malloc_printf(
  154. "Real size smaller than"
  155. " expected\n");
  156. }
  157. if (nsz != rsz) {
  158. malloc_printf(
  159. "nallocm()/allocm() rsize"
  160. " mismatch\n");
  161. }
  162. if ((uintptr_t)p & (alignment-1)) {
  163. malloc_printf(
  164. "%p inadequately aligned for"
  165. " alignment: %zu\n", p, alignment);
  166. }
  167. sallocm(ps[i], &rsz, 0);
  168. total += rsz;
  169. if (total >= (MAXALIGN << 1))
  170. break;
  171. }
  172. for (i = 0; i < NITER; i++) {
  173. if (ps[i] != NULL) {
  174. dallocm(ps[i], 0);
  175. ps[i] = NULL;
  176. }
  177. }
  178. }
  179. }
  180. malloc_printf("Test end\n");
  181. return (0);
  182. }