ph.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. #include "test/jemalloc_test.h"
  2. #include "jemalloc/internal/ph.h"
  3. typedef struct node_s node_t;
  4. ph_structs(heap, node_t);
  5. struct node_s {
  6. #define NODE_MAGIC 0x9823af7e
  7. uint32_t magic;
  8. heap_link_t link;
  9. uint64_t key;
  10. };
  11. static int
  12. node_cmp(const node_t *a, const node_t *b) {
  13. int ret;
  14. ret = (a->key > b->key) - (a->key < b->key);
  15. if (ret == 0) {
  16. /*
  17. * Duplicates are not allowed in the heap, so force an
  18. * arbitrary ordering for non-identical items with equal keys.
  19. */
  20. ret = (((uintptr_t)a) > ((uintptr_t)b))
  21. - (((uintptr_t)a) < ((uintptr_t)b));
  22. }
  23. return ret;
  24. }
  25. static int
  26. node_cmp_magic(const node_t *a, const node_t *b) {
  27. expect_u32_eq(a->magic, NODE_MAGIC, "Bad magic");
  28. expect_u32_eq(b->magic, NODE_MAGIC, "Bad magic");
  29. return node_cmp(a, b);
  30. }
  31. ph_gen(static, heap, node_t, link, node_cmp_magic);
  32. static node_t *
  33. node_next_get(const node_t *node) {
  34. return phn_next_get((node_t *)node, offsetof(node_t, link));
  35. }
  36. static node_t *
  37. node_prev_get(const node_t *node) {
  38. return phn_prev_get((node_t *)node, offsetof(node_t, link));
  39. }
  40. static node_t *
  41. node_lchild_get(const node_t *node) {
  42. return phn_lchild_get((node_t *)node, offsetof(node_t, link));
  43. }
  44. static void
  45. node_print(const node_t *node, unsigned depth) {
  46. unsigned i;
  47. node_t *leftmost_child, *sibling;
  48. for (i = 0; i < depth; i++) {
  49. malloc_printf("\t");
  50. }
  51. malloc_printf("%2"FMTu64"\n", node->key);
  52. leftmost_child = node_lchild_get(node);
  53. if (leftmost_child == NULL) {
  54. return;
  55. }
  56. node_print(leftmost_child, depth + 1);
  57. for (sibling = node_next_get(leftmost_child); sibling !=
  58. NULL; sibling = node_next_get(sibling)) {
  59. node_print(sibling, depth + 1);
  60. }
  61. }
  62. static void
  63. heap_print(const heap_t *heap) {
  64. node_t *auxelm;
  65. malloc_printf("vvv heap %p vvv\n", heap);
  66. if (heap->ph.root == NULL) {
  67. goto label_return;
  68. }
  69. node_print(heap->ph.root, 0);
  70. for (auxelm = node_next_get(heap->ph.root); auxelm != NULL;
  71. auxelm = node_next_get(auxelm)) {
  72. expect_ptr_eq(node_next_get(node_prev_get(auxelm)), auxelm,
  73. "auxelm's prev doesn't link to auxelm");
  74. node_print(auxelm, 0);
  75. }
  76. label_return:
  77. malloc_printf("^^^ heap %p ^^^\n", heap);
  78. }
  79. static unsigned
  80. node_validate(const node_t *node, const node_t *parent) {
  81. unsigned nnodes = 1;
  82. node_t *leftmost_child, *sibling;
  83. if (parent != NULL) {
  84. expect_d_ge(node_cmp_magic(node, parent), 0,
  85. "Child is less than parent");
  86. }
  87. leftmost_child = node_lchild_get(node);
  88. if (leftmost_child == NULL) {
  89. return nnodes;
  90. }
  91. expect_ptr_eq(node_prev_get(leftmost_child),
  92. (void *)node, "Leftmost child does not link to node");
  93. nnodes += node_validate(leftmost_child, node);
  94. for (sibling = node_next_get(leftmost_child); sibling !=
  95. NULL; sibling = node_next_get(sibling)) {
  96. expect_ptr_eq(node_next_get(node_prev_get(sibling)), sibling,
  97. "sibling's prev doesn't link to sibling");
  98. nnodes += node_validate(sibling, node);
  99. }
  100. return nnodes;
  101. }
  102. static unsigned
  103. heap_validate(const heap_t *heap) {
  104. unsigned nnodes = 0;
  105. node_t *auxelm;
  106. if (heap->ph.root == NULL) {
  107. goto label_return;
  108. }
  109. nnodes += node_validate(heap->ph.root, NULL);
  110. for (auxelm = node_next_get(heap->ph.root); auxelm != NULL;
  111. auxelm = node_next_get(auxelm)) {
  112. expect_ptr_eq(node_next_get(node_prev_get(auxelm)), auxelm,
  113. "auxelm's prev doesn't link to auxelm");
  114. nnodes += node_validate(auxelm, NULL);
  115. }
  116. label_return:
  117. if (false) {
  118. heap_print(heap);
  119. }
  120. return nnodes;
  121. }
  122. TEST_BEGIN(test_ph_empty) {
  123. heap_t heap;
  124. heap_new(&heap);
  125. expect_true(heap_empty(&heap), "Heap should be empty");
  126. expect_ptr_null(heap_first(&heap), "Unexpected node");
  127. expect_ptr_null(heap_any(&heap), "Unexpected node");
  128. }
  129. TEST_END
  130. static void
  131. node_remove(heap_t *heap, node_t *node) {
  132. heap_remove(heap, node);
  133. node->magic = 0;
  134. }
  135. static node_t *
  136. node_remove_first(heap_t *heap) {
  137. node_t *node = heap_remove_first(heap);
  138. node->magic = 0;
  139. return node;
  140. }
  141. static node_t *
  142. node_remove_any(heap_t *heap) {
  143. node_t *node = heap_remove_any(heap);
  144. node->magic = 0;
  145. return node;
  146. }
  147. TEST_BEGIN(test_ph_random) {
  148. #define NNODES 25
  149. #define NBAGS 250
  150. #define SEED 42
  151. sfmt_t *sfmt;
  152. uint64_t bag[NNODES];
  153. heap_t heap;
  154. node_t nodes[NNODES];
  155. unsigned i, j, k;
  156. sfmt = init_gen_rand(SEED);
  157. for (i = 0; i < NBAGS; i++) {
  158. switch (i) {
  159. case 0:
  160. /* Insert in order. */
  161. for (j = 0; j < NNODES; j++) {
  162. bag[j] = j;
  163. }
  164. break;
  165. case 1:
  166. /* Insert in reverse order. */
  167. for (j = 0; j < NNODES; j++) {
  168. bag[j] = NNODES - j - 1;
  169. }
  170. break;
  171. default:
  172. for (j = 0; j < NNODES; j++) {
  173. bag[j] = gen_rand64_range(sfmt, NNODES);
  174. }
  175. }
  176. for (j = 1; j <= NNODES; j++) {
  177. /* Initialize heap and nodes. */
  178. heap_new(&heap);
  179. expect_u_eq(heap_validate(&heap), 0,
  180. "Incorrect node count");
  181. for (k = 0; k < j; k++) {
  182. nodes[k].magic = NODE_MAGIC;
  183. nodes[k].key = bag[k];
  184. }
  185. /* Insert nodes. */
  186. for (k = 0; k < j; k++) {
  187. heap_insert(&heap, &nodes[k]);
  188. if (i % 13 == 12) {
  189. expect_ptr_not_null(heap_any(&heap),
  190. "Heap should not be empty");
  191. /* Trigger merging. */
  192. expect_ptr_not_null(heap_first(&heap),
  193. "Heap should not be empty");
  194. }
  195. expect_u_eq(heap_validate(&heap), k + 1,
  196. "Incorrect node count");
  197. }
  198. expect_false(heap_empty(&heap),
  199. "Heap should not be empty");
  200. /* Remove nodes. */
  201. switch (i % 6) {
  202. case 0:
  203. for (k = 0; k < j; k++) {
  204. expect_u_eq(heap_validate(&heap), j - k,
  205. "Incorrect node count");
  206. node_remove(&heap, &nodes[k]);
  207. expect_u_eq(heap_validate(&heap), j - k
  208. - 1, "Incorrect node count");
  209. }
  210. break;
  211. case 1:
  212. for (k = j; k > 0; k--) {
  213. node_remove(&heap, &nodes[k-1]);
  214. expect_u_eq(heap_validate(&heap), k - 1,
  215. "Incorrect node count");
  216. }
  217. break;
  218. case 2: {
  219. node_t *prev = NULL;
  220. for (k = 0; k < j; k++) {
  221. node_t *node = node_remove_first(&heap);
  222. expect_u_eq(heap_validate(&heap), j - k
  223. - 1, "Incorrect node count");
  224. if (prev != NULL) {
  225. expect_d_ge(node_cmp(node,
  226. prev), 0,
  227. "Bad removal order");
  228. }
  229. prev = node;
  230. }
  231. break;
  232. } case 3: {
  233. node_t *prev = NULL;
  234. for (k = 0; k < j; k++) {
  235. node_t *node = heap_first(&heap);
  236. expect_u_eq(heap_validate(&heap), j - k,
  237. "Incorrect node count");
  238. if (prev != NULL) {
  239. expect_d_ge(node_cmp(node,
  240. prev), 0,
  241. "Bad removal order");
  242. }
  243. node_remove(&heap, node);
  244. expect_u_eq(heap_validate(&heap), j - k
  245. - 1, "Incorrect node count");
  246. prev = node;
  247. }
  248. break;
  249. } case 4: {
  250. for (k = 0; k < j; k++) {
  251. node_remove_any(&heap);
  252. expect_u_eq(heap_validate(&heap), j - k
  253. - 1, "Incorrect node count");
  254. }
  255. break;
  256. } case 5: {
  257. for (k = 0; k < j; k++) {
  258. node_t *node = heap_any(&heap);
  259. expect_u_eq(heap_validate(&heap), j - k,
  260. "Incorrect node count");
  261. node_remove(&heap, node);
  262. expect_u_eq(heap_validate(&heap), j - k
  263. - 1, "Incorrect node count");
  264. }
  265. break;
  266. } default:
  267. not_reached();
  268. }
  269. expect_ptr_null(heap_first(&heap),
  270. "Heap should be empty");
  271. expect_ptr_null(heap_any(&heap),
  272. "Heap should be empty");
  273. expect_true(heap_empty(&heap), "Heap should be empty");
  274. }
  275. }
  276. fini_gen_rand(sfmt);
  277. #undef NNODES
  278. #undef SEED
  279. }
  280. TEST_END
  281. int
  282. main(void) {
  283. return test(
  284. test_ph_empty,
  285. test_ph_random);
  286. }