source: trunk/libs/newlib/src/newlib/libc/search/hcreate_r.c

Last change on this file was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 5.0 KB
Line 
1/* $NetBSD: hcreate.c,v 1.2 2001/02/19 21:26:04 ross Exp $ */
2
3/*
4 * Copyright (c) 2001 Christopher G. Demetriou
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
17 *
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.
28 *
29 * <<Id: LICENSE_GC,v 1.1 2001/10/01 23:24:05 cgd Exp>>
30 */
31
32/*
33 * hcreate() / hsearch() / hdestroy()
34 *
35 * SysV/XPG4 hash table functions.
36 *
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.
39 *
40 * I tried to look at Knuth (as cited by the Solaris manual page), but
41 * nobody had a copy in the office, so...
42 */
43
44#include <sys/cdefs.h>
45#if 0
46#if defined(LIBC_SCCS) && !defined(lint)
47__RCSID("$NetBSD: hcreate.c,v 1.2 2001/02/19 21:26:04 ross Exp $");
48#endif /* LIBC_SCCS and not lint */
49#endif
50
51#include <sys/types.h>
52#include <sys/queue.h>
53#include <errno.h>
54#include <search.h>
55#include <stdlib.h>
56#include <string.h>
57
58/*
59 * DO NOT MAKE THIS STRUCTURE LARGER THAN 32 BYTES (4 ptrs on 64-bit
60 * ptr machine) without adjusting MAX_BUCKETS_LG2 below.
61 */
62struct internal_entry {
63        SLIST_ENTRY(internal_entry) link;
64        ENTRY ent;
65};
66SLIST_HEAD(internal_head, internal_entry);
67
68#define MIN_BUCKETS_LG2 4
69#define MIN_BUCKETS     (1 << MIN_BUCKETS_LG2)
70
71/*
72 * max * sizeof internal_entry must fit into size_t.
73 * assumes internal_entry is <= 32 (2^5) bytes.
74 */
75#define MAX_BUCKETS_LG2 (sizeof (size_t) * 8 - 1 - 5)
76#define MAX_BUCKETS     ((size_t)1 << MAX_BUCKETS_LG2)
77
78/* Default hash function, from db/hash/hash_func.c */
79extern __uint32_t (*__default_hash)(const void *, size_t);
80
81int
82hcreate_r(size_t nel, struct hsearch_data *htab)
83{
84        size_t idx;
85        unsigned int p2;
86
87        /* Make sure this this isn't called when a table already exists. */
88        if (htab->htable != NULL) {
89                errno = EINVAL;
90                return 0;
91        }
92
93        /* If nel is too small, make it min sized. */
94        if (nel < MIN_BUCKETS)
95                nel = MIN_BUCKETS;
96
97        /* If it's too large, cap it. */
98        if (nel > MAX_BUCKETS)
99                nel = MAX_BUCKETS;
100
101        /* If it's is not a power of two in size, round up. */
102        if ((nel & (nel - 1)) != 0) {
103                for (p2 = 0; nel != 0; p2++)
104                        nel >>= 1;
105                nel = 1 << p2;
106        }
107       
108        /* Allocate the table. */
109        htab->htablesize = nel;
110        htab->htable = malloc(htab->htablesize * sizeof htab->htable[0]);
111        if (htab->htable == NULL) {
112                errno = ENOMEM;
113                return 0;
114        }
115
116        /* Initialize it. */
117        for (idx = 0; idx < htab->htablesize; idx++)
118                SLIST_INIT(&(htab->htable[idx]));
119
120        return 1;
121}
122
123void
124hdestroy_r(struct hsearch_data *htab)
125{
126#if 0
127        struct internal_entry *ie;
128        size_t idx;
129#endif
130        if (htab->htable == NULL)
131                return;
132
133#if 0
134        for (idx = 0; idx < htab->htablesize; idx++) {
135                while (!SLIST_EMPTY(&(htab->htable[idx]))) {
136                        ie = SLIST_FIRST(&(htab->htable[idx]));
137                        SLIST_REMOVE_HEAD(&(htab->htable[idx]), link);
138                        free(ie->ent.key);
139                        free(ie);
140                }
141        }
142#endif
143        free(htab->htable);
144        htab->htable = NULL;
145}
146
147int
148hsearch_r(ENTRY item, ACTION action, ENTRY **retval, struct hsearch_data *htab)
149{
150        struct internal_head *head;
151        struct internal_entry *ie;
152        __uint32_t hashval;
153        size_t len;
154
155        len = strlen(item.key);
156        hashval = (*__default_hash)(item.key, len);
157
158        head = &(htab->htable[hashval & (htab->htablesize - 1)]);
159        ie = SLIST_FIRST(head);
160        while (ie != NULL) {
161                if (strcmp(ie->ent.key, item.key) == 0)
162                        break;
163                ie = SLIST_NEXT(ie, link);
164        }
165
166        if (ie != NULL)
167          {
168            *retval = &ie->ent;
169            return 1;
170          }
171        else if (action == FIND)
172          {
173            *retval = NULL;
174            return 0;
175          }
176
177        ie = malloc(sizeof *ie);
178        if (ie == NULL)
179          {
180            *retval = NULL;
181            return 0;
182          }
183        ie->ent.key = item.key;
184        ie->ent.data = item.data;
185
186        SLIST_INSERT_HEAD(head, ie, link);
187        *retval = &ie->ent;
188        return 1;
189}
Note: See TracBrowser for help on using the repository browser.