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
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.
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]
23 * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
43 init_hash(unsigned int size
)
49 if ((size
& (size
- 1)) != 0)
50 abend("Size must be power of two", NULL
);
52 htp
= (htbl_t
*)my_malloc(sizeof (htbl_t
), NULL
);
54 htp
->tbl
= (hashb_t
*)
55 my_calloc((size_t)size
, sizeof (hashb_t
), NULL
);
58 for (i
= 0; i
< size
; i
++) {
60 (void) mutex_init(&temp
->block
, USYNC_THREAD
, NULL
);
67 destroy_hash(htbl_t
*htp
)
74 for (i
= 0; i
< htp
->size
; i
++) {
76 (void) mutex_destroy(&cur
->block
);
93 free((char *)htp
->tbl
);
99 hash_str(char *str
, unsigned int sz
)
106 for (p
= str
; *p
!= '\0'; p
++) {
107 hash
= (hash
<< 4) + *p
;
108 if ((g
= (hash
& 0xf0000000)) != 0) {
114 return (hash
& (sz
- 1));
119 add_fcall(htbl_t
*htp
, char *lib
, char *key
, unsigned long cnt
)
126 bucket
= hash_str(key
, htp
->size
);
127 cur
= &htp
->tbl
[bucket
];
129 (void) mutex_lock(&cur
->block
);
132 while (tmp
!= NULL
) {
133 if (strcmp(tmp
->key
, key
) == 0) {
134 if (strcmp(tmp
->lib
, lib
) == 0) {
136 (void) mutex_unlock(&cur
->block
);
144 * If we're still here, there was no such fcall recorded
145 * so we make a new entry and add it to the table
148 new = (hentry_t
*)my_malloc(sizeof (hentry_t
), NULL
);
149 new->key
= strdup(key
);
150 if (new->key
== NULL
)
151 abend("Out of memory in htbl.c", NULL
);
152 new->lib
= strdup(lib
);
153 if (new->lib
== NULL
)
154 abend("Out of memory in htbl.c", NULL
);
157 new->next
= cur
->first
;
164 (void) mutex_unlock(&cur
->block
);
168 * iterate_hash locks the table and returns an enumeration struct
169 * using this it is possible to iterate through the entries of a hash table
170 * once finished, use iter_free to unlock the table and free the struct
174 iterate_hash(htbl_t
*tbl
)
180 hentry_t
*tmp
= NULL
;
182 new = (hiter_t
*)my_malloc(sizeof (hiter_t
), NULL
);
185 for (i
= 0; i
< tbl
->size
; i
++) {
187 (void) mutex_lock(&cur
->block
);
201 iter_free(hiter_t
*itr
)
208 for (i
= 0; i
< tbl
->size
; i
++) {
210 (void) mutex_unlock(&cur
->block
);
217 iter_next(hiter_t
*itr
)
236 for (i
= i
+ 1; i
< hash
->size
; i
++) {
251 elements_in_table(htbl_t
*tbl
)
254 hiter_t
*itr
= iterate_hash(tbl
);
255 hentry_t
*tmp
= iter_next(itr
);
256 while (tmp
!= NULL
) {
258 tmp
= iter_next(itr
);