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 #include "hashlib.h"
31da2e3ebdSchin
32da2e3ebdSchin /*
33da2e3ebdSchin * free (remove) a hash table
34da2e3ebdSchin * can be called for partially constructed tables
35da2e3ebdSchin * scope covered table pointer is returned
36da2e3ebdSchin * root info freed when last reference freed
37da2e3ebdSchin */
38da2e3ebdSchin
39da2e3ebdSchin Hash_table_t*
hashfree(register Hash_table_t * tab)40da2e3ebdSchin hashfree(register Hash_table_t* tab)
41da2e3ebdSchin {
42da2e3ebdSchin register Hash_bucket_t** sp;
43da2e3ebdSchin register Hash_bucket_t* b;
44da2e3ebdSchin register Hash_bucket_t* p;
45da2e3ebdSchin Hash_bucket_t** sx;
46da2e3ebdSchin Hash_root_t* rp;
47da2e3ebdSchin Hash_table_t* tp;
48da2e3ebdSchin Hash_free_f freevalue;
49da2e3ebdSchin Hash_free_f freebucket;
50da2e3ebdSchin Hash_region_f region;
51da2e3ebdSchin void* handle;
52da2e3ebdSchin
53da2e3ebdSchin if (!tab) return(0);
54da2e3ebdSchin if (tab->table)
55da2e3ebdSchin {
56da2e3ebdSchin freebucket = 0;
57da2e3ebdSchin freevalue = 0;
58da2e3ebdSchin if (tab->root->local->free)
59da2e3ebdSchin {
60da2e3ebdSchin if (tab->root->flags & HASH_BUCKET) freebucket = tab->root->local->free;
61da2e3ebdSchin else freevalue = tab->root->local->free;
62da2e3ebdSchin }
63da2e3ebdSchin if (region = tab->root->local->region)
64da2e3ebdSchin handle = tab->root->local->handle;
65da2e3ebdSchin sx = &tab->table[tab->size];
66da2e3ebdSchin sp = &tab->table[0];
67da2e3ebdSchin while (sp < sx)
68da2e3ebdSchin {
69da2e3ebdSchin b = *sp++;
70da2e3ebdSchin while (b)
71da2e3ebdSchin {
72da2e3ebdSchin p = b;
73da2e3ebdSchin b = b->next;
74da2e3ebdSchin if (freebucket) (*freebucket)((char*)p);
75da2e3ebdSchin else if (freevalue && p->value) (*freevalue)(p->value);
76da2e3ebdSchin if (p->hash & HASH_FREENAME)
77da2e3ebdSchin {
78da2e3ebdSchin p->hash &= ~HASH_FREENAME;
79da2e3ebdSchin if (region) (*region)(handle, p->name, 0, 0);
80da2e3ebdSchin else free(p->name);
81da2e3ebdSchin }
82da2e3ebdSchin if (!(p->hash & HASH_KEEP))
83da2e3ebdSchin {
84da2e3ebdSchin if (region) (*region)(handle, p, 0, 0);
85da2e3ebdSchin else free(p);
86da2e3ebdSchin }
87da2e3ebdSchin else if (p->hash & HASH_HIDES)
88da2e3ebdSchin {
89da2e3ebdSchin p->hash &= ~HASH_HIDES;
90da2e3ebdSchin p->name = ((Hash_bucket_t*)p->name)->name;
91da2e3ebdSchin }
92da2e3ebdSchin }
93da2e3ebdSchin }
94da2e3ebdSchin if ((tab->flags & (HASH_RESIZE|HASH_STATIC)) != HASH_STATIC)
95da2e3ebdSchin {
96da2e3ebdSchin if (region) (*region)(handle, tab->table, 0, 0);
97da2e3ebdSchin else free(tab->table);
98da2e3ebdSchin }
99da2e3ebdSchin }
100da2e3ebdSchin else region = 0;
101da2e3ebdSchin if (tab->root)
102da2e3ebdSchin {
103da2e3ebdSchin if (!region)
104da2e3ebdSchin {
105da2e3ebdSchin /*
106da2e3ebdSchin * remove from the table lists
107da2e3ebdSchin */
108da2e3ebdSchin
109da2e3ebdSchin if ((tp = tab->root->references) != tab)
110da2e3ebdSchin {
111da2e3ebdSchin for (; tp; tp = tp->next)
112da2e3ebdSchin if (tp->next == tab)
113da2e3ebdSchin {
114da2e3ebdSchin tp->next = tab->next;
115da2e3ebdSchin break;
116da2e3ebdSchin }
117da2e3ebdSchin }
118da2e3ebdSchin else if (!(tab->root->references = tp->next))
119da2e3ebdSchin {
120da2e3ebdSchin if ((rp = hash_info.list) != tab->root)
121da2e3ebdSchin {
122da2e3ebdSchin for (; rp; rp = rp->next)
123da2e3ebdSchin if (rp->next == tab->root)
124da2e3ebdSchin {
125da2e3ebdSchin rp->next = tab->root->next;
126da2e3ebdSchin break;
127da2e3ebdSchin }
128da2e3ebdSchin }
129da2e3ebdSchin else hash_info.list = rp->next;
130da2e3ebdSchin }
131da2e3ebdSchin }
132da2e3ebdSchin if (!(tab->root->references))
133da2e3ebdSchin {
134da2e3ebdSchin if (tab->root->local)
135da2e3ebdSchin free(tab->root->local);
136da2e3ebdSchin if (region) (*region)(handle, tab->root, 0, 0);
137da2e3ebdSchin else free(tab->root);
138da2e3ebdSchin }
139da2e3ebdSchin }
140da2e3ebdSchin if (tp = tab->scope) tp->frozen--;
141da2e3ebdSchin if (region) (*region)(handle, tab, 0, 0);
142da2e3ebdSchin else free(tab);
143da2e3ebdSchin return(tp);
144da2e3ebdSchin }
145