Home | History | Annotate | Line # | Download | only in stdlib
hcreate.c revision 1.3
      1  1.3  lukem /* $NetBSD: hcreate.c,v 1.3 2001/02/21 16:31:01 lukem Exp $ */
      2  1.1    cgd 
      3  1.1    cgd /*
      4  1.1    cgd  * Copyright (c) 2001 Christopher G. Demetriou
      5  1.1    cgd  * All rights reserved.
      6  1.1    cgd  *
      7  1.1    cgd  * Redistribution and use in source and binary forms, with or without
      8  1.1    cgd  * modification, are permitted provided that the following conditions
      9  1.1    cgd  * are met:
     10  1.1    cgd  * 1. Redistributions of source code must retain the above copyright
     11  1.1    cgd  *    notice, this list of conditions and the following disclaimer.
     12  1.1    cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1    cgd  *    notice, this list of conditions and the following disclaimer in the
     14  1.1    cgd  *    documentation and/or other materials provided with the distribution.
     15  1.1    cgd  * 3. All advertising materials mentioning features or use of this software
     16  1.1    cgd  *    must display the following acknowledgement:
     17  1.1    cgd  *          This product includes software developed for the
     18  1.1    cgd  *          NetBSD Project.  See http://www.netbsd.org/ for
     19  1.1    cgd  *          information about NetBSD.
     20  1.1    cgd  * 4. The name of the author may not be used to endorse or promote products
     21  1.1    cgd  *    derived from this software without specific prior written permission.
     22  1.1    cgd  *
     23  1.1    cgd  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24  1.1    cgd  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  1.1    cgd  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  1.1    cgd  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     27  1.1    cgd  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     28  1.1    cgd  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29  1.1    cgd  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30  1.1    cgd  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31  1.1    cgd  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     32  1.1    cgd  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  1.1    cgd  *
     34  1.1    cgd  * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
     35  1.1    cgd  */
     36  1.1    cgd 
     37  1.1    cgd /*
     38  1.1    cgd  * hcreate() / hsearch() / hdestroy()
     39  1.1    cgd  *
     40  1.1    cgd  * SysV/XPG4 hash table functions.
     41  1.1    cgd  *
     42  1.1    cgd  * Implementation done based on NetBSD manual page and Solaris manual page,
     43  1.1    cgd  * plus my own personal experience about how they're supposed to work.
     44  1.1    cgd  *
     45  1.1    cgd  * I tried to look at Knuth (as cited by the Solaris manual page), but
     46  1.1    cgd  * nobody had a copy in the office, so...
     47  1.1    cgd  */
     48  1.1    cgd 
     49  1.1    cgd #include <sys/cdefs.h>
     50  1.1    cgd #if defined(LIBC_SCCS) && !defined(lint)
     51  1.3  lukem __RCSID("$NetBSD: hcreate.c,v 1.3 2001/02/21 16:31:01 lukem Exp $");
     52  1.1    cgd #endif /* LIBC_SCCS and not lint */
     53  1.1    cgd 
     54  1.1    cgd #if !defined(lint)
     55  1.1    cgd __COPYRIGHT(
     56  1.1    cgd "@(#) Copyright (c) 2001 Christopher G. Demetriou.  All rights reserved.\n");
     57  1.1    cgd #endif /* not lint */
     58  1.1    cgd 
     59  1.1    cgd #include "namespace.h"
     60  1.1    cgd #include <assert.h>
     61  1.1    cgd #include <errno.h>
     62  1.1    cgd #include <inttypes.h>
     63  1.1    cgd #include <search.h>
     64  1.1    cgd #include <stdlib.h>
     65  1.1    cgd #include <string.h>
     66  1.1    cgd #include <sys/queue.h>
     67  1.1    cgd 
     68  1.1    cgd /*
     69  1.1    cgd  * DO NOT MAKE THIS STRUCTURE LARGER THAN 32 BYTES (4 ptrs on 64-bit
     70  1.1    cgd  * ptr machine) without adjusting MAX_BUCKETS_LG2 below.
     71  1.1    cgd  */
     72  1.1    cgd struct internal_entry {
     73  1.1    cgd 	SLIST_ENTRY(internal_entry) link;
     74  1.1    cgd 	ENTRY ent;
     75  1.1    cgd };
     76  1.1    cgd SLIST_HEAD(internal_head, internal_entry);
     77  1.1    cgd 
     78  1.1    cgd #define	MIN_BUCKETS_LG2	4
     79  1.1    cgd #define	MIN_BUCKETS	(1 << MIN_BUCKETS_LG2)
     80  1.1    cgd 
     81  1.1    cgd /*
     82  1.1    cgd  * max * sizeof internal_entry must fit into size_t.
     83  1.1    cgd  * assumes internal_entry is <= 32 (2^5) bytes.
     84  1.1    cgd  */
     85  1.1    cgd #define	MAX_BUCKETS_LG2	(sizeof (size_t) * 8 - 1 - 5)
     86  1.2   ross #define	MAX_BUCKETS	((size_t)1 << MAX_BUCKETS_LG2)
     87  1.1    cgd 
     88  1.1    cgd /* Default hash function, from db/hash/hash_func.c */
     89  1.1    cgd extern u_int32_t (*__default_hash)(const void *, size_t);
     90  1.1    cgd 
     91  1.1    cgd static struct internal_head *htable;
     92  1.1    cgd static size_t htablesize;
     93  1.1    cgd 
     94  1.1    cgd int
     95  1.1    cgd hcreate(size_t nel)
     96  1.1    cgd {
     97  1.1    cgd 	size_t idx;
     98  1.1    cgd 	unsigned int p2;
     99  1.1    cgd 
    100  1.1    cgd 	/* Make sure this this isn't called when a table already exists. */
    101  1.1    cgd 	_DIAGASSERT(htable == NULL);
    102  1.1    cgd 	if (htable != NULL) {
    103  1.1    cgd 		errno = EINVAL;
    104  1.1    cgd 		return 0;
    105  1.1    cgd 	}
    106  1.1    cgd 
    107  1.1    cgd 	/* If nel is too small, make it min sized. */
    108  1.1    cgd 	if (nel < MIN_BUCKETS)
    109  1.1    cgd 		nel = MIN_BUCKETS;
    110  1.1    cgd 
    111  1.1    cgd 	/* If it's too large, cap it. */
    112  1.1    cgd 	if (nel > MAX_BUCKETS)
    113  1.1    cgd 		nel = MAX_BUCKETS;
    114  1.1    cgd 
    115  1.1    cgd 	/* If it's is not a power of two in size, round up. */
    116  1.1    cgd 	if ((nel & (nel - 1)) != 0) {
    117  1.1    cgd 		for (p2 = 0; nel != 0; p2++)
    118  1.1    cgd 			nel >>= 1;
    119  1.1    cgd 		_DIAGASSERT(p2 <= MAX_BUCKETS_LG2);
    120  1.1    cgd 		nel = 1 << p2;
    121  1.1    cgd 	}
    122  1.1    cgd 
    123  1.1    cgd 	/* Allocate the table. */
    124  1.1    cgd 	htablesize = nel;
    125  1.1    cgd 	htable = malloc(htablesize * sizeof htable[0]);
    126  1.1    cgd 	if (htable == NULL) {
    127  1.1    cgd 		errno = ENOMEM;
    128  1.1    cgd 		return 0;
    129  1.1    cgd 	}
    130  1.1    cgd 
    131  1.1    cgd 	/* Initialize it. */
    132  1.1    cgd 	for (idx = 0; idx < htablesize; idx++)
    133  1.1    cgd 		SLIST_INIT(&htable[idx]);
    134  1.1    cgd 
    135  1.1    cgd 	return 1;
    136  1.1    cgd }
    137  1.1    cgd 
    138  1.1    cgd void
    139  1.1    cgd hdestroy(void)
    140  1.1    cgd {
    141  1.1    cgd 	struct internal_entry *ie;
    142  1.1    cgd 	size_t idx;
    143  1.1    cgd 
    144  1.1    cgd 	_DIAGASSERT(htable != NULL);
    145  1.1    cgd 	if (htable == NULL)
    146  1.1    cgd 		return;
    147  1.1    cgd 
    148  1.1    cgd 	for (idx = 0; idx < htablesize; idx++) {
    149  1.1    cgd 		while (!SLIST_EMPTY(&htable[idx])) {
    150  1.1    cgd 			ie = SLIST_FIRST(&htable[idx]);
    151  1.1    cgd 			SLIST_REMOVE_HEAD(&htable[idx], link);
    152  1.1    cgd 			free(ie->ent.key);
    153  1.1    cgd 			free(ie);
    154  1.1    cgd 		}
    155  1.1    cgd 	}
    156  1.1    cgd 	free(htable);
    157  1.1    cgd 	htable = NULL;
    158  1.1    cgd }
    159  1.1    cgd 
    160  1.1    cgd ENTRY *
    161  1.1    cgd hsearch(ENTRY item, ACTION action)
    162  1.1    cgd {
    163  1.1    cgd 	struct internal_head *head;
    164  1.1    cgd 	struct internal_entry *ie;
    165  1.1    cgd 	uint32_t hashval;
    166  1.1    cgd 	size_t len;
    167  1.1    cgd 
    168  1.1    cgd 	_DIAGASSERT(htable != NULL);
    169  1.3  lukem 	_DIAGASSERT(item.key != NULL);
    170  1.3  lukem 	_DIAGASSERT(action == ENTER || action == FIND);
    171  1.1    cgd 
    172  1.1    cgd 	len = strlen(item.key);
    173  1.1    cgd 	hashval = (*__default_hash)(item.key, len);
    174  1.1    cgd 
    175  1.1    cgd 	head = &htable[hashval & (htablesize - 1)];
    176  1.1    cgd 	ie = SLIST_FIRST(head);
    177  1.1    cgd 	while (ie != NULL) {
    178  1.1    cgd 		if (strcmp(ie->ent.key, item.key) == 0)
    179  1.1    cgd 			break;
    180  1.1    cgd 		ie = SLIST_NEXT(ie, link);
    181  1.1    cgd 	}
    182  1.1    cgd 
    183  1.1    cgd 	if (ie != NULL)
    184  1.1    cgd 		return &ie->ent;
    185  1.1    cgd 	else if (action == FIND)
    186  1.1    cgd 		return NULL;
    187  1.1    cgd 
    188  1.1    cgd 	ie = malloc(sizeof *ie);
    189  1.1    cgd 	if (ie == NULL)
    190  1.1    cgd 		return NULL;
    191  1.1    cgd 	ie->ent.key = item.key;
    192  1.1    cgd 	ie->ent.data = item.data;
    193  1.1    cgd 
    194  1.1    cgd 	SLIST_INSERT_HEAD(head, ie, link);
    195  1.1    cgd 	return &ie->ent;
    196  1.1    cgd }
    197