14887Schin /***********************************************************************
24887Schin * *
34887Schin * This software is part of the ast package *
4*12068SRoger.Faulkner@Oracle.COM * Copyright (c) 1985-2010 AT&T Intellectual Property *
54887Schin * and is licensed under the *
64887Schin * Common Public License, Version 1.0 *
78462SApril.Chin@Sun.COM * by AT&T Intellectual Property *
84887Schin * *
94887Schin * A copy of the License is available at *
104887Schin * http://www.opensource.org/licenses/cpl1.0.txt *
114887Schin * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
124887Schin * *
134887Schin * Information and Software Systems Research *
144887Schin * AT&T Research *
154887Schin * Florham Park NJ *
164887Schin * *
174887Schin * Glenn Fowler <gsf@research.att.com> *
184887Schin * David Korn <dgk@research.att.com> *
194887Schin * Phong Vo <kpv@research.att.com> *
204887Schin * *
214887Schin ***********************************************************************/
224887Schin #pragma prototyped
234887Schin /*
244887Schin * Glenn Fowler
254887Schin * AT&T Research
264887Schin *
274887Schin * hash table library
284887Schin */
294887Schin
304887Schin #include "hashlib.h"
314887Schin
324887Schin /*
334887Schin * free (remove) a hash table
344887Schin * can be called for partially constructed tables
354887Schin * scope covered table pointer is returned
364887Schin * root info freed when last reference freed
374887Schin */
384887Schin
394887Schin Hash_table_t*
hashfree(register Hash_table_t * tab)404887Schin hashfree(register Hash_table_t* tab)
414887Schin {
424887Schin register Hash_bucket_t** sp;
434887Schin register Hash_bucket_t* b;
444887Schin register Hash_bucket_t* p;
454887Schin Hash_bucket_t** sx;
464887Schin Hash_root_t* rp;
474887Schin Hash_table_t* tp;
484887Schin Hash_free_f freevalue;
494887Schin Hash_free_f freebucket;
504887Schin Hash_region_f region;
514887Schin void* handle;
524887Schin
534887Schin if (!tab) return(0);
544887Schin if (tab->table)
554887Schin {
564887Schin freebucket = 0;
574887Schin freevalue = 0;
584887Schin if (tab->root->local->free)
594887Schin {
604887Schin if (tab->root->flags & HASH_BUCKET) freebucket = tab->root->local->free;
614887Schin else freevalue = tab->root->local->free;
624887Schin }
634887Schin if (region = tab->root->local->region)
644887Schin handle = tab->root->local->handle;
654887Schin sx = &tab->table[tab->size];
664887Schin sp = &tab->table[0];
674887Schin while (sp < sx)
684887Schin {
694887Schin b = *sp++;
704887Schin while (b)
714887Schin {
724887Schin p = b;
734887Schin b = b->next;
744887Schin if (freebucket) (*freebucket)((char*)p);
754887Schin else if (freevalue && p->value) (*freevalue)(p->value);
764887Schin if (p->hash & HASH_FREENAME)
774887Schin {
784887Schin p->hash &= ~HASH_FREENAME;
794887Schin if (region) (*region)(handle, p->name, 0, 0);
804887Schin else free(p->name);
814887Schin }
824887Schin if (!(p->hash & HASH_KEEP))
834887Schin {
844887Schin if (region) (*region)(handle, p, 0, 0);
854887Schin else free(p);
864887Schin }
874887Schin else if (p->hash & HASH_HIDES)
884887Schin {
894887Schin p->hash &= ~HASH_HIDES;
904887Schin p->name = ((Hash_bucket_t*)p->name)->name;
914887Schin }
924887Schin }
934887Schin }
944887Schin if ((tab->flags & (HASH_RESIZE|HASH_STATIC)) != HASH_STATIC)
954887Schin {
964887Schin if (region) (*region)(handle, tab->table, 0, 0);
974887Schin else free(tab->table);
984887Schin }
994887Schin }
1004887Schin else region = 0;
1014887Schin if (tab->root)
1024887Schin {
1034887Schin if (!region)
1044887Schin {
1054887Schin /*
1064887Schin * remove from the table lists
1074887Schin */
1084887Schin
1094887Schin if ((tp = tab->root->references) != tab)
1104887Schin {
1114887Schin for (; tp; tp = tp->next)
1124887Schin if (tp->next == tab)
1134887Schin {
1144887Schin tp->next = tab->next;
1154887Schin break;
1164887Schin }
1174887Schin }
1184887Schin else if (!(tab->root->references = tp->next))
1194887Schin {
1204887Schin if ((rp = hash_info.list) != tab->root)
1214887Schin {
1224887Schin for (; rp; rp = rp->next)
1234887Schin if (rp->next == tab->root)
1244887Schin {
1254887Schin rp->next = tab->root->next;
1264887Schin break;
1274887Schin }
1284887Schin }
1294887Schin else hash_info.list = rp->next;
1304887Schin }
1314887Schin }
1324887Schin if (!(tab->root->references))
1334887Schin {
1344887Schin if (tab->root->local)
1354887Schin free(tab->root->local);
1364887Schin if (region) (*region)(handle, tab->root, 0, 0);
1374887Schin else free(tab->root);
1384887Schin }
1394887Schin }
1404887Schin if (tp = tab->scope) tp->frozen--;
1414887Schin if (region) (*region)(handle, tab, 0, 0);
1424887Schin else free(tab);
1434887Schin return(tp);
1444887Schin }
145