8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / common / mdesc / mdesc_scandag.c
blobad1c74c9c28ef36b015c042e444fbe2cbd86c467
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <sys/types.h>
30 #include <sys/param.h>
31 #ifdef _KERNEL
32 #include <sys/systm.h>
33 #else
34 #include <string.h>
35 #include <strings.h>
36 #endif
38 #include <sys/mdesc.h>
39 #include <sys/mdesc_impl.h>
41 static int
42 mdl_scan_dag(md_impl_t *mdp,
43 int nodeidx,
44 mde_str_cookie_t node_cookie,
45 mde_str_cookie_t arc_cookie,
46 uint8_t *dseenp,
47 int *idxp,
48 mde_cookie_t *stashp,
49 int level);
52 int
53 md_scan_dag(md_t *ptr,
54 mde_cookie_t startnode,
55 mde_str_cookie_t node_name_cookie,
56 mde_str_cookie_t arc_name_cookie,
57 mde_cookie_t *stashp)
59 int res;
60 int idx;
61 uint8_t *seenp;
62 md_impl_t *mdp;
63 int start;
65 mdp = (md_impl_t *)ptr;
68 * Possible the caller was lazy and didn't check the
69 * validitiy of either the node name or the arc name
70 * on calling ... in which case fail to find any
71 * nodes.
72 * This is distinct, from a fail (-1) since we return
73 * that nothing was found.
76 if (node_name_cookie == MDE_INVAL_STR_COOKIE ||
77 arc_name_cookie == MDE_INVAL_STR_COOKIE) return 0;
80 * if we want to start at the top, start at index 0
83 start = (int)startnode;
84 if (start == MDE_INVAL_ELEM_COOKIE) start = 0;
87 * Scan from the start point until the first node.
89 while (MDE_TAG(&mdp->mdep[start]) == MDET_NULL) start++;
92 * This was a bogus start point if no node found
94 if (MDE_TAG(&mdp->mdep[start]) != MDET_NODE) {
95 return (-1); /* illegal start node specified */
99 * Allocate a recursion mask on the local stack fail
100 * if we can't allocate the recursion detection.
102 seenp = (uint8_t *)mdp->allocp(mdp->element_count);
103 if (seenp == NULL)
104 return (-1);
105 (void) memset(seenp, 0, mdp->element_count);
108 * Now build the list of requested nodes.
110 idx = 0;
111 res = mdl_scan_dag(mdp, start,
112 node_name_cookie, arc_name_cookie,
113 seenp, &idx, stashp, 0);
115 mdp->freep(seenp, mdp->element_count);
117 return (res >= 0 ? idx : res);
124 static int
125 mdl_scan_dag(md_impl_t *mdp,
126 int nodeidx,
127 mde_str_cookie_t node_name_cookie,
128 mde_str_cookie_t arc_name_cookie,
129 uint8_t *seenp,
130 int *idxp,
131 mde_cookie_t *stashp,
132 int level)
134 md_element_t *mdep;
136 mdep = &(mdp->mdep[nodeidx]);
138 /* see if cookie is infact a node */
139 if (MDE_TAG(mdep) != MDET_NODE)
140 return (-1);
142 /* have we been here before ? */
143 if (seenp[nodeidx])
144 return (0);
145 seenp[nodeidx] = 1;
147 /* is this node of the type we seek ? */
149 #ifdef DEBUG_LIBMDESC
151 int x;
152 for (x = 0; x < level; x++)
153 printf("-");
154 printf("%d (%s)\n", nodeidx, (char *)(mdp->datap + MDE_NAME(mdep)));
156 #endif
158 if (MDE_NAME(mdep) == node_name_cookie) {
159 /* record the node in the list and keep searching */
160 if (stashp != NULL) {
161 stashp[*idxp] = (mde_cookie_t)nodeidx;
163 (*idxp)++;
164 #ifdef DEBUG_LIBMDESC
165 printf("\t* %d\n", *idxp);
166 #endif
170 * Simply walk the elements in the node.
171 * if we find a matching arc, then recursively call
172 * the subordinate looking for a match
175 for (mdep++; MDE_TAG(mdep) != MDET_NODE_END; mdep++) {
176 if (MDE_TAG(mdep) == MDET_PROP_ARC &&
177 MDE_NAME(mdep) == arc_name_cookie) {
178 int res;
180 res = mdl_scan_dag(mdp,
181 (int)mdep->d.prop_idx,
182 node_name_cookie,
183 arc_name_cookie,
184 seenp, idxp, stashp, level+1);
186 if (res == -1)
187 return (res);
191 return (0);