1 /*- 2 * Copyright (c) 2003-2008 Joseph Koshy 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/types.h> 28 #include <sys/cpuset.h> 29 #include <sys/param.h> 30 #include <sys/stat.h> 31 #include <sys/pmc.h> 32 33 #include <assert.h> 34 #include <err.h> 35 #include <errno.h> 36 #include <pmc.h> 37 #include <pmclog.h> 38 #include <stdio.h> 39 #include <stdlib.h> 40 #include <string.h> 41 #include <sysexits.h> 42 43 #include "libpmcstat.h" 44 45 static LIST_HEAD(,pmcstat_string) pmcstat_string_hash[PMCSTAT_NHASH]; 46 47 /* 48 * Intern a copy of string 's', and return a pointer to the 49 * interned structure. 50 */ 51 52 pmcstat_interned_string 53 pmcstat_string_intern(const char *s) 54 { 55 struct pmcstat_string *ps; 56 const struct pmcstat_string *cps; 57 int hash, len; 58 59 if ((cps = pmcstat_string_lookup(s)) != NULL) 60 return (cps); 61 62 hash = pmcstat_string_compute_hash(s); 63 len = strlen(s); 64 65 if ((ps = malloc(sizeof(*ps))) == NULL) 66 err(EX_OSERR, "ERROR: Could not intern string"); 67 ps->ps_len = len; 68 ps->ps_hash = hash; 69 ps->ps_string = strdup(s); 70 LIST_INSERT_HEAD(&pmcstat_string_hash[hash], ps, ps_next); 71 return ((pmcstat_interned_string) ps); 72 } 73 74 const char * 75 pmcstat_string_unintern(pmcstat_interned_string str) 76 { 77 const char *s; 78 79 s = ((const struct pmcstat_string *) str)->ps_string; 80 return (s); 81 } 82 83 /* 84 * Compute a 'hash' value for a string. 85 */ 86 87 int 88 pmcstat_string_compute_hash(const char *s) 89 { 90 unsigned hash; 91 92 for (hash = 2166136261; *s; s++) 93 hash = (hash ^ *s) * 16777619; 94 95 return (hash & PMCSTAT_HASH_MASK); 96 } 97 98 pmcstat_interned_string 99 pmcstat_string_lookup(const char *s) 100 { 101 struct pmcstat_string *ps; 102 int hash, len; 103 104 hash = pmcstat_string_compute_hash(s); 105 len = strlen(s); 106 107 LIST_FOREACH(ps, &pmcstat_string_hash[hash], ps_next) 108 if (ps->ps_len == len && ps->ps_hash == hash && 109 strcmp(ps->ps_string, s) == 0) 110 return (ps); 111 return (NULL); 112 } 113 114 int 115 pmcstat_string_lookup_hash(pmcstat_interned_string s) 116 { 117 const struct pmcstat_string *ps; 118 119 ps = (const struct pmcstat_string *) s; 120 return (ps->ps_hash); 121 } 122 123 /* 124 * Destroy the string table, free'ing up space. 125 */ 126 127 void 128 pmcstat_string_shutdown(void) 129 { 130 int i; 131 struct pmcstat_string *ps, *pstmp; 132 133 for (i = 0; i < PMCSTAT_NHASH; i++) 134 LIST_FOREACH_SAFE(ps, &pmcstat_string_hash[i], ps_next, 135 pstmp) { 136 LIST_REMOVE(ps, ps_next); 137 free(ps->ps_string); 138 free(ps); 139 } 140 } 141 142 /* 143 * Initialize the string interning facility. 144 */ 145 146 void 147 pmcstat_string_initialize(void) 148 { 149 int i; 150 151 for (i = 0; i < PMCSTAT_NHASH; i++) 152 LIST_INIT(&pmcstat_string_hash[i]); 153 } 154