xref: /titanic_51/usr/src/uts/common/sys/group.h (revision 8fd04b8338ed5093ec2d1e668fa620b7de44c177)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef	_GROUP_H
27 #define	_GROUP_H
28 
29 /*
30  * Group Abstraction
31  */
32 
33 #ifdef	__cplusplus
34 extern "C" {
35 #endif
36 
37 #if (defined(_KERNEL) || defined(_KMEMUSER))
38 #include <sys/types.h>
39 
40 #define	GRP_RESIZE	0x1	/* Resize group capacity if needed */
41 #define	GRP_NORESIZE	0x2	/* Do not resize group capacity; may fail */
42 
43 /*
44  * group structure
45  */
46 typedef struct group {
47 	uint_t		grp_size;	/* # of elements */
48 	uint_t		grp_capacity;	/* current group capacity */
49 	void		**grp_set;	/* element vector */
50 } group_t;
51 
52 typedef uint_t group_iter_t;
53 
54 
55 /*
56  * Return the number of elements in the group
57  */
58 #define	GROUP_SIZE(grp)			((grp)->grp_size)
59 
60 /*
61  * Access the element at the specified group index
62  */
63 #define	GROUP_ACCESS(grp, index)	((grp)->grp_set[index])
64 
65 /*
66  * Group creation / destruction
67  */
68 void		group_create(group_t *);
69 void		group_destroy(group_t *);
70 
71 /*
72  * Expand a group's holding capacity
73  */
74 void		group_expand(group_t *, uint_t);
75 
76 /*
77  * Group element iteration
78  */
79 void		group_iter_init(group_iter_t *);
80 void		*group_iterate(group_t *, group_iter_t *);
81 
82 /*
83  * Add / remove an element (or elements) from the group
84  */
85 int		group_add(group_t *, void *, int);
86 int		group_remove(group_t *, void *, int);
87 void		group_empty(group_t *);
88 
89 /*
90  * Add / remove / access an element at a specified index.
91  * The group must already have sufficient capacity to hold
92  * an element at the specified index.
93  */
94 int		group_add_at(group_t *, void *, uint_t);
95 void		group_remove_at(group_t *, uint_t);
96 
97 /*
98  * Search for an element in a group.
99  * Returns an index that may be used with the *_at()
100  * routines above to add or remove the element.
101  */
102 uint_t		group_find(group_t *, void *);
103 
104 /*
105  * Convert a group to a string with list of integers.
106  *
107  * The consecutive integer values are represented using x-y notation.
108  * The resulting string looks like "1,2-5,8"
109  *
110  * The convert argument is used to map group elements to integer IDs.
111  * The output buffer and its length are specfied in the arguments.
112  */
113 extern char *group2intlist(group_t *, char *, size_t, int (convert)(void*));
114 
115 #endif	/* !_KERNEL && !_KMEMUSER */
116 
117 #ifdef	__cplusplus
118 }
119 #endif
120 
121 #endif /* _GROUP_H */
122