1da2e3ebdSchin /***********************************************************************
2da2e3ebdSchin * *
3da2e3ebdSchin * This software is part of the ast package *
4*3e14f97fSRoger A. Faulkner * Copyright (c) 1985-2010 AT&T Intellectual Property *
5da2e3ebdSchin * and is licensed under the *
6da2e3ebdSchin * Common Public License, Version 1.0 *
77c2fbfb3SApril Chin * by AT&T Intellectual Property *
8da2e3ebdSchin * *
9da2e3ebdSchin * A copy of the License is available at *
10da2e3ebdSchin * http://www.opensource.org/licenses/cpl1.0.txt *
11da2e3ebdSchin * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
12da2e3ebdSchin * *
13da2e3ebdSchin * Information and Software Systems Research *
14da2e3ebdSchin * AT&T Research *
15da2e3ebdSchin * Florham Park NJ *
16da2e3ebdSchin * *
17da2e3ebdSchin * Glenn Fowler <gsf@research.att.com> *
18da2e3ebdSchin * David Korn <dgk@research.att.com> *
19da2e3ebdSchin * Phong Vo <kpv@research.att.com> *
20da2e3ebdSchin * *
21da2e3ebdSchin ***********************************************************************/
22da2e3ebdSchin #pragma prototyped
23da2e3ebdSchin /*
24da2e3ebdSchin * Glenn Fowler
25da2e3ebdSchin * AT&T Research
26da2e3ebdSchin *
27da2e3ebdSchin * hash table library
28da2e3ebdSchin */
29da2e3ebdSchin
30da2e3ebdSchin static const char id_hash[] = "\n@(#)$Id: hash (AT&T Research) 1996-08-11 $\0\n";
31da2e3ebdSchin
32da2e3ebdSchin #include "hashlib.h"
33da2e3ebdSchin
34da2e3ebdSchin Hash_info_t hash_info = { 0 };
35da2e3ebdSchin
36da2e3ebdSchin /*
37da2e3ebdSchin * create a new hash table
38da2e3ebdSchin */
39da2e3ebdSchin
40da2e3ebdSchin Hash_table_t*
hashalloc(Hash_table_t * ref,...)41da2e3ebdSchin hashalloc(Hash_table_t* ref, ...)
42da2e3ebdSchin {
43da2e3ebdSchin register Hash_table_t* tab;
44da2e3ebdSchin register Hash_table_t* ret = 0;
45da2e3ebdSchin register int internal;
46da2e3ebdSchin int n;
47da2e3ebdSchin va_list ap;
48da2e3ebdSchin va_list va[4];
49da2e3ebdSchin va_list* vp = va;
50da2e3ebdSchin Hash_region_f region = 0;
51da2e3ebdSchin void* handle;
52da2e3ebdSchin
53da2e3ebdSchin va_start(ap, ref);
54da2e3ebdSchin
55da2e3ebdSchin /*
56da2e3ebdSchin * check for HASH_region which must be first
57da2e3ebdSchin */
58da2e3ebdSchin
59da2e3ebdSchin n = va_arg(ap, int);
60da2e3ebdSchin if (!ref && n == HASH_region)
61da2e3ebdSchin {
62da2e3ebdSchin region = va_arg(ap, Hash_region_f);
63da2e3ebdSchin handle = va_arg(ap, void*);
64da2e3ebdSchin n = va_arg(ap, int);
65da2e3ebdSchin if (!(tab = (Hash_table_t*)(*region)(handle, NiL, sizeof(Hash_table_t), 0)))
66da2e3ebdSchin goto out;
67da2e3ebdSchin memset(tab, 0, sizeof(Hash_table_t));
68da2e3ebdSchin }
69da2e3ebdSchin else if (!(tab = newof(0, Hash_table_t, 1, 0)))
70da2e3ebdSchin goto out;
71da2e3ebdSchin tab->bucketsize = (sizeof(Hash_header_t) + sizeof(char*) - 1) / sizeof(char*);
72da2e3ebdSchin if (ref)
73da2e3ebdSchin {
74da2e3ebdSchin tab->flags = ref->flags & ~HASH_RESET;
75da2e3ebdSchin tab->root = ref->root;
76da2e3ebdSchin internal = HASH_INTERNAL;
77da2e3ebdSchin }
78da2e3ebdSchin else
79da2e3ebdSchin {
80da2e3ebdSchin if (region)
81da2e3ebdSchin {
82da2e3ebdSchin if (!(tab->root = (Hash_root_t*)(*region)(handle, NiL, sizeof(Hash_root_t), 0)))
83da2e3ebdSchin goto out;
84da2e3ebdSchin memset(tab->root, 0, sizeof(Hash_root_t));
85da2e3ebdSchin }
86da2e3ebdSchin else if (!(tab->root = newof(0, Hash_root_t, 1, 0)))
87da2e3ebdSchin goto out;
88da2e3ebdSchin if (!(tab->root->local = newof(0, Hash_local_t, 1, 0)))
89da2e3ebdSchin goto out;
90da2e3ebdSchin if (tab->root->local->region = region)
91da2e3ebdSchin tab->root->local->handle = handle;
92da2e3ebdSchin tab->root->meanchain = HASHMEANCHAIN;
93da2e3ebdSchin internal = 0;
94da2e3ebdSchin }
95da2e3ebdSchin tab->size = HASHMINSIZE;
96da2e3ebdSchin for (;;)
97da2e3ebdSchin {
98da2e3ebdSchin switch (n)
99da2e3ebdSchin {
100da2e3ebdSchin case HASH_alloc:
101da2e3ebdSchin if (ref) goto out;
102da2e3ebdSchin tab->root->local->alloc = va_arg(ap, Hash_alloc_f);
103da2e3ebdSchin break;
104da2e3ebdSchin case HASH_bucketsize:
105da2e3ebdSchin n = (va_arg(ap, int) + sizeof(char*) - 1) / sizeof(char*);
106da2e3ebdSchin if (n > UCHAR_MAX) goto out;
107da2e3ebdSchin if (n > tab->bucketsize) tab->bucketsize = n;
108da2e3ebdSchin break;
109da2e3ebdSchin case HASH_clear:
110da2e3ebdSchin tab->flags &= ~(va_arg(ap, int) & ~internal);
111da2e3ebdSchin break;
112da2e3ebdSchin case HASH_compare:
113da2e3ebdSchin if (ref) goto out;
114da2e3ebdSchin tab->root->local->compare = va_arg(ap, Hash_compare_f);
115da2e3ebdSchin break;
116da2e3ebdSchin case HASH_free:
117da2e3ebdSchin if (ref) goto out;
118da2e3ebdSchin tab->root->local->free = va_arg(ap, Hash_free_f);
119da2e3ebdSchin break;
120da2e3ebdSchin case HASH_hash:
121da2e3ebdSchin if (ref) goto out;
122da2e3ebdSchin tab->root->local->hash = va_arg(ap, Hash_hash_f);
123da2e3ebdSchin break;
124da2e3ebdSchin case HASH_meanchain:
125da2e3ebdSchin if (ref) goto out;
126da2e3ebdSchin tab->root->meanchain = va_arg(ap, int);
127da2e3ebdSchin break;
128da2e3ebdSchin case HASH_name:
129da2e3ebdSchin tab->name = va_arg(ap, char*);
130da2e3ebdSchin break;
131da2e3ebdSchin case HASH_namesize:
132da2e3ebdSchin if (ref) goto out;
133da2e3ebdSchin tab->root->namesize = va_arg(ap, int);
134da2e3ebdSchin break;
135da2e3ebdSchin case HASH_region:
136da2e3ebdSchin goto out;
137da2e3ebdSchin case HASH_set:
138da2e3ebdSchin tab->flags |= (va_arg(ap, int) & ~internal);
139da2e3ebdSchin break;
140da2e3ebdSchin case HASH_size:
141da2e3ebdSchin tab->size = va_arg(ap, int);
142da2e3ebdSchin if (tab->size & (tab->size - 1)) tab->flags |= HASH_FIXED;
143da2e3ebdSchin break;
144da2e3ebdSchin case HASH_table:
145da2e3ebdSchin tab->table = va_arg(ap, Hash_bucket_t**);
146da2e3ebdSchin tab->flags |= HASH_STATIC;
147da2e3ebdSchin break;
148da2e3ebdSchin case HASH_va_list:
149da2e3ebdSchin if (vp < &va[elementsof(va)])
150da2e3ebdSchin {
151da2e3ebdSchin va_copy(*vp, ap);
152da2e3ebdSchin vp++;
153da2e3ebdSchin }
154da2e3ebdSchin va_copy(ap, va_listval(va_arg(ap, va_listarg)));
155da2e3ebdSchin break;
156da2e3ebdSchin case 0:
157da2e3ebdSchin if (vp > va)
158da2e3ebdSchin {
159da2e3ebdSchin vp--;
160da2e3ebdSchin va_copy(ap, *vp);
161da2e3ebdSchin break;
162da2e3ebdSchin }
163da2e3ebdSchin if (tab->flags & HASH_SCOPE)
164da2e3ebdSchin {
165da2e3ebdSchin if (!(tab->scope = ref)) goto out;
166da2e3ebdSchin ref->frozen++;
167da2e3ebdSchin }
168da2e3ebdSchin if (!tab->table)
169da2e3ebdSchin {
170da2e3ebdSchin if (region)
171da2e3ebdSchin {
172da2e3ebdSchin if (!(tab->table = (Hash_bucket_t**)(*region)(handle, NiL, sizeof(Hash_bucket_t*) * tab->size, 0)))
173da2e3ebdSchin goto out;
174da2e3ebdSchin memset(tab->table, 0, sizeof(Hash_bucket_t*) * tab->size);
175da2e3ebdSchin }
176da2e3ebdSchin else if (!(tab->table = newof(0, Hash_bucket_t*, tab->size, 0))) goto out;
177da2e3ebdSchin }
178da2e3ebdSchin if (!ref)
179da2e3ebdSchin {
180da2e3ebdSchin tab->root->flags = tab->flags & HASH_INTERNAL;
181da2e3ebdSchin tab->root->next = hash_info.list;
182da2e3ebdSchin hash_info.list = tab->root;
183da2e3ebdSchin }
184da2e3ebdSchin if (!region)
185da2e3ebdSchin {
186da2e3ebdSchin tab->next = tab->root->references;
187da2e3ebdSchin tab->root->references = tab;
188da2e3ebdSchin }
189da2e3ebdSchin ret = tab;
190da2e3ebdSchin goto out;
191da2e3ebdSchin default:
192da2e3ebdSchin goto out;
193da2e3ebdSchin }
194da2e3ebdSchin n = va_arg(ap, int);
195da2e3ebdSchin }
196da2e3ebdSchin out:
197da2e3ebdSchin va_end(ap);
198da2e3ebdSchin if (!ret) hashfree(tab);
199da2e3ebdSchin return(ret);
200da2e3ebdSchin }
201