1fb2f18f8Sesaxe /* 2fb2f18f8Sesaxe * CDDL HEADER START 3fb2f18f8Sesaxe * 4fb2f18f8Sesaxe * The contents of this file are subject to the terms of the 5fb2f18f8Sesaxe * Common Development and Distribution License (the "License"). 6fb2f18f8Sesaxe * You may not use this file except in compliance with the License. 7fb2f18f8Sesaxe * 8fb2f18f8Sesaxe * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fb2f18f8Sesaxe * or http://www.opensolaris.org/os/licensing. 10fb2f18f8Sesaxe * See the License for the specific language governing permissions 11fb2f18f8Sesaxe * and limitations under the License. 12fb2f18f8Sesaxe * 13fb2f18f8Sesaxe * When distributing Covered Code, include this CDDL HEADER in each 14fb2f18f8Sesaxe * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fb2f18f8Sesaxe * If applicable, add the following below this CDDL HEADER, with the 16fb2f18f8Sesaxe * fields enclosed by brackets "[]" replaced with your own identifying 17fb2f18f8Sesaxe * information: Portions Copyright [yyyy] [name of copyright owner] 18fb2f18f8Sesaxe * 19fb2f18f8Sesaxe * CDDL HEADER END 20fb2f18f8Sesaxe */ 21fb2f18f8Sesaxe /* 220e751525SEric Saxe * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23fb2f18f8Sesaxe * Use is subject to license terms. 24fb2f18f8Sesaxe */ 25fb2f18f8Sesaxe 26fb2f18f8Sesaxe #ifndef _GROUP_H 27fb2f18f8Sesaxe #define _GROUP_H 28fb2f18f8Sesaxe 29fb2f18f8Sesaxe /* 30fb2f18f8Sesaxe * Group Abstraction 31fb2f18f8Sesaxe */ 32fb2f18f8Sesaxe 33fb2f18f8Sesaxe #ifdef __cplusplus 34fb2f18f8Sesaxe extern "C" { 35fb2f18f8Sesaxe #endif 36fb2f18f8Sesaxe 37fb2f18f8Sesaxe #if (defined(_KERNEL) || defined(_KMEMUSER)) 38fb2f18f8Sesaxe #include <sys/types.h> 39fb2f18f8Sesaxe 40fb2f18f8Sesaxe #define GRP_RESIZE 0x1 /* Resize group capacity if needed */ 41fb2f18f8Sesaxe #define GRP_NORESIZE 0x2 /* Do not resize group capacity; may fail */ 42fb2f18f8Sesaxe 43fb2f18f8Sesaxe /* 44fb2f18f8Sesaxe * group structure 45fb2f18f8Sesaxe */ 46fb2f18f8Sesaxe typedef struct group { 47fb2f18f8Sesaxe uint_t grp_size; /* # of elements */ 48fb2f18f8Sesaxe uint_t grp_capacity; /* current group capacity */ 49fb2f18f8Sesaxe void **grp_set; /* element vector */ 50fb2f18f8Sesaxe } group_t; 51fb2f18f8Sesaxe 52fb2f18f8Sesaxe typedef uint_t group_iter_t; 53fb2f18f8Sesaxe 54fb2f18f8Sesaxe 55fb2f18f8Sesaxe /* 56fb2f18f8Sesaxe * Return the number of elements in the group 57fb2f18f8Sesaxe */ 58fb2f18f8Sesaxe #define GROUP_SIZE(grp) ((grp)->grp_size) 59fb2f18f8Sesaxe 60fb2f18f8Sesaxe /* 61fb2f18f8Sesaxe * Access the element at the specified group index 62fb2f18f8Sesaxe */ 63fb2f18f8Sesaxe #define GROUP_ACCESS(grp, index) ((grp)->grp_set[index]) 64fb2f18f8Sesaxe 65fb2f18f8Sesaxe /* 66fb2f18f8Sesaxe * Group creation / destruction 67fb2f18f8Sesaxe */ 68fb2f18f8Sesaxe void group_create(group_t *); 69fb2f18f8Sesaxe void group_destroy(group_t *); 70fb2f18f8Sesaxe 71fb2f18f8Sesaxe /* 72fb2f18f8Sesaxe * Expand a group's holding capacity 73fb2f18f8Sesaxe */ 74fb2f18f8Sesaxe void group_expand(group_t *, uint_t); 75fb2f18f8Sesaxe 76fb2f18f8Sesaxe /* 77fb2f18f8Sesaxe * Group element iteration 78fb2f18f8Sesaxe */ 79fb2f18f8Sesaxe void group_iter_init(group_iter_t *); 800e751525SEric Saxe void *group_iterate(group_t *, group_iter_t *); 81fb2f18f8Sesaxe 82fb2f18f8Sesaxe /* 830e751525SEric Saxe * Add / remove an element (or elements) from the group 84fb2f18f8Sesaxe */ 85fb2f18f8Sesaxe int group_add(group_t *, void *, int); 86fb2f18f8Sesaxe int group_remove(group_t *, void *, int); 870e751525SEric Saxe void group_empty(group_t *); 88fb2f18f8Sesaxe 89fb2f18f8Sesaxe /* 90fb2f18f8Sesaxe * Add / remove / access an element at a specified index. 91fb2f18f8Sesaxe * The group must already have sufficient capacity to hold 92fb2f18f8Sesaxe * an element at the specified index. 93fb2f18f8Sesaxe */ 94fb2f18f8Sesaxe int group_add_at(group_t *, void *, uint_t); 95fb2f18f8Sesaxe void group_remove_at(group_t *, uint_t); 96fb2f18f8Sesaxe 970e751525SEric Saxe /* 980e751525SEric Saxe * Search for an element in a group. 990e751525SEric Saxe * Returns an index that may be used with the *_at() 1000e751525SEric Saxe * routines above to add or remove the element. 1010e751525SEric Saxe */ 1020e751525SEric Saxe uint_t group_find(group_t *, void *); 1030e751525SEric Saxe 104*b885580bSAlexander Kolbasov /* 105*b885580bSAlexander Kolbasov * Convert a group to a string with list of integers. 106*b885580bSAlexander Kolbasov * 107*b885580bSAlexander Kolbasov * The consecutive integer values are represented using x-y notation. 108*b885580bSAlexander Kolbasov * The resulting string looks like "1,2-5,8" 109*b885580bSAlexander Kolbasov * 110*b885580bSAlexander Kolbasov * The convert argument is used to map group elements to integer IDs. 111*b885580bSAlexander Kolbasov * The output buffer and its length are specfied in the arguments. 112*b885580bSAlexander Kolbasov */ 113*b885580bSAlexander Kolbasov extern char *group2intlist(group_t *, char *, size_t, int (convert)(void*)); 114*b885580bSAlexander Kolbasov 115fb2f18f8Sesaxe #endif /* !_KERNEL && !_KMEMUSER */ 116fb2f18f8Sesaxe 117fb2f18f8Sesaxe #ifdef __cplusplus 118fb2f18f8Sesaxe } 119fb2f18f8Sesaxe #endif 120fb2f18f8Sesaxe 121fb2f18f8Sesaxe #endif /* _GROUP_H */ 122