microbench.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include "test/jemalloc_test.h"
  2. #include "test/bench.h"
  3. static void
  4. malloc_free(void) {
  5. /* The compiler can optimize away free(malloc(1))! */
  6. void *p = malloc(1);
  7. if (p == NULL) {
  8. test_fail("Unexpected malloc() failure");
  9. return;
  10. }
  11. free(p);
  12. }
  13. static void
  14. mallocx_free(void) {
  15. void *p = mallocx(1, 0);
  16. if (p == NULL) {
  17. test_fail("Unexpected mallocx() failure");
  18. return;
  19. }
  20. free(p);
  21. }
  22. TEST_BEGIN(test_malloc_vs_mallocx) {
  23. compare_funcs(10*1000*1000, 100*1000*1000, "malloc",
  24. malloc_free, "mallocx", mallocx_free);
  25. }
  26. TEST_END
  27. static void
  28. malloc_dallocx(void) {
  29. void *p = malloc(1);
  30. if (p == NULL) {
  31. test_fail("Unexpected malloc() failure");
  32. return;
  33. }
  34. dallocx(p, 0);
  35. }
  36. static void
  37. malloc_sdallocx(void) {
  38. void *p = malloc(1);
  39. if (p == NULL) {
  40. test_fail("Unexpected malloc() failure");
  41. return;
  42. }
  43. sdallocx(p, 1, 0);
  44. }
  45. TEST_BEGIN(test_free_vs_dallocx) {
  46. compare_funcs(10*1000*1000, 100*1000*1000, "free", malloc_free,
  47. "dallocx", malloc_dallocx);
  48. }
  49. TEST_END
  50. TEST_BEGIN(test_dallocx_vs_sdallocx) {
  51. compare_funcs(10*1000*1000, 100*1000*1000, "dallocx", malloc_dallocx,
  52. "sdallocx", malloc_sdallocx);
  53. }
  54. TEST_END
  55. static void
  56. malloc_mus_free(void) {
  57. void *p;
  58. p = malloc(1);
  59. if (p == NULL) {
  60. test_fail("Unexpected malloc() failure");
  61. return;
  62. }
  63. TEST_MALLOC_SIZE(p);
  64. free(p);
  65. }
  66. static void
  67. malloc_sallocx_free(void) {
  68. void *p;
  69. p = malloc(1);
  70. if (p == NULL) {
  71. test_fail("Unexpected malloc() failure");
  72. return;
  73. }
  74. if (sallocx(p, 0) < 1) {
  75. test_fail("Unexpected sallocx() failure");
  76. }
  77. free(p);
  78. }
  79. TEST_BEGIN(test_mus_vs_sallocx) {
  80. compare_funcs(10*1000*1000, 100*1000*1000, "malloc_usable_size",
  81. malloc_mus_free, "sallocx", malloc_sallocx_free);
  82. }
  83. TEST_END
  84. static void
  85. malloc_nallocx_free(void) {
  86. void *p;
  87. p = malloc(1);
  88. if (p == NULL) {
  89. test_fail("Unexpected malloc() failure");
  90. return;
  91. }
  92. if (nallocx(1, 0) < 1) {
  93. test_fail("Unexpected nallocx() failure");
  94. }
  95. free(p);
  96. }
  97. TEST_BEGIN(test_sallocx_vs_nallocx) {
  98. compare_funcs(10*1000*1000, 100*1000*1000, "sallocx",
  99. malloc_sallocx_free, "nallocx", malloc_nallocx_free);
  100. }
  101. TEST_END
  102. int
  103. main(void) {
  104. return test_no_reentrancy(
  105. test_malloc_vs_mallocx,
  106. test_free_vs_dallocx,
  107. test_dallocx_vs_sdallocx,
  108. test_mus_vs_sallocx,
  109. test_sallocx_vs_nallocx);
  110. }