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