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