| 481 | } |
| 482 | |
| 483 | static struct plugin **plugin_hook_make_ordered(const tal_t *ctx, |
| 484 | struct plugin_hook *hook) |
| 485 | { |
| 486 | struct hook_node *graph, *n; |
| 487 | struct hook_instance **done; |
| 488 | |
| 489 | /* Populate graph nodes */ |
| 490 | graph = tal_arr(tmpctx, struct hook_node, tal_count(hook->hooks)); |
| 491 | for (size_t i = 0; i < tal_count(graph); i++) { |
| 492 | graph[i].finished = false; |
| 493 | graph[i].hook = hook->hooks[i]; |
| 494 | graph[i].num_incoming = 0; |
| 495 | graph[i].outgoing = tal_arr(graph, struct hook_node *, 0); |
| 496 | } |
| 497 | |
| 498 | /* Add edges. */ |
| 499 | for (size_t i = 0; i < tal_count(graph); i++) { |
| 500 | for (size_t j = 0; j < tal_count(graph[i].hook->before); j++) { |
| 501 | n = find_hook(graph, graph[i].hook->before[j]); |
| 502 | if (!n) { |
| 503 | /* This is useful for typos! */ |
| 504 | log_debug(graph[i].hook->plugin->log, |
| 505 | "hook %s before unknown plugin %s", |
| 506 | hook->name, |
| 507 | graph[i].hook->before[j]); |
| 508 | continue; |
| 509 | } |
| 510 | tal_arr_expand(&graph[i].outgoing, n); |
| 511 | n->num_incoming++; |
| 512 | } |
| 513 | for (size_t j = 0; j < tal_count(graph[i].hook->after); j++) { |
| 514 | n = find_hook(graph, graph[i].hook->after[j]); |
| 515 | if (!n) { |
| 516 | /* This is useful for typos! */ |
| 517 | log_debug(graph[i].hook->plugin->log, |
| 518 | "hook %s after unknown plugin %s", |
| 519 | hook->name, |
| 520 | graph[i].hook->after[j]); |
| 521 | continue; |
| 522 | } |
| 523 | tal_arr_expand(&n->outgoing, &graph[i]); |
| 524 | graph[i].num_incoming++; |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | done = tal_arr(tmpctx, struct hook_instance *, 0); |
| 529 | while ((n = get_best_candidate(graph)) != NULL) { |
| 530 | tal_arr_expand(&done, n->hook); |
| 531 | n->finished = true; |
| 532 | for (size_t i = 0; i < tal_count(n->outgoing); i++) |
| 533 | n->outgoing[i]->num_incoming--; |
| 534 | } |
| 535 | |
| 536 | if (tal_count(done) != tal_count(hook->hooks)) { |
| 537 | struct plugin **ret = tal_arr(ctx, struct plugin *, 0); |
| 538 | for (size_t i = 0; i < tal_count(graph); i++) { |
| 539 | if (!graph[i].finished) |
| 540 | tal_arr_expand(&ret, graph[i].hook->plugin); |
no test coverage detected