2 * Copyright (c) 2003-2008 Joseph Koshy
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 #include <sys/types.h>
29 #include <sys/cpuset.h>
30 #include <sys/param.h>
44 #include "libpmcstat.h"
46 static LIST_HEAD(,pmcstat_string) pmcstat_string_hash[PMCSTAT_NHASH];
49 * Intern a copy of string 's', and return a pointer to the
53 pmcstat_interned_string
54 pmcstat_string_intern(const char *s)
56 struct pmcstat_string *ps;
57 const struct pmcstat_string *cps;
60 if ((cps = pmcstat_string_lookup(s)) != NULL)
63 hash = pmcstat_string_compute_hash(s);
66 if ((ps = malloc(sizeof(*ps))) == NULL)
67 err(EX_OSERR, "ERROR: Could not intern string");
70 ps->ps_string = strdup(s);
71 LIST_INSERT_HEAD(&pmcstat_string_hash[hash], ps, ps_next);
72 return ((pmcstat_interned_string) ps);
76 pmcstat_string_unintern(pmcstat_interned_string str)
80 s = ((const struct pmcstat_string *) str)->ps_string;
85 * Compute a 'hash' value for a string.
89 pmcstat_string_compute_hash(const char *s)
93 for (hash = 2166136261; *s; s++)
94 hash = (hash ^ *s) * 16777619;
96 return (hash & PMCSTAT_HASH_MASK);
99 pmcstat_interned_string
100 pmcstat_string_lookup(const char *s)
102 struct pmcstat_string *ps;
105 hash = pmcstat_string_compute_hash(s);
108 LIST_FOREACH(ps, &pmcstat_string_hash[hash], ps_next)
109 if (ps->ps_len == len && ps->ps_hash == hash &&
110 strcmp(ps->ps_string, s) == 0)
116 pmcstat_string_lookup_hash(pmcstat_interned_string s)
118 const struct pmcstat_string *ps;
120 ps = (const struct pmcstat_string *) s;
121 return (ps->ps_hash);
125 * Destroy the string table, free'ing up space.
129 pmcstat_string_shutdown(void)
132 struct pmcstat_string *ps, *pstmp;
134 for (i = 0; i < PMCSTAT_NHASH; i++)
135 LIST_FOREACH_SAFE(ps, &pmcstat_string_hash[i], ps_next,
137 LIST_REMOVE(ps, ps_next);
144 * Initialize the string interning facility.
148 pmcstat_string_initialize(void)
152 for (i = 0; i < PMCSTAT_NHASH; i++)
153 LIST_INIT(&pmcstat_string_hash[i]);