xref: /illumos-gate/usr/src/common/mdesc/mdesc_getproparcs.c (revision 581cede61ac9c14d8d4ea452562a567189eead78)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <sys/mdesc.h>
30 #include <sys/mdesc_impl.h>
31 
32 static int md_find_node_arcs(md_impl_t *, mde_cookie_t, mde_str_cookie_t, int,
33     mde_cookie_t *, size_t);
34 
35 
36 /*
37  * Return an array containing the node indexes for the arcs in
38  * the given node.  The array is allocated using the allocator
39  * defined at machine description initialization time and the
40  * number of arcs found returned.
41  *
42  * Input		Description
43  * -------------------	----------------------------------------
44  * md_t *		Pointer to md session
45  * mde_cookie_t		Node containing arcs
46  * char *		Arc name to count (e.g. "fwd" or "back")
47  * mde_cookie_t *	Buffer to store indexes, or NULL
48  * size_t		Size of buffer
49  *
50  * Output		Description
51  * -------------------	----------------------------------------
52  * int			Count of arcs in node
53  */
54 int
55 md_get_prop_arcs(md_t *ptr, mde_cookie_t node, char *namep, mde_cookie_t *arcp,
56     size_t arcsize)
57 {
58 	int		 result;
59 	mde_str_cookie_t prop_name;
60 	md_impl_t	*mdp;
61 
62 	mdp = (md_impl_t *)ptr;
63 
64 	if (node == MDE_INVAL_ELEM_COOKIE) {
65 		return (-1);
66 	}
67 
68 	prop_name = md_find_name(ptr, namep);
69 	if (prop_name == MDE_INVAL_STR_COOKIE) {
70 		return (-1);
71 	}
72 
73 	result = md_find_node_arcs(mdp, node, prop_name, MDET_PROP_ARC, arcp,
74 	    arcsize);
75 
76 	return (result);
77 }
78 
79 
80 /*
81  * Find the number of arcs in the node of the requested prop_name.  If storage
82  * is given in arcp, store the first arcsize number of node indexes.
83  */
84 static int
85 md_find_node_arcs(md_impl_t *mdp, mde_cookie_t node,
86     mde_str_cookie_t prop_name, int tag_type, mde_cookie_t *arcp,
87     size_t arcsize)
88 {
89 	int		result;
90 	md_element_t	*mdep;
91 	int		idx;
92 
93 	/* Get the private node information from session data */
94 	idx = (int)node;
95 	mdep = &(mdp->mdep[idx]);
96 
97 	/* Make sure the cookie is in fact a node */
98 	if (MDE_TAG(mdep) != MDET_NODE) {
99 		return (-1);
100 	}
101 
102 	/*
103 	 * Walk the elements in the node and find all the arcs of the
104 	 * requested type, and store them in an array.
105 	 */
106 	result = 0;
107 	for (idx++, mdep++; MDE_TAG(mdep) != MDET_NODE_END; idx++, mdep++) {
108 		if ((MDE_TAG(mdep) == tag_type) &&
109 		    (MDE_NAME(mdep) == prop_name)) {
110 			if (arcp != NULL && result < arcsize) {
111 				arcp[result] =
112 				    (mde_cookie_t)MDE_PROP_INDEX(mdep);
113 			}
114 
115 			/* Increment the count of arcs found */
116 			result++;
117 		}
118 	}
119 
120 	/* Return the total count of arcs in the node */
121 	return (result);
122 }
123