6069 libdisasm: instrlen arch op should have a sane default
[illumos-gate.git] / usr / src / lib / libnsctl / common / hash.c
blob1cc7e8f257e1cda2c2eb7baa2bf54e0dbca8c9fa
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <string.h>
27 #include <stdlib.h>
28 #include <sys/nsctl/nsc_hash.h>
30 #define HASH_PRIME 2039
32 static int calc_hash(const char *);
34 hash_node_t **
35 nsc_create_hash()
37 hash_node_t **hash;
39 hash = (hash_node_t **)calloc(HASH_PRIME, sizeof (hash_node_t *));
40 return (hash);
43 int
44 nsc_insert_node(hash_node_t **hash, void *data, const char *key)
46 int index;
47 hash_node_t *node;
49 node = (hash_node_t *)malloc(sizeof (hash_node_t));
50 if (!node) {
51 return (-1);
53 node->key = strdup(key);
54 node->data = data;
57 * possible enhancement would be to search
58 * in this index for a duplicate
60 index = calc_hash(key);
61 node->next = hash[ index ];
62 hash[ index ] = node;
64 return (0);
68 * lookup
70 * Description:
71 * Searches the hash to find a node.
73 * Return values:
74 * 0 if not found.
75 * pointer to node if found.
77 void *
78 nsc_lookup(hash_node_t **hash, const char *key)
80 int index;
81 hash_node_t *node;
83 index = calc_hash(key);
84 node = hash[ index ];
85 while (node) {
86 if (strcmp(node->key, key) == 0)
87 return (node->data);
88 node = node->next;
90 return (0);
93 void *
94 nsc_remove_node(hash_node_t **hash, char *key)
96 int index;
97 hash_node_t *node, *prev;
98 void *retval;
100 index = calc_hash(key);
101 if (!hash[ index ]) {
102 return (0);
105 if (strcmp(hash[ index ]->key, key) == 0) {
106 node = hash[ index ];
107 retval = node->data;
108 hash[ index ] = hash[ index ]->next;
109 free(node->key);
110 free(node);
111 return (retval);
113 prev = hash[ index ];
114 node = prev->next;
115 while (node && (strcmp(node->key, key) != 0)) {
116 prev = node;
117 node = node->next;
120 /* did we find it? */
121 if (node) {
122 prev->next = node->next;
123 retval = node->data;
124 free(node->key);
125 free(node);
126 return (retval);
128 return (0);
131 void
132 nsc_remove_all(hash_node_t **hash, void (*callback)(void *))
134 int i;
135 hash_node_t *p, *next;
137 for (i = 0; i < HASH_PRIME; i++) {
138 p = hash[ i ];
139 while (p) {
140 next = p->next;
141 if (callback) {
142 callback(p->data);
144 free(p->key);
145 free(p);
146 p = next;
149 free(hash);
152 /* ---------------------------------------------------------------------- */
155 * Basic rotating hash, as per Knuth.
157 static int
158 calc_hash(const char *key)
160 unsigned int hash, i;
161 int len = strlen(key);
162 for (hash = len, i = 0; i < len; i++) {
163 hash = (hash << 5) ^ (hash >> 27) ^ key[ i ];
165 return (hash % HASH_PRIME);