bitmap.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #define JEMALLOC_MANGLE
  2. #include "jemalloc_test.h"
  3. #if (LG_BITMAP_MAXBITS > 12)
  4. # define MAXBITS 4500
  5. #else
  6. # define MAXBITS (1U << LG_BITMAP_MAXBITS)
  7. #endif
  8. static void
  9. test_bitmap_size(void)
  10. {
  11. size_t i, prev_size;
  12. prev_size = 0;
  13. for (i = 1; i <= MAXBITS; i++) {
  14. size_t size = bitmap_size(i);
  15. assert(size >= prev_size);
  16. prev_size = size;
  17. }
  18. }
  19. static void
  20. test_bitmap_init(void)
  21. {
  22. size_t i;
  23. for (i = 1; i <= MAXBITS; i++) {
  24. bitmap_info_t binfo;
  25. bitmap_info_init(&binfo, i);
  26. {
  27. size_t j;
  28. bitmap_t *bitmap = malloc(sizeof(bitmap_t) *
  29. bitmap_info_ngroups(&binfo));
  30. bitmap_init(bitmap, &binfo);
  31. for (j = 0; j < i; j++)
  32. assert(bitmap_get(bitmap, &binfo, j) == false);
  33. free(bitmap);
  34. }
  35. }
  36. }
  37. static void
  38. test_bitmap_set(void)
  39. {
  40. size_t i;
  41. for (i = 1; i <= MAXBITS; i++) {
  42. bitmap_info_t binfo;
  43. bitmap_info_init(&binfo, i);
  44. {
  45. size_t j;
  46. bitmap_t *bitmap = malloc(sizeof(bitmap_t) *
  47. bitmap_info_ngroups(&binfo));
  48. bitmap_init(bitmap, &binfo);
  49. for (j = 0; j < i; j++)
  50. bitmap_set(bitmap, &binfo, j);
  51. assert(bitmap_full(bitmap, &binfo));
  52. free(bitmap);
  53. }
  54. }
  55. }
  56. static void
  57. test_bitmap_unset(void)
  58. {
  59. size_t i;
  60. for (i = 1; i <= MAXBITS; i++) {
  61. bitmap_info_t binfo;
  62. bitmap_info_init(&binfo, i);
  63. {
  64. size_t j;
  65. bitmap_t *bitmap = malloc(sizeof(bitmap_t) *
  66. bitmap_info_ngroups(&binfo));
  67. bitmap_init(bitmap, &binfo);
  68. for (j = 0; j < i; j++)
  69. bitmap_set(bitmap, &binfo, j);
  70. assert(bitmap_full(bitmap, &binfo));
  71. for (j = 0; j < i; j++)
  72. bitmap_unset(bitmap, &binfo, j);
  73. for (j = 0; j < i; j++)
  74. bitmap_set(bitmap, &binfo, j);
  75. assert(bitmap_full(bitmap, &binfo));
  76. free(bitmap);
  77. }
  78. }
  79. }
  80. static void
  81. test_bitmap_sfu(void)
  82. {
  83. size_t i;
  84. for (i = 1; i <= MAXBITS; i++) {
  85. bitmap_info_t binfo;
  86. bitmap_info_init(&binfo, i);
  87. {
  88. ssize_t j;
  89. bitmap_t *bitmap = malloc(sizeof(bitmap_t) *
  90. bitmap_info_ngroups(&binfo));
  91. bitmap_init(bitmap, &binfo);
  92. /* Iteratively set bits starting at the beginning. */
  93. for (j = 0; j < i; j++)
  94. assert(bitmap_sfu(bitmap, &binfo) == j);
  95. assert(bitmap_full(bitmap, &binfo));
  96. /*
  97. * Iteratively unset bits starting at the end, and
  98. * verify that bitmap_sfu() reaches the unset bits.
  99. */
  100. for (j = i - 1; j >= 0; j--) {
  101. bitmap_unset(bitmap, &binfo, j);
  102. assert(bitmap_sfu(bitmap, &binfo) == j);
  103. bitmap_unset(bitmap, &binfo, j);
  104. }
  105. assert(bitmap_get(bitmap, &binfo, 0) == false);
  106. /*
  107. * Iteratively set bits starting at the beginning, and
  108. * verify that bitmap_sfu() looks past them.
  109. */
  110. for (j = 1; j < i; j++) {
  111. bitmap_set(bitmap, &binfo, j - 1);
  112. assert(bitmap_sfu(bitmap, &binfo) == j);
  113. bitmap_unset(bitmap, &binfo, j);
  114. }
  115. assert(bitmap_sfu(bitmap, &binfo) == i - 1);
  116. assert(bitmap_full(bitmap, &binfo));
  117. free(bitmap);
  118. }
  119. }
  120. }
  121. int
  122. main(void)
  123. {
  124. malloc_printf("Test begin\n");
  125. test_bitmap_size();
  126. test_bitmap_init();
  127. test_bitmap_set();
  128. test_bitmap_unset();
  129. test_bitmap_sfu();
  130. malloc_printf("Test end\n");
  131. return (0);
  132. }