2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: hashalloc.c /main/3 1995/11/01 17:31:52 rswiston $ */
24 /***************************************************************
26 * AT&T - PROPRIETARY *
28 * THIS IS PROPRIETARY SOURCE CODE LICENSED BY *
31 * Copyright (c) 1995 AT&T Corp. *
32 * All Rights Reserved *
34 * This software is licensed by AT&T Corp. *
35 * under the terms and conditions of the license in *
36 * http://www.research.att.com/orgs/ssr/book/reuse *
38 * This software was created by the *
39 * Software Engineering Research Department *
40 * AT&T Bell Laboratories *
42 * For further information contact *
43 * gsf@research.att.com *
45 ***************************************************************/
47 /* : : generated by proto : : */
49 #if !defined(__PROTO__)
50 #if defined(__STDC__) || defined(__cplusplus) || defined(_proto) || defined(c_plusplus)
51 #if defined(__cplusplus)
52 #define __MANGLE__ "C"
57 #define __PROTO__(x) x
59 #define __PARAM__(n,o) n
60 #if !defined(__STDC__) && !defined(__cplusplus)
61 #if !defined(c_plusplus)
72 #define __PROTO__(x) ()
73 #define __OTORP__(x) x
74 #define __PARAM__(n,o) o
82 #if defined(__cplusplus) || defined(c_plusplus)
83 #define __VARARG__ ...
87 #if defined(__STDARG__)
88 #define __VA_START__(p,a) va_start(p,a)
90 #define __VA_START__(p,a) va_start(p)
93 static const char id_hash[] = "\n@(#)hash (AT&T Bell Laboratories) 05/09/95\0\n";
97 #if _DLL_INDIRECT_DATA && !_DLL
98 static Hash_info_t hash_info_data;
99 Hash_info_t hash_info = &hash_info_data;
101 Hash_info_t hash_info = { 0 };
105 * create a new hash table
109 hashalloc __PARAM__((Hash_table_t* ref, ...), (va_alist)) __OTORP__(va_dcl)
110 { __OTORP__(Hash_table_t* ref; )
111 register Hash_table_t* tab;
112 register Hash_table_t* ret = 0;
113 register int internal;
118 HASHregion region = 0;
121 __VA_START__(ap, ref); __OTORP__(ref = va_arg(ap, Hash_table_t* );)
124 * check for HASH_region which must be first
128 if (!ref && n == HASH_region)
130 region = va_arg(ap, HASHregion);
131 handle = va_arg(ap, __V_*);
133 if (!(tab = (Hash_table_t*)(*region)(handle, NiL, sizeof(Hash_table_t), 0)))
135 memset(tab, 0, sizeof(Hash_table_t));
137 else if (!(tab = newof(0, Hash_table_t, 1, 0)))
139 tab->bucketsize = (sizeof(Hash_header_t) + sizeof(char*) - 1) / sizeof(char*);
142 tab->flags = ref->flags & ~HASH_RESET;
143 tab->root = ref->root;
144 internal = HASH_INTERNAL;
150 if (!(tab->root = (Hash_root_t*)(*region)(handle, NiL, sizeof(Hash_root_t), 0)))
152 memset(tab->root, 0, sizeof(Hash_root_t));
154 else if (!(tab->root = newof(0, Hash_root_t, 1, 0)))
156 if (!(tab->root->local = newof(0, Hash_local_t, 1, 0)))
158 if (tab->root->local->region = region)
159 tab->root->local->handle = handle;
160 tab->root->meanchain = HASHMEANCHAIN;
163 tab->size = HASHMINSIZE;
170 tab->root->local->alloc = va_arg(ap, HASHalloc);
172 case HASH_bucketsize:
173 n = (va_arg(ap, int) + sizeof(char*) - 1) / sizeof(char*);
174 if (n > UCHAR_MAX) goto out;
175 if (n > tab->bucketsize) tab->bucketsize = n;
178 tab->flags &= ~(va_arg(ap, int) & ~internal);
182 tab->root->local->compare = va_arg(ap, HASHcompare);
186 tab->root->local->free = va_arg(ap, HASHfree);
190 tab->root->local->hash = va_arg(ap, HASHhash);
194 tab->root->meanchain = va_arg(ap, int);
197 tab->name = va_arg(ap, char*);
201 tab->root->namesize = va_arg(ap, int);
206 tab->flags |= (va_arg(ap, int) & ~internal);
209 tab->size = va_arg(ap, int);
210 if (tab->size & (tab->size - 1)) tab->flags |= HASH_FIXED;
213 tab->table = va_arg(ap, Hash_bucket_t**);
214 tab->flags |= HASH_STATIC;
218 if (vp < &va[elementsof(va)])
220 __va_copy( *vp, ap );
223 __va_copy(ap, *((va_list *) va_arg(ap, va_list)) );
225 if (vp < &va[elementsof(va)]) *vp++ = ap;
226 ap = va_arg(ap, va_list);
234 __va_copy( ap, *vp );
240 if (tab->flags & HASH_SCOPE)
242 if (!(tab->scope = ref)) goto out;
249 if (!(tab->table = (Hash_bucket_t**)(*region)(handle, NiL, sizeof(Hash_bucket_t*) * tab->size, 0)))
251 memset(tab->table, 0, sizeof(Hash_bucket_t*) * tab->size);
253 else if (!(tab->table = newof(0, Hash_bucket_t*, tab->size, 0))) goto out;
257 tab->root->flags = tab->flags & HASH_INTERNAL;
258 tab->root->next = hash_info.list;
259 hash_info.list = tab->root;
263 tab->next = tab->root->references;
264 tab->root->references = tab;
275 if (!ret) hashfree(tab);