forked from dtrace4linux/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathctf_hash.c
More file actions
142 lines (112 loc) · 3.23 KB
/
Copy pathctf_hash.c
File metadata and controls
142 lines (112 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only.
* See the file usr/src/LICENSING.NOTICE in this distribution or
* http://www.opensolaris.org/license/ for details.
*/
//#pragma ident "@(#)ctf_hash.c 1.3 04/05/04 SMI"
#include <ctf_impl.h>
static const ushort_t _CTF_EMPTY[1] = { 0 };
int
ctf_hash_create(ctf_hash_t *hp, ulong_t nelems)
{
if (nelems > USHRT_MAX)
return (EOVERFLOW);
/*
* If the hash table is going to be empty, don't bother allocating any
* memory and make the only bucket point to a zero so lookups fail.
*/
if (nelems == 0) {
bzero(hp, sizeof (ctf_hash_t));
hp->h_buckets = (ushort_t *)_CTF_EMPTY;
hp->h_nbuckets = 1;
return (0);
}
hp->h_nbuckets = 211; /* use a prime number of hash buckets */
hp->h_nelems = nelems + 1; /* we use index zero as a sentinel */
hp->h_free = 1; /* first free element is index 1 */
hp->h_buckets = ctf_alloc(sizeof (ushort_t) * hp->h_nbuckets);
hp->h_chains = ctf_alloc(sizeof (ctf_helem_t) * hp->h_nelems);
if (hp->h_buckets == NULL || hp->h_chains == NULL) {
ctf_hash_destroy(hp);
return (EAGAIN);
}
bzero(hp->h_buckets, sizeof (ushort_t) * hp->h_nbuckets);
bzero(hp->h_chains, sizeof (ctf_helem_t) * hp->h_nelems);
return (0);
}
uint_t
ctf_hash_size(const ctf_hash_t *hp)
{
return (hp->h_nelems ? hp->h_nelems - 1 : 0);
}
static ulong_t
ctf_hash_compute(const char *key, size_t len)
{
ulong_t g, h = 0;
const char *p, *q = key + len;
size_t n = 0;
for (p = key; p < q; p++, n++) {
h = (h << 4) + *p;
if ((g = (h & 0xf0000000)) != 0) {
h ^= (g >> 24);
h ^= g;
}
}
return (h);
}
int
ctf_hash_insert(ctf_hash_t *hp, ctf_file_t *fp, ushort_t type, uint_t name)
{
ctf_strs_t *ctsp = &fp->ctf_str[CTF_NAME_STID(name)];
const char *str = ctsp->cts_strs + CTF_NAME_OFFSET(name);
ctf_helem_t *hep = &hp->h_chains[hp->h_free];
ulong_t h;
if (type == 0)
return (EINVAL);
if (hp->h_free >= hp->h_nelems)
return (EOVERFLOW);
if (ctsp->cts_strs == NULL)
return (ECTF_STRTAB);
if (ctsp->cts_len <= CTF_NAME_OFFSET(name))
return (ECTF_BADNAME);
if (str[0] == '\0')
return (0); /* just ignore empty strings on behalf of caller */
hep->h_name = name;
hep->h_type = type;
h = ctf_hash_compute(str, strlen(str)) % hp->h_nbuckets;
hep->h_next = hp->h_buckets[h];
hp->h_buckets[h] = hp->h_free++;
return (0);
}
ctf_helem_t *
ctf_hash_lookup(ctf_hash_t *hp, ctf_file_t *fp, const char *key, size_t len)
{
ctf_helem_t *hep;
ctf_strs_t *ctsp;
const char *str;
ushort_t i;
ulong_t h = ctf_hash_compute(key, len) % hp->h_nbuckets;
for (i = hp->h_buckets[h]; i != 0; i = hep->h_next) {
hep = &hp->h_chains[i];
ctsp = &fp->ctf_str[CTF_NAME_STID(hep->h_name)];
str = ctsp->cts_strs + CTF_NAME_OFFSET(hep->h_name);
if (strncmp(key, str, len) == 0 && str[len] == '\0')
return (hep);
}
return (NULL);
}
void
ctf_hash_destroy(ctf_hash_t *hp)
{
if (hp->h_buckets != NULL && hp->h_nbuckets != 1) {
ctf_free(hp->h_buckets, sizeof (ushort_t) * hp->h_nbuckets);
hp->h_buckets = NULL;
}
if (hp->h_chains != NULL) {
ctf_free(hp->h_chains, sizeof (ctf_helem_t) * hp->h_nelems);
hp->h_chains = NULL;
}
}