1 /* $NetBSD: hcreate.c,v 1.10 2014/07/20 20:17:21 christos Exp $ */
4 * Copyright (c) 2001 Christopher G. Demetriou
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
33 * hcreate() / hsearch() / hdestroy()
35 * SysV/XPG4 hash table functions.
37 * Implementation done based on NetBSD manual page and Solaris manual page,
38 * plus my own personal experience about how they're supposed to work.
40 * I tried to look at Knuth (as cited by the Solaris manual page), but
41 * nobody had a copy in the office, so...
44 #include <sys/cdefs.h>
45 #if defined(LIBC_SCCS) && !defined(lint)
46 __RCSID("$NetBSD: hcreate.c,v 1.10 2014/07/20 20:17:21 christos Exp $");
47 #endif /* LIBC_SCCS and not lint */
50 __COPYRIGHT("@(#) Copyright (c) 2001\
51 Christopher G. Demetriou. All rights reserved.");
54 #include "namespace.h"
61 #include <sys/queue.h>
64 * DO NOT MAKE THIS STRUCTURE LARGER THAN 32 BYTES (4 ptrs on 64-bit
65 * ptr machine) without adjusting MAX_BUCKETS_LG2 below.
67 struct internal_entry
{
68 SLIST_ENTRY(internal_entry
) link
;
71 SLIST_HEAD(internal_head
, internal_entry
);
73 #define MIN_BUCKETS_LG2 4
74 #define MIN_BUCKETS (1 << MIN_BUCKETS_LG2)
77 * max * sizeof internal_entry must fit into size_t.
78 * assumes internal_entry is <= 32 (2^5) bytes.
80 #define MAX_BUCKETS_LG2 (sizeof (size_t) * 8 - 1 - 5)
81 #define MAX_BUCKETS ((size_t)1 << MAX_BUCKETS_LG2)
83 /* Default hash function, from db/hash/hash_func.c */
84 extern u_int32_t (*__default_hash
)(const void *, size_t);
86 static struct hsearch_data htable
;
91 _DIAGASSERT(htable
.table
== NULL
);
93 /* Make sure this isn't called when a table already exists. */
94 if (htable
.table
!= NULL
) {
98 return hcreate_r(nel
, &htable
);
102 hcreate_r(size_t nel
, struct hsearch_data
*head
)
104 struct internal_head
*table
;
109 /* If nel is too small, make it min sized. */
110 if (nel
< MIN_BUCKETS
)
113 /* If it's too large, cap it. */
114 if (nel
> MAX_BUCKETS
)
117 /* If it's is not a power of two in size, round up. */
118 if ((nel
& (nel
- 1)) != 0) {
119 for (p2
= 0; nel
!= 0; p2
++)
121 _DIAGASSERT(p2
<= MAX_BUCKETS_LG2
);
125 /* Allocate the table. */
128 p
= malloc(nel
* sizeof table
[0]);
137 for (idx
= 0; idx
< nel
; idx
++)
138 SLIST_INIT(&table
[idx
]);
144 hdestroy1(void (*freekey
)(void *), void (*freedata
)(void *))
146 _DIAGASSERT(htable
.table
!= NULL
);
147 hdestroy1_r(&htable
, freekey
, freedata
);
153 hdestroy1(NULL
, NULL
);
157 hdestroy1_r(struct hsearch_data
*head
, void (*freekey
)(void *),
158 void (*freedata
)(void *))
160 struct internal_entry
*ie
;
163 struct internal_head
*table
;
172 for (idx
= 0; idx
< head
->size
; idx
++) {
173 while (!SLIST_EMPTY(&table
[idx
])) {
174 ie
= SLIST_FIRST(&table
[idx
]);
175 SLIST_REMOVE_HEAD(&table
[idx
], link
);
177 (*freekey
)(ie
->ent
.key
);
179 (*freedata
)(ie
->ent
.data
);
187 hdestroy_r(struct hsearch_data
*head
)
189 hdestroy1_r(head
, NULL
, NULL
);
193 hsearch(ENTRY item
, ACTION action
)
196 _DIAGASSERT(htable
.table
!= NULL
);
197 (void)hsearch_r(item
, action
, &ep
, &htable
);
202 hsearch_r(ENTRY item
, ACTION action
, ENTRY
**itemp
, struct hsearch_data
*head
)
204 struct internal_head
*table
, *chain
;
205 struct internal_entry
*ie
;
210 _DIAGASSERT(item
.key
!= NULL
);
211 _DIAGASSERT(action
== ENTER
|| action
== FIND
);
216 len
= strlen(item
.key
);
217 hashval
= (*__default_hash
)(item
.key
, len
);
219 chain
= &table
[hashval
& (head
->size
- 1)];
220 ie
= SLIST_FIRST(chain
);
222 if (strcmp(ie
->ent
.key
, item
.key
) == 0)
224 ie
= SLIST_NEXT(ie
, link
);
230 } else if (action
== FIND
) {
236 ie
= malloc(sizeof *ie
);
239 ie
->ent
.key
= item
.key
;
240 ie
->ent
.data
= item
.data
;
242 SLIST_INSERT_HEAD(chain
, ie
, link
);