1178479Sjb/*
2178479Sjb * CDDL HEADER START
3178479Sjb *
4178479Sjb * The contents of this file are subject to the terms of the
5178479Sjb * Common Development and Distribution License, Version 1.0 only
6178479Sjb * (the "License").  You may not use this file except in compliance
7178479Sjb * with the License.
8178479Sjb *
9178479Sjb * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10178479Sjb * or http://www.opensolaris.org/os/licensing.
11178479Sjb * See the License for the specific language governing permissions
12178479Sjb * and limitations under the License.
13178479Sjb *
14178479Sjb * When distributing Covered Code, include this CDDL HEADER in each
15178479Sjb * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16178479Sjb * If applicable, add the following below this CDDL HEADER, with the
17178479Sjb * fields enclosed by brackets "[]" replaced with your own identifying
18178479Sjb * information: Portions Copyright [yyyy] [name of copyright owner]
19178479Sjb *
20178479Sjb * CDDL HEADER END
21178479Sjb */
22178479Sjb/*
23178479Sjb * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24178479Sjb * Use is subject to license terms.
25178479Sjb */
26178479Sjb
27178479Sjb#pragma ident	"%Z%%M%	%I%	%E% SMI"
28178479Sjb
29178479Sjb#include <assert.h>
30178479Sjb
31178479Sjb#include <dt_inttab.h>
32178479Sjb#include <dt_impl.h>
33178479Sjb
34178479Sjbdt_inttab_t *
35178479Sjbdt_inttab_create(dtrace_hdl_t *dtp)
36178479Sjb{
37178479Sjb	uint_t len = _dtrace_intbuckets;
38178479Sjb	dt_inttab_t *ip;
39178479Sjb
40178479Sjb	assert((len & (len - 1)) == 0);
41178479Sjb
42178479Sjb	if ((ip = dt_zalloc(dtp, sizeof (dt_inttab_t))) == NULL ||
43178479Sjb	    (ip->int_hash = dt_zalloc(dtp, sizeof (void *) * len)) == NULL) {
44178479Sjb		dt_free(dtp, ip);
45178479Sjb		return (NULL);
46178479Sjb	}
47178479Sjb
48178479Sjb	ip->int_hdl = dtp;
49178479Sjb	ip->int_hashlen = len;
50178479Sjb
51178479Sjb	return (ip);
52178479Sjb}
53178479Sjb
54178479Sjbvoid
55178479Sjbdt_inttab_destroy(dt_inttab_t *ip)
56178479Sjb{
57178479Sjb	dt_inthash_t *hp, *np;
58178479Sjb
59178479Sjb	for (hp = ip->int_head; hp != NULL; hp = np) {
60178479Sjb		np = hp->inh_next;
61178479Sjb		dt_free(ip->int_hdl, hp);
62178479Sjb	}
63178479Sjb
64178479Sjb	dt_free(ip->int_hdl, ip->int_hash);
65178479Sjb	dt_free(ip->int_hdl, ip);
66178479Sjb}
67178479Sjb
68178479Sjbint
69178479Sjbdt_inttab_insert(dt_inttab_t *ip, uint64_t value, uint_t flags)
70178479Sjb{
71178479Sjb	uint_t h = value & (ip->int_hashlen - 1);
72178479Sjb	dt_inthash_t *hp;
73178479Sjb
74178479Sjb	if (flags & DT_INT_SHARED) {
75178479Sjb		for (hp = ip->int_hash[h]; hp != NULL; hp = hp->inh_hash) {
76178479Sjb			if (hp->inh_value == value && hp->inh_flags == flags)
77178479Sjb				return (hp->inh_index);
78178479Sjb		}
79178479Sjb	}
80178479Sjb
81178479Sjb	if ((hp = dt_alloc(ip->int_hdl, sizeof (dt_inthash_t))) == NULL)
82178479Sjb		return (-1);
83178479Sjb
84178479Sjb	hp->inh_hash = ip->int_hash[h];
85178479Sjb	hp->inh_next = NULL;
86178479Sjb	hp->inh_value = value;
87178479Sjb	hp->inh_index = ip->int_index++;
88178479Sjb	hp->inh_flags = flags;
89178479Sjb
90178479Sjb	ip->int_hash[h] = hp;
91178479Sjb	ip->int_nelems++;
92178479Sjb
93178479Sjb	if (ip->int_head == NULL)
94178479Sjb		ip->int_head = hp;
95178479Sjb	else
96178479Sjb		ip->int_tail->inh_next = hp;
97178479Sjb
98178479Sjb	ip->int_tail = hp;
99178479Sjb	return (hp->inh_index);
100178479Sjb}
101178479Sjb
102178479Sjbuint_t
103178479Sjbdt_inttab_size(const dt_inttab_t *ip)
104178479Sjb{
105178479Sjb	return (ip->int_nelems);
106178479Sjb}
107178479Sjb
108178479Sjbvoid
109178479Sjbdt_inttab_write(const dt_inttab_t *ip, uint64_t *dst)
110178479Sjb{
111178479Sjb	const dt_inthash_t *hp;
112178479Sjb
113178479Sjb	for (hp = ip->int_head; hp != NULL; hp = hp->inh_next)
114178479Sjb		*dst++ = hp->inh_value;
115178479Sjb}
116