xref: /freebsd/lib/libpmcstat/libpmcstat_string.c (revision f7c32ed617858bcd22f8d1b03199099d50125721)
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/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/types.h>
31 #include <sys/cpuset.h>
32 #include <sys/param.h>
33 #include <sys/stat.h>
34 #include <sys/pmc.h>
35 
36 #include <assert.h>
37 #include <err.h>
38 #include <errno.h>
39 #include <pmc.h>
40 #include <pmclog.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <sysexits.h>
45 
46 #include "libpmcstat.h"
47 
48 static LIST_HEAD(,pmcstat_string)	pmcstat_string_hash[PMCSTAT_NHASH];
49 
50 /*
51  * Intern a copy of string 's', and return a pointer to the
52  * interned structure.
53  */
54 
55 pmcstat_interned_string
56 pmcstat_string_intern(const char *s)
57 {
58 	struct pmcstat_string *ps;
59 	const struct pmcstat_string *cps;
60 	int hash, len;
61 
62 	if ((cps = pmcstat_string_lookup(s)) != NULL)
63 		return (cps);
64 
65 	hash = pmcstat_string_compute_hash(s);
66 	len  = strlen(s);
67 
68 	if ((ps = malloc(sizeof(*ps))) == NULL)
69 		err(EX_OSERR, "ERROR: Could not intern string");
70 	ps->ps_len = len;
71 	ps->ps_hash = hash;
72 	ps->ps_string = strdup(s);
73 	LIST_INSERT_HEAD(&pmcstat_string_hash[hash], ps, ps_next);
74 	return ((pmcstat_interned_string) ps);
75 }
76 
77 const char *
78 pmcstat_string_unintern(pmcstat_interned_string str)
79 {
80 	const char *s;
81 
82 	s = ((const struct pmcstat_string *) str)->ps_string;
83 	return (s);
84 }
85 
86 /*
87  * Compute a 'hash' value for a string.
88  */
89 
90 int
91 pmcstat_string_compute_hash(const char *s)
92 {
93 	unsigned hash;
94 
95 	for (hash = 2166136261; *s; s++)
96 		hash = (hash ^ *s) * 16777619;
97 
98 	return (hash & PMCSTAT_HASH_MASK);
99 }
100 
101 pmcstat_interned_string
102 pmcstat_string_lookup(const char *s)
103 {
104 	struct pmcstat_string *ps;
105 	int hash, len;
106 
107 	hash = pmcstat_string_compute_hash(s);
108 	len = strlen(s);
109 
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)
113 		    return (ps);
114 	return (NULL);
115 }
116 
117 int
118 pmcstat_string_lookup_hash(pmcstat_interned_string s)
119 {
120 	const struct pmcstat_string *ps;
121 
122 	ps = (const struct pmcstat_string *) s;
123 	return (ps->ps_hash);
124 }
125 
126 /*
127  * Destroy the string table, free'ing up space.
128  */
129 
130 void
131 pmcstat_string_shutdown(void)
132 {
133 	int i;
134 	struct pmcstat_string *ps, *pstmp;
135 
136 	for (i = 0; i < PMCSTAT_NHASH; i++)
137 		LIST_FOREACH_SAFE(ps, &pmcstat_string_hash[i], ps_next,
138 		    pstmp) {
139 			LIST_REMOVE(ps, ps_next);
140 			free(ps->ps_string);
141 			free(ps);
142 		}
143 }
144 
145 /*
146  * Initialize the string interning facility.
147  */
148 
149 void
150 pmcstat_string_initialize(void)
151 {
152 	int i;
153 
154 	for (i = 0; i < PMCSTAT_NHASH; i++)
155 		LIST_INIT(&pmcstat_string_hash[i]);
156 }
157