DFS from `entry`, children capped and ordered by (desc out_count, id): * the busiest callee first reads as the "trunk" of the journey. */
| 221 | /* DFS from `entry`, children capped and ordered by (desc out_count, id): |
| 222 | * the busiest callee first reads as the "trunk" of the journey. */ |
| 223 | static void fl_walk(const flow_cache_t *cache, flow_t *flow, int32_t entry) { |
| 224 | memset(flow, 0, sizeof(*flow)); |
| 225 | flow->entry = entry; |
| 226 | flow->terminal = entry; |
| 227 | bool *visited = cbm_atlas_calloc_array((size_t)cache->node_count, sizeof(bool)); |
| 228 | if (!visited) |
| 229 | return; |
| 230 | |
| 231 | typedef struct { |
| 232 | int32_t node; |
| 233 | int32_t parent_step; |
| 234 | uint8_t depth; |
| 235 | } frame_t; |
| 236 | frame_t stack[FLOW_MAX_STEPS * FLOW_MAX_CHILDREN]; |
| 237 | int sp = 0; |
| 238 | stack[sp++] = (frame_t){entry, -1, 0}; |
| 239 | int deepest = -1; |
| 240 | while (sp > 0 && flow->step_count < FLOW_MAX_STEPS) { |
| 241 | frame_t frame = stack[--sp]; |
| 242 | if (visited[frame.node]) |
| 243 | continue; |
| 244 | visited[frame.node] = true; |
| 245 | int step = flow->step_count; |
| 246 | flow->steps[step] = frame.node; |
| 247 | flow->parent[step] = frame.parent_step; |
| 248 | flow->depth[step] = frame.depth; |
| 249 | flow->step_count++; |
| 250 | const flow_node_t *node = &cache->nodes[frame.node]; |
| 251 | if (frame.depth > deepest || |
| 252 | (frame.depth == deepest && node->out_count == 0 && !flow->sink_terminated)) { |
| 253 | deepest = frame.depth; |
| 254 | flow->terminal = frame.node; |
| 255 | flow->sink_terminated = node->out_count == 0; |
| 256 | } |
| 257 | if (frame.depth >= FLOW_MAX_DEPTH) |
| 258 | continue; |
| 259 | /* Collect up to FLOW_MAX_CHILDREN unvisited callees, best last so |
| 260 | * the busiest pops first (LIFO). */ |
| 261 | int32_t picked[FLOW_MAX_CHILDREN]; |
| 262 | int npicked = 0; |
| 263 | for (int e = 0; e < node->out_count; e++) { |
| 264 | int32_t callee = cache->out_edges[node->first_out + e]; |
| 265 | if (visited[callee]) |
| 266 | continue; |
| 267 | /* Insertion into a small best-of list by (out_count desc, id). */ |
| 268 | int pos = npicked; |
| 269 | while (pos > 0) { |
| 270 | const flow_node_t *have = &cache->nodes[picked[pos - 1]]; |
| 271 | const flow_node_t *cand = &cache->nodes[callee]; |
| 272 | if (have->out_count > cand->out_count || |
| 273 | (have->out_count == cand->out_count && picked[pos - 1] < callee)) |
| 274 | break; |
| 275 | pos--; |
| 276 | } |
| 277 | if (npicked < FLOW_MAX_CHILDREN) { |
| 278 | for (int m = npicked; m > pos; m--) |
| 279 | picked[m] = picked[m - 1]; |
| 280 | picked[pos] = callee; |
no test coverage detected