xref: /onnv-gate/usr/src/cmd/wbem/provider/tools/rds/rdtable.c (revision 0:68f95e015346)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 2000-2001 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <string.h>
30 #include <stdlib.h>
31 
32 #include "rdutil.h"
33 #include "rdtable.h"
34 
35 static lwpid_t	*lwpid_tbl[LWPID_TBL_SZ];
36 
37 void
lwpid_init()38 lwpid_init()
39 {
40 	(void) memset(&lwpid_tbl, 0, sizeof (lwpid_t *) * LWPID_TBL_SZ);
41 }
42 
43 void
lwpid_add(lwp_info_t * lwp,pid_t pid,id_t lwpid)44 lwpid_add(lwp_info_t *lwp, pid_t pid, id_t lwpid)
45 {
46 	lwpid_t	*elm = Zalloc(sizeof (lwpid_t));
47 	int hash = pid % LWPID_TBL_SZ;
48 
49 	elm->l_pid = pid;
50 	elm->l_lwpid = lwpid;
51 	elm->l_lwp = lwp;
52 	elm->l_next = lwpid_tbl[hash]; /* add in front of chain */
53 	lwpid_tbl[hash] = elm;
54 }
55 
56 void
lwpid_del(pid_t pid,id_t lwpid)57 lwpid_del(pid_t pid, id_t lwpid)
58 {
59 	lwpid_t	*elm, *elm_prev;
60 	int hash = pid % LWPID_TBL_SZ;
61 
62 	elm = lwpid_tbl[hash];
63 	elm_prev = NULL;
64 
65 	while (elm) {
66 		if ((elm->l_pid == pid) && (elm->l_lwpid == lwpid)) {
67 			if (!elm_prev)	/* first chain element */
68 				lwpid_tbl[hash] = elm->l_next;
69 			else
70 				elm_prev->l_next = elm->l_next;
71 			free(elm);
72 			break;
73 		} else {
74 			elm_prev = elm;
75 			elm = elm->l_next;
76 		}
77 	}
78 }
79 
80 static lwpid_t *
lwpid_getptr(pid_t pid,id_t lwpid)81 lwpid_getptr(pid_t pid, id_t lwpid)
82 {
83 	lwpid_t *elm = lwpid_tbl[pid % LWPID_TBL_SZ];
84 	while (elm) {
85 		if ((elm->l_pid == pid) && (elm->l_lwpid == lwpid))
86 			return (elm);
87 		else
88 			elm = elm->l_next;
89 	}
90 	return (NULL);
91 }
92 
93 lwp_info_t *
lwpid_get(pid_t pid,id_t lwpid)94 lwpid_get(pid_t pid, id_t lwpid)
95 {
96 	lwpid_t	*elm = lwpid_getptr(pid, lwpid);
97 	if (elm)
98 		return (elm->l_lwp);
99 	else
100 		return (NULL);
101 }
102 
103 int
lwpid_pidcheck(pid_t pid)104 lwpid_pidcheck(pid_t pid)
105 {
106 	lwpid_t *elm;
107 	elm = lwpid_tbl[pid % LWPID_TBL_SZ];
108 	while (elm) {
109 		if (elm->l_pid == pid)
110 			return (1);
111 		else
112 			elm = elm->l_next;
113 	}
114 	return (0);
115 }
116