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 (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.
  24  */
  25 
  26 /*
  27  * Routines for manipulating iidesc_t structures
  28  */
  29 
  30 #include <stdio.h>
  31 #include <stdlib.h>
  32 #include <strings.h>
  33 
  34 #include "ctftools.h"
  35 #include "memory.h"
  36 #include "list.h"
  37 #include "hash.h"
  38 
  39 typedef struct iidesc_find {
  40         iidesc_t *iif_tgt;
  41         iidesc_t *iif_ret;
  42 } iidesc_find_t;
  43 
  44 iidesc_t *
  45 iidesc_new(char *name)
  46 {
  47         iidesc_t *ii;
  48 
  49         ii = xcalloc(sizeof (iidesc_t));
  50         if (name)
  51                 ii->ii_name = xstrdup(name);
  52 
  53         return (ii);
  54 }
  55 
  56 int
  57 iidesc_hash(int nbuckets, void *arg)
  58 {
  59         iidesc_t *ii = arg;
  60         int h = 0;
  61 
  62         if (ii->ii_name)
  63                 return (hash_name(nbuckets, ii->ii_name));
  64 
  65         return (h);
  66 }
  67 
  68 static int
  69 iidesc_cmp(iidesc_t *src, iidesc_find_t *find)
  70 {
  71         iidesc_t *tgt = find->iif_tgt;
  72 
  73         if (src->ii_type != tgt->ii_type ||
  74             !streq(src->ii_name, tgt->ii_name))
  75                 return (0);
  76 
  77         find->iif_ret = src;
  78 
  79         return (-1);
  80 }
  81 
  82 void
  83 iidesc_add(hash_t *hash, iidesc_t *new)
  84 {
  85         iidesc_find_t find;
  86 
  87         find.iif_tgt = new;
  88         find.iif_ret = NULL;
  89 
  90         (void) hash_match(hash, new, (int (*)())iidesc_cmp, &find);
  91 
  92         if (find.iif_ret != NULL) {
  93                 iidesc_t *old = find.iif_ret;
  94                 iidesc_t tmp;
  95                 /* replacing existing one */
  96                 bcopy(old, &tmp, sizeof (tmp));
  97                 bcopy(new, old, sizeof (*old));
  98                 bcopy(&tmp, new, sizeof (*new));
  99 
 100                 iidesc_free(new);
 101                 return;
 102         }
 103 
 104         hash_add(hash, new);
 105 }
 106 
 107 void
 108 iter_iidescs_by_name(tdata_t *td, const char *name,
 109     int (*func)(iidesc_t *, void *), void *data)
 110 {
 111         iidesc_t tmpdesc;
 112         bzero(&tmpdesc, sizeof (iidesc_t));
 113         tmpdesc.ii_name = (char *)name;
 114         (void) hash_match(td->td_iihash, &tmpdesc, (int (*)())func, data);
 115 }
 116 
 117 iidesc_t *
 118 iidesc_dup(iidesc_t *src)
 119 {
 120         iidesc_t *tgt;
 121 
 122         tgt = xmalloc(sizeof (iidesc_t));
 123         bcopy(src, tgt, sizeof (iidesc_t));
 124 
 125         tgt->ii_name = src->ii_name ? xstrdup(src->ii_name) : NULL;
 126         tgt->ii_owner = src->ii_owner ? xstrdup(src->ii_owner) : NULL;
 127 
 128         if (tgt->ii_nargs) {
 129                 tgt->ii_args = xmalloc(sizeof (tdesc_t *) * tgt->ii_nargs);
 130                 bcopy(src->ii_args, tgt->ii_args,
 131                     sizeof (tdesc_t *) * tgt->ii_nargs);
 132         }
 133 
 134         return (tgt);
 135 }
 136 
 137 iidesc_t *
 138 iidesc_dup_rename(iidesc_t *src, char const *name, char const *owner)
 139 {
 140         iidesc_t *tgt = iidesc_dup(src);
 141         free(tgt->ii_name);
 142         free(tgt->ii_owner);
 143 
 144         tgt->ii_name = name ? xstrdup(name) : NULL;
 145         tgt->ii_owner = owner ? xstrdup(owner) : NULL;
 146 
 147         return (tgt);
 148 }
 149 
 150 /*ARGSUSED*/
 151 void
 152 iidesc_free_cb(void *ptr, void *private)
 153 {
 154         iidesc_t *idp = ptr;
 155 
 156         free(idp->ii_name);
 157         free(idp->ii_args);
 158         free(idp->ii_owner);
 159         free(idp);
 160 }
 161 
 162 void
 163 iidesc_free(iidesc_t *idp)
 164 {
 165         iidesc_free_cb(idp, NULL);
 166 }
 167 
 168 int
 169 iidesc_dump(iidesc_t *ii)
 170 {
 171         printf("type: %d  name %s\n", ii->ii_type,
 172             (ii->ii_name ? ii->ii_name : "(anon)"));
 173 
 174         return (0);
 175 }
 176 
 177 int
 178 iidesc_count_type(void *data, void *private)
 179 {
 180         iidesc_t *ii = data;
 181         iitype_t match = (iitype_t)private;
 182 
 183         return (ii->ii_type == match);
 184 }
 185 
 186 void
 187 iidesc_stats(hash_t *ii)
 188 {
 189         printf("GFun: %5d SFun: %5d GVar: %5d SVar: %5d T %5d SOU: %5d\n",
 190             hash_iter(ii, iidesc_count_type, (void *)II_GFUN),
 191             hash_iter(ii, iidesc_count_type, (void *)II_SFUN),
 192             hash_iter(ii, iidesc_count_type, (void *)II_GVAR),
 193             hash_iter(ii, iidesc_count_type, (void *)II_SVAR),
 194             hash_iter(ii, iidesc_count_type, (void *)II_TYPE),
 195             hash_iter(ii, iidesc_count_type, (void *)II_SOU));
 196 }