Path: blob/main/cddl/contrib/opensolaris/lib/libdtrace/common/dt_inttab.c
39562 views
/*1* CDDL HEADER START2*3* The contents of this file are subject to the terms of the4* Common Development and Distribution License, Version 1.0 only5* (the "License"). You may not use this file except in compliance6* with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or http://www.opensolaris.org/os/licensing.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/21/*22* Copyright 2004 Sun Microsystems, Inc. All rights reserved.23* Use is subject to license terms.24*/2526#pragma ident "%Z%%M% %I% %E% SMI"2728#include <assert.h>2930#include <dt_inttab.h>31#include <dt_impl.h>3233dt_inttab_t *34dt_inttab_create(dtrace_hdl_t *dtp)35{36uint_t len = _dtrace_intbuckets;37dt_inttab_t *ip;3839assert((len & (len - 1)) == 0);4041if ((ip = dt_zalloc(dtp, sizeof (dt_inttab_t))) == NULL ||42(ip->int_hash = dt_zalloc(dtp, sizeof (void *) * len)) == NULL) {43dt_free(dtp, ip);44return (NULL);45}4647ip->int_hdl = dtp;48ip->int_hashlen = len;4950return (ip);51}5253void54dt_inttab_destroy(dt_inttab_t *ip)55{56dt_inthash_t *hp, *np;5758for (hp = ip->int_head; hp != NULL; hp = np) {59np = hp->inh_next;60dt_free(ip->int_hdl, hp);61}6263dt_free(ip->int_hdl, ip->int_hash);64dt_free(ip->int_hdl, ip);65}6667int68dt_inttab_insert(dt_inttab_t *ip, uint64_t value, uint_t flags)69{70uint_t h = value & (ip->int_hashlen - 1);71dt_inthash_t *hp;7273if (flags & DT_INT_SHARED) {74for (hp = ip->int_hash[h]; hp != NULL; hp = hp->inh_hash) {75if (hp->inh_value == value && hp->inh_flags == flags)76return (hp->inh_index);77}78}7980if ((hp = dt_alloc(ip->int_hdl, sizeof (dt_inthash_t))) == NULL)81return (-1);8283hp->inh_hash = ip->int_hash[h];84hp->inh_next = NULL;85hp->inh_value = value;86hp->inh_index = ip->int_index++;87hp->inh_flags = flags;8889ip->int_hash[h] = hp;90ip->int_nelems++;9192if (ip->int_head == NULL)93ip->int_head = hp;94else95ip->int_tail->inh_next = hp;9697ip->int_tail = hp;98return (hp->inh_index);99}100101uint_t102dt_inttab_size(const dt_inttab_t *ip)103{104return (ip->int_nelems);105}106107void108dt_inttab_write(const dt_inttab_t *ip, uint64_t *dst)109{110const dt_inthash_t *hp;111112for (hp = ip->int_head; hp != NULL; hp = hp->inh_next)113*dst++ = hp->inh_value;114}115116117