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 __FBSDID("$FreeBSD$");
30 #include <sys/types.h>
31 #include <sys/cpuset.h>
32 #include <sys/param.h>
46 #include "libpmcstat.h"
48 static LIST_HEAD(,pmcstat_string) pmcstat_string_hash[PMCSTAT_NHASH];
51 * Intern a copy of string 's', and return a pointer to the
55 pmcstat_interned_string
56 pmcstat_string_intern(const char *s)
58 struct pmcstat_string *ps;
59 const struct pmcstat_string *cps;
62 if ((cps = pmcstat_string_lookup(s)) != NULL)
65 hash = pmcstat_string_compute_hash(s);
68 if ((ps = malloc(sizeof(*ps))) == NULL)
69 err(EX_OSERR, "ERROR: Could not intern string");
72 ps->ps_string = strdup(s);
73 LIST_INSERT_HEAD(&pmcstat_string_hash[hash], ps, ps_next);
74 return ((pmcstat_interned_string) ps);
78 pmcstat_string_unintern(pmcstat_interned_string str)
82 s = ((const struct pmcstat_string *) str)->ps_string;
87 * Compute a 'hash' value for a string.
91 pmcstat_string_compute_hash(const char *s)
95 for (hash = 2166136261; *s; s++)
96 hash = (hash ^ *s) * 16777619;
98 return (hash & PMCSTAT_HASH_MASK);
101 pmcstat_interned_string
102 pmcstat_string_lookup(const char *s)
104 struct pmcstat_string *ps;
107 hash = pmcstat_string_compute_hash(s);
110 LIST_FOREACH(ps, &pmcstat_string_hash[hash], ps_next)
111 if (ps->ps_len == len && ps->ps_hash == hash &&
112 strcmp(ps->ps_string, s) == 0)
118 pmcstat_string_lookup_hash(pmcstat_interned_string s)
120 const struct pmcstat_string *ps;
122 ps = (const struct pmcstat_string *) s;
123 return (ps->ps_hash);
127 * Destroy the string table, free'ing up space.
131 pmcstat_string_shutdown(void)
134 struct pmcstat_string *ps, *pstmp;
136 for (i = 0; i < PMCSTAT_NHASH; i++)
137 LIST_FOREACH_SAFE(ps, &pmcstat_string_hash[i], ps_next,
139 LIST_REMOVE(ps, ps_next);
146 * Initialize the string interning facility.
150 pmcstat_string_initialize(void)
154 for (i = 0; i < PMCSTAT_NHASH; i++)
155 LIST_INIT(&pmcstat_string_hash[i]);