summaryrefslogtreecommitdiff
path: root/src/libs6rc/s6rc_graph_closure.c
blob: 0ec2def678916b077c119edb705a02efffc03310 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/* ISC license. */

#include <skalibs/bitarray.h>
#include <s6-rc/s6rc-db.h>
#include <s6-rc/s6rc-utils.h>

typedef struct recinfo_s recinfo_t, *recinfo_t_ref ;
struct recinfo_s
{
  s6rc_db_t const *db ;
  unsigned int n ;
  unsigned char *bits ;
  unsigned char *mark ;
  unsigned char mask ;
  unsigned char h : 1 ;
} ;

static void s6rc_graph_closure_rec (recinfo_t *recinfo, unsigned int i)
{
  if (!bitarray_peek(recinfo->mark, i))
  {
    unsigned int j = recinfo->db->services[i].ndeps[recinfo->h] ;
    bitarray_set(recinfo->mark, i) ;
    while (j--) s6rc_graph_closure_rec(recinfo, recinfo->db->deps[recinfo->h * recinfo->db->ndeps + recinfo->db->services[i].deps[recinfo->h] + j]) ;
    recinfo->bits[i] |= recinfo->mask ;
  }
}

void s6rc_graph_closure (s6rc_db_t const *db, unsigned char *bits, unsigned int bitno, int h)
{
  unsigned int n = db->nshort + db->nlong ;
  unsigned int m = bitarray_div8(n) ;
  unsigned char mark[m] ;
  recinfo_t info = { .db = db, .n = n, .bits = bits, .mark = mark, .mask = 1 << (bitno & 7), .h = !!h } ;
  register unsigned int i = n ;
  byte_zero(mark, m) ;
  while (i--)
    if (bits[i] & info.mask) s6rc_graph_closure_rec(&info, i) ;
}