1 | /*
|
---|
2 | * CDDL HEADER START
|
---|
3 | *
|
---|
4 | * The contents of this file are subject to the terms of the
|
---|
5 | * Common Development and Distribution License, Version 1.0 only
|
---|
6 | * (the "License"). You may not use this file except in compliance
|
---|
7 | * with the License.
|
---|
8 | *
|
---|
9 | * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
|
---|
10 | * or http://www.opensolaris.org/os/licensing.
|
---|
11 | * See the License for the specific language governing permissions
|
---|
12 | * and limitations under the License.
|
---|
13 | *
|
---|
14 | * When distributing Covered Code, include this CDDL HEADER in each
|
---|
15 | * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
|
---|
16 | * If applicable, add the following below this CDDL HEADER, with the
|
---|
17 | * fields enclosed by brackets "[]" replaced with your own identifying
|
---|
18 | * information: Portions Copyright [yyyy] [name of copyright owner]
|
---|
19 | *
|
---|
20 | * CDDL HEADER END
|
---|
21 | */
|
---|
22 |
|
---|
23 | /*
|
---|
24 | * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
|
---|
25 | * Use is subject to license terms.
|
---|
26 | */
|
---|
27 |
|
---|
28 | #ifndef VBOX
|
---|
29 | #pragma ident "%Z%%M% %I% %E% SMI"
|
---|
30 | #endif
|
---|
31 |
|
---|
32 | #include <ctf_impl.h>
|
---|
33 |
|
---|
34 | static const ushort_t _CTF_EMPTY[1] = { 0 };
|
---|
35 |
|
---|
36 | int
|
---|
37 | ctf_hash_create(ctf_hash_t *hp, ulong_t nelems)
|
---|
38 | {
|
---|
39 | if (nelems > USHRT_MAX)
|
---|
40 | return (EOVERFLOW);
|
---|
41 |
|
---|
42 | /*
|
---|
43 | * If the hash table is going to be empty, don't bother allocating any
|
---|
44 | * memory and make the only bucket point to a zero so lookups fail.
|
---|
45 | */
|
---|
46 | if (nelems == 0) {
|
---|
47 | bzero(hp, sizeof (ctf_hash_t));
|
---|
48 | hp->h_buckets = (ushort_t *)_CTF_EMPTY;
|
---|
49 | hp->h_nbuckets = 1;
|
---|
50 | return (0);
|
---|
51 | }
|
---|
52 |
|
---|
53 | hp->h_nbuckets = 211; /* use a prime number of hash buckets */
|
---|
54 | hp->h_nelems = nelems + 1; /* we use index zero as a sentinel */
|
---|
55 | hp->h_free = 1; /* first free element is index 1 */
|
---|
56 |
|
---|
57 | hp->h_buckets = ctf_alloc(sizeof (ushort_t) * hp->h_nbuckets);
|
---|
58 | hp->h_chains = ctf_alloc(sizeof (ctf_helem_t) * hp->h_nelems);
|
---|
59 |
|
---|
60 | if (hp->h_buckets == NULL || hp->h_chains == NULL) {
|
---|
61 | ctf_hash_destroy(hp);
|
---|
62 | return (EAGAIN);
|
---|
63 | }
|
---|
64 |
|
---|
65 | bzero(hp->h_buckets, sizeof (ushort_t) * hp->h_nbuckets);
|
---|
66 | bzero(hp->h_chains, sizeof (ctf_helem_t) * hp->h_nelems);
|
---|
67 |
|
---|
68 | return (0);
|
---|
69 | }
|
---|
70 |
|
---|
71 | uint_t
|
---|
72 | ctf_hash_size(const ctf_hash_t *hp)
|
---|
73 | {
|
---|
74 | return (hp->h_nelems ? hp->h_nelems - 1 : 0);
|
---|
75 | }
|
---|
76 |
|
---|
77 | static ulong_t
|
---|
78 | ctf_hash_compute(const char *key, size_t len)
|
---|
79 | {
|
---|
80 | ulong_t g, h = 0;
|
---|
81 | const char *p, *q = key + len;
|
---|
82 | size_t n = 0;
|
---|
83 |
|
---|
84 | for (p = key; p < q; p++, n++) {
|
---|
85 | h = (h << 4) + *p;
|
---|
86 |
|
---|
87 | if ((g = (h & 0xf0000000)) != 0) {
|
---|
88 | h ^= (g >> 24);
|
---|
89 | h ^= g;
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | return (h);
|
---|
94 | }
|
---|
95 |
|
---|
96 | int
|
---|
97 | ctf_hash_insert(ctf_hash_t *hp, ctf_file_t *fp, ushort_t type, uint_t name)
|
---|
98 | {
|
---|
99 | ctf_strs_t *ctsp = &fp->ctf_str[CTF_NAME_STID(name)];
|
---|
100 | const char *str = ctsp->cts_strs + CTF_NAME_OFFSET(name);
|
---|
101 | ctf_helem_t *hep = &hp->h_chains[hp->h_free];
|
---|
102 | ulong_t h;
|
---|
103 |
|
---|
104 | if (type == 0)
|
---|
105 | return (EINVAL);
|
---|
106 |
|
---|
107 | if (hp->h_free >= hp->h_nelems)
|
---|
108 | return (EOVERFLOW);
|
---|
109 |
|
---|
110 | if (ctsp->cts_strs == NULL)
|
---|
111 | return (ECTF_STRTAB);
|
---|
112 |
|
---|
113 | if (ctsp->cts_len <= CTF_NAME_OFFSET(name))
|
---|
114 | return (ECTF_BADNAME);
|
---|
115 |
|
---|
116 | if (str[0] == '\0')
|
---|
117 | return (0); /* just ignore empty strings on behalf of caller */
|
---|
118 |
|
---|
119 | hep->h_name = name;
|
---|
120 | hep->h_type = type;
|
---|
121 | h = ctf_hash_compute(str, strlen(str)) % hp->h_nbuckets;
|
---|
122 | hep->h_next = hp->h_buckets[h];
|
---|
123 | hp->h_buckets[h] = hp->h_free++;
|
---|
124 |
|
---|
125 | return (0);
|
---|
126 | }
|
---|
127 |
|
---|
128 | /*
|
---|
129 | * Wrapper for ctf_hash_lookup/ctf_hash_insert: if the key is already in the
|
---|
130 | * hash, override the previous definition with this new official definition.
|
---|
131 | * If the key is not present, then call ctf_hash_insert() and hash it in.
|
---|
132 | */
|
---|
133 | int
|
---|
134 | ctf_hash_define(ctf_hash_t *hp, ctf_file_t *fp, ushort_t type, uint_t name)
|
---|
135 | {
|
---|
136 | const char *str = ctf_strptr(fp, name);
|
---|
137 | ctf_helem_t *hep = ctf_hash_lookup(hp, fp, str, strlen(str));
|
---|
138 |
|
---|
139 | if (hep == NULL)
|
---|
140 | return (ctf_hash_insert(hp, fp, type, name));
|
---|
141 |
|
---|
142 | hep->h_type = type;
|
---|
143 | return (0);
|
---|
144 | }
|
---|
145 |
|
---|
146 | ctf_helem_t *
|
---|
147 | ctf_hash_lookup(ctf_hash_t *hp, ctf_file_t *fp, const char *key, size_t len)
|
---|
148 | {
|
---|
149 | ctf_helem_t *hep;
|
---|
150 | ctf_strs_t *ctsp;
|
---|
151 | const char *str;
|
---|
152 | ushort_t i;
|
---|
153 |
|
---|
154 | ulong_t h = ctf_hash_compute(key, len) % hp->h_nbuckets;
|
---|
155 |
|
---|
156 | for (i = hp->h_buckets[h]; i != 0; i = hep->h_next) {
|
---|
157 | hep = &hp->h_chains[i];
|
---|
158 | ctsp = &fp->ctf_str[CTF_NAME_STID(hep->h_name)];
|
---|
159 | str = ctsp->cts_strs + CTF_NAME_OFFSET(hep->h_name);
|
---|
160 |
|
---|
161 | if (strncmp(key, str, len) == 0 && str[len] == '\0')
|
---|
162 | return (hep);
|
---|
163 | }
|
---|
164 |
|
---|
165 | return (NULL);
|
---|
166 | }
|
---|
167 |
|
---|
168 | void
|
---|
169 | ctf_hash_destroy(ctf_hash_t *hp)
|
---|
170 | {
|
---|
171 | if (hp->h_buckets != NULL && hp->h_nbuckets != 1) {
|
---|
172 | ctf_free(hp->h_buckets, sizeof (ushort_t) * hp->h_nbuckets);
|
---|
173 | hp->h_buckets = NULL;
|
---|
174 | }
|
---|
175 |
|
---|
176 | if (hp->h_chains != NULL) {
|
---|
177 | ctf_free(hp->h_chains, sizeof (ctf_helem_t) * hp->h_nelems);
|
---|
178 | hp->h_chains = NULL;
|
---|
179 | }
|
---|
180 | }
|
---|