Path: blob/main/cddl/contrib/opensolaris/tools/ctf/cvt/iidesc.c
39586 views
/*1* CDDL HEADER START2*3* The contents of this file are subject to the terms of the4* Common Development and Distribution License (the "License").5* You may not use this file except in compliance with the License.6*7* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE8* or http://www.opensolaris.org/os/licensing.9* See the License for the specific language governing permissions10* and limitations under the License.11*12* When distributing Covered Code, include this CDDL HEADER in each13* file and include the License file at usr/src/OPENSOLARIS.LICENSE.14* If applicable, add the following below this CDDL HEADER, with the15* fields enclosed by brackets "[]" replaced with your own identifying16* information: Portions Copyright [yyyy] [name of copyright owner]17*18* CDDL HEADER END19*/20/*21* Copyright 2006 Sun Microsystems, Inc. All rights reserved.22* Use is subject to license terms.23*/2425#pragma ident "%Z%%M% %I% %E% SMI"2627/*28* Routines for manipulating iidesc_t structures29*/3031#include <stdio.h>32#include <stdlib.h>33#include <strings.h>3435#include "ctftools.h"36#include "memory.h"37#include "list.h"38#include "hash.h"3940typedef struct iidesc_find {41iidesc_t *iif_tgt;42iidesc_t *iif_ret;43} iidesc_find_t;4445iidesc_t *46iidesc_new(char *name)47{48iidesc_t *ii;4950ii = xcalloc(sizeof (iidesc_t));51if (name)52ii->ii_name = xstrdup(name);5354return (ii);55}5657int58iidesc_hash(int nbuckets, void *arg)59{60iidesc_t *ii = arg;61int h = 0;6263if (ii->ii_name)64return (hash_name(nbuckets, ii->ii_name));6566return (h);67}6869static int70iidesc_cmp(void *arg1, void *arg2)71{72iidesc_t *src = arg1;73iidesc_find_t *find = arg2;74iidesc_t *tgt = find->iif_tgt;7576if (src->ii_type != tgt->ii_type ||77!streq(src->ii_name, tgt->ii_name))78return (0);7980find->iif_ret = src;8182return (-1);83}8485void86iidesc_add(hash_t *hash, iidesc_t *new)87{88iidesc_find_t find;8990find.iif_tgt = new;91find.iif_ret = NULL;9293(void) hash_match(hash, new, iidesc_cmp, &find);9495if (find.iif_ret != NULL) {96iidesc_t *old = find.iif_ret;97iidesc_t tmp;98/* replacing existing one */99bcopy(old, &tmp, sizeof (tmp));100bcopy(new, old, sizeof (*old));101bcopy(&tmp, new, sizeof (*new));102103iidesc_free(new, NULL);104return;105}106107hash_add(hash, new);108}109110void111iter_iidescs_by_name(tdata_t *td, char const *name,112int (*func)(void *, void *), void *data)113{114iidesc_t tmpdesc;115bzero(&tmpdesc, sizeof(tmpdesc));116tmpdesc.ii_name = xstrdup(name);117(void) hash_match(td->td_iihash, &tmpdesc, func, data);118free(tmpdesc.ii_name);119}120121iidesc_t *122iidesc_dup(iidesc_t *src)123{124iidesc_t *tgt;125126tgt = xmalloc(sizeof (iidesc_t));127bcopy(src, tgt, sizeof (iidesc_t));128129tgt->ii_name = src->ii_name ? xstrdup(src->ii_name) : NULL;130tgt->ii_owner = src->ii_owner ? xstrdup(src->ii_owner) : NULL;131132if (tgt->ii_nargs) {133tgt->ii_args = xmalloc(sizeof (tdesc_t *) * tgt->ii_nargs);134bcopy(src->ii_args, tgt->ii_args,135sizeof (tdesc_t *) * tgt->ii_nargs);136}137138return (tgt);139}140141iidesc_t *142iidesc_dup_rename(iidesc_t *src, char const *name, char const *owner)143{144iidesc_t *tgt = iidesc_dup(src);145free(tgt->ii_name);146free(tgt->ii_owner);147148tgt->ii_name = name ? xstrdup(name) : NULL;149tgt->ii_owner = owner ? xstrdup(owner) : NULL;150151return (tgt);152}153154/*ARGSUSED*/155void156iidesc_free(void *arg, void *private __unused)157{158iidesc_t *idp = arg;159if (idp->ii_name)160free(idp->ii_name);161if (idp->ii_nargs)162free(idp->ii_args);163if (idp->ii_owner)164free(idp->ii_owner);165free(idp);166}167168int169iidesc_dump(iidesc_t *ii)170{171printf("type: %d name %s\n", ii->ii_type,172(ii->ii_name ? ii->ii_name : "(anon)"));173174return (0);175}176177int178iidesc_count_type(void *data, void *private)179{180iidesc_t *ii = data;181iitype_t match = (iitype_t)(uintptr_t)private;182183return (ii->ii_type == match);184}185186void187iidesc_stats(hash_t *ii)188{189printf("GFun: %5d SFun: %5d GVar: %5d SVar: %5d T %5d SOU: %5d\n",190hash_iter(ii, iidesc_count_type, (void *)II_GFUN),191hash_iter(ii, iidesc_count_type, (void *)II_SFUN),192hash_iter(ii, iidesc_count_type, (void *)II_GVAR),193hash_iter(ii, iidesc_count_type, (void *)II_SVAR),194hash_iter(ii, iidesc_count_type, (void *)II_TYPE),195hash_iter(ii, iidesc_count_type, (void *)II_SOU));196}197198199