4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 * Portions Copyright 2016 Pedro Giffuni. All rights reserved.
31 #pragma ident "%Z%%M% %I% %E% SMI"
33 #include <sys/types.h>
34 #include <sys/sysmacros.h>
39 #include <dt_strtab.h>
43 dt_strtab_grow(dt_strtab_t *sp)
47 if ((ptr = malloc(sp->str_bufsz)) == NULL)
50 bufs = realloc(sp->str_bufs, (sp->str_nbufs + 1) * sizeof (char *));
60 sp->str_bufs[sp->str_nbufs - 1] = sp->str_ptr;
66 dt_strtab_create(size_t bufsz)
68 dt_strtab_t *sp = malloc(sizeof (dt_strtab_t));
69 uint_t nbuckets = _dtrace_strbuckets;
76 bzero(sp, sizeof (dt_strtab_t));
77 sp->str_hash = calloc(nbuckets, sizeof (dt_strhash_t *));
79 if (sp->str_hash == NULL)
82 sp->str_hashsz = nbuckets;
86 sp->str_bufsz = bufsz;
90 if (dt_strtab_grow(sp) == -1)
93 *sp->str_ptr++ = '\0';
97 dt_strtab_destroy(sp);
102 dt_strtab_destroy(dt_strtab_t *sp)
104 dt_strhash_t *hp, *hq;
107 for (i = 0; i < sp->str_hashsz; i++) {
108 for (hp = sp->str_hash[i]; hp != NULL; hp = hq) {
114 for (i = 0; i < sp->str_nbufs; i++)
115 free(sp->str_bufs[i]);
117 if (sp->str_hash != NULL)
119 if (sp->str_bufs != NULL)
126 dt_strtab_hash(const char *key, size_t *len)
132 for (p = key; *p != '\0'; p++, n++) {
135 if ((g = (h & 0xf0000000)) != 0) {
148 dt_strtab_compare(dt_strtab_t *sp, dt_strhash_t *hp,
149 const char *str, size_t len)
151 ulong_t b = hp->str_buf;
152 const char *buf = hp->str_data;
157 if (buf == sp->str_bufs[b] + sp->str_bufsz)
158 buf = sp->str_bufs[++b];
160 resid = sp->str_bufs[b] + sp->str_bufsz - buf;
163 if ((rv = strncmp(buf, str, n)) != 0)
175 dt_strtab_copyin(dt_strtab_t *sp, const char *str, size_t len)
177 char *old_p = sp->str_ptr;
178 ulong_t old_n = sp->str_nbufs;
180 ulong_t b = sp->str_nbufs - 1;
184 if (sp->str_ptr == sp->str_bufs[b] + sp->str_bufsz) {
185 if (dt_strtab_grow(sp) == -1)
190 resid = sp->str_bufs[b] + sp->str_bufsz - sp->str_ptr;
192 bcopy(str, sp->str_ptr, n);
202 while (sp->str_nbufs != old_n)
203 free(sp->str_bufs[--sp->str_nbufs]);
210 dt_strtab_index(dt_strtab_t *sp, const char *str)
216 if (str == NULL || str[0] == '\0')
217 return (0); /* we keep a \0 at offset 0 to simplify things */
219 h = dt_strtab_hash(str, &len) % sp->str_hashsz;
221 for (hp = sp->str_hash[h]; hp != NULL; hp = hp->str_next) {
222 if (dt_strtab_compare(sp, hp, str, len + 1) == 0)
223 return (hp->str_off);
230 dt_strtab_insert(dt_strtab_t *sp, const char *str)
237 if ((off = dt_strtab_index(sp, str)) != -1)
240 h = dt_strtab_hash(str, &len) % sp->str_hashsz;
243 * Create a new hash bucket, initialize it, and insert it at the front
244 * of the hash chain for the appropriate bucket.
246 if ((hp = malloc(sizeof (dt_strhash_t))) == NULL)
249 hp->str_data = sp->str_ptr;
250 hp->str_buf = sp->str_nbufs - 1;
251 hp->str_off = sp->str_size;
253 hp->str_next = sp->str_hash[h];
256 * Now copy the string data into our buffer list, and then update
257 * the global counts of strings and bytes. Return str's byte offset.
259 if (dt_strtab_copyin(sp, str, len + 1) == -1)
263 sp->str_size += len + 1;
264 sp->str_hash[h] = hp;
266 return (hp->str_off);
270 dt_strtab_size(const dt_strtab_t *sp)
272 return (sp->str_size);
276 dt_strtab_write(const dt_strtab_t *sp, dt_strtab_write_f *func, void *private)
278 ssize_t res, total = 0;
282 for (i = 0; i < sp->str_nbufs; i++, total += res) {
283 if (i == sp->str_nbufs - 1)
284 n = sp->str_ptr - sp->str_bufs[i];
288 if ((res = func(sp->str_bufs[i], n, total, private)) <= 0)
292 if (total == 0 && sp->str_size != 0)