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
35static lwpid_t	*lwpid_tbl[LWPID_TBL_SZ];
36
37void
38lwpid_init()
39{
40	(void) memset(&lwpid_tbl, 0, sizeof (lwpid_t *) * LWPID_TBL_SZ);
41}
42
43void
44lwpid_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
56void
57lwpid_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
80static lwpid_t *
81lwpid_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
93lwp_info_t *
94lwpid_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
103int
104lwpid_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